-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.tsx
126 lines (120 loc) · 3.21 KB
/
Button.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
119
120
121
122
123
124
125
126
import React from 'react';
import {
GestureResponderEvent,
LayoutChangeEvent,
Pressable,
StyleProp,
Text,
TextStyle,
ViewStyle,
} from 'react-native';
import type { ImageStyle } from 'react-native-fast-image';
import { useThemeContext } from '../contexts/ThemeContext';
import { getScaleFactor } from '../styles/createScaleFactor';
import createStyleSheet from '../styles/createStyleSheet';
import type { ButtonStateColor } from '../types';
import type { LocalIconName } from './Icon';
import { LocalIcon } from './Icon';
type ButtonProps = React.PropsWithChildren<{
icon?: LocalIconName | undefined;
iconStyle?: StyleProp<ImageStyle>;
disabled?: boolean | undefined;
onPress?: () => void | undefined;
style?: StyleProp<ViewStyle> | undefined;
color?: Partial<ButtonStateColor> | undefined;
font?: StyleProp<TextStyle> | undefined;
onPressIn?: ((event: GestureResponderEvent) => void) | null | undefined;
onPressOut?: ((event: GestureResponderEvent) => void) | null | undefined;
onLayout?: ((event: LayoutChangeEvent) => void) | undefined;
}>;
export default function Button({
icon,
iconStyle,
disabled,
onPress,
style,
color,
font,
children,
onPressIn,
onPressOut,
onLayout,
}: ButtonProps): JSX.Element {
const { colors, fonts } = useThemeContext();
const sf = getScaleFactor();
const getStateColor = (pressed: boolean, disabled?: boolean) => {
if (disabled) {
if (color?.disabled !== undefined) {
return color.disabled;
}
return colors.button.disabled;
}
if (pressed) {
if (color?.pressed !== undefined) {
return color.pressed;
}
return colors.button.pressed;
}
if (color?.enabled !== undefined) {
return color.enabled;
}
return colors.button.enabled;
};
return (
<Pressable
onLayout={onLayout}
disabled={disabled}
onPress={onPress}
onPressIn={onPressIn}
onPressOut={onPressOut}
style={({ pressed }) => {
const s = getStateColor(pressed, disabled);
return [{ backgroundColor: s.background }, styles.container, style];
}}
>
{({ pressed }) => {
const s = getStateColor(pressed, disabled);
return (
<React.Fragment>
{icon && (
<LocalIcon
size={sf(28)}
name={icon}
color={s.content}
containerStyle={[styles.icon, iconStyle]}
/>
)}
{typeof children === 'string' ? (
children.length !== 0 ? (
<Text
style={[
styles.text,
{ color: s.content },
fonts.button,
font,
]}
>
{children}
</Text>
) : null
) : (
children
)}
</React.Fragment>
);
}}
</Pressable>
);
}
const styles = createStyleSheet({
container: {
flexDirection: 'row',
paddingHorizontal: 8,
paddingVertical: 4,
borderRadius: 4,
alignItems: 'center',
justifyContent: 'center',
},
icon: { marginVertical: -4, marginRight: 8 },
text: {},
});