11'use client' ;
22
3- import { useViewportRange } from '@ui5/webcomponents-react-base' ;
3+ import { useStylesheet , useViewportRange } from '@ui5/webcomponents-react-base' ;
44import { clsx } from 'clsx' ;
55import type { CSSProperties , ReactNode } from 'react' ;
66import React , { forwardRef , isValidElement } from 'react' ;
7- import { createUseStyles } from 'react-jss' ;
87import { GridPosition } from '../../enums/index.js' ;
98import { flattenFragments } from '../../internal/utils.js' ;
109import type { CommonProps } from '../../types/index.js' ;
11- import { styles } from './Grid.jss .js' ;
10+ import { classNames , styleData } from './Grid.module.css .js' ;
1211
1312export 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+
4849const 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;
5051const 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 > ( ) ;
5455DefaultSpanMap . set ( 'Phone' , 1 ) ;
5556DefaultSpanMap . set ( 'Tablet' , 2 ) ;
5657DefaultSpanMap . set ( 'Desktop' , 4 ) ;
5758DefaultSpanMap . set ( 'LargeDesktop' , 4 ) ;
5859
59- const DefaultIndentMap = new Map ( ) ;
60+ const DefaultIndentMap = new Map < Range , number > ( ) ;
6061DefaultIndentMap . set ( 'Phone' , 0 ) ;
6162DefaultIndentMap . set ( 'Tablet' , 0 ) ;
6263DefaultIndentMap . set ( 'Desktop' , 0 ) ;
6364DefaultIndentMap . 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 */
7979const 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