Skip to content

Commit 8335e79

Browse files
authored
feat: added honorable Flex replacement (#601)
1 parent 070b365 commit 8335e79

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/components/Flex.tsx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// drop-in replacement for anywhere 'honorable' Flex is used. Moving forward a regular div or a styled component should just be used instead
2+
3+
import {
4+
type CSSProperties,
5+
type ComponentProps,
6+
type Ref,
7+
forwardRef,
8+
memo,
9+
} from 'react'
10+
import { useTheme } from 'styled-components'
11+
12+
type FlexBaseProps = {
13+
/**
14+
* Alias for flexDirection
15+
*/
16+
direction?: 'row' | 'column'
17+
/**
18+
* wrap flex property
19+
*/
20+
wrap?: 'wrap' | 'nowrap' | 'wrap-reverse' | boolean
21+
/**
22+
* Alias for flexBasis
23+
*/
24+
basis?: string | number
25+
/**
26+
* Alias for flexGrow
27+
*/
28+
grow?: boolean | number
29+
/**
30+
* Alias for flexShrink
31+
*/
32+
shrink?: boolean | number
33+
/**
34+
* Alias for alignItems
35+
*/
36+
align?: 'flex-start' | 'flex-end' | 'center' | 'baseline' | 'stretch'
37+
/**
38+
* Alias for justifyContent
39+
*/
40+
justify?:
41+
| 'flex-start'
42+
| 'flex-end'
43+
| 'center'
44+
| 'space-between'
45+
| 'space-around'
46+
| 'space-evenly'
47+
48+
gap?: string
49+
}
50+
51+
type FlexProps = CSSProperties & ComponentProps<'div'> & FlexBaseProps
52+
53+
function FlexRef(props: FlexProps, ref: Ref<any>) {
54+
const {
55+
direction,
56+
wrap,
57+
basis,
58+
grow,
59+
shrink,
60+
align,
61+
justify,
62+
gap,
63+
children,
64+
...otherProps
65+
} = props
66+
const theme = useTheme()
67+
68+
return (
69+
<div
70+
ref={ref}
71+
style={{
72+
display: 'flex',
73+
flexDirection: direction,
74+
flexWrap: typeof wrap === 'boolean' ? 'wrap' : wrap,
75+
flexBasis: basis,
76+
flexGrow: typeof grow === 'boolean' ? 1 : grow,
77+
flexShrink: typeof shrink === 'boolean' ? 1 : shrink,
78+
alignItems: align,
79+
justifyContent: justify,
80+
gap: (theme.spacing as any)[gap] || 0,
81+
...otherProps,
82+
}}
83+
>
84+
{children}
85+
</div>
86+
)
87+
}
88+
89+
const BaseFlex = forwardRef(FlexRef)
90+
91+
const Flex = memo(BaseFlex)
92+
93+
export default Flex

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export { default as ContentCard } from './components/ContentCard'
2525
export { default as Date } from './components/Date'
2626
export { default as Divider } from './components/Divider'
2727
export { default as EmptyState } from './components/EmptyState'
28+
export { default as Flex } from './components/Flex'
2829
export { default as FormField } from './components/FormField'
2930
export { default as Highlight } from './components/Highlight'
3031
export type { IconFrameProps } from './components/IconFrame'

0 commit comments

Comments
 (0)