forked from vonovak/react-navigation-header-buttons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeaderButton.js
94 lines (84 loc) · 2.16 KB
/
HeaderButton.js
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
/**
* @flow
*/
import * as React from 'react';
import { StyleSheet, View, TouchableWithoutFeedback, Platform } from 'react-native';
import TouchableItem from './TouchableItem';
import type { ViewStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet';
import { useTheme } from '@react-navigation/native';
const BUTTON_HIT_SLOP = Object.freeze({ top: 5, bottom: 5, left: 5, right: 5 });
// for renderVisibleButton() function
export type VisibleButtonProps = {
IconComponent?: React.ComponentType<any>,
iconSize?: number,
color?: string,
iconName?: string,
title: string,
buttonStyle?: ViewStyleProp,
};
// from <Item />
export type ItemProps = {
...$Exact<React.ElementConfig<typeof TouchableWithoutFeedback>>,
...VisibleButtonProps,
onPress: ?() => any,
style?: ViewStyleProp,
};
type OtherProps = {
background?: any,
foreground?: any,
pressColor?: any,
renderButtonElement: (VisibleButtonProps) => React.Element<any>,
...
};
export type HeaderButtonProps = ItemProps & OtherProps;
export function HeaderButton(props: HeaderButtonProps): React.Node {
const { colors, dark } = useTheme();
const themeColor = Platform.select({
ios: colors.primary,
default: colors.text,
});
const themePressColorAndroid = dark ? 'rgba(255, 255, 255, .32)' : 'rgba(0, 0, 0, .32)';
const {
onPress,
style,
renderButtonElement,
background,
iconName,
title,
buttonStyle,
IconComponent,
iconSize,
color,
pressColor,
...other
} = props;
const usedColor = color || themeColor;
const usedPressColor = pressColor || themePressColorAndroid;
const ButtonElement = renderButtonElement({
iconName,
title,
buttonStyle,
IconComponent,
iconSize,
color: usedColor,
});
return (
<TouchableItem
borderless
onPress={onPress}
hitSlop={BUTTON_HIT_SLOP}
rippleRadius={20}
style={StyleSheet.compose(styles.buttonContainer, style)}
pressColor={usedPressColor}
{...other}
>
<View>{ButtonElement}</View>
</TouchableItem>
);
}
const styles = StyleSheet.create({
buttonContainer: {
alignItems: 'center',
justifyContent: 'center',
},
});