-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This uses atomic classes to cover all the scenarios of rendering a Stack
- Loading branch information
1 parent
3c1de03
commit 18a6f92
Showing
12 changed files
with
231 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as CSS from 'csstype'; | ||
|
||
import { Breakpoint } from './base'; | ||
import { PolymorphicComponent } from './Box'; | ||
|
||
type CssProperty<T> = T | Array<T> | Partial<Record<Breakpoint, T>>; | ||
|
||
type StackBaseProps = { | ||
display?: CssProperty<'flex' | 'inline-flex'>; | ||
spacing?: CssProperty<number | string>; | ||
direction?: CssProperty<CSS.StandardLonghandProperties['flexDirection']>; | ||
justifyContent?: CssProperty<CSS.StandardLonghandProperties['justifyContent']>; | ||
alignItems?: CssProperty<CSS.StandardLonghandProperties['alignItems']>; | ||
divider?: React.ReactNode; | ||
className?: string; | ||
}; | ||
|
||
declare const Stack: PolymorphicComponent<StackBaseProps>; | ||
|
||
export default Stack; |
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,50 @@ | ||
import clsx from 'clsx'; | ||
import * as React from 'react'; | ||
|
||
import { stackAtomics } from './baseAtomics'; | ||
|
||
const Stack = React.forwardRef(function Stack( | ||
{ | ||
children, | ||
spacing = 0, | ||
style, | ||
className, | ||
display = 'flex', | ||
component = 'div', | ||
direction = 'column', | ||
alignItems, | ||
justifyContent, | ||
...rest | ||
}, | ||
ref, | ||
) { | ||
const stackAtomicsObj = { | ||
display, | ||
direction, | ||
}; | ||
if (spacing) { | ||
stackAtomicsObj.spacing = spacing; | ||
} | ||
if (alignItems) { | ||
stackAtomicsObj.alignItems = alignItems; | ||
} | ||
if (justifyContent) { | ||
stackAtomicsObj.justifyContent = justifyContent; | ||
} | ||
const stackClasses = stackAtomics(stackAtomicsObj); | ||
const Component = component; | ||
return ( | ||
<Component | ||
ref={ref} | ||
className={clsx(stackClasses.className, className)} | ||
style={{ ...style, ...stackClasses.style }} | ||
{...rest} | ||
> | ||
{children} | ||
</Component> | ||
); | ||
}); | ||
|
||
Stack.displayName = 'Stack'; | ||
|
||
export default Stack; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { generateAtomics } from './generateAtomics'; | ||
|
||
export const stackAtomics = generateAtomics(({ theme }) => ({ | ||
conditions: Object.keys(theme.breakpoints.values).reduce((acc, breakpoint) => { | ||
acc[breakpoint] = `@media (min-width: ${theme.breakpoints.values[breakpoint]}${ | ||
theme.breakpoints.unit ?? 'px' | ||
})`; | ||
return acc; | ||
}, {}), | ||
defaultCondition: theme.defaultBreakpoint ?? 'xs', | ||
properties: { | ||
display: ['flex', 'inline-flex'], | ||
flexDirection: ['column', 'column-reverse', 'row', 'row-reverse'], | ||
justifyContent: [ | ||
'end', | ||
'start', | ||
'flex-end', | ||
'flex-start', | ||
'center', | ||
'space-between', | ||
'space-around', | ||
'space-evenly', | ||
], | ||
alignItems: [ | ||
'center', | ||
'end', | ||
'flex-end', | ||
'flex-start', | ||
'self-end', | ||
'self-start', | ||
'start', | ||
'baseline', | ||
'normal', | ||
'stretch', | ||
], | ||
gap: ['--Stack-gap'], | ||
}, | ||
shorthands: { | ||
direction: ['flexDirection'], | ||
spacing: ['gap'], | ||
}, | ||
multiplier: 8, | ||
})); |
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
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,13 @@ | ||
import * as React from 'react'; | ||
import Stack from '../src/Stack'; | ||
|
||
<Stack | ||
display={{ | ||
xs: 'flex', | ||
xl: 'inline-flex', | ||
}} | ||
direction="column" | ||
/>; | ||
|
||
// @ts-expect-error | ||
<Stack display={{ ml: 'block' }} divider={<h1>Hello</h1>} />; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.