-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathHeaderButton.tsx
118 lines (102 loc) · 2.86 KB
/
HeaderButton.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import * as React from 'react';
import {
StyleSheet,
Platform,
ColorValue,
Text,
ViewStyle,
TextStyle,
} from 'react-native';
import { useTheme } from '@react-navigation/native';
import { PlatformPressable } from '@react-navigation/elements';
import type { ComponentProps, ReactNode } from 'react';
// for renderVisibleButton() function
export type VisibleButtonProps = {
title: string;
IconComponent?: React.ComponentType<{
name: any; // TODO generify to support icon names
style?: any;
color?: ColorValue;
size?: number;
}>;
iconName?: string;
iconSize?: number;
color?: ColorValue;
buttonStyle?: ViewStyle | TextStyle;
};
type PlatformPressableProps = ComponentProps<typeof PlatformPressable>;
export type RenderButtonCallbackParams = Omit<VisibleButtonProps, 'color'> & {
// color is always available, either from user or from react-navigation theme
color: ColorValue;
};
type RenderButtonType = {
renderButton: <T extends RenderButtonCallbackParams>(params: T) => ReactNode;
};
type BaseProps = Omit<PlatformPressableProps, 'children'> & VisibleButtonProps;
// for <Item />, some things are optional while they are required for HeaderButton
export type ItemProps = BaseProps & Partial<RenderButtonType>;
export type HeaderButtonProps = BaseProps & RenderButtonType;
export function HeaderButton(props: HeaderButtonProps) {
const { colors } = useTheme();
const themeColor = Platform.select({
ios: colors.primary,
default: colors.text,
});
const { renderButton, style, color, ...other } = props;
const ButtonElement = renderButton({
color: color || themeColor,
...other,
});
return (
<PlatformPressable
hitSlop={buttonHitSlop}
// @ts-expect-error typings too strict
style={StyleSheet.compose(styles.buttonContainer, style)}
android_ripple={rippleConfig}
{...other}
>
{ButtonElement}
</PlatformPressable>
);
}
export function defaultRenderVisibleButton(
visibleButtonProps: VisibleButtonProps
): React.ReactElement {
const { IconComponent, iconSize, color, iconName, title, buttonStyle } =
visibleButtonProps;
return IconComponent && iconName ? (
<IconComponent
name={iconName}
color={color}
size={iconSize}
style={buttonStyle}
/>
) : (
<Text style={[styles.text, { color }, buttonStyle]}>{title}</Text>
);
}
const styles = StyleSheet.create({
text: {
...Platform.select({
android: {
fontFamily: 'sans-serif-medium',
fontSize: 14,
textTransform: 'uppercase',
},
default: {
fontSize: 17,
textTransform: 'capitalize',
},
}),
},
buttonContainer: {
alignItems: 'center',
justifyContent: 'center',
},
});
const rippleConfig = {
foreground: true,
borderless: true,
radius: 20,
};
const buttonHitSlop = { top: 5, bottom: 5, left: 5, right: 5 };