-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathTouchableRipple.native.tsx
More file actions
171 lines (152 loc) · 4.33 KB
/
TouchableRipple.native.tsx
File metadata and controls
171 lines (152 loc) · 4.33 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as React from 'react';
import {
PressableAndroidRippleConfig,
StyleProp,
Platform,
ViewStyle,
StyleSheet,
GestureResponderEvent,
View,
ColorValue,
} from 'react-native';
import type { PressableProps } from './Pressable';
import { Pressable } from './Pressable';
import { getTouchableRippleColors } from './utils';
import { Settings, SettingsContext } from '../../core/settings';
import { useInternalTheme } from '../../core/theming';
import type { ThemeProp } from '../../types';
import { forwardRef } from '../../utils/forwardRef';
import hasTouchHandler from '../../utils/hasTouchHandler';
const ANDROID_VERSION_LOLLIPOP = 21;
const ANDROID_VERSION_PIE = 28;
export type Props = PressableProps & {
borderless?: boolean;
background?: PressableAndroidRippleConfig;
centered?: boolean;
disabled?: boolean;
onPress?: (e: GestureResponderEvent) => void | null;
onLongPress?: (e: GestureResponderEvent) => void;
onPressIn?: (e: GestureResponderEvent) => void;
onPressOut?: (e: GestureResponderEvent) => void;
rippleColor?: ColorValue;
underlayColor?: string;
children: React.ReactNode;
style?: StyleProp<ViewStyle>;
theme?: ThemeProp;
/**
* Debounce time in milliseconds to prevent rapid successive presses.
* When set, subsequent onPress calls within this time window will be ignored.
*/
debounce?: number;
};
const TouchableRipple = (
{
style,
background,
borderless = false,
disabled: disabledProp,
rippleColor,
underlayColor,
children,
theme: themeOverrides,
debounce,
...rest
}: Props,
ref: React.ForwardedRef<View>
) => {
const theme = useInternalTheme(themeOverrides);
const { rippleEffectEnabled } = React.useContext<Settings>(SettingsContext);
const { onPress, onLongPress, onPressIn, onPressOut } = rest;
const lastPressTime = React.useRef<number>(0);
const debouncedOnPress = React.useCallback(
(e: GestureResponderEvent) => {
if (!onPress) return;
if (debounce && debounce > 0) {
const now = Date.now();
if (now - lastPressTime.current < debounce) {
return; // Ignore this press as it's within the debounce window
}
lastPressTime.current = now;
}
onPress(e);
},
[onPress, debounce]
);
const hasPassedTouchHandler = hasTouchHandler({
onPress,
onLongPress,
onPressIn,
onPressOut,
});
const disabled = disabledProp || !hasPassedTouchHandler;
const { calculatedRippleColor, calculatedUnderlayColor } =
getTouchableRippleColors({
theme,
rippleColor,
underlayColor,
});
// A workaround for ripple on Android P is to use useForeground + overflow: 'hidden'
// https://github.com/facebook/react-native/issues/6480
const useForeground =
Platform.OS === 'android' &&
Platform.Version >= ANDROID_VERSION_PIE &&
borderless;
if (TouchableRipple.supported) {
const androidRipple = rippleEffectEnabled
? background ?? {
color: calculatedRippleColor,
borderless,
foreground: useForeground,
}
: undefined;
return (
<Pressable
{...rest}
ref={ref}
disabled={disabled}
style={[borderless && styles.overflowHidden, style]}
android_ripple={androidRipple}
onPress={debouncedOnPress}
>
{React.Children.only(children)}
</Pressable>
);
}
return (
<Pressable
{...rest}
ref={ref}
disabled={disabled}
style={[borderless && styles.overflowHidden, style]}
onPress={debouncedOnPress}
>
{({ pressed }: { pressed: boolean }) => (
<>
{pressed && rippleEffectEnabled && (
<View
testID="touchable-ripple-underlay"
style={[
styles.underlay,
{ backgroundColor: calculatedUnderlayColor },
]}
/>
)}
{React.Children.only(children)}
</>
)}
</Pressable>
);
};
TouchableRipple.supported =
Platform.OS === 'android' && Platform.Version >= ANDROID_VERSION_LOLLIPOP;
const styles = StyleSheet.create({
overflowHidden: {
overflow: 'hidden',
},
underlay: {
...StyleSheet.absoluteFillObject,
zIndex: 2,
},
});
const Component = forwardRef(TouchableRipple);
export default Component as typeof Component & { supported: boolean };