-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathButton.tsx
More file actions
99 lines (91 loc) · 2.34 KB
/
Button.tsx
File metadata and controls
99 lines (91 loc) · 2.34 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
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
import React, { useMemo } from "react";
import {
Platform,
TouchableNativeFeedback,
TouchableOpacity,
View,
} from "react-native";
import { useKeyboardState } from "../../hooks";
import type { KeyboardToolbarTheme } from "./types";
import type { PropsWithChildren } from "react";
import type { GestureResponderEvent, ViewStyle } from "react-native";
type ButtonProps = {
disabled?: boolean;
onPress: (event: GestureResponderEvent) => void;
accessibilityLabel: string;
accessibilityHint: string;
testID: string;
rippleRadius?: number;
style?: ViewStyle;
theme: KeyboardToolbarTheme;
};
const ButtonIOS = ({
children,
onPress,
disabled,
accessibilityLabel,
accessibilityHint,
testID,
style,
}: PropsWithChildren<ButtonProps>) => {
// immediately switch to plain view to avoid animation flickering
// when fade out animation happens and view becomes disabled
const Container = disabled
? (View as unknown as typeof TouchableOpacity)
: TouchableOpacity;
const accessibilityState = useMemo(() => ({ disabled }), [disabled]);
return (
<Container
accessibilityHint={accessibilityHint}
accessibilityLabel={accessibilityLabel}
accessibilityRole="button"
accessibilityState={accessibilityState}
style={style}
testID={testID}
onPress={onPress}
>
{children}
</Container>
);
};
const ButtonAndroid = ({
children,
onPress,
disabled,
accessibilityLabel,
accessibilityHint,
testID,
rippleRadius = 18,
style,
theme,
}: PropsWithChildren<ButtonProps>) => {
const colorScheme = useKeyboardState((state) => state.appearance);
const accessibilityState = useMemo(() => ({ disabled }), [disabled]);
const ripple = useMemo(
() =>
TouchableNativeFeedback.Ripple(
theme[colorScheme].ripple,
true,
rippleRadius,
),
[colorScheme, rippleRadius, theme],
);
return (
<TouchableNativeFeedback
accessibilityHint={accessibilityHint}
accessibilityLabel={accessibilityLabel}
accessibilityRole="button"
accessibilityState={accessibilityState}
background={ripple}
style={style}
testID={testID}
onPress={onPress}
>
<View style={style}>{children}</View>
</TouchableNativeFeedback>
);
};
export default Platform.select({
android: ButtonAndroid,
default: ButtonIOS,
});