@lynx-js/lynx-ui-common 3.130.0 → 3.133.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/hooks/index.d.ts +1 -3
  3. package/dist/hooks/index.d.ts.map +1 -1
  4. package/dist/hooks/index.jsx +2 -3
  5. package/dist/hooks/index.jsx.map +1 -1
  6. package/dist/hooks/usePreCommit.d.ts +15 -0
  7. package/dist/hooks/usePreCommit.d.ts.map +1 -0
  8. package/dist/hooks/usePreCommit.jsx +49 -0
  9. package/dist/hooks/usePreCommit.jsx.map +1 -0
  10. package/dist/types/interfaces/BaseScrollEvents.d.ts +0 -1
  11. package/dist/types/interfaces/BaseScrollEvents.d.ts.map +1 -1
  12. package/dist/types/interfaces/LazyInterface.d.ts +0 -1
  13. package/dist/types/interfaces/LazyInterface.d.ts.map +1 -1
  14. package/dist/types/interfaces/index.d.ts +0 -2
  15. package/dist/types/interfaces/index.d.ts.map +1 -1
  16. package/dist/utils/areDepsEqual.d.ts +6 -0
  17. package/dist/utils/areDepsEqual.d.ts.map +1 -0
  18. package/dist/utils/areDepsEqual.jsx +9 -0
  19. package/dist/utils/areDepsEqual.jsx.map +1 -0
  20. package/package.json +3 -3
  21. package/src/hooks/docs/useBounce.mdx +0 -1
  22. package/src/hooks/docs/useRefresh.mdx +0 -1
  23. package/src/hooks/index.ts +1 -3
  24. package/src/hooks/usePreCommit.tsx +111 -0
  25. package/src/types/interfaces/BaseScrollEvents.ts +0 -1
  26. package/src/types/interfaces/LazyInterface.tsx +0 -1
  27. package/src/types/interfaces/index.ts +0 -2
  28. package/src/utils/areDepsEqual.ts +26 -0
  29. package/dist/hooks/useBounce.d.ts +0 -26
  30. package/dist/hooks/useBounce.d.ts.map +0 -1
  31. package/dist/hooks/useBounce.jsx +0 -383
  32. package/dist/hooks/useBounce.jsx.map +0 -1
  33. package/dist/hooks/useRefresh.d.ts +0 -24
  34. package/dist/hooks/useRefresh.d.ts.map +0 -1
  35. package/dist/hooks/useRefresh.jsx +0 -554
  36. package/dist/hooks/useRefresh.jsx.map +0 -1
  37. package/dist/types/interfaces/BounceableInterface.d.ts +0 -120
  38. package/dist/types/interfaces/BounceableInterface.d.ts.map +0 -1
  39. package/dist/types/interfaces/BounceableInterface.jsx +0 -0
  40. package/dist/types/interfaces/RefreshInterface.d.ts +0 -98
  41. package/dist/types/interfaces/RefreshInterface.d.ts.map +0 -1
  42. package/dist/types/interfaces/RefreshInterface.jsx +0 -10
  43. package/dist/types/interfaces/RefreshInterface.jsx.map +0 -1
  44. package/src/hooks/useBounce.tsx +0 -814
  45. package/src/hooks/useRefresh.ts +0 -1054
  46. package/src/types/interfaces/BounceableInterface.ts +0 -125
  47. package/src/types/interfaces/RefreshInterface.ts +0 -105
