-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update Flex to do all that Stack does
- Loading branch information
Showing
4 changed files
with
110 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,96 @@ | ||
import { styled } from '~/stitches' | ||
import { CSS, styled } from '~/stitches' | ||
import { createThemeVariants } from '~/utilities' | ||
|
||
export const Flex = styled('div', { display: 'flex' }) | ||
const createVariants = <T extends GlobalValue[]>( | ||
variants: T, | ||
fn: (string: T[number]) => CSS | ||
) => { | ||
return variants.reduce( | ||
(prev, variant) => ({ ...prev, [variant]: fn(variant) }), | ||
{} as Record<T[number], CSS> | ||
) | ||
} | ||
|
||
const globalValues = [ | ||
'inherit', | ||
'initial', | ||
'revert', | ||
'revert-layer', | ||
'unset' | ||
] as const | ||
|
||
/* | ||
* The following type is partially a hack to get the rest of the createVariants array parameter to be recognised as const. | ||
* Thus giving is the correct types generated for these generated variant props. | ||
* No clue how we can do it cleaner. | ||
* Mad props to: Elliot for getting this to working as is. | ||
* | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
type GlobalValue = typeof globalValues[number] | (string & {}) | ||
|
||
export const Flex = styled('div', { | ||
display: 'flex', | ||
variants: { | ||
direction: createVariants( | ||
[...globalValues, 'row', 'row-reverse', 'column', 'column-reverse'], | ||
(v) => { | ||
return { flexDirection: v } | ||
} | ||
), | ||
wrap: createVariants( | ||
[...globalValues, 'nowrap', 'wrap', 'wrap-reverse'], | ||
(v) => { | ||
return { flexWrap: v } | ||
} | ||
), | ||
// Why is both `start` and `flex-start` valid values? Answer: https://csslayout.news/whats-the-difference-between-the-alignment-values-of-start-flex-start-and-self-start/ | ||
justify: createVariants( | ||
[ | ||
...globalValues, | ||
'normal', | ||
'unsafe', | ||
'safe', | ||
'start', | ||
'center', | ||
'end', | ||
'flex-start', | ||
'flex-end', | ||
'left', | ||
'right', | ||
'space-between', | ||
'space-around', | ||
'space-evenly', | ||
'stretch' | ||
], | ||
(v) => { | ||
return { justifyContent: v } | ||
} | ||
), | ||
align: createVariants( | ||
[ | ||
...globalValues, | ||
'normal', | ||
'unsafe', | ||
'safe', | ||
'center', | ||
'start', | ||
'end', | ||
'self-start', | ||
'self-end', | ||
'flex-start', | ||
'flex-end', | ||
'baseline', | ||
'first baseline', | ||
'last baseline', | ||
'stretch' | ||
], | ||
(v) => { | ||
return { alignItems: v } | ||
} | ||
), | ||
gap: createThemeVariants('space', { gap: '$key' }) | ||
} | ||
}) | ||
|
||
Flex.displayName = 'Flex' |
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