-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added honorable Flex replacement (#601)
- Loading branch information
1 parent
070b365
commit 8335e79
Showing
2 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// drop-in replacement for anywhere 'honorable' Flex is used. Moving forward a regular div or a styled component should just be used instead | ||
|
||
import { | ||
type CSSProperties, | ||
type ComponentProps, | ||
type Ref, | ||
forwardRef, | ||
memo, | ||
} from 'react' | ||
import { useTheme } from 'styled-components' | ||
|
||
type FlexBaseProps = { | ||
/** | ||
* Alias for flexDirection | ||
*/ | ||
direction?: 'row' | 'column' | ||
/** | ||
* wrap flex property | ||
*/ | ||
wrap?: 'wrap' | 'nowrap' | 'wrap-reverse' | boolean | ||
/** | ||
* Alias for flexBasis | ||
*/ | ||
basis?: string | number | ||
/** | ||
* Alias for flexGrow | ||
*/ | ||
grow?: boolean | number | ||
/** | ||
* Alias for flexShrink | ||
*/ | ||
shrink?: boolean | number | ||
/** | ||
* Alias for alignItems | ||
*/ | ||
align?: 'flex-start' | 'flex-end' | 'center' | 'baseline' | 'stretch' | ||
/** | ||
* Alias for justifyContent | ||
*/ | ||
justify?: | ||
| 'flex-start' | ||
| 'flex-end' | ||
| 'center' | ||
| 'space-between' | ||
| 'space-around' | ||
| 'space-evenly' | ||
|
||
gap?: string | ||
} | ||
|
||
type FlexProps = CSSProperties & ComponentProps<'div'> & FlexBaseProps | ||
|
||
function FlexRef(props: FlexProps, ref: Ref<any>) { | ||
const { | ||
direction, | ||
wrap, | ||
basis, | ||
grow, | ||
shrink, | ||
align, | ||
justify, | ||
gap, | ||
children, | ||
...otherProps | ||
} = props | ||
const theme = useTheme() | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
style={{ | ||
display: 'flex', | ||
flexDirection: direction, | ||
flexWrap: typeof wrap === 'boolean' ? 'wrap' : wrap, | ||
flexBasis: basis, | ||
flexGrow: typeof grow === 'boolean' ? 1 : grow, | ||
flexShrink: typeof shrink === 'boolean' ? 1 : shrink, | ||
alignItems: align, | ||
justifyContent: justify, | ||
gap: (theme.spacing as any)[gap] || 0, | ||
...otherProps, | ||
}} | ||
> | ||
{children} | ||
</div> | ||
) | ||
} | ||
|
||
const BaseFlex = forwardRef(FlexRef) | ||
|
||
const Flex = memo(BaseFlex) | ||
|
||
export default 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