-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
178 additions
and
158 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,108 +1,105 @@ | ||
import { ForwardedRef, forwardRef } from 'react'; | ||
import { | ||
FlexStyle, | ||
StyleProp, | ||
StyleSheet, | ||
View, | ||
ViewStyle, | ||
} from 'react-native'; | ||
import { Padding, PolymorphicComponentProps } from '../shared/types'; | ||
import { getPaddingStyle } from '../shared/utils'; | ||
import { GapContext, useGap } from './GapContext'; | ||
import { | ||
ComponentWithViewStyle, | ||
Padding, | ||
PolymorphicComponentProps, | ||
Radius, | ||
} from '../shared/types'; | ||
import { getPaddingStyle, getRadiusStyle } from '../shared/utils'; | ||
import { FlexGap } from './types'; | ||
import { getFlexDirection, getGapStyle } from './utils'; | ||
|
||
export type FlexProps<C extends React.ElementType> = PolymorphicComponentProps< | ||
C, | ||
{ | ||
row?: boolean; | ||
reverse?: boolean; | ||
gap?: FlexGap; | ||
padding?: Padding; | ||
align?: FlexStyle['alignItems']; | ||
alignSelf?: FlexStyle['alignSelf']; | ||
justify?: FlexStyle['justifyContent']; | ||
grow?: number | boolean; | ||
shrink?: number | boolean; | ||
basis?: FlexStyle['flexBasis']; | ||
wrap?: boolean; | ||
absolute?: boolean; | ||
zIndex?: ViewStyle['zIndex']; | ||
top?: ViewStyle['top']; | ||
left?: ViewStyle['left']; | ||
bottom?: ViewStyle['bottom']; | ||
right?: ViewStyle['right']; | ||
width?: ViewStyle['width']; | ||
height?: ViewStyle['height']; | ||
fill?: boolean; | ||
} | ||
>; | ||
|
||
export type FlexComponent = <C extends React.ElementType = typeof View>( | ||
props: FlexProps<C> | ||
) => React.ReactElement | null; | ||
export type FlexProps<C extends ComponentWithViewStyle = typeof View> = | ||
PolymorphicComponentProps< | ||
C, | ||
{ | ||
row?: boolean; | ||
reverse?: boolean; | ||
gap?: FlexGap; | ||
padding?: Padding; | ||
align?: FlexStyle['alignItems']; | ||
alignSelf?: FlexStyle['alignSelf']; | ||
justify?: FlexStyle['justifyContent']; | ||
grow?: number | boolean; | ||
shrink?: number | boolean; | ||
basis?: FlexStyle['flexBasis']; | ||
wrap?: boolean; | ||
absolute?: boolean; | ||
zIndex?: number; | ||
top?: FlexStyle['top']; | ||
left?: FlexStyle['left']; | ||
bottom?: FlexStyle['bottom']; | ||
right?: FlexStyle['right']; | ||
width?: FlexStyle['width']; | ||
height?: FlexStyle['height']; | ||
fill?: boolean; | ||
radius?: Radius; | ||
background?: ViewStyle['backgroundColor']; | ||
style?: StyleProp<ViewStyle>; | ||
} | ||
>; | ||
|
||
export const Flex = forwardRef( | ||
<C extends React.ElementType = typeof View>( | ||
export const Flex = <C extends ComponentWithViewStyle = typeof View>({ | ||
as, | ||
row = false, | ||
reverse = false, | ||
gap, | ||
padding, | ||
align = 'stretch', | ||
alignSelf = 'auto', | ||
justify = 'flex-start', | ||
grow = 0, | ||
shrink = 0, | ||
basis = 'auto', | ||
wrap = false, | ||
absolute = false, | ||
zIndex, | ||
top, | ||
left, | ||
bottom, | ||
right, | ||
width, | ||
height, | ||
fill = false, | ||
radius, | ||
background, | ||
style, | ||
forwardedRef, | ||
...props | ||
}: FlexProps<C>) => { | ||
const Comp = as || View; | ||
const finalStyle: StyleProp<ViewStyle> = [ | ||
getPaddingStyle(padding), | ||
getGapStyle(gap), | ||
getRadiusStyle(radius), | ||
{ | ||
as, | ||
row = false, | ||
reverse = false, | ||
gap, | ||
padding, | ||
align = 'stretch', | ||
alignSelf = 'auto', | ||
justify = 'flex-start', | ||
grow = 0, | ||
shrink = 0, | ||
basis = 'auto', | ||
wrap = false, | ||
absolute = false, | ||
flexGrow: Number(grow), | ||
flexShrink: Number(shrink), | ||
flexBasis: basis, | ||
alignItems: align, | ||
alignSelf, | ||
justifyContent: justify, | ||
flexDirection: getFlexDirection(row, reverse), | ||
flexWrap: wrap ? 'wrap' : 'nowrap', | ||
position: absolute ? 'absolute' : 'relative', | ||
zIndex, | ||
top, | ||
left, | ||
bottom, | ||
right, | ||
width, | ||
height, | ||
fill = false, | ||
style, | ||
...props | ||
}: FlexProps<C>, | ||
ref?: ForwardedRef<C> | ||
) => { | ||
const Comp = as || View; | ||
const inheritedGap = useGap(); | ||
const finalGap = typeof gap === 'number' ? gap : inheritedGap; | ||
const finalStyle: StyleProp<ViewStyle> = [ | ||
getPaddingStyle(padding), | ||
getGapStyle(finalGap), | ||
{ | ||
flexGrow: Number(grow), | ||
flexShrink: Number(shrink), | ||
flexBasis: basis, | ||
alignItems: align, | ||
alignSelf, | ||
justifyContent: justify, | ||
flexDirection: getFlexDirection(row, reverse), | ||
flexWrap: wrap ? 'wrap' : 'nowrap', | ||
position: absolute ? 'absolute' : 'relative', | ||
zIndex, | ||
top, | ||
left, | ||
bottom, | ||
right, | ||
width, | ||
height, | ||
}, | ||
fill ? StyleSheet.absoluteFill : null, | ||
style, | ||
]; | ||
backgroundColor: background, | ||
}, | ||
fill ? StyleSheet.absoluteFill : null, | ||
style, | ||
]; | ||
|
||
return ( | ||
<GapContext.Provider value={finalGap}> | ||
<Comp ref={ref} style={finalStyle} {...props} /> | ||
</GapContext.Provider> | ||
); | ||
} | ||
); | ||
return <Comp ref={forwardedRef} style={finalStyle} {...props} />; | ||
}; |
This file was deleted.
Oops, something went wrong.
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,4 +1,3 @@ | ||
export * from './Flex'; | ||
export * from './GapContext'; | ||
export * from './types'; | ||
export * from './utils'; |
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,28 +1,47 @@ | ||
import { StyleProp, ViewStyle } from 'react-native'; | ||
|
||
export type Padding = | ||
| number | ||
| [vertical: number, horizontal: number] | ||
| [top: number, horizontal: number, bottom: number] | ||
| [top: number, right: number, bottom: number, left: number]; | ||
|
||
export type Radius = | ||
| number | ||
| [topLeftAndBottomRight: number, topRightAndBottomLeft: number] | ||
| [topLeft: number, topRightAndBottomLeft: number, bottomRight: number] | ||
| [ | ||
topLeft: number, | ||
topRight: number, | ||
bottomRight: number, | ||
bottomLeft: number | ||
]; | ||
|
||
export type Size = number | [number, number]; | ||
|
||
export type AsProp<C extends React.ElementType> = { | ||
as?: C; | ||
}; | ||
|
||
export type ForwardedRefProp<C extends React.ElementType> = { | ||
forwardedRef?: PolymorphicRef<C>; | ||
}; | ||
|
||
export type PropsToOmit<C extends React.ElementType, P> = keyof (AsProp<C> & P); | ||
|
||
export type PolymorphicComponentProps< | ||
C extends React.ElementType, | ||
Props = {} | ||
C extends ComponentWithViewStyle<Props>, | ||
Props extends { style?: Style } = {}, | ||
Style extends StyleProp<ViewStyle> = StyleProp<ViewStyle> | ||
> = Props & | ||
AsProp<C> & | ||
ForwardedRefProp<C> & | ||
Omit<React.ComponentPropsWithoutRef<C>, PropsToOmit<C, Props>>; | ||
|
||
export type PolymorphicComponentPropsWithRef< | ||
C extends React.ElementType, | ||
Props = {} | ||
> = PolymorphicComponentProps<C, Props> & { ref?: PolymorphicRef<C> }; | ||
export type ComponentWithViewStyle< | ||
Props extends { style?: Style } = {}, | ||
Style extends StyleProp<ViewStyle> = StyleProp<ViewStyle> | ||
> = React.JSXElementConstructor<Props>; | ||
|
||
export type PolymorphicRef<C extends React.ElementType> = | ||
React.ComponentPropsWithRef<C>['ref']; |
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
Oops, something went wrong.