-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(web-react): Add
spacing
property to Grid
#DS-1388
- Loading branch information
Showing
12 changed files
with
285 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/web-react/src/components/Grid/demo/GridCustomSpacing.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
import Grid from '../Grid'; | ||
import GridItemFactory from './GridItemFactory'; | ||
|
||
const GridCustomSpacing = () => ( | ||
<Grid cols={{ mobile: 2, tablet: 3, desktop: 4 }} spacing="space-1000"> | ||
<GridItemFactory items={4} label="1/2, 1/3, 1/4" /> | ||
</Grid> | ||
); | ||
|
||
export default GridCustomSpacing; |
18 changes: 18 additions & 0 deletions
18
packages/web-react/src/components/Grid/demo/GridResponsiveCustomHorizontalSpacing.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
import Grid from '../Grid'; | ||
import GridItemFactory from './GridItemFactory'; | ||
|
||
const GridResponsiveCustomHorizontalSpacing = () => ( | ||
<Grid | ||
cols={{ mobile: 2, tablet: 3, desktop: 4 }} | ||
spacingX={{ | ||
mobile: 'space-100', | ||
tablet: 'space-400', | ||
desktop: 'space-800', | ||
}} | ||
> | ||
<GridItemFactory items={4} label="1/2, 1/3, 1/4" /> | ||
</Grid> | ||
); | ||
|
||
export default GridResponsiveCustomHorizontalSpacing; |
23 changes: 23 additions & 0 deletions
23
packages/web-react/src/components/Grid/demo/GridResponsiveCustomSpacing.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import Grid from '../Grid'; | ||
import GridItemFactory from './GridItemFactory'; | ||
|
||
const GridResponsiveCustomSpacing = () => ( | ||
<Grid | ||
cols={{ mobile: 2, tablet: 3, desktop: 4 }} | ||
spacingY={{ | ||
mobile: 'space-800', | ||
tablet: 'space-1000', | ||
desktop: 'space-0', | ||
}} | ||
spacingX={{ | ||
mobile: 'space-1000', | ||
tablet: 'space-900', | ||
desktop: 'space-1200', | ||
}} | ||
> | ||
<GridItemFactory items={4} label="1/2, 1/3, 1/4" /> | ||
</Grid> | ||
); | ||
|
||
export default GridResponsiveCustomSpacing; |
18 changes: 18 additions & 0 deletions
18
packages/web-react/src/components/Grid/demo/GridResponsiveCustomVerticalSpacing.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
import Grid from '../Grid'; | ||
import GridItemFactory from './GridItemFactory'; | ||
|
||
const GridResponsiveCustomVerticalSpacing = () => ( | ||
<Grid | ||
cols={{ mobile: 2, tablet: 3, desktop: 4 }} | ||
spacingY={{ | ||
mobile: 'space-100', | ||
tablet: 'space-400', | ||
desktop: 'space-800', | ||
}} | ||
> | ||
<GridItemFactory items={8} label="1/2, 1/3, 1/4" /> | ||
</Grid> | ||
); | ||
|
||
export default GridResponsiveCustomVerticalSpacing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 69 additions & 9 deletions
78
packages/web-react/src/components/Grid/useGridStyleProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,97 @@ | ||
import classNames from 'classnames'; | ||
import { ElementType } from 'react'; | ||
import { CSSProperties, ElementType } from 'react'; | ||
import { useClassNamePrefix } from '../../hooks'; | ||
import { SpiritGridProps } from '../../types'; | ||
|
||
interface GridCSSProperties extends CSSProperties { | ||
[key: string]: string | undefined | number; | ||
} | ||
|
||
const setStyleProperty = (styleObject: GridCSSProperties, propertyName: string, value: string | undefined) => { | ||
(styleObject as Record<string, string | undefined>)[propertyName] = `var(--spirit-${value})`; | ||
}; | ||
|
||
const setGridStyle = (styleObject: GridCSSProperties, baseVarName: string, propValue: GridCSSProperties) => { | ||
if (typeof propValue === 'object' && propValue !== null) { | ||
Object.keys(propValue).forEach((key) => { | ||
const suffix = key === 'mobile' ? '' : `-${key}`; | ||
const propName = `--${baseVarName}${suffix}`; | ||
setStyleProperty(styleObject, propName, propValue[key as keyof GridCSSProperties]?.toString()); | ||
}); | ||
} else { | ||
const propName = `--${baseVarName}`; | ||
setStyleProperty(styleObject, propName, propValue); | ||
} | ||
}; | ||
|
||
export interface GridStyles<T> { | ||
/** className props */ | ||
classProps: string; | ||
/** Props for the grid element. */ | ||
props: T; | ||
/** Style props for the element */ | ||
styleProps: GridCSSProperties; | ||
} | ||
|
||
export function useGridStyleProps(props: SpiritGridProps<ElementType>): GridStyles<SpiritGridProps<ElementType>> { | ||
const { cols, ...restProps } = props; | ||
|
||
const gridClass = useClassNamePrefix('Grid'); | ||
|
||
const gridStyle: GridCSSProperties = {}; | ||
|
||
const typePropNames = Object.keys(props).filter((propName) => propName.startsWith('spacing')); | ||
|
||
typePropNames.forEach((propName) => { | ||
let type: string; | ||
|
||
if (props[propName]) { | ||
if (propName.startsWith('spacingX')) { | ||
type = 'spacing-x'; | ||
} else if (propName.startsWith('spacingY')) { | ||
type = 'spacing-y'; | ||
} else { | ||
type = propName; | ||
} | ||
|
||
if (type === 'spacing') { | ||
setGridStyle( | ||
gridStyle, | ||
`grid-spacing-x${propName.replace(type, '').toLowerCase()}`, | ||
props[propName] as GridCSSProperties, | ||
); | ||
setGridStyle( | ||
gridStyle, | ||
`grid-spacing-y${propName.replace(type, '').toLowerCase()}`, | ||
props[propName] as GridCSSProperties, | ||
); | ||
} else { | ||
setGridStyle(gridStyle, `grid-${type}`, props[propName] as GridCSSProperties); | ||
} | ||
} | ||
|
||
delete props[propName]; | ||
}); | ||
|
||
let classes: string; | ||
let gridColsClass: string; | ||
|
||
if (typeof cols === 'object' && cols !== null) { | ||
const classes: string[] = []; | ||
const classList: string[] = []; | ||
Object.keys(cols).forEach((key) => { | ||
const infix = key === 'mobile' ? '' : `--${key}`; | ||
classes.push(`${gridClass}${infix}--cols-${cols[key as keyof typeof cols]}`); | ||
classList.push(`${gridClass}${infix}--cols-${cols[key as keyof typeof cols]}`); | ||
}); | ||
|
||
return { | ||
classProps: classNames(gridClass, classes), | ||
props: restProps, | ||
}; | ||
classes = classNames(gridClass, classList); | ||
} else { | ||
gridColsClass = `${gridClass}--cols-${cols}`; | ||
classes = classNames(gridClass, { [gridColsClass]: cols }); | ||
} | ||
const gridColsClass = `${gridClass}--cols-${cols}`; | ||
const classes = classNames(gridClass, { [gridColsClass]: cols }); | ||
|
||
return { | ||
classProps: classes, | ||
props: restProps, | ||
styleProps: gridStyle, | ||
}; | ||
} |
Oops, something went wrong.