Repository navigation
Expand file tree
/
Copy pathTouchable.tsx
More file actions
241 lines (212 loc) · 7.55 KB
/
Copy pathTouchable.tsx
File metadata and controls
241 lines (212 loc) · 7.55 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import React, { use, useCallback, useMemo, useRef, useState } from 'react';
import type { NativeSyntheticEvent } from 'react-native';
import { Platform } from 'react-native';
import GestureHandlerButton, {
type ButtonEvent,
} from '../../../components/GestureHandlerButton';
import { getTVProps } from '../../../components/utils';
import { getNextHandlerTag } from '../../../handlers/getNextHandlerTag';
import { getModuleId } from '../../../moduleId';
import {
isKeyboardDismissingTap,
JSResponderContext,
updateResponderEventValue,
} from '../../scrollViewInterop';
import type { AnimationDuration, TouchableProps } from './TouchableProps';
const isAndroid = Platform.OS === 'android';
const TRANSPARENT_RIPPLE = { rippleColor: 'transparent' as const };
const DEFAULT_IN_DURATION_MS = 50;
const DEFAULT_OUT_DURATION_MS = 100;
// Clamp user-supplied durations to finite, non-negative milliseconds.
// Negative, NaN, or Infinity values would produce invalid CSS transitions
// on web and negative setTimeout delays in branch 3 of the press-out path.
function sanitizeDuration(value: number): number {
return Number.isFinite(value) && value >= 0 ? value : 0;
}
function resolveAnimationDuration(value: AnimationDuration | undefined) {
if (value === undefined) {
return {
tapAnimationInDuration: DEFAULT_IN_DURATION_MS,
tapAnimationOutDuration: DEFAULT_OUT_DURATION_MS,
longPressAnimationOutDuration: DEFAULT_OUT_DURATION_MS,
hoverAnimationInDuration: DEFAULT_IN_DURATION_MS,
hoverAnimationOutDuration: DEFAULT_OUT_DURATION_MS,
};
}
if (typeof value === 'number') {
const sanitized = sanitizeDuration(value);
return {
tapAnimationInDuration: sanitized,
tapAnimationOutDuration: sanitized,
longPressAnimationOutDuration: sanitized,
hoverAnimationInDuration: sanitized,
hoverAnimationOutDuration: sanitized,
};
}
// The union guarantees variant 2 supplies top-level `in`/`out`, variant 3
// supplies both category objects — so per-category fallback to base is
// always defined for well-typed input; the 0 fallbacks here are unreachable.
const baseIn = 'in' in value ? value.in : 0;
const baseOut = 'out' in value ? value.out : 0;
const tapOut = value.tap?.out ?? baseOut;
return {
tapAnimationInDuration: sanitizeDuration(value.tap?.in ?? baseIn),
tapAnimationOutDuration: sanitizeDuration(tapOut),
longPressAnimationOutDuration: sanitizeDuration(
value.longPress?.out ?? tapOut
),
hoverAnimationInDuration: sanitizeDuration(value.hover?.in ?? baseIn),
hoverAnimationOutDuration: sanitizeDuration(value.hover?.out ?? baseOut),
};
}
export const Touchable = (props: TouchableProps) => {
const {
underlayColor = 'transparent',
defaultUnderlayOpacity = 0,
activeUnderlayOpacity = 0.105,
defaultOpacity = 1,
animationDuration,
androidRipple,
delayLongPress = 600,
onLongPress,
onPress,
onPressIn,
onPressOut,
onHoverIn,
onHoverOut,
children,
disabled = false,
cancelOnLeave = true,
immediatePressOut = true,
ref,
...rest
} = props;
const [handlerTag] = useState(() => getNextHandlerTag());
const resolvedDurations = resolveAnimationDuration(animationDuration);
const resolvedDelayLongPress = sanitizeDuration(delayLongPress);
const shouldUseNativeRipple = isAndroid && androidRipple !== undefined;
const jsResponderContext = use(JSResponderContext);
const dropKeyboardTapRef = useRef<boolean | null>(null);
// The button handles presses natively, without a NativeDetector, so it has
// to mark its responder events as RNGH-handled itself — otherwise a tap on
// it looks unhandled to an enclosing ScrollView in
// keyboardShouldPersistTaps='handled' mode and dismisses the keyboard.
const handleStartShouldSetResponderCapture = useCallback(() => {
if (!disabled) {
updateResponderEventValue(jsResponderContext, true);
}
return false;
}, [disabled, jsResponderContext]);
const internalOnPress = useCallback(
(e: NativeSyntheticEvent<ButtonEvent>) => {
if (dropKeyboardTapRef.current) {
return;
}
onPress?.(e.nativeEvent);
},
[onPress]
);
const internalOnPressIn = useCallback(
(e: NativeSyntheticEvent<ButtonEvent>) => {
// PressIn opens every press sequence; capture the verdict once per
// sequence so a re-entry PressIn (with cancelOnLeave={false}) doesn't
// overwrite it after the keyboard is already dismissed.
dropKeyboardTapRef.current ??=
isKeyboardDismissingTap(jsResponderContext);
if (dropKeyboardTapRef.current) {
return;
}
onPressIn?.(e.nativeEvent);
},
[jsResponderContext, onPressIn]
);
const internalOnPressOut = useCallback(
(e: NativeSyntheticEvent<ButtonEvent>) => {
if (dropKeyboardTapRef.current) {
return;
}
onPressOut?.(e.nativeEvent);
},
[onPressOut]
);
const internalOnLongPress = useCallback(
(e: NativeSyntheticEvent<ButtonEvent>) => {
if (dropKeyboardTapRef.current) {
return;
}
onLongPress?.(e.nativeEvent);
},
[onLongPress]
);
// Left undefined when the corresponding prop is absent so web can skip
// building a hover payload nobody consumes — it costs a synchronous layout
// read per pointer enter/leave. The native platforms emit either way.
const internalOnHoverIn = useMemo(
() =>
onHoverIn
? (e: NativeSyntheticEvent<ButtonEvent>) => onHoverIn(e.nativeEvent)
: undefined,
[onHoverIn]
);
const internalOnHoverOut = useMemo(
() =>
onHoverOut
? (e: NativeSyntheticEvent<ButtonEvent>) => onHoverOut(e.nativeEvent)
: undefined,
[onHoverOut]
);
// InteractionFinished is dispatched after the terminal PressOut/Press
// events, so resetting synchronously here is safe.
const internalOnInteractionFinished = useCallback(() => {
dropKeyboardTapRef.current = null;
}, []);
const rippleProps = shouldUseNativeRipple
? {
rippleColor: androidRipple?.color,
rippleRadius: androidRipple?.radius,
borderless: androidRipple?.borderless,
foreground: androidRipple?.foreground,
}
: TRANSPARENT_RIPPLE;
const tvProps = getTVProps(rest);
const hitSlop =
typeof props.hitSlop === 'number'
? {
top: props.hitSlop,
left: props.hitSlop,
bottom: props.hitSlop,
right: props.hitSlop,
}
: props.hitSlop;
return (
<GestureHandlerButton
{...rest}
{...tvProps}
{...rippleProps}
{...resolvedDurations}
ref={ref ?? null}
enabled={!disabled}
onStartShouldSetResponderCapture={handleStartShouldSetResponderCapture}
moduleId={getModuleId()}
handlerTag={handlerTag}
cancelOnLeave={cancelOnLeave}
gestureTestID={props.testID}
gestureHitSlop={hitSlop}
defaultOpacity={defaultOpacity}
defaultUnderlayOpacity={defaultUnderlayOpacity}
activeUnderlayOpacity={activeUnderlayOpacity}
underlayColor={underlayColor}
longPressDuration={resolvedDelayLongPress}
immediatePressOut={immediatePressOut}
hasLongPressHandler={onLongPress !== undefined}
onButtonPress={internalOnPress}
onButtonPressIn={internalOnPressIn}
onButtonPressOut={internalOnPressOut}
onButtonLongPress={internalOnLongPress}
onButtonHoverIn={internalOnHoverIn}
onButtonHoverOut={internalOnHoverOut}
onButtonInteractionFinished={internalOnInteractionFinished}>
{children}
</GestureHandlerButton>
);
};