Skip to content

Commit

Permalink
feat: added honorable Flex replacement (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsladerman authored Jun 4, 2024
1 parent 070b365 commit 8335e79
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/components/Flex.tsx
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export { default as ContentCard } from './components/ContentCard'
export { default as Date } from './components/Date'
export { default as Divider } from './components/Divider'
export { default as EmptyState } from './components/EmptyState'
export { default as Flex } from './components/Flex'
export { default as FormField } from './components/FormField'
export { default as Highlight } from './components/Highlight'
export type { IconFrameProps } from './components/IconFrame'
Expand Down

0 comments on commit 8335e79

Please sign in to comment.