-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathButtonsWrapper.tsx
82 lines (76 loc) · 2.16 KB
/
ButtonsWrapper.tsx
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as React from 'react';
import { Platform, StyleProp, StyleSheet, View, ViewStyle } from 'react-native';
/*
* the `left` prop can be provided to HeaderButtons as well as OverflowMenu
* this will render some extra margins on the corresponding side
* but must be considered only once which is the job of this Context
* */
export const ButtonsExtraMarginContext = React.createContext<
'toBeHandled' | 'alreadyHandled'
>('toBeHandled');
export type ButtonsWrapperProps = {
left?: boolean;
// when rendered as a header for a tab navigator, the margins that native stack adds are not there,
// so we need to add margins manually
preset?: 'tabHeader' | 'stackHeader';
children: React.ReactNode;
style?: StyleProp<ViewStyle>;
};
export const ButtonsWrapper = ({
left,
children,
style,
preset = 'stackHeader',
}: ButtonsWrapperProps) => {
const marginStatus = React.useContext(ButtonsExtraMarginContext);
const extraSideMargin =
preset === 'tabHeader' || marginStatus === 'toBeHandled'
? getHeaderMargin(left)
: undefined;
return (
<ButtonsExtraMarginContext.Provider value="alreadyHandled">
<View style={[styles.row, extraSideMargin, style]}>{children}</View>
</ButtonsExtraMarginContext.Provider>
);
};
export const getHeaderMargin = (left: boolean = false) => {
return left ? styles.extraEdgeMarginOnLeft : styles.extraEdgeMarginOnRight;
};
const styles = StyleSheet.create({
row: {
flexDirection: 'row',
justifyContent: 'flex-end',
...Platform.select({
android: {
columnGap: 18,
},
default: {
columnGap: 24,
},
}),
},
extraEdgeMarginOnLeft: {
// not used in native stack
// only applies in JS stack or when rendered as a header for a tab navigator
...Platform.select({
android: {
marginLeft: 15,
},
default: {
marginLeft: 14,
},
}),
},
extraEdgeMarginOnRight: {
// not used in native stack
// only applies in JS stack or when rendered as a header for a tab navigator
...Platform.select({
android: {
marginRight: 14,
},
default: {
marginRight: 15,
},
}),
},
});