@@ -1,814 +0,0 @@
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 { runOnBackground, useMainThreadRef } from '@lynx-js/react'
6
-
7
- import type { CommonEvent, MainThread, ScrollEvent } from '@lynx-js/types'
8
-
9
- import type { BounceableBasicProps, scrollToBouncesInfo } from '../types'
10
- import { selectorMT } from '../utils/selector'
11
-
12
- export interface useBounceOptions {
13
- bounceableOptions: BounceableBasicProps
14
- /**
15
- * idSelector of scroll container
16
- */
17
- id?: string
18
- /**
19
- * scroll direction
20
- */
21
- scrollOrientation: 'vertical' | 'horizontal'
22
- }
23
-
24
- export interface bounceHandlers {
25
- 'main-thread:bindtouchstart': (event: MainThread.TouchEvent) => void
26
- 'main-thread:bindtouchend': () => void
27
- 'main-thread:bindtouchmove': (event: MainThread.TouchEvent) => void
28
- 'main-thread:bindlayoutchange': (event: CommonEvent) => void
29
- 'main-thread:bindscroll': (event: ScrollEvent) => void
30
- onUpperExposure: (_event: CommonEvent) => void
31
- onUpperDisexposure: (_event: CommonEvent) => void
32
- onLowerExposure: (_event: CommonEvent) => void
33
- onLowerDisexposure: (_event: CommonEvent) => void
34
- }
35
-
36
- export function useBounce(options: useBounceOptions): bounceHandlers {
37
- // Control the damping coefficient during rubber effect. The smaller it is, the harder the spring effect.
38
- const rubberC = useMainThreadRef(0.55)
39
- // Control the fling deceleration coefficient during dropdown. The smaller it is, the faster it stops.
40
- const flingDeceleratingRate = useMainThreadRef(0.99)
41
- // The rebound coefficient during bounce back, the larger it is, the faster the rebound speed.
42
- const beta = useMainThreadRef(15)
43
-
44
- const debugLog = useMainThreadRef(
45
- options.bounceableOptions.debugLog ?? false,
46
- )
47
-
48
- const singleSidedBounce = useMainThreadRef(
49
- options.bounceableOptions.singleSidedBounce ?? 'both',
50
- )
51
-
52
- // The starting point of current touch procedure
53
- const startTouch = useMainThreadRef<MainThread.TouchEvent['touches'] | null>(
54
- null,
55
- )
56
- // Record the previous touch and clear it on touchEnd. If current is not empty, then it means that the scrollable object is being dragged.
57
- const prevTouch = useMainThreadRef<MainThread.TouchEvent['touches'] | null>(
58
- null,
59
- )
60
-
61
- // Current scroll velocity.
62
- const scrollVelocity = useMainThreadRef(0)
63
- // Current bouncing position.
64
- const bouncingPositionInfo = useMainThreadRef({})
65
-
66
- // If reaches edge and bouncing starts during dragging, save the touch point and calculate later touch delta for rubberEffect.
67
- const startBouncingTouch = useMainThreadRef<
68
- MainThread.TouchEvent['touches'] | null
69
- >(null)
70
- // If reaches edge and bouncing starts during dragging, save current bouncingPosition and continues rubberEffect's calculation from this point.
71
- const startTouchBouncingDelta = useMainThreadRef({})
72
- // when touch starts during bouncing, save current bouncingPosition and continues rubberEffect's calculation from this point.
73
- const bouncingTouchStartPosition = useMainThreadRef({})
74
-
75
- // Used to record the previous scroll position and calculate current scroll velocity.
76
- const prevScroll = useMainThreadRef({})
77
-
78
- // scroll container's basic info.
79
- const height = useMainThreadRef(
80
- options.bounceableOptions.estimatedHeight
81
- ?? SystemInfo.pixelHeight / SystemInfo.pixelRatio,
82
- )
83
- const width = useMainThreadRef(
84
- options.bounceableOptions.estimatedWidth
85
- ?? SystemInfo.pixelWidth / SystemInfo.pixelRatio,
86
- )
87
- const scrollX = useMainThreadRef(0)
88
- const scrollY = useMainThreadRef(0)
89
-
90
- // Flags for ending bouncing animation.
91
- const touchEndFrameEnableFlag = useMainThreadRef(false)
92
- const touchingEndBouncingBackEnableFlag = useMainThreadRef(false)
93
- const flingEndWithBouncingEnableFlag = useMainThreadRef(false)
94
-
95
- // Flags for reaches upper/lower edge.
96
- // When content's size is smaller than viewport, these two flags are both true.
97
- const toUpper = useMainThreadRef(false)
98
- const toLower = useMainThreadRef(false)
99
-
100
- // Read from props
101
- const containerID = options.id ?? 'bounceableContainer'
102
- const { enableBounces, onScrollToBounces } = options.bounceableOptions
103
- const alwaysBouncing = options.bounceableOptions.alwaysBouncing ?? false
104
- const scrollOrientation = options.scrollOrientation ?? 'vertical'
105
- const enableBounceEventInFling =
106
- options.bounceableOptions.enableBounceEventInFling ?? true
107
- const startBounceTriggerDistance =
108
- options.bounceableOptions.startBounceTriggerDistance ?? 0
109
- const endBounceTriggerDistance =
110
- options.bounceableOptions.endBounceTriggerDistance ?? 0
111
- // Temporary workaround as requestAnimationFrame will crash below Lynx 2.15.2.
112
- const temporaryValidVersionCheckForRAF = useMainThreadRef(
113
- options.bounceableOptions.validAnimationVersion ?? false,
114
- )
115
-
116
- const bouncingStatus = {
117
- inScrollingRange: 0,
118
- upperBouncing: 1,
119
- lowerBouncing: 2,
120
- noBouncing: 3,
121
- alwaysBouncing: 4,
122
- }
123
-
124
- function isVertical() {
125
- 'main thread'
126
- return scrollOrientation === 'vertical'
127
- }
128
-
129
- function threshold() {
130
- 'main thread'
131
- return 1.0 / Number(SystemInfo.pixelRatio)
132
- }
133
-
134
- function isAndroid() {
135
- 'main thread'
136
- return SystemInfo.platform === 'Android'
137
- }
138
-
139
- function isEmpty(obj: Record<string, unknown>) {
140
- 'main thread'
141
- return !obj || Object.keys(obj).length === 0
142
- }
143
-
144
- function enableScroll(enable: boolean) {
145
- 'main thread'
146
- // Android will trigger scroll during bouncing, cancel it while bouncing.
147
- if (isAndroid()) {
148
- selectorMT(containerID)?.setAttribute('enable-scroll', enable)
149
- }
150
- }
151
-
152
- // Clear all temps when a touch procedure ends.
153
- function clearTouchInfo() {
154
- 'main thread'
155
- if (debugLog.current) {
156
- console.info('clearTouchInfo')
157
- }
158
- startTouch.current = null
159
- bouncingTouchStartPosition.current = 0
160
- prevTouch.current = null
161
- startBouncingTouch.current = null
162
- startTouchBouncingDelta.current = 0
163
- }
164
-
165
- // Temporary workaround as requestAnimationFrame will crash below Lynx 2.15.2.
166
- function customRequestAnimationFrame(animationFunc: () => void) {
167
- 'main thread'
168
- if (temporaryValidVersionCheckForRAF.current) {
169
- requestAnimationFrame(animationFunc)
170
- } else {
171
- setTimeout(animationFunc, 8)
172
- }
173
- }
174
-
175
- // Calculate current touch delta based on 'startPoint'. The startPoint here should be startBouncingTouch which means this bouncing effect happens during a touch procedure.
176
- function getCurrentDelta(event: MainThread.TouchEvent) {
177
- 'main thread'
178
- if (startTouch.current !== null) {
179
- if (startBouncingTouch.current === null) {
180
- return scrollOrientation === 'vertical'
181
- ? event.touches[0].pageY - startTouch.current[0].pageY
182
- : event.touches[0].pageX - startTouch.current[0].pageX
183
- } else {
184
- return scrollOrientation === 'vertical'
185
- ? event.touches[0].pageY - startBouncingTouch.current[0].pageY
186
- : event.touches[0].pageX - startBouncingTouch.current[0].pageX
187
- }
188
- }
189
- return 0
190
- }
191
-
192
- // Key bouncing status judgement.
193
- function getBouncingStatus() {
194
- 'main thread'
195
- // @ts-expect-error Expected
196
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
197
- const currentPosition = bouncingPositionInfo.current?.bouncingOffset ?? 0
198
- if (Boolean(toUpper.current) && Boolean(toLower.current)) {
199
- return alwaysBouncing
200
- ? bouncingStatus.alwaysBouncing
201
- : bouncingStatus.noBouncing
202
- }
203
-
204
- // upper bouncing
205
- const initialLayoutPosition = 0
206
- if (debugLog.current) {
207
- console.info(
208
- 'getBouncingStatus',
209
- initialLayoutPosition,
210
- 'currentPosition',
211
- currentPosition,
212
- )
213
- }
214
-
215
- if (currentPosition > initialLayoutPosition) {
216
- return bouncingStatus.upperBouncing
217
- }
218
-
219
- // bottom bouncing
220
- if (currentPosition < initialLayoutPosition) {
221
- return bouncingStatus.lowerBouncing
222
- }
223
- return bouncingStatus.inScrollingRange
224
- }
225
-
226
- // Whether fling and bouncing back step should be triggered.
227
- function shouldBounceWhenTouchEnd(status: number) {
228
- 'main thread'
229
- if (
230
- status === bouncingStatus.inScrollingRange
231
- || status === bouncingStatus.noBouncing
232
- ) {
233
- return false
234
- }
235
-
236
- if (
237
- status === bouncingStatus.upperBouncing
238
- || status === bouncingStatus.lowerBouncing
239
- ) {
240
- return true
241
- }
242
-
243
- if (status === bouncingStatus.alwaysBouncing) {
244
- return alwaysBouncing
245
- }
246
- return false
247
- }
248
-
249
- // Use translate to implement real bouncing effect
250
- function bouncingSetStyle(offset: number) {
251
- 'main thread'
252
- if (isNaN(Number(offset))) {
253
- console.info('ERROR! bouncingOffset is NaN')
254
- return
255
- }
256
- // update bouncingPositionInfo first
257
- if (isEmpty(bouncingPositionInfo.current)) {
258
- // @ts-expect-error expected
259
- bouncingPositionInfo.current.velocity = 0
260
- } else {
261
- // @ts-expect-error expected
262
- const timeDiff = Date.now() - bouncingPositionInfo.current.timeStamp
263
- // @ts-expect-error expected
264
- const offsetDiff = offset - bouncingPositionInfo.current.bouncingOffset
265
- || 0
266
- if (timeDiff > 0) {
267
- // @ts-expect-error expected
268
- bouncingPositionInfo.current.velocity = offsetDiff / timeDiff
269
- }
270
- // If timeDiff <= 0, we keep the previous velocity to maintain momentum continuity.
271
- }
272
- // @ts-expect-error expected
273
- bouncingPositionInfo.current.bouncingOffset = offset
274
- // @ts-expect-error expected
275
- bouncingPositionInfo.current.timeStamp = Date.now()
276
- // When bouncing effect ends, clear startTouch and bouncingTouchStartPosition
277
- if (offset === 0 && prevTouch.current !== null) {
278
- startTouch.current = prevTouch.current
279
- bouncingTouchStartPosition.current = {}
280
- }
281
- // trigger translate
282
- if (isVertical()) {
283
- selectorMT(containerID)?.setStyleProperty(
284
- 'transform',
285
- `translateY(${offset}px)`,
286
- )
287
- selectorMT(`${containerID}-upperBounceWrapper`)?.setStyleProperty(
288
- 'transform',
289
- `translateY(${offset}px)`,
290
- )
291
- selectorMT(`${containerID}-lowerBounceWrapper`)?.setStyleProperty(
292
- 'transform',
293
- `translateY(${offset}px)`,
294
- )
295
- } else {
296
- selectorMT(containerID)?.setStyleProperty(
297
- 'transform',
298
- `translateX(${offset}px)`,
299
- )
300
- selectorMT(`${containerID}-upperBounceWrapper`)?.setStyleProperty(
301
- 'transform',
302
- `translateX(${offset}px)`,
303
- )
304
- selectorMT(`${containerID}-lowerBounceWrapper`)?.setStyleProperty(
305
- 'transform',
306
- `translateX(${offset}px)`,
307
- )
308
- }
309
- }
310
-
311
- function onScrollToBouncesJS(info: scrollToBouncesInfo) {
312
- if (enableBounces && onScrollToBounces) {
313
- onScrollToBounces(info)
314
- }
315
- }
316
-
317
- // send the scrollToBounces event
318
- function triggerScrollToBouncesEvent(isUpper: boolean) {
319
- 'main thread'
320
- if (debugLog.current) {
321
- console.info('triggerScrollToBouncesEvent', isUpper ? 'upper' : 'lower')
322
- }
323
-
324
- const info: scrollToBouncesInfo = {
325
- direction: isUpper ? 'upper' : 'lower',
326
- }
327
- runOnBackground(onScrollToBouncesJS)(info)
328
- }
329
-
330
- function rubberEffect(isNegative: 1 | -1, delta: number) {
331
- 'main thread'
332
- const scrollViewFrameSize = isVertical() ? height.current : width.current
333
- // If the touchStartPosition is not zero, it means this rubber effect happens during a touch or another bouncing effect. So the real 'start delta' should be recalculated.
334
- const touchStartPosition = Math.abs(
335
- Number(
336
- isEmpty(startTouchBouncingDelta.current)
337
- ? bouncingTouchStartPosition.current
338
- : startTouchBouncingDelta.current,
339
- ),
340
- )
341
- // This deltaYForTouchStartPosition is the inverse function of rubberEffect function
342
- const deltaYForTouchStartPosition =
343
- (scrollViewFrameSize * touchStartPosition)
344
- / ((scrollViewFrameSize - touchStartPosition) * rubberC.current) || 0
345
- // Here it calculate the next touchDelta. deltaYForNextPosition = (touch point where bouncing happens + corresponding touch delta where previous bouncing position locates)
346
- // This isNegative is used to handle the upper/lower bounce. RubberEffect function is different in the range (-inf,0) and [0, inf). We only use the [0, inf) part.
347
- const deltaYForNextPosition = bouncingTouchStartPosition.current
348
- ? deltaYForTouchStartPosition + isNegative * delta
349
- : isNegative * delta
350
- // Real calculation. Note that we don't use the raw touchDelta for calculating, instead, we use deltaYForNextPosition.
351
- const rubberBandingDistance = Math.max(
352
- 0,
353
- (1.0
354
- - 1.0
355
- / ((deltaYForNextPosition * rubberC.current) / scrollViewFrameSize
356
- + 1.0))
357
- * scrollViewFrameSize,
358
- )
359
- if (debugLog.current) {
360
- console.info(
361
- 'rubberEffect',
362
- 'scrollViewFrameSize',
363
- scrollViewFrameSize,
364
- 'touchStartPosition',
365
- touchStartPosition,
366
- 'deltaYForTouchStartPosition',
367
- deltaYForTouchStartPosition,
368
- 'deltaYForNextPosition',
369
- deltaYForNextPosition,
370
- 'rubberBandingDistance',
371
- rubberBandingDistance,
372
- )
373
- }
374
-
375
- bouncingSetStyle(isNegative * rubberBandingDistance)
376
- }
377
-
378
- function triggerRubberEffectIfCrossingEdge(event: MainThread.TouchEvent) {
379
- 'main thread'
380
- const delta = getCurrentDelta(event)
381
- if (debugLog.current) {
382
- console.info(
383
- 'isCrossingEdge',
384
- 'delta',
385
- delta,
386
- 'toUpper',
387
- toUpper.current,
388
- 'toLower',
389
- toLower.current,
390
- )
391
- }
392
-
393
- // Crossing top range
394
- if (
395
- toUpper.current === true && delta > 0
396
- && (singleSidedBounce.current === 'both'
397
- || singleSidedBounce.current === 'upper')
398
- ) {
399
- enableScroll(false)
400
- rubberEffect(delta > 0 ? 1 : -1, delta)
401
- }
402
-
403
- // Crossing bottom range
404
- if (
405
- toLower.current === true && delta < 0
406
- && (singleSidedBounce.current === 'both'
407
- || singleSidedBounce.current === 'lower')
408
- ) {
409
- enableScroll(false)
410
- rubberEffect(delta > 0 ? 1 : -1, delta)
411
- }
412
- enableScroll(true)
413
- }
414
-
415
- // return true if scrollToBounces should be sent
416
- function isOverTriggerDistance() {
417
- 'main thread'
418
- // @ts-expect-error expected
419
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
420
- const bouncingTop: number = bouncingPositionInfo.current.bouncingOffset ?? 0
421
- const triggerDistance =
422
- // @ts-expect-error expected
423
- bouncingPositionInfo.current?.bouncingOffset > 0
424
- ? startBounceTriggerDistance
425
- : endBounceTriggerDistance
426
- const delta = Math.abs(bouncingTop) - Math.abs(triggerDistance)
427
- return delta > threshold()
428
- }
429
-
430
- function bouncingBack() {
431
- 'main thread'
432
- if (debugLog.current) {
433
- console.info('fling forward end')
434
-
435
- console.info('bounce back starts')
436
- }
437
-
438
- // step3: bounce back
439
- // @ts-expect-error expected
440
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
441
- const C1 = bouncingPositionInfo.current.bouncingOffset ?? 0
442
- const startTime = Date.now()
443
- const touchingEndBouncingBackFrame = () => {
444
- if (Boolean(touchingEndBouncingBackEnableFlag.current) === false) {
445
- return
446
- }
447
- const currentTime = Date.now() - startTime
448
- if (debugLog.current) {
449
- console.info('touchingEndBouncingBackFrame')
450
- }
451
-
452
- // critical damping
453
- const C2 = beta.current * C1
454
- const easedDistance = (C1 + C2 * (currentTime / 1000))
455
- * Math.pow(Math.E, -beta.current * (currentTime / 1000))
456
- bouncingSetStyle(easedDistance)
457
- if (Math.abs(easedDistance) < threshold()) {
458
- // Make sure it backs to 0
459
- bouncingSetStyle(0)
460
- if (debugLog.current) {
461
- console.info('bounce back ends')
462
- }
463
- } else {
464
- customRequestAnimationFrame(touchingEndBouncingBackFrame)
465
- }
466
- }
467
-
468
- customRequestAnimationFrame(touchingEndBouncingBackFrame)
469
- touchingEndBouncingBackEnableFlag.current = true
470
- }
471
-
472
- // bindtouchstart. Save startTouch and bouncingPosition if a bouncing effect is happening.
473
- function bounceableTouchStart(event: MainThread.TouchEvent) {
474
- 'main thread'
475
- startTouch.current = event.touches
476
- if (debugLog.current) {
477
- console.info('bounceableTouchStart')
478
- }
479
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
480
- bouncingTouchStartPosition.current =
481
- // @ts-expect-error expected
482
- bouncingPositionInfo.current?.bouncingOffset ?? 0
483
- // reset all animation flags. No animation should play during a touch.
484
- touchEndFrameEnableFlag.current = false
485
- touchingEndBouncingBackEnableFlag.current = false
486
- flingEndWithBouncingEnableFlag.current = false
487
- }
488
-
489
- function bounceableTouchMove(event: MainThread.TouchEvent) {
490
- 'main thread'
491
- prevTouch.current = event.touches
492
- if (startTouch.current === null) {
493
- // It means current situation is illegal. A touchMove event happens without a touchStart event beforehand. Or a touchMove event happens after a touchEnd event.
494
- console.info('ERROR! touch not started')
495
- } else {
496
- const delta = getCurrentDelta(event)
497
- const currentBouncingStatus = getBouncingStatus()
498
- // Judge the status and trigger rubberEffect. The TouchMove should only consider rubberEffect and nothing else.
499
- switch (currentBouncingStatus) {
500
- case bouncingStatus.upperBouncing: {
501
- enableScroll(false)
502
- rubberEffect(1, delta)
503
- if (debugLog.current) {
504
- console.info('bounceableTouchMove upperBouncing')
505
- }
506
- break
507
- }
508
- case bouncingStatus.lowerBouncing: {
509
- enableScroll(false)
510
- rubberEffect(-1, delta)
511
- if (debugLog.current) {
512
- console.info('bounceableTouchMove lowerBouncing')
513
- }
514
- break
515
- }
516
- case bouncingStatus.alwaysBouncing: {
517
- if (debugLog.current) {
518
- console.info('bounceableTouchMove alwaysBouncing')
519
- }
520
- if (alwaysBouncing) {
521
- triggerRubberEffectIfCrossingEdge(event)
522
- }
523
- break
524
- }
525
- case bouncingStatus.inScrollingRange: {
526
- if (debugLog.current) {
527
- console.info('bounceableTouchMove inScrollingRange')
528
- }
529
- triggerRubberEffectIfCrossingEdge(event)
530
- break
531
- }
532
- default:
533
- break
534
- }
535
- }
536
- }
537
-
538
- // bindtouchend. In touchEnd event the fling forward animation and bouncingBack animation should be triggered if necessary.
539
- function bounceableTouchEnd() {
540
- 'main thread'
541
- // Reset the state. The touch information should be reset, and we should no longer ban the enable-scroll.
542
- clearTouchInfo()
543
- enableScroll(true)
544
- if (debugLog.current) {
545
- console.info('bounceableTouchEnd')
546
- }
547
- if (shouldBounceWhenTouchEnd(getBouncingStatus())) {
548
- const startTime = Date.now()
549
- // @ts-expect-error expected
550
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
551
- const dragEndVelocity = bouncingPositionInfo.current?.velocity
552
- // @ts-expect-error expected
553
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
554
- const dragEndPosition = bouncingPositionInfo.current?.bouncingOffset ?? 0
555
- // Send scrolltobounces event.
556
- if (isOverTriggerDistance()) {
557
- triggerScrollToBouncesEvent(
558
- // @ts-expect-error expected
559
- bouncingPositionInfo.current?.bouncingOffset > 0,
560
- )
561
- }
562
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
563
- let currentVelocity: number = dragEndVelocity
564
- const touchEndProcessFrame = () => {
565
- if (Boolean(touchEndFrameEnableFlag.current) === false) {
566
- return
567
- }
568
- if (debugLog.current) {
569
- console.info('touchEndProcessFrame')
570
- }
571
- if (Math.abs(currentVelocity) <= threshold()) {
572
- // If the velocity is already under threshold, skip fling and start step3: bounce back.
573
- bouncingBack()
574
- } else {
575
- // If the drag end with a velocity, start fling to consume it.
576
- // step 2: fling down
577
- if (debugLog.current) {
578
- console.info('fling down start')
579
- }
580
- // This function is obtained by integrating the velocity function with a deceleration rate of flingDeceleratingRate.
581
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
582
- const distance: number = dragEndPosition
583
- + (dragEndVelocity
584
- * (Math.pow(
585
- flingDeceleratingRate.current,
586
- Date.now() - startTime,
587
- )
588
- - 1))
589
- / Math.log(flingDeceleratingRate.current)
590
- bouncingSetStyle(distance)
591
- currentVelocity = dragEndVelocity
592
- * Math.pow(flingDeceleratingRate.current, Date.now() - startTime)
593
- if (touchEndFrameEnableFlag.current) {
594
- customRequestAnimationFrame(touchEndProcessFrame)
595
- }
596
- }
597
- }
598
- customRequestAnimationFrame(touchEndProcessFrame)
599
- touchEndFrameEnableFlag.current = true
600
- }
601
- }
602
-
603
- // Save the bindscroll info and calculate velocity.
604
- function bounceableHandleScroll(event: ScrollEvent) {
605
- 'main thread'
606
- scrollX.current = event.detail.scrollLeft
607
- scrollY.current = event.detail.scrollTop
608
- if (!isEmpty(prevScroll.current)) {
609
- // @ts-expect-error expected
610
- const timeDuration = Date.now() - prevScroll.current.timestamp
611
- if (timeDuration > 0) {
612
- const deltaY = isVertical() // @ts-expect-error expected
613
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
614
- ? event.detail.scrollTop - prevScroll.current.detail.scrollTop // @ts-expect-error expected
615
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
616
- : event.detail.scrollLeft - prevScroll.current.detail.scrollLeft
617
- const velocity = deltaY / timeDuration
618
- if (Number.isFinite(velocity)) {
619
- scrollVelocity.current = velocity
620
- }
621
- }
622
- }
623
- prevScroll.current = event
624
- }
625
-
626
- function onUpperDisexposure(_event: CommonEvent) {
627
- 'main thread'
628
- if (debugLog.current) {
629
- console.info('upper disexposure')
630
- }
631
- toUpper.current = false
632
- }
633
-
634
- // Handle scrolltoupper event.
635
- function onUpperExposure(_event: CommonEvent) {
636
- 'main thread'
637
- if (debugLog.current) {
638
- console.info('upper exposure')
639
- }
640
- toUpper.current = true
641
- if (
642
- singleSidedBounce.current !== 'upper'
643
- && singleSidedBounce.current !== 'both'
644
- ) {
645
- return
646
- }
647
- if (prevScroll.current) {
648
- // reaches edge during touch
649
- startBouncingTouch.current = prevTouch.current
650
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
651
- startTouchBouncingDelta.current =
652
- // @ts-expect-error expected
653
- bouncingPositionInfo.current?.bouncingOffset ?? 0
654
- } else if (Math.abs(scrollVelocity.current) > threshold()) {
655
- // reaches edge during fling
656
- const velocity = scrollVelocity.current
657
- let currentVelocity = velocity
658
- const startTime = Date.now()
659
- if (debugLog.current) {
660
- console.info('fling to upper event start with velocity', velocity)
661
- }
662
- let sentScrollToBouncesEvent = false // only sent scrollToBounces once
663
- const flingEndWithBouncingFrame = () => {
664
- if (Boolean(flingEndWithBouncingEnableFlag.current) === false) {
665
- if (debugLog.current) {
666
- console.info('flingEndWithBouncingEnableFlag early return')
667
- }
668
- return
669
- }
670
- if (Math.abs(currentVelocity) <= threshold()) {
671
- flingEndWithBouncingEnableFlag.current = false
672
- if (debugLog.current) {
673
- console.info('fling to upper event end')
674
-
675
- console.info('bouncing to upper event start')
676
- }
677
- bouncingBack()
678
- } else {
679
- // step 1: fling
680
- const distance = (currentVelocity
681
- * (Math.pow(flingDeceleratingRate.current, Date.now() - startTime)
682
- - 1))
683
- / Math.log(flingDeceleratingRate.current)
684
- bouncingSetStyle(-distance)
685
- if (!sentScrollToBouncesEvent && isOverTriggerDistance()) {
686
- triggerScrollToBouncesEvent(
687
- // @ts-expect-error expected
688
- bouncingPositionInfo.current?.bouncingOffset > 0,
689
- )
690
- sentScrollToBouncesEvent = true
691
- }
692
- currentVelocity = velocity
693
- * Math.pow(flingDeceleratingRate.current, Date.now() - startTime)
694
- if (flingEndWithBouncingEnableFlag.current) {
695
- customRequestAnimationFrame(flingEndWithBouncingFrame)
696
- }
697
- }
698
- }
699
- customRequestAnimationFrame(flingEndWithBouncingFrame)
700
- flingEndWithBouncingEnableFlag.current = true
701
- }
702
- }
703
-
704
- function onLowerDisexposure(_event: CommonEvent) {
705
- 'main thread'
706
- if (debugLog.current) {
707
- console.info('lower disexposure')
708
- }
709
- toLower.current = false
710
- }
711
-
712
- function onLowerExposure(_event: CommonEvent) {
713
- 'main thread'
714
- if (debugLog.current) {
715
- console.info('lower exposure')
716
- }
717
- toLower.current = true
718
- if (
719
- singleSidedBounce.current !== 'lower'
720
- && singleSidedBounce.current !== 'both'
721
- ) {
722
- return
723
- }
724
- // reaches lower during dragging
725
- if (prevTouch.current !== null) {
726
- startBouncingTouch.current = prevTouch.current
727
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
728
- startTouchBouncingDelta.current =
729
- // @ts-expect-error expected
730
- bouncingPositionInfo.current?.bouncingOffset ?? 0
731
- } else if (enableBounceEventInFling && scrollVelocity.current > 0) {
732
- const velocity = scrollVelocity.current
733
- if (debugLog.current) {
734
- console.info('lower velocity', velocity)
735
- }
736
-
737
- // scroll to end with velocity, start bouncing effect
738
- if (Math.abs(velocity) > threshold()) {
739
- let currentVelocity = velocity
740
- const startTime = Date.now()
741
- if (debugLog.current) {
742
- console.info('fling to lower event start with velocity', velocity)
743
- }
744
- let sentScrollToBouncesEvent = false // only sent scrollToBounces once
745
- const flingEndWithBouncingFrame = () => {
746
- if (Boolean(flingEndWithBouncingEnableFlag.current) === false) {
747
- if (debugLog.current) {
748
- console.info('flingEndWithBouncingEnableFlag early return')
749
- }
750
- return
751
- }
752
- if (Math.abs(currentVelocity) <= threshold()) {
753
- flingEndWithBouncingEnableFlag.current = false
754
- if (debugLog.current) {
755
- console.info('fling to lower event end')
756
-
757
- console.info('bouncing to lower event start')
758
- }
759
- bouncingBack()
760
- } else {
761
- // step 1: fling
762
- const distance = (currentVelocity
763
- * (Math.pow(
764
- flingDeceleratingRate.current,
765
- Date.now() - startTime,
766
- )
767
- - 1))
768
- / Math.log(flingDeceleratingRate.current)
769
- bouncingSetStyle(-distance)
770
- if (!sentScrollToBouncesEvent && isOverTriggerDistance()) {
771
- triggerScrollToBouncesEvent(
772
- // @ts-expect-error expected
773
- bouncingPositionInfo.current?.bouncingOffset > 0,
774
- )
775
- sentScrollToBouncesEvent = true
776
- }
777
- currentVelocity = velocity
778
- * Math.pow(flingDeceleratingRate.current, Date.now() - startTime)
779
- if (flingEndWithBouncingEnableFlag) {
780
- customRequestAnimationFrame(flingEndWithBouncingFrame)
781
- }
782
- }
783
- }
784
- customRequestAnimationFrame(flingEndWithBouncingFrame)
785
- flingEndWithBouncingEnableFlag.current = true
786
- }
787
- }
788
- }
789
-
790
- // Used to get the size of scroll container.
791
- function bounceableLayoutChange(event: CommonEvent) {
792
- 'main thread'
793
- // @ts-expect-error expected
794
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
795
- height.current = (isAndroid() ? event.params?.height : event.detail?.height)
796
- ?? 0
797
- // @ts-expect-error expected
798
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
799
- width.current = (isAndroid() ? event.params?.width : event.detail?.width)
800
- ?? 0
801
- }
802
-
803
- return {
804
- 'main-thread:bindtouchstart': bounceableTouchStart,
805
- 'main-thread:bindtouchend': bounceableTouchEnd,
806
- 'main-thread:bindtouchmove': bounceableTouchMove,
807
- 'main-thread:bindlayoutchange': bounceableLayoutChange,
808
- 'main-thread:bindscroll': bounceableHandleScroll,
809
- onUpperExposure,
810
- onUpperDisexposure,
811
- onLowerExposure,
812
- onLowerDisexposure,
813
- }
814
- }