forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitStyleProp.ts
More file actions
61 lines (59 loc) · 1.44 KB
/
Copy pathsplitStyleProp.ts
File metadata and controls
61 lines (59 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { FlexStyle, StyleProp, StyleSheet } from 'react-native';
const OUTER_PROPS: { [key in keyof FlexStyle]?: true } = {
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>(
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 };
}