@lynx-js/lynx-ui-swiper 0.0.0 → 3.131.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 (137) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/LICENSE +201 -0
  3. package/README.md +51 -0
  4. package/dist/Swiper/index.d.ts +11 -0
  5. package/dist/Swiper/index.d.ts.map +1 -0
  6. package/dist/Swiper/index.jsx +261 -0
  7. package/dist/Swiper/index.jsx.map +1 -0
  8. package/dist/Swiper/styles.css +47 -0
  9. package/dist/Swiper/styles.css.map +1 -0
  10. package/dist/SwiperItem/index.d.ts +46 -0
  11. package/dist/SwiperItem/index.d.ts.map +1 -0
  12. package/dist/SwiperItem/index.jsx +86 -0
  13. package/dist/SwiperItem/index.jsx.map +1 -0
  14. package/dist/const/index.d.ts +5 -0
  15. package/dist/const/index.d.ts.map +1 -0
  16. package/dist/const/index.jsx +7 -0
  17. package/dist/const/index.jsx.map +1 -0
  18. package/dist/hooks/useAnimate.d.ts +5 -0
  19. package/dist/hooks/useAnimate.d.ts.map +1 -0
  20. package/dist/hooks/useAnimate.jsx +47 -0
  21. package/dist/hooks/useAnimate.jsx.map +1 -0
  22. package/dist/hooks/useAutoPlay.d.ts +16 -0
  23. package/dist/hooks/useAutoPlay.d.ts.map +1 -0
  24. package/dist/hooks/useAutoPlay.jsx +58 -0
  25. package/dist/hooks/useAutoPlay.jsx.map +1 -0
  26. package/dist/hooks/useAxisLock.d.ts +12 -0
  27. package/dist/hooks/useAxisLock.d.ts.map +1 -0
  28. package/dist/hooks/useAxisLock.jsx +58 -0
  29. package/dist/hooks/useAxisLock.jsx.map +1 -0
  30. package/dist/hooks/useBounceConfig.d.ts +12 -0
  31. package/dist/hooks/useBounceConfig.d.ts.map +1 -0
  32. package/dist/hooks/useBounceConfig.jsx +15 -0
  33. package/dist/hooks/useBounceConfig.jsx.map +1 -0
  34. package/dist/hooks/useBounceView.d.ts +17 -0
  35. package/dist/hooks/useBounceView.d.ts.map +1 -0
  36. package/dist/hooks/useBounceView.jsx +16 -0
  37. package/dist/hooks/useBounceView.jsx.map +1 -0
  38. package/dist/hooks/useChange.d.ts +22 -0
  39. package/dist/hooks/useChange.d.ts.map +1 -0
  40. package/dist/hooks/useChange.jsx +53 -0
  41. package/dist/hooks/useChange.jsx.map +1 -0
  42. package/dist/hooks/useDirection.d.ts +13 -0
  43. package/dist/hooks/useDirection.d.ts.map +1 -0
  44. package/dist/hooks/useDirection.jsx +37 -0
  45. package/dist/hooks/useDirection.jsx.map +1 -0
  46. package/dist/hooks/useFirstScreenStyle.d.ts +34 -0
  47. package/dist/hooks/useFirstScreenStyle.d.ts.map +1 -0
  48. package/dist/hooks/useFirstScreenStyle.jsx +63 -0
  49. package/dist/hooks/useFirstScreenStyle.jsx.map +1 -0
  50. package/dist/hooks/useModeConfig.d.ts +14 -0
  51. package/dist/hooks/useModeConfig.d.ts.map +1 -0
  52. package/dist/hooks/useModeConfig.jsx +33 -0
  53. package/dist/hooks/useModeConfig.jsx.map +1 -0
  54. package/dist/hooks/useOffset.d.ts +37 -0
  55. package/dist/hooks/useOffset.d.ts.map +1 -0
  56. package/dist/hooks/useOffset.jsx +362 -0
  57. package/dist/hooks/useOffset.jsx.map +1 -0
  58. package/dist/hooks/useOffsetLimit.d.ts +16 -0
  59. package/dist/hooks/useOffsetLimit.d.ts.map +1 -0
  60. package/dist/hooks/useOffsetLimit.jsx +51 -0
  61. package/dist/hooks/useOffsetLimit.jsx.map +1 -0
  62. package/dist/hooks/useReset.d.ts +16 -0
  63. package/dist/hooks/useReset.d.ts.map +1 -0
  64. package/dist/hooks/useReset.jsx +68 -0
  65. package/dist/hooks/useReset.jsx.map +1 -0
  66. package/dist/hooks/useSwipeCallback.d.ts +14 -0
  67. package/dist/hooks/useSwipeCallback.d.ts.map +1 -0
  68. package/dist/hooks/useSwipeCallback.jsx +40 -0
  69. package/dist/hooks/useSwipeCallback.jsx.map +1 -0
  70. package/dist/hooks/useVelocity.d.ts +14 -0
  71. package/dist/hooks/useVelocity.d.ts.map +1 -0
  72. package/dist/hooks/useVelocity.jsx +62 -0
  73. package/dist/hooks/useVelocity.jsx.map +1 -0
  74. package/dist/hooks/useVelocity_experiment.d.ts +12 -0
  75. package/dist/hooks/useVelocity_experiment.d.ts.map +1 -0
  76. package/dist/hooks/useVelocity_experiment.jsx +38 -0
  77. package/dist/hooks/useVelocity_experiment.jsx.map +1 -0
  78. package/dist/index.d.ts +5 -0
  79. package/dist/index.d.ts.map +1 -0
  80. package/dist/index.jsx +3 -0
  81. package/dist/store/index.d.ts +3 -0
  82. package/dist/store/index.d.ts.map +1 -0
  83. package/dist/store/index.jsx +18 -0
  84. package/dist/store/index.jsx.map +1 -0
  85. package/dist/types/index.d.ts +62 -0
  86. package/dist/types/index.d.ts.map +1 -0
  87. package/dist/types/index.docs.d.ts +448 -0
  88. package/dist/types/index.docs.d.ts.map +1 -0
  89. package/dist/types/index.jsx +9 -0
  90. package/dist/types/index.jsx.map +1 -0
  91. package/dist/utils/animate.d.ts +4 -0
  92. package/dist/utils/animate.d.ts.map +1 -0
  93. package/dist/utils/animate.jsx +29 -0
  94. package/dist/utils/animate.jsx.map +1 -0
  95. package/dist/utils/compareProps.d.ts +2 -0
  96. package/dist/utils/compareProps.d.ts.map +1 -0
  97. package/dist/utils/compareProps.jsx +14 -0
  98. package/dist/utils/compareProps.jsx.map +1 -0
  99. package/dist/utils/customRound.d.ts +10 -0
  100. package/dist/utils/customRound.d.ts.map +1 -0
  101. package/dist/utils/customRound.jsx +9 -0
  102. package/dist/utils/customRound.jsx.map +1 -0
  103. package/dist/utils/index.d.ts +20 -0
  104. package/dist/utils/index.d.ts.map +1 -0
  105. package/dist/utils/index.jsx +55 -0
  106. package/dist/utils/index.jsx.map +1 -0
  107. package/package.json +47 -4
  108. package/src/Swiper/index.tsx +414 -0
  109. package/src/Swiper/styles.css +44 -0
  110. package/src/SwiperItem/index.tsx +205 -0
  111. package/src/const/index.ts +11 -0
  112. package/src/hooks/useAnimate.ts +80 -0
  113. package/src/hooks/useAutoPlay.ts +95 -0
  114. package/src/hooks/useAxisLock.ts +92 -0
  115. package/src/hooks/useBounceConfig.ts +29 -0
  116. package/src/hooks/useBounceView.tsx +50 -0
  117. package/src/hooks/useChange.ts +131 -0
  118. package/src/hooks/useDirection.ts +59 -0
  119. package/src/hooks/useFirstScreenStyle.ts +200 -0
  120. package/src/hooks/useModeConfig.ts +56 -0
  121. package/src/hooks/useOffset.ts +609 -0
  122. package/src/hooks/useOffsetLimit.ts +98 -0
  123. package/src/hooks/useReset.ts +144 -0
  124. package/src/hooks/useSwipeCallback.ts +66 -0
  125. package/src/hooks/useVelocity.ts +85 -0
  126. package/src/hooks/useVelocity_experiment.ts +50 -0
  127. package/src/index.ts +8 -0
  128. package/src/store/index.ts +23 -0
  129. package/src/types/index.docs.ts +465 -0
  130. package/src/types/index.ts +105 -0
  131. package/src/utils/animate.ts +49 -0
  132. package/src/utils/compareProps.ts +54 -0
  133. package/src/utils/customRound.ts +23 -0
  134. package/src/utils/index.ts +117 -0
  135. package/tsconfig.docs.json +7 -0
  136. package/tsconfig.json +9 -0
  137. /package/{index.js → dist/types/index.docs.jsx} +0 -0
