@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.
- package/CHANGELOG.md +16 -0
- package/LICENSE +201 -0
- package/README.md +51 -0
- package/dist/Swiper/index.d.ts +11 -0
- package/dist/Swiper/index.d.ts.map +1 -0
- package/dist/Swiper/index.jsx +261 -0
- package/dist/Swiper/index.jsx.map +1 -0
- package/dist/Swiper/styles.css +47 -0
- package/dist/Swiper/styles.css.map +1 -0
- package/dist/SwiperItem/index.d.ts +46 -0
- package/dist/SwiperItem/index.d.ts.map +1 -0
- package/dist/SwiperItem/index.jsx +86 -0
- package/dist/SwiperItem/index.jsx.map +1 -0
- package/dist/const/index.d.ts +5 -0
- package/dist/const/index.d.ts.map +1 -0
- package/dist/const/index.jsx +7 -0
- package/dist/const/index.jsx.map +1 -0
- package/dist/hooks/useAnimate.d.ts +5 -0
- package/dist/hooks/useAnimate.d.ts.map +1 -0
- package/dist/hooks/useAnimate.jsx +47 -0
- package/dist/hooks/useAnimate.jsx.map +1 -0
- package/dist/hooks/useAutoPlay.d.ts +16 -0
- package/dist/hooks/useAutoPlay.d.ts.map +1 -0
- package/dist/hooks/useAutoPlay.jsx +58 -0
- package/dist/hooks/useAutoPlay.jsx.map +1 -0
- package/dist/hooks/useAxisLock.d.ts +12 -0
- package/dist/hooks/useAxisLock.d.ts.map +1 -0
- package/dist/hooks/useAxisLock.jsx +58 -0
- package/dist/hooks/useAxisLock.jsx.map +1 -0
- package/dist/hooks/useBounceConfig.d.ts +12 -0
- package/dist/hooks/useBounceConfig.d.ts.map +1 -0
- package/dist/hooks/useBounceConfig.jsx +15 -0
- package/dist/hooks/useBounceConfig.jsx.map +1 -0
- package/dist/hooks/useBounceView.d.ts +17 -0
- package/dist/hooks/useBounceView.d.ts.map +1 -0
- package/dist/hooks/useBounceView.jsx +16 -0
- package/dist/hooks/useBounceView.jsx.map +1 -0
- package/dist/hooks/useChange.d.ts +22 -0
- package/dist/hooks/useChange.d.ts.map +1 -0
- package/dist/hooks/useChange.jsx +53 -0
- package/dist/hooks/useChange.jsx.map +1 -0
- package/dist/hooks/useDirection.d.ts +13 -0
- package/dist/hooks/useDirection.d.ts.map +1 -0
- package/dist/hooks/useDirection.jsx +37 -0
- package/dist/hooks/useDirection.jsx.map +1 -0
- package/dist/hooks/useFirstScreenStyle.d.ts +34 -0
- package/dist/hooks/useFirstScreenStyle.d.ts.map +1 -0
- package/dist/hooks/useFirstScreenStyle.jsx +63 -0
- package/dist/hooks/useFirstScreenStyle.jsx.map +1 -0
- package/dist/hooks/useModeConfig.d.ts +14 -0
- package/dist/hooks/useModeConfig.d.ts.map +1 -0
- package/dist/hooks/useModeConfig.jsx +33 -0
- package/dist/hooks/useModeConfig.jsx.map +1 -0
- package/dist/hooks/useOffset.d.ts +37 -0
- package/dist/hooks/useOffset.d.ts.map +1 -0
- package/dist/hooks/useOffset.jsx +362 -0
- package/dist/hooks/useOffset.jsx.map +1 -0
- package/dist/hooks/useOffsetLimit.d.ts +16 -0
- package/dist/hooks/useOffsetLimit.d.ts.map +1 -0
- package/dist/hooks/useOffsetLimit.jsx +51 -0
- package/dist/hooks/useOffsetLimit.jsx.map +1 -0
- package/dist/hooks/useReset.d.ts +16 -0
- package/dist/hooks/useReset.d.ts.map +1 -0
- package/dist/hooks/useReset.jsx +68 -0
- package/dist/hooks/useReset.jsx.map +1 -0
- package/dist/hooks/useSwipeCallback.d.ts +14 -0
- package/dist/hooks/useSwipeCallback.d.ts.map +1 -0
- package/dist/hooks/useSwipeCallback.jsx +40 -0
- package/dist/hooks/useSwipeCallback.jsx.map +1 -0
- package/dist/hooks/useVelocity.d.ts +14 -0
- package/dist/hooks/useVelocity.d.ts.map +1 -0
- package/dist/hooks/useVelocity.jsx +62 -0
- package/dist/hooks/useVelocity.jsx.map +1 -0
- package/dist/hooks/useVelocity_experiment.d.ts +12 -0
- package/dist/hooks/useVelocity_experiment.d.ts.map +1 -0
- package/dist/hooks/useVelocity_experiment.jsx +38 -0
- package/dist/hooks/useVelocity_experiment.jsx.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +3 -0
- package/dist/store/index.d.ts +3 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.jsx +18 -0
- package/dist/store/index.jsx.map +1 -0
- package/dist/types/index.d.ts +62 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.docs.d.ts +448 -0
- package/dist/types/index.docs.d.ts.map +1 -0
- package/dist/types/index.jsx +9 -0
- package/dist/types/index.jsx.map +1 -0
- package/dist/utils/animate.d.ts +4 -0
- package/dist/utils/animate.d.ts.map +1 -0
- package/dist/utils/animate.jsx +29 -0
- package/dist/utils/animate.jsx.map +1 -0
- package/dist/utils/compareProps.d.ts +2 -0
- package/dist/utils/compareProps.d.ts.map +1 -0
- package/dist/utils/compareProps.jsx +14 -0
- package/dist/utils/compareProps.jsx.map +1 -0
- package/dist/utils/customRound.d.ts +10 -0
- package/dist/utils/customRound.d.ts.map +1 -0
- package/dist/utils/customRound.jsx +9 -0
- package/dist/utils/customRound.jsx.map +1 -0
- package/dist/utils/index.d.ts +20 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.jsx +55 -0
- package/dist/utils/index.jsx.map +1 -0
- package/package.json +47 -4
- package/src/Swiper/index.tsx +414 -0
- package/src/Swiper/styles.css +44 -0
- package/src/SwiperItem/index.tsx +205 -0
- package/src/const/index.ts +11 -0
- package/src/hooks/useAnimate.ts +80 -0
- package/src/hooks/useAutoPlay.ts +95 -0
- package/src/hooks/useAxisLock.ts +92 -0
- package/src/hooks/useBounceConfig.ts +29 -0
- package/src/hooks/useBounceView.tsx +50 -0
- package/src/hooks/useChange.ts +131 -0
- package/src/hooks/useDirection.ts +59 -0
- package/src/hooks/useFirstScreenStyle.ts +200 -0
- package/src/hooks/useModeConfig.ts +56 -0
- package/src/hooks/useOffset.ts +609 -0
- package/src/hooks/useOffsetLimit.ts +98 -0
- package/src/hooks/useReset.ts +144 -0
- package/src/hooks/useSwipeCallback.ts +66 -0
- package/src/hooks/useVelocity.ts +83 -0
- package/src/hooks/useVelocity_experiment.ts +50 -0
- package/src/index.ts +8 -0
- package/src/store/index.ts +23 -0
- package/src/types/index.docs.ts +465 -0
- package/src/types/index.ts +105 -0
- package/src/utils/animate.ts +49 -0
- package/src/utils/compareProps.ts +54 -0
- package/src/utils/customRound.ts +23 -0
- package/src/utils/index.ts +117 -0
- package/tsconfig.docs.json +7 -0
- package/tsconfig.json +9 -0
- /package/{index.js → dist/types/index.docs.jsx} +0 -0
|
@@ -0,0 +1,205 @@
|
|
|
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 {
|
|
6
|
+
memo,
|
|
7
|
+
runOnMainThread,
|
|
8
|
+
useContext,
|
|
9
|
+
useEffect,
|
|
10
|
+
useMainThreadRef,
|
|
11
|
+
useMemo,
|
|
12
|
+
} from '@lynx-js/react'
|
|
13
|
+
import type { ReactElement, RefObject } from '@lynx-js/react'
|
|
14
|
+
|
|
15
|
+
import type { CSSProperties, MainThread } from '@lynx-js/types'
|
|
16
|
+
|
|
17
|
+
import { SwiperContext } from '../store'
|
|
18
|
+
import type { CompoundModeConfig, SwiperContextProps } from '../types'
|
|
19
|
+
|
|
20
|
+
export interface SwiperItemProps {
|
|
21
|
+
/**
|
|
22
|
+
* Index of current SwiperItem, out of Swiper's data.
|
|
23
|
+
* @Android
|
|
24
|
+
* @iOS
|
|
25
|
+
* @Harmony
|
|
26
|
+
*/
|
|
27
|
+
index: number
|
|
28
|
+
/**
|
|
29
|
+
* Children of SwiperItem
|
|
30
|
+
* @Android
|
|
31
|
+
* @iOS
|
|
32
|
+
* @Harmony
|
|
33
|
+
*/
|
|
34
|
+
children: ReactElement | ReactElement[]
|
|
35
|
+
/**
|
|
36
|
+
* Style of SwiperItem
|
|
37
|
+
* @Android
|
|
38
|
+
* @iOS
|
|
39
|
+
* @Harmony
|
|
40
|
+
*/
|
|
41
|
+
style?: CSSProperties
|
|
42
|
+
/**
|
|
43
|
+
* Real index of current SwiperItem, out of Swiper's data.
|
|
44
|
+
* Same with `index`, but will be different from `index` when in loop mode.
|
|
45
|
+
* @Android
|
|
46
|
+
* @iOS
|
|
47
|
+
* @Harmony
|
|
48
|
+
*/
|
|
49
|
+
realIndex?: number
|
|
50
|
+
/**
|
|
51
|
+
* `overlap` attribute passed to SwiperItem's direct child.
|
|
52
|
+
* This property is used only when opacity has issues.
|
|
53
|
+
* Refer to {@link https://lynxjs.org/en/api/css/properties/opacity#opacity}
|
|
54
|
+
* @Android
|
|
55
|
+
* @iOS
|
|
56
|
+
* @Harmony
|
|
57
|
+
*/
|
|
58
|
+
overlap?: boolean
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface IFirstScreenStyle extends
|
|
62
|
+
Pick<
|
|
63
|
+
SwiperContextProps,
|
|
64
|
+
| 'itemWidth'
|
|
65
|
+
| 'itemHeight'
|
|
66
|
+
| 'initialIndex'
|
|
67
|
+
| 'customAnimationFirstScreen'
|
|
68
|
+
| 'RTL'
|
|
69
|
+
>
|
|
70
|
+
{
|
|
71
|
+
itemRef: RefObject<MainThread.Element | undefined>
|
|
72
|
+
spaceBetween: number
|
|
73
|
+
realIndex: number
|
|
74
|
+
modeConfig: CompoundModeConfig
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function useFirstScreenStyle(props: IFirstScreenStyle) {
|
|
78
|
+
const {
|
|
79
|
+
itemWidth,
|
|
80
|
+
spaceBetween,
|
|
81
|
+
itemHeight,
|
|
82
|
+
itemRef,
|
|
83
|
+
customAnimationFirstScreen,
|
|
84
|
+
realIndex,
|
|
85
|
+
modeConfig,
|
|
86
|
+
initialIndex,
|
|
87
|
+
RTL,
|
|
88
|
+
} = props
|
|
89
|
+
|
|
90
|
+
const containerStyle = useMemo(() => {
|
|
91
|
+
if (modeConfig.mode === 'normal') {
|
|
92
|
+
let style: CSSProperties = {
|
|
93
|
+
width: `${itemWidth}px`,
|
|
94
|
+
height: `${itemHeight}px`,
|
|
95
|
+
[RTL === true ? 'marginLeft' : 'marginRight']: `${spaceBetween}px`,
|
|
96
|
+
}
|
|
97
|
+
if (customAnimationFirstScreen) {
|
|
98
|
+
const customStyle = customAnimationFirstScreen(
|
|
99
|
+
-initialIndex + realIndex,
|
|
100
|
+
realIndex,
|
|
101
|
+
)
|
|
102
|
+
style = {
|
|
103
|
+
...style,
|
|
104
|
+
...customStyle,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return style
|
|
109
|
+
} else if (modeConfig.mode === 'custom') {
|
|
110
|
+
let style: CSSProperties = {
|
|
111
|
+
width: `${itemWidth}px`,
|
|
112
|
+
height: `${itemHeight}px`,
|
|
113
|
+
position: 'absolute',
|
|
114
|
+
}
|
|
115
|
+
if (customAnimationFirstScreen) {
|
|
116
|
+
const customStyle = customAnimationFirstScreen(
|
|
117
|
+
-initialIndex + realIndex,
|
|
118
|
+
realIndex,
|
|
119
|
+
)
|
|
120
|
+
style = {
|
|
121
|
+
...style,
|
|
122
|
+
...customStyle,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return style
|
|
127
|
+
}
|
|
128
|
+
}, [])
|
|
129
|
+
|
|
130
|
+
function updateMainThreadR(propsFromJS: {
|
|
131
|
+
itemWidth: number
|
|
132
|
+
itemHeight: SwiperContextProps['itemHeight']
|
|
133
|
+
spaceBetween: number
|
|
134
|
+
}) {
|
|
135
|
+
'main thread'
|
|
136
|
+
if (itemRef.current) {
|
|
137
|
+
itemRef.current.setStyleProperties({
|
|
138
|
+
width: `${propsFromJS.itemWidth}px`,
|
|
139
|
+
height: `${propsFromJS.itemHeight}px`,
|
|
140
|
+
marginInlineEnd: `${propsFromJS.spaceBetween}px`,
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
runOnMainThread(updateMainThreadR)({
|
|
147
|
+
itemWidth,
|
|
148
|
+
itemHeight,
|
|
149
|
+
spaceBetween,
|
|
150
|
+
})
|
|
151
|
+
}, [itemWidth, itemHeight, spaceBetween])
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
containerStyle,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const SwiperItem = (
|
|
159
|
+
{ index, realIndex = index, children, overlap }: SwiperItemProps,
|
|
160
|
+
) => {
|
|
161
|
+
const {
|
|
162
|
+
itemWidth,
|
|
163
|
+
itemHeight,
|
|
164
|
+
setChildrenRef,
|
|
165
|
+
modeConfig,
|
|
166
|
+
customAnimationFirstScreen,
|
|
167
|
+
initialIndex = 0,
|
|
168
|
+
spaceBetween,
|
|
169
|
+
RTL,
|
|
170
|
+
} = useContext(SwiperContext)
|
|
171
|
+
const swiperItemRef = useMainThreadRef<MainThread.Element>()
|
|
172
|
+
|
|
173
|
+
const { containerStyle } = useFirstScreenStyle({
|
|
174
|
+
itemWidth,
|
|
175
|
+
itemHeight,
|
|
176
|
+
spaceBetween,
|
|
177
|
+
itemRef: swiperItemRef,
|
|
178
|
+
modeConfig,
|
|
179
|
+
realIndex,
|
|
180
|
+
initialIndex,
|
|
181
|
+
customAnimationFirstScreen,
|
|
182
|
+
RTL,
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
function setRef(ref: MainThread.Element) {
|
|
186
|
+
'main thread'
|
|
187
|
+
swiperItemRef.current = ref
|
|
188
|
+
setChildrenRef(ref, realIndex ?? index)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return (
|
|
192
|
+
<view
|
|
193
|
+
class='swiper-item'
|
|
194
|
+
style={containerStyle}
|
|
195
|
+
main-thread:ref={setRef}
|
|
196
|
+
overlap={overlap}
|
|
197
|
+
>
|
|
198
|
+
{children}
|
|
199
|
+
</view>
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const MemoSwiperItem = memo(SwiperItem) as typeof SwiperItem
|
|
204
|
+
|
|
205
|
+
export { MemoSwiperItem as SwiperItem }
|
|
@@ -0,0 +1,11 @@
|
|
|
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 const LOOP_COUNT = 2
|
|
6
|
+
|
|
7
|
+
export const TAP_THRESHOLD = 8
|
|
8
|
+
|
|
9
|
+
export const CROSS_AXIS_THRESHOLD = 20
|
|
10
|
+
|
|
11
|
+
export const GESTURE_THRESHOLD = 8 // Slop area in pixels
|
|
@@ -0,0 +1,80 @@
|
|
|
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
|
+
function animateInner(
|
|
8
|
+
duration: number,
|
|
9
|
+
onUpdate: (progress: number) => void,
|
|
10
|
+
onFinished?: () => void,
|
|
11
|
+
) {
|
|
12
|
+
'main thread'
|
|
13
|
+
let startTs = 0
|
|
14
|
+
let rafId = 0
|
|
15
|
+
|
|
16
|
+
function tick(ts: number) {
|
|
17
|
+
const progress = Math.max(
|
|
18
|
+
Math.min(((ts - startTs) * 100) / duration, 100),
|
|
19
|
+
0,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
onUpdate(progress)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function updateRafId(id: number) {
|
|
26
|
+
rafId = id
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function step(ts: number) {
|
|
30
|
+
if (!startTs) {
|
|
31
|
+
startTs = Number(ts)
|
|
32
|
+
}
|
|
33
|
+
// make sure progress can reach 100%
|
|
34
|
+
if (ts - startTs <= duration + 100) {
|
|
35
|
+
tick(ts)
|
|
36
|
+
updateRafId(requestAnimationFrame(step))
|
|
37
|
+
} else {
|
|
38
|
+
onFinished?.()
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
updateRafId(requestAnimationFrame(step))
|
|
43
|
+
|
|
44
|
+
function cancel() {
|
|
45
|
+
cancelAnimationFrame(rafId)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
cancel,
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function useAnimate() {
|
|
54
|
+
const lastCancelRef = useMainThreadRef<() => void>()
|
|
55
|
+
|
|
56
|
+
function cancel() {
|
|
57
|
+
'main thread'
|
|
58
|
+
lastCancelRef.current?.()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function animate(
|
|
62
|
+
duration: number,
|
|
63
|
+
onUpdate: (progress: number) => void,
|
|
64
|
+
onFinished?: () => void,
|
|
65
|
+
) {
|
|
66
|
+
'main thread'
|
|
67
|
+
cancel()
|
|
68
|
+
const { cancel: innerCancel } = animateInner(
|
|
69
|
+
duration,
|
|
70
|
+
onUpdate,
|
|
71
|
+
onFinished,
|
|
72
|
+
)
|
|
73
|
+
lastCancelRef.current = innerCancel
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
cancel,
|
|
78
|
+
animate,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
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 {
|
|
6
|
+
runOnMainThread,
|
|
7
|
+
useEffect,
|
|
8
|
+
useMainThreadRef,
|
|
9
|
+
useRef,
|
|
10
|
+
} from '@lynx-js/react'
|
|
11
|
+
|
|
12
|
+
import { useMemoizedFn } from '@lynx-js/lynx-ui-common'
|
|
13
|
+
|
|
14
|
+
interface IAutoPlay {
|
|
15
|
+
autoPlay: boolean
|
|
16
|
+
duration: number
|
|
17
|
+
dataCount: number
|
|
18
|
+
autoPlayInterval: number
|
|
19
|
+
prevMT: () => void
|
|
20
|
+
nextMT: () => void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function useAutoplay({
|
|
24
|
+
autoPlay,
|
|
25
|
+
autoPlayInterval,
|
|
26
|
+
nextMT,
|
|
27
|
+
duration,
|
|
28
|
+
}: IAutoPlay) {
|
|
29
|
+
const stoppedRef = useRef<boolean>(false)
|
|
30
|
+
const timerMTRef = useMainThreadRef<number>(0)
|
|
31
|
+
const stoppedMTRef = useMainThreadRef<boolean>(false)
|
|
32
|
+
const autoPlayMTRef = useMainThreadRef<boolean>(autoPlay)
|
|
33
|
+
|
|
34
|
+
function playMT() {
|
|
35
|
+
'main thread'
|
|
36
|
+
if (stoppedRef.current || !autoPlayMTRef.current) {
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
timerMTRef.current && clearTimeout(timerMTRef.current)
|
|
41
|
+
timerMTRef.current = setTimeout(() => {
|
|
42
|
+
nextMT()
|
|
43
|
+
playMT()
|
|
44
|
+
}, autoPlayInterval + duration) as unknown as number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function pauseMT() {
|
|
48
|
+
'main thread'
|
|
49
|
+
if (!autoPlayMTRef.current) {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
timerMTRef.current && clearTimeout(timerMTRef.current)
|
|
53
|
+
stoppedMTRef.current = true
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function startMT() {
|
|
57
|
+
'main thread'
|
|
58
|
+
if (!autoPlayMTRef.current) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
stoppedMTRef.current = false
|
|
62
|
+
playMT()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function setAutoPlayRef(autoPlayValue: boolean) {
|
|
66
|
+
'main thread'
|
|
67
|
+
autoPlayMTRef.current = autoPlayValue
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const start = useMemoizedFn(() => {
|
|
71
|
+
runOnMainThread(startMT)()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
const pause = useMemoizedFn(() => {
|
|
75
|
+
runOnMainThread(pauseMT)()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
runOnMainThread(setAutoPlayRef)(autoPlay)
|
|
80
|
+
if (autoPlay) {
|
|
81
|
+
start()
|
|
82
|
+
} else {
|
|
83
|
+
pause()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return pause
|
|
87
|
+
}, [start, pause, autoPlay])
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
pause,
|
|
91
|
+
start,
|
|
92
|
+
pauseMT,
|
|
93
|
+
startMT,
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
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 { GESTURE_THRESHOLD } from '../const'
|
|
10
|
+
import type { SwiperProps } from '../types'
|
|
11
|
+
|
|
12
|
+
function isAngleInRanges(angle: number, ranges: [number, number][]) {
|
|
13
|
+
'main thread'
|
|
14
|
+
return ranges.some(([start, end]) => {
|
|
15
|
+
if (start <= end) {
|
|
16
|
+
return angle >= start && angle <= end
|
|
17
|
+
} else {
|
|
18
|
+
return angle >= start || angle <= end // Handles wrap-around cases like [-180, -135] or [135, 180]
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function useAxisLock({
|
|
24
|
+
consumeSlideEvent,
|
|
25
|
+
experimentalHorizontalSwipeOnly,
|
|
26
|
+
}: {
|
|
27
|
+
consumeSlideEvent: NonNullable<SwiperProps<unknown>['consumeSlideEvent']>
|
|
28
|
+
experimentalHorizontalSwipeOnly: boolean
|
|
29
|
+
}) {
|
|
30
|
+
const gestureLockedRef = useMainThreadRef<boolean>(false)
|
|
31
|
+
const isMainAxisLockedRef = useMainThreadRef<boolean>(false)
|
|
32
|
+
const touchStartRef = useMainThreadRef<number>(0)
|
|
33
|
+
const touchStartCrossAxisRef = useMainThreadRef<number>(0)
|
|
34
|
+
|
|
35
|
+
function handleTouchStart(event: MainThread.TouchEvent) {
|
|
36
|
+
'main thread'
|
|
37
|
+
|
|
38
|
+
touchStartRef.current = event.detail.x
|
|
39
|
+
|
|
40
|
+
touchStartCrossAxisRef.current = event.detail.y
|
|
41
|
+
|
|
42
|
+
gestureLockedRef.current = false
|
|
43
|
+
isMainAxisLockedRef.current = false
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function handleTouchMove(event: MainThread.TouchEvent) {
|
|
47
|
+
'main thread'
|
|
48
|
+
|
|
49
|
+
if (!experimentalHorizontalSwipeOnly) {
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const deltaX = event.detail.x - touchStartRef.current
|
|
54
|
+
|
|
55
|
+
const deltaY = event.detail.y - touchStartCrossAxisRef.current
|
|
56
|
+
|
|
57
|
+
// Calculate total displacement (distance)
|
|
58
|
+
const displacement = Math.sqrt(deltaX * deltaX + deltaY * deltaY)
|
|
59
|
+
|
|
60
|
+
if (!gestureLockedRef.current && displacement > GESTURE_THRESHOLD) {
|
|
61
|
+
const angle = Math.atan2(deltaY, deltaX) * 180 / Math.PI
|
|
62
|
+
|
|
63
|
+
if (isAngleInRanges(angle, consumeSlideEvent)) {
|
|
64
|
+
gestureLockedRef.current = true
|
|
65
|
+
isMainAxisLockedRef.current = false
|
|
66
|
+
} else {
|
|
67
|
+
gestureLockedRef.current = true
|
|
68
|
+
isMainAxisLockedRef.current = true
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return isMainAxisLockedRef.current
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function handleVelocity() {
|
|
76
|
+
'main thread'
|
|
77
|
+
return isMainAxisLockedRef.current
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function handleTouchEnd() {
|
|
81
|
+
'main thread'
|
|
82
|
+
gestureLockedRef.current = false
|
|
83
|
+
isMainAxisLockedRef.current = false
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
handleTouchStart,
|
|
88
|
+
handleTouchMove,
|
|
89
|
+
handleTouchEnd,
|
|
90
|
+
handleVelocity,
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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 { BounceConfig } from '../types'
|
|
6
|
+
|
|
7
|
+
function useBounceConfig(bounceConfig?: BounceConfig) {
|
|
8
|
+
const {
|
|
9
|
+
startBounceItem,
|
|
10
|
+
endBounceItem,
|
|
11
|
+
enable = false,
|
|
12
|
+
startBounceItemWidth = 50,
|
|
13
|
+
endBounceItemWidth = 50,
|
|
14
|
+
onStartBounceItemBounce,
|
|
15
|
+
onEndBounceItemBounce,
|
|
16
|
+
} = bounceConfig ?? {}
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
enableBounce: enable,
|
|
20
|
+
startBounceItem,
|
|
21
|
+
endBounceItem,
|
|
22
|
+
startBounceItemWidth,
|
|
23
|
+
endBounceItemWidth,
|
|
24
|
+
onStartBounceItemBounce,
|
|
25
|
+
onEndBounceItemBounce,
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { useBounceConfig }
|
|
@@ -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 type { ReactElement } from '@lynx-js/react'
|
|
6
|
+
|
|
7
|
+
import type { CompoundModeConfig, SwiperProps } from '../types'
|
|
8
|
+
|
|
9
|
+
function useBounceView({
|
|
10
|
+
startBounceItem,
|
|
11
|
+
endBounceItem,
|
|
12
|
+
RTL,
|
|
13
|
+
itemWidth,
|
|
14
|
+
count,
|
|
15
|
+
containerWidth,
|
|
16
|
+
mode,
|
|
17
|
+
loop,
|
|
18
|
+
}: {
|
|
19
|
+
startBounceItem?: ReactElement
|
|
20
|
+
endBounceItem?: ReactElement
|
|
21
|
+
RTL: SwiperProps<unknown>['RTL']
|
|
22
|
+
itemWidth: number
|
|
23
|
+
containerWidth: number
|
|
24
|
+
count: number
|
|
25
|
+
mode: CompoundModeConfig['mode']
|
|
26
|
+
loop: boolean
|
|
27
|
+
}) {
|
|
28
|
+
const bounceStartView = startBounceItem
|
|
29
|
+
? (
|
|
30
|
+
<view class={RTL ? 'bounce-start-item-RTL' : 'bounce-start-item'}>
|
|
31
|
+
{startBounceItem}
|
|
32
|
+
</view>
|
|
33
|
+
)
|
|
34
|
+
: null
|
|
35
|
+
|
|
36
|
+
const shouldShowEndBounce = mode === 'normal' && loop === false
|
|
37
|
+
&& count * itemWidth >= containerWidth
|
|
38
|
+
|
|
39
|
+
const bounceEndView = endBounceItem && shouldShowEndBounce
|
|
40
|
+
? (
|
|
41
|
+
<view class={RTL ? 'bounce-end-item-RTL' : 'bounce-end-item'}>
|
|
42
|
+
{endBounceItem}
|
|
43
|
+
</view>
|
|
44
|
+
)
|
|
45
|
+
: null
|
|
46
|
+
|
|
47
|
+
return { bounceStartView, bounceEndView }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { useBounceView }
|
|
@@ -0,0 +1,131 @@
|
|
|
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 { SwipeDirection } from '../types'
|
|
8
|
+
import type {
|
|
9
|
+
CompoundModeConfig,
|
|
10
|
+
OffsetLimitResult,
|
|
11
|
+
SwiperProps,
|
|
12
|
+
} from '../types'
|
|
13
|
+
import { eqOrGtMT, eqOrLtMT, fixPrecisionMT, safeEqualMT } from '../utils'
|
|
14
|
+
|
|
15
|
+
interface UseChangeOpts extends
|
|
16
|
+
Pick<
|
|
17
|
+
SwiperProps<unknown>,
|
|
18
|
+
'containerWidth' | 'onSwipeStart' | 'onSwipeStop'
|
|
19
|
+
>
|
|
20
|
+
{
|
|
21
|
+
onChange?: (offset: number) => void
|
|
22
|
+
size: number
|
|
23
|
+
spaceBetween: number
|
|
24
|
+
dataCount: number
|
|
25
|
+
offsetLimit: OffsetLimitResult
|
|
26
|
+
'main-thread:onOffsetChange'?: (offset: number) => void
|
|
27
|
+
modeConfig: CompoundModeConfig
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* This hook handles change related things
|
|
32
|
+
* Including `onChange` and `main-thread:onOffsetChange`
|
|
33
|
+
*/
|
|
34
|
+
export function useChange({
|
|
35
|
+
onChange,
|
|
36
|
+
size: _size,
|
|
37
|
+
spaceBetween,
|
|
38
|
+
dataCount,
|
|
39
|
+
onSwipeStart,
|
|
40
|
+
onSwipeStop,
|
|
41
|
+
offsetLimit,
|
|
42
|
+
'main-thread:onOffsetChange': onOffsetChange,
|
|
43
|
+
}: UseChangeOpts) {
|
|
44
|
+
const prevIndexMTRef = useMainThreadRef<number>(0)
|
|
45
|
+
const prevOffsetMTRef = useMainThreadRef<number>(0)
|
|
46
|
+
const currentIndexMTRef = useMainThreadRef<number>(0)
|
|
47
|
+
|
|
48
|
+
const size = _size + spaceBetween
|
|
49
|
+
|
|
50
|
+
function callOnChange(currentIndex: number) {
|
|
51
|
+
if (onChange) {
|
|
52
|
+
onChange(currentIndex)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function callOnSwipeStart(currentIndex: number) {
|
|
57
|
+
if (onSwipeStart) {
|
|
58
|
+
onSwipeStart(currentIndex)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function callOnSwipeStop(currentIndex: number) {
|
|
62
|
+
if (onSwipeStop) {
|
|
63
|
+
onSwipeStop(currentIndex)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function setChangeSwipeStopMT() {
|
|
68
|
+
'main thread'
|
|
69
|
+
runOnBackground(callOnSwipeStop)(currentIndexMTRef.current)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function setChangeSwipeStartMT() {
|
|
73
|
+
'main thread'
|
|
74
|
+
runOnBackground(callOnSwipeStart)(currentIndexMTRef.current)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function setChangeOffset(offset: number, direction: SwipeDirection) {
|
|
78
|
+
'main thread'
|
|
79
|
+
|
|
80
|
+
function mod(n: number, m: number) {
|
|
81
|
+
return ((n % m) + m) % m
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Calculate raw index with proper sign handling
|
|
85
|
+
const rawIndex = fixPrecisionMT(-offset / size)
|
|
86
|
+
let currentIndex: number
|
|
87
|
+
|
|
88
|
+
if (direction === SwipeDirection.NORMAL) {
|
|
89
|
+
currentIndex = Math.floor(rawIndex)
|
|
90
|
+
} else if (direction === SwipeDirection.REVERT) {
|
|
91
|
+
currentIndex = Math.ceil(rawIndex)
|
|
92
|
+
} else {
|
|
93
|
+
currentIndex = currentIndexMTRef.current
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
currentIndex = mod(currentIndex, dataCount || Number.MAX_SAFE_INTEGER)
|
|
97
|
+
|
|
98
|
+
// If offsetLimit is applied, swiper could not scroll to the end.
|
|
99
|
+
// In this case, when it can no longer scroll, set index to 0 or max
|
|
100
|
+
// offsetLimit is always 0 in loop mode, so code below will only work when loop is false
|
|
101
|
+
if (
|
|
102
|
+
!safeEqualMT(offsetLimit.startLimit, 0)
|
|
103
|
+
&& eqOrGtMT(offset, -offsetLimit.startLimit)
|
|
104
|
+
) {
|
|
105
|
+
currentIndex = 0
|
|
106
|
+
} else if (
|
|
107
|
+
!safeEqualMT(offsetLimit.endLimit, 0)
|
|
108
|
+
&& eqOrLtMT(offset, -size * (dataCount - 1) + offsetLimit.endLimit)
|
|
109
|
+
) {
|
|
110
|
+
currentIndex = dataCount - 1
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
currentIndexMTRef.current = currentIndex
|
|
114
|
+
if (onOffsetChange && !safeEqualMT(offset, prevOffsetMTRef.current)) {
|
|
115
|
+
onOffsetChange(offset)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!safeEqualMT(prevIndexMTRef.current, currentIndex)) {
|
|
119
|
+
runOnBackground(callOnChange)(currentIndex)
|
|
120
|
+
prevIndexMTRef.current = currentIndex
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
prevOffsetMTRef.current = offset
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
setChangeOffset,
|
|
128
|
+
setChangeSwipeStartMT,
|
|
129
|
+
setChangeSwipeStopMT,
|
|
130
|
+
}
|
|
131
|
+
}
|