@lynx-js/lynx-ui-swipe-action 0.0.0 → 3.130.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/LICENSE +201 -0
- package/README.md +44 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +313 -0
- package/dist/index.jsx.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.docs.d.ts +102 -0
- package/dist/types/index.docs.d.ts.map +1 -0
- package/dist/types/index.jsx +0 -0
- package/package.json +47 -4
- package/src/index.tsx +592 -0
- package/src/types/index.docs.ts +109 -0
- package/src/types/index.ts +5 -0
- package/tsconfig.docs.json +7 -0
- package/tsconfig.json +9 -0
- /package/{index.js → dist/types/index.docs.jsx} +0 -0
package/src/index.tsx
ADDED
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import type { ForwardedRef } from '@lynx-js/react'
|
|
6
|
+
import {
|
|
7
|
+
forwardRef,
|
|
8
|
+
memo,
|
|
9
|
+
runOnBackground,
|
|
10
|
+
runOnMainThread,
|
|
11
|
+
useImperativeHandle,
|
|
12
|
+
useMainThreadRef,
|
|
13
|
+
useState,
|
|
14
|
+
} from '@lynx-js/react'
|
|
15
|
+
|
|
16
|
+
import { NativeGesture, useGesture } from '@lynx-js/gesture-runtime'
|
|
17
|
+
import type { NativeGestureChangeEvent } from '@lynx-js/gesture-runtime'
|
|
18
|
+
import { noop, selectorMT } from '@lynx-js/lynx-ui-common'
|
|
19
|
+
|
|
20
|
+
import type {
|
|
21
|
+
SwipeActionProps,
|
|
22
|
+
SwipeActionRef,
|
|
23
|
+
SwipeAction as swipeActionType,
|
|
24
|
+
} from './types/index'
|
|
25
|
+
|
|
26
|
+
export type { SwipeActionProps, SwipeActionRef }
|
|
27
|
+
|
|
28
|
+
export const SwipeAction = memo(forwardRef(SwipeActionImpl)) as swipeActionType
|
|
29
|
+
|
|
30
|
+
function SwipeActionImpl(
|
|
31
|
+
props: SwipeActionProps,
|
|
32
|
+
ref: ForwardedRef<SwipeActionRef>,
|
|
33
|
+
) {
|
|
34
|
+
const {
|
|
35
|
+
enableSwipe,
|
|
36
|
+
swipeActionId = 'swipeActionId',
|
|
37
|
+
style,
|
|
38
|
+
className,
|
|
39
|
+
displayArea,
|
|
40
|
+
actionArea,
|
|
41
|
+
estimatedActionAreaSize = 0,
|
|
42
|
+
onAction = noop,
|
|
43
|
+
onSwipeStart = noop,
|
|
44
|
+
onSwipeEnd = noop,
|
|
45
|
+
debugLog = false,
|
|
46
|
+
iosEnableSimultaneousTouch = true,
|
|
47
|
+
} = props
|
|
48
|
+
const sampleCurveX = (tx: number, a: number, b: number, c: number) => {
|
|
49
|
+
'main thread'
|
|
50
|
+
return ((a * tx + b) * tx + c) * tx
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const sampleCurveY = (ty: number, a: number, b: number, c: number) => {
|
|
54
|
+
'main thread'
|
|
55
|
+
return ((a * ty + b) * ty + c) * ty
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const solveCurveX = (
|
|
59
|
+
x: number,
|
|
60
|
+
ax: number,
|
|
61
|
+
bx: number,
|
|
62
|
+
cx: number,
|
|
63
|
+
epsilon = 1e-6,
|
|
64
|
+
) => {
|
|
65
|
+
'main thread'
|
|
66
|
+
let t2 = x
|
|
67
|
+
let x2: number, d2: number
|
|
68
|
+
// Newton's method for solving the t-value corresponding to the x-coordinate of a curve.
|
|
69
|
+
for (let i = 0; i < 8; i++) {
|
|
70
|
+
x2 = sampleCurveX(t2, ax, bx, cx) - x
|
|
71
|
+
if (Math.abs(x2) < epsilon) {
|
|
72
|
+
return t2
|
|
73
|
+
}
|
|
74
|
+
d2 = (3 * ax * t2 + 2 * bx) * t2 + cx
|
|
75
|
+
if (Math.abs(d2) < 1e-6) {
|
|
76
|
+
break
|
|
77
|
+
}
|
|
78
|
+
t2 = t2 - x2 / d2
|
|
79
|
+
}
|
|
80
|
+
// Bisection method.
|
|
81
|
+
let t0 = 0
|
|
82
|
+
let t1 = 1
|
|
83
|
+
t2 = x
|
|
84
|
+
while (t0 < t1) {
|
|
85
|
+
x2 = sampleCurveX(t2, ax, bx, cx)
|
|
86
|
+
if (Math.abs(x2 - x) < epsilon) {
|
|
87
|
+
return t2
|
|
88
|
+
}
|
|
89
|
+
if (x > x2) {
|
|
90
|
+
t0 = t2
|
|
91
|
+
} else {
|
|
92
|
+
t1 = t2
|
|
93
|
+
}
|
|
94
|
+
t2 = (t1 - t0) * 0.5 + t0
|
|
95
|
+
}
|
|
96
|
+
return t2 // Approximate solution.
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const cubicBezier = (
|
|
100
|
+
t: number,
|
|
101
|
+
p1x: number,
|
|
102
|
+
p1y: number,
|
|
103
|
+
p2x: number,
|
|
104
|
+
p2y: number,
|
|
105
|
+
) => {
|
|
106
|
+
'main thread'
|
|
107
|
+
// calculate the coefficients of a polynomial
|
|
108
|
+
const cx = 3 * p1x
|
|
109
|
+
const bx = 3 * (p2x - p1x) - cx
|
|
110
|
+
const ax = 1 - cx - bx
|
|
111
|
+
|
|
112
|
+
const cy = 3 * p1y
|
|
113
|
+
const by = 3 * (p2y - p1y) - cy
|
|
114
|
+
const ay = 1 - cy - by
|
|
115
|
+
|
|
116
|
+
return sampleCurveY(solveCurveX(t, ax, bx, cx), ay, by, cy)
|
|
117
|
+
}
|
|
118
|
+
enum movingDirection {
|
|
119
|
+
left = 0,
|
|
120
|
+
right = 1,
|
|
121
|
+
}
|
|
122
|
+
const animationDuration = 350 // ms
|
|
123
|
+
const transformEllison = 0.03 // px
|
|
124
|
+
const swipeMinimumVelocity = useMainThreadRef<number>(0.1) // px/ms If velocity is less than this, it will not be considered as a swipe
|
|
125
|
+
const prevTouchPointX = useMainThreadRef<number>(0)
|
|
126
|
+
const prevTouchPointY = useMainThreadRef<number>(0)
|
|
127
|
+
const [displayAreaSize, setDisplayAreaSize] = useState(0)
|
|
128
|
+
|
|
129
|
+
const lastMovingDirection = useMainThreadRef<movingDirection>(0)
|
|
130
|
+
|
|
131
|
+
const currentTransform = useMainThreadRef<number>(0)
|
|
132
|
+
const lastTouchMoveGestureEvent = useMainThreadRef<NativeGestureChangeEvent>()
|
|
133
|
+
const transformInTouchStart = useMainThreadRef<number>(0)
|
|
134
|
+
|
|
135
|
+
const transformStartPosition = useMainThreadRef<number>(0)
|
|
136
|
+
const currentVelocity = useMainThreadRef<number>(0)
|
|
137
|
+
const isFirstJudge = useMainThreadRef<boolean>(true)
|
|
138
|
+
const isStartWithHorizontal = useMainThreadRef<boolean>(false)
|
|
139
|
+
const currentAnimationFrame = useMainThreadRef<number>(-1)
|
|
140
|
+
|
|
141
|
+
const swipeGesture: NativeGesture = useGesture(NativeGesture)
|
|
142
|
+
|
|
143
|
+
const calculateVelocityGesture = (event: NativeGestureChangeEvent) => {
|
|
144
|
+
'main thread'
|
|
145
|
+
if (event == null || lastTouchMoveGestureEvent.current == null) {
|
|
146
|
+
currentVelocity.current = 0
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
const deltaTime = event.params.timestamp
|
|
150
|
+
- (lastTouchMoveGestureEvent.current?.params.timestamp ?? 0)
|
|
151
|
+
const deltaX = event.params.clientX
|
|
152
|
+
- lastTouchMoveGestureEvent.current?.params.clientX
|
|
153
|
+
const velocity = deltaX / deltaTime
|
|
154
|
+
currentVelocity.current = velocity
|
|
155
|
+
lastTouchMoveGestureEvent.current = event
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const [actionAreaSize, setActionAreaSizeInner] = useState(
|
|
159
|
+
estimatedActionAreaSize,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
const actionAreaSizeMTRef = useMainThreadRef<number>(
|
|
163
|
+
estimatedActionAreaSize,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
const totalAreaSize = displayAreaSize + actionAreaSize
|
|
167
|
+
|
|
168
|
+
const clearPreviousSwipeAnimation = () => {
|
|
169
|
+
'main thread'
|
|
170
|
+
if (currentAnimationFrame.current !== -1) {
|
|
171
|
+
cancelAnimationFrame(currentAnimationFrame.current)
|
|
172
|
+
currentAnimationFrame.current = -1
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function setActionAreaSize(value: number) {
|
|
177
|
+
setActionAreaSizeInner(value)
|
|
178
|
+
runOnMainThread((areaSize: number) => {
|
|
179
|
+
'main thread'
|
|
180
|
+
actionAreaSizeMTRef.current = areaSize
|
|
181
|
+
})(value)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const setTransformProperty = (value: number) => {
|
|
185
|
+
'main thread'
|
|
186
|
+
selectorMT(swipeActionId)?.setStyleProperty(
|
|
187
|
+
'transform',
|
|
188
|
+
`translateX(${value}px)`,
|
|
189
|
+
)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const setTransform = (value: number) => {
|
|
193
|
+
'main thread'
|
|
194
|
+
if (Math.abs(value) < transformEllison) {
|
|
195
|
+
setTransformProperty(0)
|
|
196
|
+
currentTransform.current = 0
|
|
197
|
+
return
|
|
198
|
+
}
|
|
199
|
+
// Clamp delta to valid range [-actionAreaSize, 0]
|
|
200
|
+
let delta = Math.min(0, value)
|
|
201
|
+
delta = Math.max(delta, -actionAreaSizeMTRef.current)
|
|
202
|
+
|
|
203
|
+
setTransformProperty(delta)
|
|
204
|
+
if (debugLog) {
|
|
205
|
+
console.info(
|
|
206
|
+
'setTransform id',
|
|
207
|
+
swipeActionId,
|
|
208
|
+
delta,
|
|
209
|
+
actionAreaSizeMTRef.current,
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
currentTransform.current = delta
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const isHorizontalTouchMoveGesture = (event: NativeGestureChangeEvent) => {
|
|
216
|
+
'main thread'
|
|
217
|
+
|
|
218
|
+
const deltaX = event.params.clientX - prevTouchPointX.current
|
|
219
|
+
|
|
220
|
+
const deltaY = event.params.clientY - prevTouchPointY.current
|
|
221
|
+
return Math.abs(deltaX) >= Math.abs(deltaY)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
swipeGesture.onTouchesDown((event, manager) => {
|
|
225
|
+
'main thread'
|
|
226
|
+
if (debugLog) {
|
|
227
|
+
console.info(swipeActionId, 'touch start')
|
|
228
|
+
}
|
|
229
|
+
manager.interceptGesture(true)
|
|
230
|
+
|
|
231
|
+
isFirstJudge.current = true
|
|
232
|
+
// initialize touch point for this gesture
|
|
233
|
+
|
|
234
|
+
prevTouchPointY.current = event.params.clientY
|
|
235
|
+
|
|
236
|
+
prevTouchPointX.current = event.params.clientX
|
|
237
|
+
transformStartPosition.current = currentTransform.current
|
|
238
|
+
transformInTouchStart.current = currentTransform.current
|
|
239
|
+
runOnBackground(onSwipeStart)(swipeActionId)
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
swipeGesture.onTouchesMove((event, manager) => {
|
|
243
|
+
'main thread'
|
|
244
|
+
if (!enableSwipe) {
|
|
245
|
+
if (debugLog) {
|
|
246
|
+
console.info(swipeActionId, 'swipe disabled')
|
|
247
|
+
}
|
|
248
|
+
return
|
|
249
|
+
}
|
|
250
|
+
if (!isFirstJudge.current && !isStartWithHorizontal.current) {
|
|
251
|
+
if (debugLog) {
|
|
252
|
+
console.info(swipeActionId, 'swipe block due to vertical gesture')
|
|
253
|
+
}
|
|
254
|
+
return
|
|
255
|
+
}
|
|
256
|
+
clearPreviousSwipeAnimation()
|
|
257
|
+
|
|
258
|
+
const currentTouchPointX = event.params.clientX
|
|
259
|
+
|
|
260
|
+
const currentTouchPointY = event.params.clientY
|
|
261
|
+
const deltaX = currentTouchPointX - prevTouchPointX.current
|
|
262
|
+
if (isFirstJudge.current) {
|
|
263
|
+
isFirstJudge.current = false
|
|
264
|
+
// only in the first judgement, we change the status of isStartWithHorizontal
|
|
265
|
+
isStartWithHorizontal.current = isHorizontalTouchMoveGesture(event)
|
|
266
|
+
if (isStartWithHorizontal.current) {
|
|
267
|
+
manager.interceptGesture(true)
|
|
268
|
+
} else {
|
|
269
|
+
manager.interceptGesture(false)
|
|
270
|
+
manager.fail()
|
|
271
|
+
|
|
272
|
+
prevTouchPointX.current = currentTouchPointX
|
|
273
|
+
|
|
274
|
+
prevTouchPointY.current = currentTouchPointY
|
|
275
|
+
return
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
prevTouchPointX.current = currentTouchPointX
|
|
280
|
+
|
|
281
|
+
prevTouchPointY.current = currentTouchPointY
|
|
282
|
+
|
|
283
|
+
calculateVelocityGesture(event)
|
|
284
|
+
// Record last gesture to determine swipe direction is right to delete or left to undo
|
|
285
|
+
lastMovingDirection.current = deltaX > 0
|
|
286
|
+
? movingDirection.left
|
|
287
|
+
: movingDirection.right
|
|
288
|
+
|
|
289
|
+
transformStartPosition.current = currentTransform.current ?? 0
|
|
290
|
+
|
|
291
|
+
if (debugLog) {
|
|
292
|
+
console.info(deltaX, transformStartPosition.current)
|
|
293
|
+
}
|
|
294
|
+
setTransform(deltaX + transformStartPosition.current)
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
const easingInOut = (t: number) => {
|
|
298
|
+
'main thread'
|
|
299
|
+
return cubicBezier(t, 0, 0, 0.4, 1)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const swipeAnimation = (
|
|
303
|
+
startTime: number,
|
|
304
|
+
startSwipePoint: number,
|
|
305
|
+
distanceToSwipe: number,
|
|
306
|
+
consumedSwipedTime: number,
|
|
307
|
+
isSwipeRight: boolean,
|
|
308
|
+
) => {
|
|
309
|
+
'main thread'
|
|
310
|
+
const swipeAnimationInner = () => {
|
|
311
|
+
const currentTime = Date.now() - startTime + consumedSwipedTime
|
|
312
|
+
const timePercentage = currentTime / animationDuration
|
|
313
|
+
const percentage = easingInOut(timePercentage)
|
|
314
|
+
if (isSwipeRight) {
|
|
315
|
+
setTransform(startSwipePoint + distanceToSwipe * percentage)
|
|
316
|
+
} else {
|
|
317
|
+
setTransform(distanceToSwipe * (1 - percentage))
|
|
318
|
+
}
|
|
319
|
+
if (debugLog) {
|
|
320
|
+
console.info(
|
|
321
|
+
'currentTime',
|
|
322
|
+
currentTime,
|
|
323
|
+
percentage,
|
|
324
|
+
distanceToSwipe,
|
|
325
|
+
distanceToSwipe * (1 - percentage),
|
|
326
|
+
)
|
|
327
|
+
}
|
|
328
|
+
if (timePercentage < 1) {
|
|
329
|
+
currentAnimationFrame.current = requestAnimationFrame(
|
|
330
|
+
swipeAnimationInner,
|
|
331
|
+
)
|
|
332
|
+
} else {
|
|
333
|
+
if (isSwipeRight) {
|
|
334
|
+
setTransform(-actionAreaSizeMTRef.current)
|
|
335
|
+
} else {
|
|
336
|
+
setTransform(0)
|
|
337
|
+
}
|
|
338
|
+
currentAnimationFrame.current = -1
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
swipeAnimationInner()
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const swipeLeftToUndo = (isCanceling: boolean) => {
|
|
345
|
+
'main thread'
|
|
346
|
+
// Undo
|
|
347
|
+
// |-------------actionAreaSize---------------|
|
|
348
|
+
// |<<<<<<<<<<<<<<<<<<<<<<<|------------------|
|
|
349
|
+
// |----distanceToSwipe----|--distanceSwiped--|
|
|
350
|
+
if (debugLog) {
|
|
351
|
+
console.info(swipeActionId, 'swipeLeftToUndo, isCanceling', isCanceling)
|
|
352
|
+
}
|
|
353
|
+
const distanceToSwipe = currentTransform.current
|
|
354
|
+
const distanceSwipedPercentage = 1
|
|
355
|
+
- Math.abs(currentTransform.current) / actionAreaSizeMTRef.current
|
|
356
|
+
// Make sure cancel process has the same duration as swipe
|
|
357
|
+
const consumedSwipedTime = isCanceling
|
|
358
|
+
? 0
|
|
359
|
+
: animationDuration * distanceSwipedPercentage
|
|
360
|
+
const startTime = Date.now()
|
|
361
|
+
clearPreviousSwipeAnimation()
|
|
362
|
+
swipeAnimation(
|
|
363
|
+
startTime,
|
|
364
|
+
currentTransform.current,
|
|
365
|
+
distanceToSwipe,
|
|
366
|
+
consumedSwipedTime,
|
|
367
|
+
false,
|
|
368
|
+
)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const swipeRightToAction = (isCanceling: boolean) => {
|
|
372
|
+
'main thread'
|
|
373
|
+
// Delete
|
|
374
|
+
// |-------------actionAreaSize---------------|
|
|
375
|
+
// |------------------|>>>>>>>>>>>>>>>>>>>>>>>|
|
|
376
|
+
// |--distanceSwiped--|----distanceToSwipe----|
|
|
377
|
+
if (debugLog) {
|
|
378
|
+
console.info(
|
|
379
|
+
swipeActionId,
|
|
380
|
+
'swipeRightToAction, isCanceling',
|
|
381
|
+
isCanceling,
|
|
382
|
+
)
|
|
383
|
+
}
|
|
384
|
+
const distanceToSwipe = -actionAreaSizeMTRef.current
|
|
385
|
+
- currentTransform.current
|
|
386
|
+
const distanceSwipedPercentage = Math.abs(currentTransform.current)
|
|
387
|
+
/ actionAreaSizeMTRef.current
|
|
388
|
+
const consumedSwipedTime = isCanceling
|
|
389
|
+
? 0
|
|
390
|
+
: animationDuration * distanceSwipedPercentage
|
|
391
|
+
const startTime = Date.now()
|
|
392
|
+
clearPreviousSwipeAnimation()
|
|
393
|
+
swipeAnimation(
|
|
394
|
+
startTime,
|
|
395
|
+
currentTransform.current,
|
|
396
|
+
distanceToSwipe,
|
|
397
|
+
consumedSwipedTime,
|
|
398
|
+
true,
|
|
399
|
+
)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const swipeWithEasingInOut = () => {
|
|
403
|
+
'main thread'
|
|
404
|
+
if (debugLog) {
|
|
405
|
+
console.info(
|
|
406
|
+
swipeActionId,
|
|
407
|
+
'swipeWithEasingInOut, lastMovingDirection',
|
|
408
|
+
lastMovingDirection.current,
|
|
409
|
+
)
|
|
410
|
+
}
|
|
411
|
+
if (lastMovingDirection.current === movingDirection.right) {
|
|
412
|
+
swipeRightToAction(false)
|
|
413
|
+
} else {
|
|
414
|
+
swipeLeftToUndo(false)
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const swipeCancelDueToSmallVelocity = () => {
|
|
419
|
+
'main thread'
|
|
420
|
+
if (
|
|
421
|
+
Math.abs(currentTransform.current)
|
|
422
|
+
> actionAreaSizeMTRef.current * 0.5
|
|
423
|
+
) {
|
|
424
|
+
swipeRightToAction(true)
|
|
425
|
+
} else {
|
|
426
|
+
swipeLeftToUndo(true)
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const sendSwipeEndJS = () => {
|
|
431
|
+
if (onSwipeEnd) {
|
|
432
|
+
onSwipeEnd(swipeActionId)
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const clearTouchInfo = () => {
|
|
437
|
+
'main thread'
|
|
438
|
+
prevTouchPointX.current = 0
|
|
439
|
+
prevTouchPointY.current = 0
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
swipeGesture.onTouchesUp((_event, _manager) => {
|
|
443
|
+
'main thread'
|
|
444
|
+
if (debugLog) {
|
|
445
|
+
console.info(
|
|
446
|
+
'touchEnd',
|
|
447
|
+
swipeActionId,
|
|
448
|
+
enableSwipe,
|
|
449
|
+
prevTouchPointX.current,
|
|
450
|
+
)
|
|
451
|
+
}
|
|
452
|
+
isFirstJudge.current = true
|
|
453
|
+
// don't need to judge if enableSwipe is true or not, whenever there is a touchEnd.
|
|
454
|
+
if (!prevTouchPointX.current) {
|
|
455
|
+
console.error('ERROR! previous touch point is null')
|
|
456
|
+
return
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
clearTouchInfo()
|
|
460
|
+
|
|
461
|
+
if (debugLog) {
|
|
462
|
+
console.info('scroll end velocity', currentVelocity.current)
|
|
463
|
+
}
|
|
464
|
+
if (isStartWithHorizontal.current) {
|
|
465
|
+
if (Math.abs(currentVelocity.current) > swipeMinimumVelocity.current) {
|
|
466
|
+
swipeWithEasingInOut()
|
|
467
|
+
} else {
|
|
468
|
+
swipeCancelDueToSmallVelocity()
|
|
469
|
+
}
|
|
470
|
+
runOnBackground(sendSwipeEndJS)()
|
|
471
|
+
}
|
|
472
|
+
isStartWithHorizontal.current = false
|
|
473
|
+
})
|
|
474
|
+
|
|
475
|
+
const showActionAreaMTS = (animated: boolean) => {
|
|
476
|
+
'main thread'
|
|
477
|
+
if (animated) {
|
|
478
|
+
swipeRightToAction(false)
|
|
479
|
+
} else {
|
|
480
|
+
clearPreviousSwipeAnimation()
|
|
481
|
+
setTransform(-actionAreaSizeMTRef.current)
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const showActionArea = (animated: boolean) => {
|
|
486
|
+
runOnMainThread(showActionAreaMTS)(animated)
|
|
487
|
+
sendSwipeEndJS()
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const closeActionAreaMTS = (animated: boolean) => {
|
|
491
|
+
'main thread'
|
|
492
|
+
if (debugLog) {
|
|
493
|
+
console.info('closeActionAreaMTS', swipeActionId)
|
|
494
|
+
}
|
|
495
|
+
clearPreviousSwipeAnimation()
|
|
496
|
+
if (animated) {
|
|
497
|
+
swipeLeftToUndo(false)
|
|
498
|
+
} else {
|
|
499
|
+
setTransform(0)
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const closeActionArea = (animated: boolean) => {
|
|
504
|
+
runOnMainThread(closeActionAreaMTS)(animated)
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
useImperativeHandle(
|
|
508
|
+
ref,
|
|
509
|
+
() => ({
|
|
510
|
+
showActionArea,
|
|
511
|
+
closeActionArea,
|
|
512
|
+
}),
|
|
513
|
+
[showActionArea, closeActionArea],
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
return (
|
|
517
|
+
<view
|
|
518
|
+
style={{
|
|
519
|
+
...style,
|
|
520
|
+
display: 'linear',
|
|
521
|
+
linearOrientation: 'horizontal',
|
|
522
|
+
width: `${totalAreaSize}px`,
|
|
523
|
+
}}
|
|
524
|
+
className={className}
|
|
525
|
+
id={swipeActionId}
|
|
526
|
+
enable-new-animator={false}
|
|
527
|
+
flatten={false}
|
|
528
|
+
main-thread:gesture={swipeGesture}
|
|
529
|
+
main-thread:binduiappear={() => {
|
|
530
|
+
'main thread'
|
|
531
|
+
setTransform(currentTransform.current)
|
|
532
|
+
}}
|
|
533
|
+
ios-enable-simultaneous-touch={iosEnableSimultaneousTouch}
|
|
534
|
+
>
|
|
535
|
+
<view
|
|
536
|
+
id={`${swipeActionId}-displayArea`}
|
|
537
|
+
style={{ height: '100%' }}
|
|
538
|
+
main-thread:binduiappear={() => {
|
|
539
|
+
'main thread'
|
|
540
|
+
setTransform(currentTransform.current)
|
|
541
|
+
}}
|
|
542
|
+
binduiappear={() => {
|
|
543
|
+
lynx
|
|
544
|
+
.createSelectorQuery()
|
|
545
|
+
.select(`#${swipeActionId}-displayArea`)
|
|
546
|
+
.invoke({
|
|
547
|
+
method: 'boundingClientRect',
|
|
548
|
+
success: (res: unknown) => {
|
|
549
|
+
// @ts-expect-error expected
|
|
550
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
551
|
+
setDisplayAreaSize(res.width)
|
|
552
|
+
},
|
|
553
|
+
})
|
|
554
|
+
.exec()
|
|
555
|
+
lynx
|
|
556
|
+
.createSelectorQuery()
|
|
557
|
+
.select(`#${swipeActionId}-actionArea`)
|
|
558
|
+
.invoke({
|
|
559
|
+
method: 'boundingClientRect',
|
|
560
|
+
success: (res: unknown) => {
|
|
561
|
+
// @ts-expect-error expected
|
|
562
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
563
|
+
setActionAreaSize(res.width)
|
|
564
|
+
},
|
|
565
|
+
})
|
|
566
|
+
.exec()
|
|
567
|
+
}}
|
|
568
|
+
main-thread:binduidisappear={() => {
|
|
569
|
+
'main thread'
|
|
570
|
+
setTransformProperty(0)
|
|
571
|
+
clearTouchInfo()
|
|
572
|
+
clearPreviousSwipeAnimation()
|
|
573
|
+
currentTransform.current = 0
|
|
574
|
+
transformStartPosition.current = 0
|
|
575
|
+
}}
|
|
576
|
+
>
|
|
577
|
+
{displayArea}
|
|
578
|
+
</view>
|
|
579
|
+
<view
|
|
580
|
+
id={`${swipeActionId}-actionArea`}
|
|
581
|
+
style={{ height: '100%' }}
|
|
582
|
+
main-thread:bindtap={() => {
|
|
583
|
+
'main thread'
|
|
584
|
+
setTransform(0)
|
|
585
|
+
runOnBackground(onAction)(swipeActionId)
|
|
586
|
+
}}
|
|
587
|
+
>
|
|
588
|
+
{actionArea}
|
|
589
|
+
</view>
|
|
590
|
+
</view>
|
|
591
|
+
)
|
|
592
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import type { ForwardedRef, ReactElement } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import type { ComponentBasicProps } from '@lynx-js/lynx-ui-common'
|
|
8
|
+
|
|
9
|
+
export type SwipeAction = (props: SwipeActionProps) => ReactElement
|
|
10
|
+
|
|
11
|
+
export interface SwipeActionProps extends ComponentBasicProps {
|
|
12
|
+
ref?: ForwardedRef<SwipeActionRef>
|
|
13
|
+
/**
|
|
14
|
+
* Whether allow swipe action. When it set to false, this item can only be used to display content.
|
|
15
|
+
* @zh 是否允许滑动操作。当设置为 false 时,SwipeAction 只能用来显示内容。
|
|
16
|
+
* @defaultValue true
|
|
17
|
+
* @Android
|
|
18
|
+
* @iOS
|
|
19
|
+
*/
|
|
20
|
+
enableSwipe: boolean
|
|
21
|
+
/**
|
|
22
|
+
* Id of the SwipeAction. The onSwipeStart, onSwipeEnd and onAction will be called with this id.
|
|
23
|
+
* @zh SwipeAction 的id。
|
|
24
|
+
* @Android
|
|
25
|
+
* @iOS
|
|
26
|
+
*/
|
|
27
|
+
swipeActionId: string
|
|
28
|
+
/**
|
|
29
|
+
* Display area of the SwipeAction. The content of this area will be displayed on the left side of the SwipeAction.
|
|
30
|
+
* @zh SwipeAction 的内容。内容会显示在 SwipeAction 的左侧。
|
|
31
|
+
* @Android
|
|
32
|
+
* @iOS
|
|
33
|
+
*/
|
|
34
|
+
displayArea: ReactElement
|
|
35
|
+
/**
|
|
36
|
+
* Action area of the SwipeAction. The content of this area will be displayed on the right side of the SwipeAction. Before swipe or showActionArea called, this area will be hidden.
|
|
37
|
+
* @zh SwipeAction 的内容。内容会显示在 SwipeAction 的右侧。在用手滚动或调用 showActionArea 前,actionArea 不会显示。
|
|
38
|
+
* @Android
|
|
39
|
+
* @iOS
|
|
40
|
+
*/
|
|
41
|
+
actionArea: ReactElement
|
|
42
|
+
/**
|
|
43
|
+
* The size of the action area. If the swipeAction is used inside List, the reuse of the List may cause the actionArea to have wrong size. You can use this property to set the initial size of the actionArea.
|
|
44
|
+
* @zh actionArea 的大小。如果在 list 中使用 SwipeAction,list 的复用可能会导致操作区域大小错误。这个属性可用于指定一个初始大小,尽可能接近于排版渲染后的大小。
|
|
45
|
+
* @defaultValue 0
|
|
46
|
+
* @Android
|
|
47
|
+
* @iOS
|
|
48
|
+
*/
|
|
49
|
+
estimatedActionAreaSize?: number
|
|
50
|
+
/**
|
|
51
|
+
* The content of the SwipeAction. The content will be displayed on the left side of the SwipeAction.
|
|
52
|
+
* @zh SwipeAction 的内容. 内容会显示在 SwipeAction 的左侧.
|
|
53
|
+
* @Android
|
|
54
|
+
* @iOS
|
|
55
|
+
*/
|
|
56
|
+
children?: ReactElement
|
|
57
|
+
/**
|
|
58
|
+
* Force touch event to send to SwipeAction.
|
|
59
|
+
* @zh 制触摸事件发送到 SwipeAction.
|
|
60
|
+
* @defaultValue true
|
|
61
|
+
* @iOS
|
|
62
|
+
*/
|
|
63
|
+
iosEnableSimultaneousTouch?: boolean
|
|
64
|
+
/**
|
|
65
|
+
* Display debug logs. Open it when you find a bug.
|
|
66
|
+
* @zh 显示调试日志。当你发现 bug 时打开它
|
|
67
|
+
* @Android
|
|
68
|
+
* @iOS
|
|
69
|
+
*/
|
|
70
|
+
debugLog?: boolean
|
|
71
|
+
/**
|
|
72
|
+
* Triggered when the SwipeAction is swiped.
|
|
73
|
+
* @zh SwipeAction 手动滚动开始时触发。
|
|
74
|
+
* @Android
|
|
75
|
+
* @iOS
|
|
76
|
+
*/
|
|
77
|
+
onSwipeStart?: (id: string) => void
|
|
78
|
+
/**
|
|
79
|
+
* Triggered when the SwipeAction swipe ends.
|
|
80
|
+
* @zh SwipeAction 手动滚动结束时触发。
|
|
81
|
+
* @Android
|
|
82
|
+
* @iOS
|
|
83
|
+
*/
|
|
84
|
+
onSwipeEnd?: (id: string) => void
|
|
85
|
+
/**
|
|
86
|
+
* Triggered when the actionArea was tapped. Can be used to delete or other actions.
|
|
87
|
+
* @zh ActionArea 被点击时触发,可用于实现删除或其他操作。
|
|
88
|
+
* @Android
|
|
89
|
+
* @iOS
|
|
90
|
+
*/
|
|
91
|
+
onAction?: (id: string) => void
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface SwipeActionRef {
|
|
95
|
+
/**
|
|
96
|
+
* Call to display the actionArea. If animated is true, the actionArea will be displayed with animation.
|
|
97
|
+
* @zh 调用以显示 actionArea。如果 animated 为 true,则显示过程将带有动画效果。
|
|
98
|
+
* @Android
|
|
99
|
+
* @iOS
|
|
100
|
+
*/
|
|
101
|
+
showActionArea: (animated: boolean) => void
|
|
102
|
+
/**
|
|
103
|
+
* Call to hide the actionArea. If animated is true, the actionArea will be hidden with animation.
|
|
104
|
+
* @zh 调用以隐藏 actionArea. 如果 animated 为 true,则隐藏过程将带有动画效果。
|
|
105
|
+
* @Android
|
|
106
|
+
* @iOS
|
|
107
|
+
*/
|
|
108
|
+
closeActionArea: (animated: boolean) => void
|
|
109
|
+
}
|