-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Split layout props in GenericTouchable instead of using containerStyle #1617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { FlexStyle, StyleProp, StyleSheet } from 'react-native'; | ||
|
||
const OUTER_PROPS: { [key in keyof FlexStyle]?: true } = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if these are really all the style props that need to be applied to the outer view or if there are more (e.g. It would probably be best to add complex layout examples to test this, as you mentioned. |
||
alignSelf: true, | ||
bottom: true, | ||
display: true, | ||
end: true, | ||
flex: true, | ||
flexBasis: true, | ||
flexGrow: true, | ||
flexShrink: true, | ||
height: true, | ||
left: true, | ||
margin: true, | ||
marginBottom: true, | ||
marginEnd: true, | ||
marginHorizontal: true, | ||
marginLeft: true, | ||
marginRight: true, | ||
marginStart: true, | ||
marginTop: true, | ||
marginVertical: true, | ||
maxHeight: true, | ||
maxWidth: true, | ||
minHeight: true, | ||
minWidth: true, | ||
position: true, | ||
right: true, | ||
start: true, | ||
top: true, | ||
width: true, | ||
zIndex: true, | ||
}; | ||
|
||
/** | ||
* Split a style prop between an "outer" and "inner" views in a way that makes it behave | ||
* as if it was a single view. | ||
* | ||
* @example | ||
* const { outer, inner } = splitStyleProp(style); | ||
* return ( | ||
* <View style={outer}> | ||
* <View style={[{ flexGrow: 1 }, inner]} /> | ||
* </View> | ||
* ); | ||
*/ | ||
export default function splitStyleProp<T extends FlexStyle>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be |
||
style?: StyleProp<T> | ||
): { outer: T; inner: T } { | ||
const resolvedStyle = StyleSheet.flatten(style); | ||
const inner: Record<string, unknown> = {}; | ||
const outer: Record<string, unknown> = {}; | ||
Object.entries(resolvedStyle).forEach(([key, value]) => { | ||
if ((OUTER_PROPS as { [key: string]: true | undefined })[key] === true) { | ||
outer[key] = value; | ||
} else { | ||
inner[key] = value; | ||
} | ||
}); | ||
return { outer: outer as T, inner: inner as T }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
style
prop will be split on every render (and also causes bridge traffic since the object changes). Not sure if this has any noticeable performance implications but that's something to keep in mind since some screens may contain a lot of touchables.