Skip to content

Commit b3811ff

Browse files
refactor(Grid): migrate from react-jss to css modules (#5689)
1 parent 4e39456 commit b3811ff

File tree

3 files changed

+77
-38
lines changed

3 files changed

+77
-38
lines changed

packages/main/src/components/Grid/Grid.jss.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
.grid {
2+
display: grid;
3+
grid-template-columns: repeat(12, 1fr);
4+
}
5+
6+
.gridSpan1 {
7+
grid-column: span 1;
8+
}
9+
10+
.gridSpan2 {
11+
grid-column: span 2;
12+
}
13+
14+
.gridSpan3 {
15+
grid-column: span 3;
16+
}
17+
18+
.gridSpan4 {
19+
grid-column: span 4;
20+
}
21+
22+
.gridSpan5 {
23+
grid-column: span 5;
24+
}
25+
26+
.gridSpan6 {
27+
grid-column: span 6;
28+
}
29+
30+
.gridSpan7 {
31+
grid-column: span 7;
32+
}
33+
34+
.gridSpan8 {
35+
grid-column: span 8;
36+
}
37+
38+
.gridSpan9 {
39+
grid-column: span 9;
40+
}
41+
42+
.gridSpan10 {
43+
grid-column: span 10;
44+
}
45+
46+
.gridSpan11 {
47+
grid-column: span 11;
48+
}
49+
50+
.gridSpan12 {
51+
grid-column: span 12;
52+
}
53+
54+
.positionCenter {
55+
margin-inline: auto;
56+
}
57+
58+
.positionRight {
59+
margin-inline: auto 0;
60+
}

packages/main/src/components/Grid/index.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use client';
22

3-
import { useViewportRange } from '@ui5/webcomponents-react-base';
3+
import { useStylesheet, useViewportRange } from '@ui5/webcomponents-react-base';
44
import { clsx } from 'clsx';
55
import type { CSSProperties, ReactNode } from 'react';
66
import React, { forwardRef, isValidElement } from 'react';
7-
import { createUseStyles } from 'react-jss';
87
import { GridPosition } from '../../enums/index.js';
98
import { flattenFragments } from '../../internal/utils.js';
109
import type { CommonProps } from '../../types/index.js';
11-
import { styles } from './Grid.jss.js';
10+
import { classNames, styleData } from './Grid.module.css.js';
1211

1312
export interface GridPropTypes extends CommonProps {
1413
/**
@@ -45,54 +44,55 @@ export interface GridPropTypes extends CommonProps {
4544
children?: ReactNode | ReactNode[];
4645
}
4746

47+
type Range = 'Phone' | 'Tablet' | 'Desktop' | 'LargeDesktop';
48+
4849
const INDENT_PATTERN =
4950
/^([X][L](?<LargeDesktop>[0-9]|1[0-2]))? ?([L](?<Desktop>[0-9]|1[0-2]))? ?([M](?<Tablet>[0-9]|1[0-2]))? ?([S](?<Phone>[0-9]|1[0-2]))?$/i;
5051
const SPAN_PATTERN =
5152
/^([X][L](?<LargeDesktop>[1-9]|1[0-2]))? ?([L](?<Desktop>[1-9]|1[0-2]))? ?([M](?<Tablet>[1-9]|1[0-2]))? ?([S](?<Phone>[1-9]|1[0-2]))?$/i;
5253

53-
const DefaultSpanMap = new Map();
54+
const DefaultSpanMap = new Map<Range, number>();
5455
DefaultSpanMap.set('Phone', 1);
5556
DefaultSpanMap.set('Tablet', 2);
5657
DefaultSpanMap.set('Desktop', 4);
5758
DefaultSpanMap.set('LargeDesktop', 4);
5859

59-
const DefaultIndentMap = new Map();
60+
const DefaultIndentMap = new Map<Range, number>();
6061
DefaultIndentMap.set('Phone', 0);
6162
DefaultIndentMap.set('Tablet', 0);
6263
DefaultIndentMap.set('Desktop', 0);
6364
DefaultIndentMap.set('LargeDesktop', 0);
6465

65-
const getSpanFromString = (span, currentRange) => {
66+
const getSpanFromString = (span: string, currentRange: Range) => {
6667
const spanConfig = SPAN_PATTERN.exec(span);
67-
return spanConfig?.groups[currentRange] ?? DefaultSpanMap.get(currentRange);
68+
return Number(spanConfig?.groups[currentRange] ?? DefaultSpanMap.get(currentRange));
6869
};
6970

70-
const getIndentFromString = (indent, currentRange) => {
71+
const getIndentFromString = (indent: string, currentRange: Range) => {
7172
const indentConfig = INDENT_PATTERN.exec(indent);
72-
return indentConfig?.groups[currentRange] ?? DefaultIndentMap.get(currentRange);
73+
return Number(indentConfig?.groups[currentRange] ?? DefaultIndentMap.get(currentRange));
7374
};
7475

75-
const useStyles = createUseStyles(styles, { name: 'Grid' });
7676
/**
7777
* A layout container component used for aligning items with various sizes in a simple grid.
7878
*/
7979
const Grid = forwardRef<HTMLDivElement, GridPropTypes>((props, ref) => {
8080
const { position, children, hSpacing, vSpacing, style, className, slot, defaultIndent, defaultSpan, ...rest } = props;
8181

82-
const classes = useStyles();
82+
useStylesheet(styleData, Grid.displayName);
8383
const currentRange = useViewportRange();
8484
const gridClasses = clsx(
85-
classes.grid,
86-
GridPosition.Center === position && classes.positionCenter,
87-
GridPosition.Right === position && classes.positionRight,
85+
classNames.grid,
86+
GridPosition.Center === position && classNames.positionCenter,
87+
GridPosition.Right === position && classNames.positionRight,
8888
className
8989
);
9090

9191
return (
9292
<div
9393
ref={ref}
9494
className={gridClasses}
95-
style={{ gridRowGap: vSpacing, gridColumnGap: hSpacing, ...style }}
95+
style={{ rowGap: vSpacing, columnGap: hSpacing, ...style }}
9696
slot={slot}
9797
{...rest}
9898
>
@@ -102,7 +102,7 @@ const Grid = forwardRef<HTMLDivElement, GridPropTypes>((props, ref) => {
102102
}
103103

104104
const childSpan = getSpanFromString(child.props['data-layout-span'] ?? defaultSpan, currentRange);
105-
const childClass = classes[`gridSpan${childSpan}`];
105+
const childClass = classNames[`gridSpan${childSpan}`];
106106

107107
const childrenWithGridLayout = [
108108
<div className={childClass} key={child.key}>
@@ -114,7 +114,7 @@ const Grid = forwardRef<HTMLDivElement, GridPropTypes>((props, ref) => {
114114
if (indentSpan && indentSpan > 0) {
115115
childrenWithGridLayout.unshift(
116116
<span
117-
className={classes[`gridSpan${indentSpan}`]}
117+
className={classNames[`gridSpan${indentSpan}`]}
118118
key={`${child.key}-indent`}
119119
data-component-name="GridIndentSpacer"
120120
aria-hidden="true"

0 commit comments

Comments
 (0)