@lynx-js/lynx-ui-swiper 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.
Files changed (137) hide show
  1. package/CHANGELOG.md +16 -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 +83 -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,59 @@
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 useDirection({ RTL }: { RTL: SwiperProps<unknown>['RTL'] }) {
13
+ const startXRef = useMainThreadRef<number>(0)
14
+ const directionRef = useMainThreadRef<SwipeDirection>(SwipeDirection.NONE)
15
+
16
+ function handleTouchStart(event: MainThread.TouchEvent) {
17
+ 'main thread'
18
+
19
+ startXRef.current = event.detail.x
20
+ directionRef.current = SwipeDirection.NONE
21
+ }
22
+
23
+ function handleTouchMove(event: MainThread.TouchEvent) {
24
+ 'main thread'
25
+
26
+ if (event.detail.x < startXRef.current) {
27
+ directionRef.current = RTL ? SwipeDirection.REVERT : SwipeDirection.NORMAL
28
+ } else if (event.detail.x > startXRef.current) {
29
+ directionRef.current = RTL ? SwipeDirection.NORMAL : SwipeDirection.REVERT
30
+ } else {
31
+ directionRef.current = SwipeDirection.NONE
32
+ }
33
+ }
34
+
35
+ function handleTouchEnd(event: MainThread.TouchEvent) {
36
+ 'main thread'
37
+
38
+ if (event.detail.x < startXRef.current) {
39
+ directionRef.current = RTL ? SwipeDirection.REVERT : SwipeDirection.NORMAL
40
+ } else if (event.detail.x > startXRef.current) {
41
+ directionRef.current = RTL ? SwipeDirection.NORMAL : SwipeDirection.REVERT
42
+ } else {
43
+ directionRef.current = SwipeDirection.NONE
44
+ }
45
+ }
46
+
47
+ function overrideDirection(direction: SwipeDirection) {
48
+ 'main thread'
49
+ directionRef.current = direction
50
+ }
51
+
52
+ return {
53
+ handleTouchStart,
54
+ handleTouchEnd,
55
+ handleTouchMove,
56
+ overrideDirection,
57
+ directionRef,
58
+ }
59
+ }
@@ -0,0 +1,200 @@
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 { useMemo } from '@lynx-js/react'
6
+
7
+ import type { CSSProperties } from '@lynx-js/types'
8
+
9
+ import type {
10
+ CompoundModeConfig,
11
+ OffsetLimitResult,
12
+ SwiperProps,
13
+ } from '../types'
14
+ import { limiterForFirstScreen } from '../utils'
15
+
16
+ interface LayoutSwiperPropsType extends
17
+ Pick<
18
+ SwiperProps<unknown>,
19
+ 'itemWidth' | 'initialIndex' | 'itemHeight' | 'trackStyle'
20
+ >,
21
+ Required<
22
+ Pick<
23
+ SwiperProps<unknown>,
24
+ | 'containerWidth'
25
+ | 'loop'
26
+ | 'loopDuplicateCount'
27
+ | 'RTL'
28
+ | 'swiperKey'
29
+ >
30
+ >
31
+ {
32
+ spaceBetween: number
33
+ dataCount: number
34
+ modeConfig: CompoundModeConfig
35
+ offsetLimit: OffsetLimitResult
36
+ }
37
+
38
+ interface EscapeProps
39
+ extends
40
+ Pick<SwiperProps<unknown>, 'itemWidth' | 'initialIndex'>,
41
+ Required<
42
+ Pick<
43
+ SwiperProps<unknown>,
44
+ 'containerWidth' | 'loop' | 'RTL'
45
+ >
46
+ >
47
+ {
48
+ spaceBetween: number
49
+ dataCount: number
50
+ modeConfig: CompoundModeConfig
51
+ offsetLimit: OffsetLimitResult
52
+ }
53
+
54
+ function getOffset(props: EscapeProps) {
55
+ const {
56
+ modeConfig,
57
+ itemWidth,
58
+ initialIndex = 0,
59
+ loop,
60
+ dataCount,
61
+ containerWidth,
62
+ offsetLimit,
63
+ RTL,
64
+ spaceBetween,
65
+ } = props
66
+ let alignOffset = 0
67
+ if (modeConfig.mode === 'normal') {
68
+ if (modeConfig.align === 'center') {
69
+ alignOffset = (containerWidth - itemWidth) / 2
70
+ } else if (modeConfig.align === 'end') {
71
+ alignOffset = containerWidth - itemWidth
72
+ } else {
73
+ alignOffset = 0
74
+ }
75
+ }
76
+
77
+ const finalOffset = limiterForFirstScreen(
78
+ -(itemWidth + spaceBetween) * initialIndex + alignOffset,
79
+ dataCount,
80
+ itemWidth,
81
+ spaceBetween,
82
+ loop,
83
+ offsetLimit,
84
+ alignOffset,
85
+ )
86
+
87
+ return RTL ? -finalOffset : finalOffset
88
+ }
89
+
90
+ export function getInnerContainerWidth(props: {
91
+ itemWidth: number
92
+ loopDuplicateCount?: number
93
+ loop: boolean
94
+ dataCount: number
95
+ spaceBetween: number
96
+ }) {
97
+ const {
98
+ itemWidth,
99
+ loopDuplicateCount = 0,
100
+ loop,
101
+ dataCount,
102
+ spaceBetween,
103
+ } = props
104
+ const width = loop
105
+ ? `${(dataCount + loopDuplicateCount * 2) * (itemWidth + spaceBetween)}px`
106
+ : `${dataCount * (itemWidth + spaceBetween) - spaceBetween}px`
107
+ return width
108
+ }
109
+
110
+ export function getInnerContainerWidthMT(props: {
111
+ itemWidth: number
112
+ loopDuplicateCount?: number
113
+ loop: boolean
114
+ dataCount: number
115
+ spaceBetween: number
116
+ }) {
117
+ 'main thread'
118
+ const {
119
+ itemWidth,
120
+ loopDuplicateCount = 0,
121
+ loop,
122
+ dataCount,
123
+ spaceBetween,
124
+ } = props
125
+ const width = loop
126
+ ? `${(dataCount + loopDuplicateCount * 2) * (itemWidth + spaceBetween)}px`
127
+ : `${dataCount * (itemWidth + spaceBetween) - spaceBetween}px`
128
+
129
+ return width
130
+ }
131
+
132
+ /**
133
+ * MTS does not support first screen mutation.
134
+ * We use `useMemo` hack to make firstScreen style correct.
135
+ * When MTS is not ready, we use `useMemo` to get the style, and set it to view. Since there's no
136
+ * dependency in `useMemo`, the view will only be set on firstScreen.
137
+ * After that, all the updated are handled by MTS via `setStyleProperties`
138
+ */
139
+ function useFirstScreenStyle(props: LayoutSwiperPropsType): {
140
+ containerStyle: CSSProperties | undefined
141
+ } {
142
+ const {
143
+ itemWidth,
144
+ spaceBetween,
145
+ containerWidth,
146
+ loop,
147
+ initialIndex,
148
+ dataCount,
149
+ itemHeight,
150
+ modeConfig,
151
+ offsetLimit,
152
+ loopDuplicateCount,
153
+ RTL,
154
+ swiperKey,
155
+ trackStyle,
156
+ } = props
157
+ const escapeProps = {
158
+ itemWidth,
159
+ containerWidth,
160
+ itemHeight,
161
+ loop,
162
+ initialIndex,
163
+ dataCount,
164
+ modeConfig,
165
+ offsetLimit,
166
+ RTL,
167
+ spaceBetween,
168
+ }
169
+
170
+ const containerStyle = useMemo(() => {
171
+ const insetInlineStartKey = RTL ? 'inset-inline-start' : 'left'
172
+ if (modeConfig.mode === 'normal') {
173
+ return {
174
+ ...trackStyle,
175
+ width: getInnerContainerWidth({
176
+ itemWidth,
177
+ loopDuplicateCount,
178
+ loop,
179
+ dataCount,
180
+ spaceBetween,
181
+ }),
182
+ [insetInlineStartKey]: loop
183
+ ? `${-loopDuplicateCount * (itemWidth + spaceBetween)}px`
184
+ : '0px',
185
+ transform: `translateX(${getOffset(escapeProps)}px)`,
186
+ height: `${itemHeight}px`,
187
+ }
188
+ } else if (modeConfig.mode === 'custom') {
189
+ return {
190
+ height: `${itemHeight}px`,
191
+ }
192
+ }
193
+ }, [swiperKey])
194
+
195
+ return {
196
+ containerStyle,
197
+ }
198
+ }
199
+
200
+ export default useFirstScreenStyle
@@ -0,0 +1,56 @@
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 { useMemo, useRef } from '@lynx-js/react'
6
+
7
+ import type {
8
+ CompoundCustomModeConfig,
9
+ CompoundModeConfig,
10
+ CompoundNormalModeConfig,
11
+ SwiperProps,
12
+ } from '../types'
13
+ import { comparePropsWithObject } from '../utils/compareProps'
14
+
15
+ /**
16
+ * Handle mode and modeConfig
17
+ * Only update reference when mode or content of modeConfig changes
18
+ * This can prevent usage like this causing reRender every time.
19
+ * ```
20
+ * <Swiper mode="normal" modeConfig={{ align: 'center' }}></Swiper>
21
+ * ```
22
+ */
23
+ export function useModeConfig({
24
+ mode = 'normal',
25
+ modeConfig = {
26
+ align: 'start',
27
+ spaceBetween: 0,
28
+ },
29
+ }: Pick<SwiperProps<unknown>, 'mode' | 'modeConfig'>) {
30
+ const prevModeConfigRef = useRef<CompoundModeConfig | null>(null)
31
+
32
+ const normalizedModeConfig = useMemo(() => {
33
+ const newConfig: CompoundModeConfig = mode === 'custom'
34
+ ? { mode, ...(modeConfig ?? {}) } as CompoundCustomModeConfig
35
+ : {
36
+ mode,
37
+ ...(modeConfig ?? { align: 'start' }),
38
+ } as CompoundNormalModeConfig
39
+
40
+ // If previous config exists and is equal to new config, return cached version
41
+ if (
42
+ prevModeConfigRef.current
43
+ && comparePropsWithObject(prevModeConfigRef.current, newConfig)
44
+ ) {
45
+ return prevModeConfigRef.current
46
+ }
47
+
48
+ // Update cache and return new config
49
+ prevModeConfigRef.current = newConfig
50
+ return newConfig
51
+ }, [mode, modeConfig])
52
+
53
+ const spaceBetween = (modeConfig.spaceBetween as number) ?? 0
54
+
55
+ return { modeConfig: normalizedModeConfig, spaceBetween }
56
+ }