@@ -0,0 +1,144 @@
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 { runOnMainThread, useEffect, useRef } from '@lynx-js/react'
6
+
7
+ import { useMemoizedFn } from '@lynx-js/lynx-ui-common'
8
+ import type { NodesRef } from '@lynx-js/types'
9
+
10
+ import type {
11
+ CompoundModeConfig,
12
+ ResetOptions,
13
+ SwiperProps,
14
+ UpdateSwiperInnerContainerOptions,
15
+ } from '../types'
16
+
17
+ interface UseResetOpts extends
18
+ Required<
19
+ Pick<
20
+ SwiperProps<unknown>,
21
+ | 'swiperKey'
22
+ | 'loop'
23
+ | 'itemWidth'
24
+ | 'itemHeight'
25
+ | 'containerWidth'
26
+ | 'initialIndex'
27
+ | 'loopDuplicateCount'
28
+ >
29
+ >
30
+ {
31
+ resetOnReuse: SwiperProps<unknown>['resetOnReuse']
32
+ dataCount: number
33
+ modeConfig: CompoundModeConfig
34
+ spaceBetween: number
35
+ resetOffsetMT: (fullReset: ResetOptions) => void
36
+ updateSwiperInnerContainerSizeMT: (
37
+ updateOptions: UpdateSwiperInnerContainerOptions,
38
+ ) => void
39
+ startAutoPlayMT: () => void
40
+ }
41
+
42
+ export function useReset({
43
+ swiperKey,
44
+ itemHeight,
45
+ itemWidth,
46
+ containerWidth,
47
+ loop,
48
+ dataCount,
49
+ modeConfig,
50
+ resetOffsetMT,
51
+ resetOnReuse,
52
+ initialIndex,
53
+ updateSwiperInnerContainerSizeMT,
54
+ loopDuplicateCount,
55
+ startAutoPlayMT,
56
+ spaceBetween,
57
+ }: UseResetOpts) {
58
+ const prevSwiperKey = useRef<unknown>(swiperKey)
59
+ const isFirstRun = useRef<boolean>(true)
60
+ const containerRef = useRef<NodesRef | null>(null)
61
+
62
+ function doResetMT(
63
+ resetOptions: ResetOptions & UpdateSwiperInnerContainerOptions,
64
+ ) {
65
+ 'main thread'
66
+ // Get props from function args, instead of automatically captured, to avoid staled states
67
+ const {
68
+ fullReset,
69
+ resetIndex,
70
+ dataCount: _dataCount,
71
+ itemWidth: _itemWidth,
72
+ loop: _loop,
73
+ loopDuplicateCount: _loopDuplicateCount,
74
+ spaceBetween: _gap,
75
+ itemHeight: _itemHeight,
76
+ } = resetOptions
77
+ resetOffsetMT({
78
+ fullReset,
79
+ resetIndex,
80
+ })
81
+ updateSwiperInnerContainerSizeMT({
82
+ dataCount: _dataCount,
83
+ itemWidth: _itemWidth,
84
+ loop: _loop,
85
+ loopDuplicateCount: _loopDuplicateCount,
86
+ spaceBetween: _gap,
87
+ itemHeight: _itemHeight,
88
+ mode: modeConfig.mode,
89
+ })
90
+ startAutoPlayMT()
91
+ }
92
+
93
+ const doReset = useMemoizedFn((fullReset: boolean) => {
94
+ runOnMainThread(doResetMT)({
95
+ fullReset,
96
+ resetIndex: fullReset ? initialIndex : undefined,
97
+ dataCount,
98
+ itemWidth,
99
+ loop,
100
+ loopDuplicateCount,
101
+ spaceBetween,
102
+ itemHeight,
103
+ mode: modeConfig.mode,
104
+ })
105
+ })
106
+
107
+ /**
108
+ * When used in list, while Swiper Component is new, container element will be reused, causing old styles are applied.
109
+ * When reused, ref setter will fire setRef with null, then with actual nodes
110
+ * We use this as the sign of reuse, and doing a clean up.
111
+ */
112
+ const setContainerRef = useMemoizedFn(
113
+ (ref: NodesRef) => {
114
+ if (containerRef.current === null && ref !== null && resetOnReuse) {
115
+ doReset(true)
116
+ }
117
+ containerRef.current = ref
118
+ },
119
+ )
120
+
121
+ useEffect(() => {
122
+ if (isFirstRun.current) {
123
+ isFirstRun.current = false
124
+ return
125
+ }
126
+ if (prevSwiperKey.current === swiperKey) {
127
+ doReset(false)
128
+ } else {
129
+ prevSwiperKey.current = swiperKey
130
+ doReset(true)
131
+ }
132
+ }, [
133
+ modeConfig,
134
+ itemWidth,
135
+ containerWidth,
136
+ loop,
137
+ dataCount,
138
+ itemHeight,
139
+ swiperKey,
140
+ loopDuplicateCount,
141
+ ])
142
+
143
+ return { setContainerRef }
144
+ }
@@ -0,0 +1,66 @@
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 { useMainThreadRef } from '@lynx-js/react'
6
+
7
+ import { useTapLock } from '@lynx-js/react-use'
8
+ import type { MainThread } from '@lynx-js/types'
9
+
10
+ /**
11
+ * Determine whether a touch is swipe or click.
12
+ */
13
+ export function useSwipeCallback({
14
+ onSwipeStartMT,
15
+ onSwipeStopMT,
16
+ }: {
17
+ onSwipeStartMT: () => void
18
+ onSwipeStopMT: () => void
19
+ }) {
20
+ const swipeStartedMTRef = useMainThreadRef<boolean>(false)
21
+
22
+ const {
23
+ handleTouchStart: tapLockTouchStart,
24
+ handleTouchMove: tapLockTouchMove,
25
+ } = useTapLock()
26
+
27
+ function setSwipeStartMT() {
28
+ 'main thread'
29
+ if (swipeStartedMTRef.current) {
30
+ return
31
+ }
32
+ swipeStartedMTRef.current = true
33
+ onSwipeStartMT()
34
+ }
35
+
36
+ function setSwipeEndMT() {
37
+ 'main thread'
38
+ if (swipeStartedMTRef.current) {
39
+ onSwipeStopMT()
40
+ swipeStartedMTRef.current = false
41
+ }
42
+ }
43
+
44
+ function handleTouchStart(event: MainThread.TouchEvent) {
45
+ 'main thread'
46
+
47
+ tapLockTouchStart(event)
48
+ }
49
+
50
+ function handleTouchMove(event: MainThread.TouchEvent) {
51
+ 'main thread'
52
+
53
+ if (!swipeStartedMTRef.current && !tapLockTouchMove(event)) {
54
+ swipeStartedMTRef.current = true
55
+ onSwipeStartMT()
56
+ return
57
+ }
58
+ }
59
+
60
+ return {
61
+ handleTouchStart,
62
+ handleTouchMove,
63
+ setSwipeEndMT,
64
+ setSwipeStartMT,
65
+ }
66
+ }
@@ -0,0 +1,85 @@
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 { useMainThreadRef } from '@lynx-js/react'
6
+
7
+ import type { MainThread } from '@lynx-js/types'
8
+
9
+ import { SwipeDirection } from '../types'
10
+ import type { SwiperProps } from '../types'
11
+
12
+ export function useVelocity({ RTL }: { RTL: SwiperProps<unknown>['RTL'] }) {
13
+ const positionQueueRef = useMainThreadRef<number[]>([])
14
+ const timeQueueRef = useMainThreadRef<number[]>([])
15
+
16
+ const reset = () => {
17
+ 'main thread'
18
+ timeQueueRef.current = []
19
+ positionQueueRef.current = []
20
+ }
21
+
22
+ const pruneQueue = (ms: number, minLength = 0) => {
23
+ 'main thread'
24
+ const timeQueue = timeQueueRef.current
25
+ const positionQueue = positionQueueRef.current
26
+
27
+ const nowTs = Date.now()
28
+ // pull old values off of the queue
29
+ while (timeQueue.length > minLength && timeQueue[0] < nowTs - ms) {
30
+ timeQueue.shift()
31
+ positionQueue.shift()
32
+ }
33
+ }
34
+
35
+ const getVelocity = () => {
36
+ 'main thread'
37
+ pruneQueue(500)
38
+ const timeQueue = timeQueueRef.current
39
+ const positionQueue = positionQueueRef.current
40
+ const { length } = timeQueue
41
+ if (length < 2) {
42
+ return {
43
+ velocity: 0,
44
+ direction: SwipeDirection.REVERT,
45
+ }
46
+ }
47
+ const distance = RTL
48
+ ? positionQueue[0] - positionQueue[length - 1]
49
+ : positionQueue[length - 1] - positionQueue[0]
50
+ const time = (timeQueue[length - 1] - timeQueue[0]) / 1000
51
+ return {
52
+ velocity: distance / time,
53
+ direction: distance > 0 ? SwipeDirection.REVERT : SwipeDirection.NORMAL,
54
+ }
55
+ }
56
+
57
+ const updatePosition = (position: number) => {
58
+ 'main thread'
59
+ const timeQueue = timeQueueRef.current
60
+ const positionQueue = positionQueueRef.current
61
+
62
+ positionQueue.push(position)
63
+ timeQueue.push(Date.now())
64
+ // Keep one older sample around so sparse touchmove delivery can still
65
+ // produce a release velocity from the latest two points.
66
+ pruneQueue(50, 2)
67
+ }
68
+
69
+ function velocityTouchStart() {
70
+ 'main thread'
71
+ reset()
72
+ }
73
+
74
+ function velocityTouchMove(event: MainThread.TouchEvent) {
75
+ 'main thread'
76
+
77
+ updatePosition(event.detail.x)
78
+ }
79
+
80
+ return {
81
+ getVelocity,
82
+ velocityTouchStart,
83
+ velocityTouchMove,
84
+ }
85
+ }
@@ -0,0 +1,50 @@
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 { useMainThreadRef } from '@lynx-js/react'
6
+
7
+ import type { MainThread } from '@lynx-js/types'
8
+
9
+ export function useVelocity({ deceleratingRate = 0.99 }) {
10
+ const touchStartRef = useMainThreadRef<number>(0)
11
+ const velocityRef = useMainThreadRef<number>(0)
12
+
13
+ function velocityTouchStart(event: MainThread.TouchEvent) {
14
+ 'main thread'
15
+
16
+ touchStartRef.current = event.detail.x
17
+ }
18
+
19
+ function velocityTouchMove(event: MainThread.TouchEvent) {
20
+ 'main thread'
21
+ const timeDuration = Date.now() - event.timestamp
22
+
23
+ const delta = event.detail.x - touchStartRef.current
24
+ const velocity = delta / timeDuration
25
+ velocityRef.current = velocity
26
+ }
27
+
28
+ function getVelocityDistance() {
29
+ 'main thread'
30
+ const velocity = velocityRef.current
31
+ const distance = Math.pow(velocity, 2) / (2 * deceleratingRate)
32
+ return distance
33
+ }
34
+
35
+ function getVelocity() {
36
+ 'main thread'
37
+ const distance = getVelocityDistance()
38
+
39
+ return {
40
+ velocity: velocityRef.current,
41
+ distance,
42
+ }
43
+ }
44
+
45
+ return {
46
+ getVelocity,
47
+ velocityTouchStart,
48
+ velocityTouchMove,
49
+ }
50
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
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
+ export { Swiper } from './Swiper'
6
+ export { SwiperItem } from './SwiperItem'
7
+ export type { SwiperItemProps } from './SwiperItem'
8
+ export type { SwiperProps, SwiperRef } from './types'
@@ -0,0 +1,23 @@
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 { createContext } from '@lynx-js/react'
6
+
7
+ import { noop } from '@lynx-js/lynx-ui-common'
8
+
9
+ import type { SwiperContextProps } from '../types'
10
+
11
+ export const SwiperContext = createContext<SwiperContextProps>({
12
+ itemWidth: 350,
13
+ itemHeight: 'auto',
14
+ setChildrenRef: noop,
15
+ modeConfig: {
16
+ mode: 'normal',
17
+ align: 'start',
18
+ },
19
+ initialIndex: 0,
20
+ loop: false,
21
+ spaceBetween: 0,
22
+ RTL: false,
23
+ })