-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathTouchable.tsx
More file actions
217 lines (200 loc) · 5.6 KB
/
Touchable.tsx
File metadata and controls
217 lines (200 loc) · 5.6 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
import React, { useRef, memo } from 'react';
import Animated, { useSharedValue, useAnimatedStyle, withSpring } from 'react-native-reanimated';
import {
Gesture,
GestureDetector,
type GestureUpdateEvent,
type PanGestureHandlerEventPayload
} from 'react-native-gesture-handler';
import { View, type AccessibilityActionEvent } from 'react-native';
import { scheduleOnRN } from 'react-native-worklets';
import Touch from '../../Touch';
import { DeleteAction } from './Actions';
import { useTheme } from '../../../theme';
import I18n from '../../../i18n';
export interface ISwipeableDeleteTouchableProps {
children: JSX.Element;
testID: string;
width: number;
rowHeight: number;
actionWidth: number;
longSwipe: number;
smallSwipe: number;
backgroundColor: string;
onPress(): void;
onDeletePress(): void;
accessibilityLabel?: string;
accessibilityHint?: string;
}
const SwipeableDeleteTouchable = ({
width,
children,
testID,
rowHeight,
actionWidth,
longSwipe,
smallSwipe,
backgroundColor,
onPress,
onDeletePress,
accessibilityLabel,
accessibilityHint
}: ISwipeableDeleteTouchableProps): React.ReactElement => {
const { colors } = useTheme();
const transX = useSharedValue(0);
const rowOffSet = useSharedValue(0);
const rowState = useSharedValue(0);
const valueRef = useRef(0);
const handlePress = () => {
if (rowState.value !== 0) {
close();
return;
}
if (onPress) {
onPress();
}
};
const close = () => {
rowState.value = 0;
transX.value = withSpring(0, { overshootClamping: true });
rowOffSet.value = 0;
valueRef.current = 0;
};
const handleDeletePress = () => {
close();
if (onDeletePress) {
onDeletePress();
}
};
const onAccessibilityAction = (event: AccessibilityActionEvent) => {
switch (event.nativeEvent.actionName) {
case 'delete':
handleDeletePress();
break;
}
};
const handleRelease = (event: GestureUpdateEvent<PanGestureHandlerEventPayload>) => {
const { translationX } = event;
valueRef.current += translationX;
let toValue = 0;
if (rowState.value === 0) {
// if no option is opened
if (I18n.isRTL) {
// RTL: swipe right (positive translationX) to show delete
if (translationX > 0 && translationX < longSwipe) {
// open delete action if swipe right
toValue = actionWidth;
rowState.value = 1;
} else if (translationX >= longSwipe) {
// long swipe right - trigger delete immediately
toValue = 0;
rowState.value = 1;
handleDeletePress();
} else {
// any other gesture (including left swipes) - stay closed
toValue = 0;
}
} else if (translationX < 0 && translationX > -longSwipe) {
// LTR: open delete action if swipe left
toValue = -actionWidth;
rowState.value = 1;
} else if (translationX <= -longSwipe) {
// LTR: long swipe left - trigger delete immediately
toValue = 0;
rowState.value = 1;
handleDeletePress();
} else {
// LTR: any other gesture (including right swipes) - stay closed
toValue = 0;
}
} else if (rowState.value === 1) {
// if delete option is opened
if (I18n.isRTL) {
// RTL: delete is on the left (positive translation)
if (valueRef.current < smallSwipe) {
toValue = 0;
rowState.value = 0;
} else if (valueRef.current > longSwipe) {
handleDeletePress();
} else {
toValue = actionWidth;
}
} else if (valueRef.current > -smallSwipe) {
// LTR: close if swipe back right
toValue = 0;
rowState.value = 0;
} else if (valueRef.current < -longSwipe) {
// LTR: trigger delete on long swipe
handleDeletePress();
} else {
// LTR: keep delete action open
toValue = -actionWidth;
}
}
// Use spring animation exactly like RoomItem
transX.value = withSpring(toValue, { overshootClamping: true });
rowOffSet.value = toValue;
valueRef.current = toValue;
};
const panGesture = Gesture.Pan()
.activeOffsetX([-10, 10]) // More sensitive horizontal detection
.failOffsetY([-20, 20]) // Fail on vertical movement to distinguish scrolling
.onUpdate(event => {
const newValue = event.translationX + rowOffSet.value;
if (I18n.isRTL) {
// RTL: allow right swipes (positive values), prevent left swipes
if (newValue < 0) {
transX.value = 0;
} else {
transX.value = newValue;
// Limit how far right it can stretch
if (transX.value > width) transX.value = width;
}
} else if (newValue > 0) {
// LTR: prevent right swipes
transX.value = 0;
} else {
// LTR: allow left swipes (negative values)
transX.value = newValue;
// Limit how far left it can stretch
if (transX.value < -width) transX.value = -width;
}
})
.onEnd(event => {
scheduleOnRN(handleRelease, event);
});
const animatedStyles = useAnimatedStyle(() => ({
transform: [{ translateX: transX.value }]
}));
return (
<GestureDetector gesture={panGesture}>
<View>
<DeleteAction
width={width}
transX={transX}
rowHeight={rowHeight}
actionWidth={actionWidth}
longSwipe={longSwipe}
onDeletePress={handleDeletePress}
testID={`${testID}-delete`}
/>
<Animated.View style={animatedStyles}>
<Touch
onPress={handlePress}
testID={testID}
style={{
backgroundColor: backgroundColor || colors.surfaceLight
}}
accessible
accessibilityLabel={accessibilityLabel}
accessibilityHint={accessibilityHint}
accessibilityActions={[{ name: 'delete', label: I18n.t('Delete') }]}
onAccessibilityAction={onAccessibilityAction}>
{children}
</Touch>
</Animated.View>
</View>
</GestureDetector>
);
};
export default memo(SwipeableDeleteTouchable);