@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.
- package/CHANGELOG.md +25 -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 +85 -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 @@
|
|
|
1
|
+
{"version":3,"file":"utils/index.jsx","sources":["webpack://@lynx-js/lynx-ui-swiper/./src/utils/index.ts"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport type { OffsetLimitResult } from '../types'\n\nfunction easeOut(progress: number) {\n 'main thread'\n return 1 - Math.pow(1 - progress, 3)\n}\n\nfunction limiter(\n offset: number,\n dataCount: number,\n size: number,\n spaceBetween: number,\n loop: boolean,\n offsetLimit: OffsetLimitResult,\n): number {\n if (dataCount === 0) {\n return 0\n }\n if (!loop) {\n if (offset >= -offsetLimit.startLimit) {\n return -offsetLimit.startLimit\n } else if (\n offset\n < -(size + spaceBetween) * (dataCount - 1) + spaceBetween\n + offsetLimit.endLimit\n ) {\n return -(size + spaceBetween) * (dataCount - 1) + spaceBetween\n + offsetLimit.startLimit\n }\n }\n\n return offset\n}\n\n/**\n * limiter is used for useOffset\n * When mode === 'normal', different aligns are applied an alignOffset based on realOffset returned by useOffset\n * We first remove the offset, and use limiter to get the realOffset from useOffset\n * And then applied alignOffset back, to make the position right.\n */\nfunction limiterForFirstScreen(\n offset: number,\n dataCount: number,\n size: number,\n spaceBetween: number,\n loop: boolean,\n offsetLimit: OffsetLimitResult,\n alignOffset: number,\n) {\n return (\n alignOffset\n + limiter(\n offset - alignOffset,\n dataCount,\n size,\n spaceBetween,\n loop,\n offsetLimit,\n )\n )\n}\n\nfunction limiterMTS(\n offset: number,\n dataCount: number,\n size: number,\n loop: boolean,\n offsetLimit: OffsetLimitResult,\n) {\n 'main thread'\n let reachingLimit = false\n let finalOffset = offset\n if (!loop) {\n if (offset >= offsetLimit.startLimit) {\n reachingLimit = true\n finalOffset = offsetLimit.startLimit\n } else if (offset <= -size * (dataCount - 1) + offsetLimit.endLimit) {\n reachingLimit = true\n finalOffset = -size * (dataCount - 1) + offsetLimit.endLimit\n }\n }\n return { offset: finalOffset, reachingLimit }\n}\n\nconst EPSILON = 1e-10\n\nexport function fixPrecisionMT(num: number): number {\n 'main thread'\n // Use a small epsilon value to handle floating point precision\n const epsilon = 0.000001\n // Round to the nearest integer if the number is very close to an integer\n if (Math.abs(num - Math.round(num)) < epsilon) {\n return Math.round(num)\n }\n return num\n}\n\nexport function safeEqualMT(a: number, b: number, epsilon = EPSILON) {\n 'main thread'\n return Math.abs(a - b) < epsilon\n}\n\nexport function eqOrGtMT(a: number, b: number, epsilon = EPSILON) {\n 'main thread'\n return Math.abs(a - b) < epsilon || a > b\n}\n\nexport function eqOrLtMT(a: number, b: number, epsilon = EPSILON) {\n 'main thread'\n return Math.abs(a - b) < epsilon || a < b\n}\n\nexport { easeOut, limiter, limiterMTS, limiterForFirstScreen }\n"],"names":["easeOut","progress","Math","limiter","offset","dataCount","size","spaceBetween","loop","offsetLimit","limiterForFirstScreen","alignOffset","limiterMTS","reachingLimit","finalOffset","EPSILON","fixPrecisionMT","num","epsilon","safeEqualMT","a","b","eqOrGtMT","eqOrLtMT"],"mappings":"AAMA,SAASA,QAAQC,QAAgB;IAC/B;IACA,OAAO,IAAIC,KAAK,GAAG,CAAC,IAAID,UAAU;AACpC;AAEA,SAASE,QACPC,MAAc,EACdC,SAAiB,EACjBC,IAAY,EACZC,YAAoB,EACpBC,IAAa,EACbC,WAA8B;IAE9B,IAAIJ,AAAc,MAAdA,WACF,OAAO;IAET,IAAI,CAACG,MACH;QAAA,IAAIJ,UAAU,CAACK,YAAY,UAAU,EACnC,OAAO,CAACA,YAAY,UAAU;aACzB,IACLL,SACI,CAAEE,CAAAA,OAAOC,YAAW,IAAMF,CAAAA,YAAY,KAAKE,eACzCE,YAAY,QAAQ,EAE1B,OAAO,CAAEH,CAAAA,OAAOC,YAAW,IAAMF,CAAAA,YAAY,KAAKE,eAC9CE,YAAY,UAAU;IAC5B;IAGF,OAAOL;AACT;AAQA,SAASM,sBACPN,MAAc,EACdC,SAAiB,EACjBC,IAAY,EACZC,YAAoB,EACpBC,IAAa,EACbC,WAA8B,EAC9BE,WAAmB;IAEnB,OACEA,cACER,QACAC,SAASO,aACTN,WACAC,MACAC,cACAC,MACAC;AAGN;AAEA,SAASG,WACPR,MAAc,EACdC,SAAiB,EACjBC,IAAY,EACZE,IAAa,EACbC,WAA8B;IAE9B;IACA,IAAII,gBAAgB;IACpB,IAAIC,cAAcV;IAClB,IAAI,CAACI,MACH;QAAA,IAAIJ,UAAUK,YAAY,UAAU,EAAE;YACpCI,gBAAgB;YAChBC,cAAcL,YAAY,UAAU;QACtC,OAAO,IAAIL,UAAU,CAACE,OAAQD,CAAAA,YAAY,KAAKI,YAAY,QAAQ,EAAE;YACnEI,gBAAgB;YAChBC,cAAc,CAACR,OAAQD,CAAAA,YAAY,KAAKI,YAAY,QAAQ;QAC9D;IAAA;IAEF,OAAO;QAAE,QAAQK;QAAaD;IAAc;AAC9C;AAEA,MAAME,UAAU;AAET,SAASC,eAAeC,GAAW;IACxC;IAEA,MAAMC,UAAU;IAEhB,IAAIhB,KAAK,GAAG,CAACe,MAAMf,KAAK,KAAK,CAACe,QAAQC,SACpC,OAAOhB,KAAK,KAAK,CAACe;IAEpB,OAAOA;AACT;AAEO,SAASE,YAAYC,CAAS,EAAEC,CAAS,EAAEH,UAAUH,OAAO;IACjE;IACA,OAAOb,KAAK,GAAG,CAACkB,IAAIC,KAAKH;AAC3B;AAEO,SAASI,SAASF,CAAS,EAAEC,CAAS,EAAEH,UAAUH,OAAO;IAC9D;IACA,OAAOb,KAAK,GAAG,CAACkB,IAAIC,KAAKH,WAAWE,IAAIC;AAC1C;AAEO,SAASE,SAASH,CAAS,EAAEC,CAAS,EAAEH,UAAUH,OAAO;IAC9D;IACA,OAAOb,KAAK,GAAG,CAACkB,IAAIC,KAAKH,WAAWE,IAAIC;AAC1C"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lynx-js/lynx-ui-swiper",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
|
|
3
|
+
"version": "3.131.0",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/lynx-family/lynx-ui.git",
|
|
7
|
+
"directory": "packages/lynx-ui-swiper"
|
|
8
|
+
},
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "The Lynx Authors"
|
|
12
|
+
},
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.jsx",
|
|
16
|
+
"module": "./dist/index.jsx",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"src/**",
|
|
20
|
+
"README.md",
|
|
21
|
+
"CHANGELOG.md",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"tsconfig.docs.json",
|
|
24
|
+
"tsconfig.json",
|
|
25
|
+
"dist/**"
|
|
26
|
+
],
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@lynx-js/react-use": "0.0.7",
|
|
29
|
+
"@lynx-js/lynx-ui-common": "3.131.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@lynx-js/react": "0.113.0",
|
|
33
|
+
"@lynx-js/types": "3.6.0",
|
|
34
|
+
"@rslib/core": "^0.16.0",
|
|
35
|
+
"@types/react": "^18.3.8",
|
|
36
|
+
"typescript": "^5.5.3"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@lynx-js/react": ">=0.100.0",
|
|
40
|
+
"@lynx-js/types": "*",
|
|
41
|
+
"@types/react": "^18"
|
|
42
|
+
},
|
|
6
43
|
"publishConfig": {
|
|
7
44
|
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"jsnext:source": "./src/index.ts",
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "rslib build",
|
|
49
|
+
"build:watch": "rslib build --watch",
|
|
50
|
+
"test": "vitest"
|
|
8
51
|
}
|
|
9
|
-
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,414 @@
|
|
|
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
|
+
forwardRef,
|
|
7
|
+
memo,
|
|
8
|
+
useImperativeHandle,
|
|
9
|
+
useMainThreadRef,
|
|
10
|
+
useMemo,
|
|
11
|
+
} from '@lynx-js/react'
|
|
12
|
+
import type { ForwardedRef } from '@lynx-js/react'
|
|
13
|
+
|
|
14
|
+
import { useTouchEmulation } from '@lynx-js/react-use'
|
|
15
|
+
import type { MainThread } from '@lynx-js/types'
|
|
16
|
+
|
|
17
|
+
import { LOOP_COUNT } from '../const'
|
|
18
|
+
import { useBounceConfig } from '../hooks/useBounceConfig'
|
|
19
|
+
import { useBounceView } from '../hooks/useBounceView'
|
|
20
|
+
import { useChange } from '../hooks/useChange'
|
|
21
|
+
import useFirstScreenStyle, {
|
|
22
|
+
getInnerContainerWidthMT,
|
|
23
|
+
} from '../hooks/useFirstScreenStyle'
|
|
24
|
+
import { useModeConfig } from '../hooks/useModeConfig'
|
|
25
|
+
import { useOffset } from '../hooks/useOffset'
|
|
26
|
+
import { useOffsetLimit } from '../hooks/useOffsetLimit'
|
|
27
|
+
import { useReset } from '../hooks/useReset'
|
|
28
|
+
import { SwiperContext } from '../store'
|
|
29
|
+
import type {
|
|
30
|
+
SwipeDirection,
|
|
31
|
+
SwiperProps,
|
|
32
|
+
SwiperRef,
|
|
33
|
+
UpdateSwiperInnerContainerOptions,
|
|
34
|
+
} from '../types'
|
|
35
|
+
import { easeOut } from '../utils'
|
|
36
|
+
import { comparePropsWithObject } from '../utils/compareProps'
|
|
37
|
+
|
|
38
|
+
import './styles.css'
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Swiper Component
|
|
42
|
+
*/
|
|
43
|
+
const Swiper = forwardRef(
|
|
44
|
+
<T,>(
|
|
45
|
+
{
|
|
46
|
+
data = [],
|
|
47
|
+
itemWidth,
|
|
48
|
+
containerWidth = lynx.__globalProps.screenWidth
|
|
49
|
+
?? SystemInfo.pixelWidth / SystemInfo.pixelRatio,
|
|
50
|
+
loop = false,
|
|
51
|
+
mode,
|
|
52
|
+
autoPlay = false,
|
|
53
|
+
autoPlayInterval = 2000,
|
|
54
|
+
duration = 500,
|
|
55
|
+
itemHeight = 'auto',
|
|
56
|
+
initialIndex = 0,
|
|
57
|
+
onChange,
|
|
58
|
+
onSwipeStart,
|
|
59
|
+
onSwipeStop,
|
|
60
|
+
modeConfig: _modeConfig,
|
|
61
|
+
offsetLimit: _offsetLimit,
|
|
62
|
+
bounceConfig,
|
|
63
|
+
children,
|
|
64
|
+
'main-thread:easing': easing = easeOut,
|
|
65
|
+
'main-thread:customAnimation': customAnimation,
|
|
66
|
+
'main-thread:onOffsetChange': onOffsetChange,
|
|
67
|
+
customAnimationFirstScreen,
|
|
68
|
+
swiperKey,
|
|
69
|
+
resetOnReuse,
|
|
70
|
+
trackStyle = {},
|
|
71
|
+
style = {},
|
|
72
|
+
loopDuplicateCount: rawLoopDuplicateCount = LOOP_COUNT,
|
|
73
|
+
experimentalHorizontalSwipeOnly = false,
|
|
74
|
+
consumeSlideEvent = [
|
|
75
|
+
[-180, -135],
|
|
76
|
+
[-45, 45],
|
|
77
|
+
[135, 180],
|
|
78
|
+
],
|
|
79
|
+
blockNativeEvent = false,
|
|
80
|
+
RTL = false,
|
|
81
|
+
}: SwiperProps<T> & { experimentalHorizontalSwipeOnly?: boolean },
|
|
82
|
+
ref: ForwardedRef<SwiperRef>,
|
|
83
|
+
) => {
|
|
84
|
+
const dataCount = data.length
|
|
85
|
+
|
|
86
|
+
const loopDuplicateCount = Math.min(rawLoopDuplicateCount, dataCount)
|
|
87
|
+
|
|
88
|
+
const containerRef = useMainThreadRef<MainThread.Element>(null)
|
|
89
|
+
const childrenMapRef = useMainThreadRef<Record<number, MainThread.Element>>(
|
|
90
|
+
{},
|
|
91
|
+
)
|
|
92
|
+
const { modeConfig, spaceBetween } = useModeConfig({
|
|
93
|
+
mode,
|
|
94
|
+
modeConfig: _modeConfig,
|
|
95
|
+
})
|
|
96
|
+
const offsetLimit = useOffsetLimit({
|
|
97
|
+
offsetLimit: _offsetLimit,
|
|
98
|
+
loop,
|
|
99
|
+
itemWidth,
|
|
100
|
+
containerWidth,
|
|
101
|
+
modeConfig,
|
|
102
|
+
dataCount,
|
|
103
|
+
spaceBetween,
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
const {
|
|
107
|
+
enableBounce,
|
|
108
|
+
startBounceItem,
|
|
109
|
+
endBounceItem,
|
|
110
|
+
startBounceItemWidth,
|
|
111
|
+
endBounceItemWidth,
|
|
112
|
+
onStartBounceItemBounce,
|
|
113
|
+
onEndBounceItemBounce,
|
|
114
|
+
} = useBounceConfig(bounceConfig)
|
|
115
|
+
|
|
116
|
+
function setChildrenRef(refI: MainThread.Element, key: number) {
|
|
117
|
+
'main thread'
|
|
118
|
+
childrenMapRef.current[key] = refI
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const contextValue = useMemo(
|
|
122
|
+
() => ({
|
|
123
|
+
loop,
|
|
124
|
+
itemWidth,
|
|
125
|
+
itemHeight,
|
|
126
|
+
setChildrenRef,
|
|
127
|
+
modeConfig,
|
|
128
|
+
initialIndex,
|
|
129
|
+
customAnimationFirstScreen,
|
|
130
|
+
spaceBetween,
|
|
131
|
+
RTL,
|
|
132
|
+
}),
|
|
133
|
+
[loop, itemWidth, itemHeight, spaceBetween, RTL],
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
const { setChangeOffset, setChangeSwipeStartMT, setChangeSwipeStopMT } =
|
|
137
|
+
useChange({
|
|
138
|
+
onChange,
|
|
139
|
+
size: itemWidth,
|
|
140
|
+
spaceBetween,
|
|
141
|
+
containerWidth,
|
|
142
|
+
dataCount,
|
|
143
|
+
modeConfig,
|
|
144
|
+
offsetLimit,
|
|
145
|
+
onSwipeStart,
|
|
146
|
+
onSwipeStop,
|
|
147
|
+
'main-thread:onOffsetChange': onOffsetChange,
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
function setOffset(offset: number) {
|
|
151
|
+
'main thread'
|
|
152
|
+
const container = containerRef.current
|
|
153
|
+
if (container) {
|
|
154
|
+
container.setStyleProperties({
|
|
155
|
+
transform: `translate(${offset}px, 0)`,
|
|
156
|
+
transition: 'none',
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function updateSwiperInnerContainerSizeMT(
|
|
162
|
+
updateOptions: UpdateSwiperInnerContainerOptions,
|
|
163
|
+
) {
|
|
164
|
+
'main thread'
|
|
165
|
+
const container = containerRef.current
|
|
166
|
+
if (container) {
|
|
167
|
+
const width = getInnerContainerWidthMT(updateOptions)
|
|
168
|
+
// These values should get from function params, instead of closure, to make it is updated
|
|
169
|
+
const {
|
|
170
|
+
loop: _loop,
|
|
171
|
+
loopDuplicateCount: _loopDuplicateCount,
|
|
172
|
+
itemWidth: _itemWidth,
|
|
173
|
+
spaceBetween: _spaceBetween,
|
|
174
|
+
itemHeight: _itemHeight,
|
|
175
|
+
} = updateOptions
|
|
176
|
+
|
|
177
|
+
if (updateOptions.mode === 'custom') {
|
|
178
|
+
// @ts-expect-error CSS types accepting number
|
|
179
|
+
container.setStyleProperties({
|
|
180
|
+
...trackStyle,
|
|
181
|
+
height: `${_itemHeight}px`,
|
|
182
|
+
})
|
|
183
|
+
} else {
|
|
184
|
+
const insetInlineStartKey = RTL ? 'inset-inline-start' : 'left'
|
|
185
|
+
// @ts-expect-error CSS types accepting number
|
|
186
|
+
container.setStyleProperties({
|
|
187
|
+
...trackStyle,
|
|
188
|
+
[insetInlineStartKey]: _loop
|
|
189
|
+
? `${-_loopDuplicateCount * (_itemWidth + _spaceBetween)}px`
|
|
190
|
+
: '0px',
|
|
191
|
+
width,
|
|
192
|
+
height: `${_itemHeight}px`,
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function onChildrenOffsetUpdate(offset: number) {
|
|
199
|
+
'main thread'
|
|
200
|
+
const startCount = loop ? -loopDuplicateCount : 0
|
|
201
|
+
const endCount = loop ? dataCount + loopDuplicateCount : dataCount
|
|
202
|
+
for (let i = startCount; i < endCount; i++) {
|
|
203
|
+
const child = childrenMapRef.current[i]
|
|
204
|
+
const childOffset = offset + i * (itemWidth + spaceBetween)
|
|
205
|
+
if (!customAnimation || !child) {
|
|
206
|
+
return
|
|
207
|
+
}
|
|
208
|
+
const childStyles =
|
|
209
|
+
customAnimation(childOffset / (itemWidth + spaceBetween), i)
|
|
210
|
+
|| {}
|
|
211
|
+
child.setStyleProperties(childStyles as Record<string, string>)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function onOffsetUpdate(offset: number, direction: SwipeDirection) {
|
|
216
|
+
'main thread'
|
|
217
|
+
if (modeConfig.mode === 'normal') {
|
|
218
|
+
let effectiveOffset = offset
|
|
219
|
+
if (modeConfig.align === 'center') {
|
|
220
|
+
effectiveOffset = offset + (containerWidth - itemWidth) / 2
|
|
221
|
+
} else if (modeConfig.align === 'end') {
|
|
222
|
+
effectiveOffset = offset + containerWidth - itemWidth
|
|
223
|
+
}
|
|
224
|
+
setOffset(RTL ? -effectiveOffset : effectiveOffset)
|
|
225
|
+
onChildrenOffsetUpdate(offset)
|
|
226
|
+
} else {
|
|
227
|
+
onChildrenOffsetUpdate(offset)
|
|
228
|
+
}
|
|
229
|
+
setChangeOffset(offset, direction)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function onSwipeStartMT() {
|
|
233
|
+
'main thread'
|
|
234
|
+
setChangeSwipeStartMT()
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function onSwipeStopMT() {
|
|
238
|
+
'main thread'
|
|
239
|
+
setChangeSwipeStopMT()
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// `containerStyle` is only used at firstScreen
|
|
243
|
+
// After that, we use MTS to update styles imperatively
|
|
244
|
+
const { containerStyle } = useFirstScreenStyle({
|
|
245
|
+
itemWidth,
|
|
246
|
+
containerWidth,
|
|
247
|
+
loop,
|
|
248
|
+
initialIndex,
|
|
249
|
+
dataCount,
|
|
250
|
+
modeConfig,
|
|
251
|
+
itemHeight,
|
|
252
|
+
offsetLimit,
|
|
253
|
+
loopDuplicateCount,
|
|
254
|
+
RTL,
|
|
255
|
+
spaceBetween,
|
|
256
|
+
swiperKey,
|
|
257
|
+
trackStyle,
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
const {
|
|
261
|
+
handleTouchMove,
|
|
262
|
+
handleTouchStart,
|
|
263
|
+
handleTouchEnd,
|
|
264
|
+
swipeNext,
|
|
265
|
+
swipePrev,
|
|
266
|
+
swipeTo,
|
|
267
|
+
resetOffsetMT,
|
|
268
|
+
cancelAnimationJS,
|
|
269
|
+
startAutoPlayMT,
|
|
270
|
+
} = useOffset(
|
|
271
|
+
{
|
|
272
|
+
autoPlay,
|
|
273
|
+
autoPlayInterval,
|
|
274
|
+
loop,
|
|
275
|
+
size: itemWidth,
|
|
276
|
+
spaceBetween,
|
|
277
|
+
dataCount,
|
|
278
|
+
duration,
|
|
279
|
+
easing,
|
|
280
|
+
offsetLimit,
|
|
281
|
+
initialIndex,
|
|
282
|
+
modeConfig,
|
|
283
|
+
containerWidth,
|
|
284
|
+
enableBounce,
|
|
285
|
+
startBounceItemWidth,
|
|
286
|
+
endBounceItemWidth,
|
|
287
|
+
onStartBounceItemBounce,
|
|
288
|
+
onEndBounceItemBounce,
|
|
289
|
+
experimentalHorizontalSwipeOnly,
|
|
290
|
+
onSwipeStartMT,
|
|
291
|
+
onSwipeStopMT,
|
|
292
|
+
consumeSlideEvent,
|
|
293
|
+
RTL,
|
|
294
|
+
},
|
|
295
|
+
onOffsetUpdate,
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
const { setContainerRef } = useReset({
|
|
299
|
+
swiperKey,
|
|
300
|
+
itemHeight,
|
|
301
|
+
itemWidth,
|
|
302
|
+
containerWidth,
|
|
303
|
+
loop,
|
|
304
|
+
dataCount,
|
|
305
|
+
modeConfig,
|
|
306
|
+
resetOffsetMT,
|
|
307
|
+
resetOnReuse,
|
|
308
|
+
initialIndex,
|
|
309
|
+
loopDuplicateCount,
|
|
310
|
+
updateSwiperInnerContainerSizeMT,
|
|
311
|
+
startAutoPlayMT,
|
|
312
|
+
spaceBetween,
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
const touchHandlers = useTouchEmulation({
|
|
316
|
+
onTouchCancelMT: handleTouchEnd,
|
|
317
|
+
onTouchEndMT: handleTouchEnd,
|
|
318
|
+
onTouchMoveMT: handleTouchMove,
|
|
319
|
+
onTouchStartMT: handleTouchStart,
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
useImperativeHandle(ref, () => ({
|
|
323
|
+
swipeNext,
|
|
324
|
+
swipePrev,
|
|
325
|
+
swipeTo,
|
|
326
|
+
cancelAnimation: cancelAnimationJS,
|
|
327
|
+
}))
|
|
328
|
+
|
|
329
|
+
const { bounceStartView, bounceEndView } = useBounceView({
|
|
330
|
+
startBounceItem,
|
|
331
|
+
endBounceItem,
|
|
332
|
+
loop,
|
|
333
|
+
mode: modeConfig.mode,
|
|
334
|
+
itemWidth,
|
|
335
|
+
count: dataCount,
|
|
336
|
+
RTL,
|
|
337
|
+
containerWidth,
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
return (
|
|
341
|
+
<SwiperContext.Provider value={contextValue}>
|
|
342
|
+
<view
|
|
343
|
+
class='swiper-root'
|
|
344
|
+
style={{
|
|
345
|
+
...style,
|
|
346
|
+
width: `${containerWidth}px`,
|
|
347
|
+
height: `${itemHeight}px`,
|
|
348
|
+
direction: RTL === 'lynx-rtl' ? 'lynx-rtl' : (RTL ? 'rtl' : 'ltr'),
|
|
349
|
+
}}
|
|
350
|
+
{...touchHandlers}
|
|
351
|
+
consume-slide-event={consumeSlideEvent}
|
|
352
|
+
block-native-event={blockNativeEvent}
|
|
353
|
+
>
|
|
354
|
+
<view
|
|
355
|
+
class='swiper-track'
|
|
356
|
+
style={containerStyle}
|
|
357
|
+
ref={setContainerRef}
|
|
358
|
+
main-thread:ref={containerRef}
|
|
359
|
+
implicit-animation='false'
|
|
360
|
+
>
|
|
361
|
+
{bounceStartView}
|
|
362
|
+
{loop
|
|
363
|
+
? (
|
|
364
|
+
<>
|
|
365
|
+
{Array.from(
|
|
366
|
+
{ length: loopDuplicateCount },
|
|
367
|
+
(_, i) => loopDuplicateCount - i - 1,
|
|
368
|
+
).map((i) =>
|
|
369
|
+
children({
|
|
370
|
+
index: data.length - i - 1,
|
|
371
|
+
item: data[data.length - i - 1],
|
|
372
|
+
realIndex: -i - 1,
|
|
373
|
+
})
|
|
374
|
+
)}
|
|
375
|
+
</>
|
|
376
|
+
)
|
|
377
|
+
: null}
|
|
378
|
+
{data.map((item: T, index: number) =>
|
|
379
|
+
children({
|
|
380
|
+
index,
|
|
381
|
+
item,
|
|
382
|
+
realIndex: index,
|
|
383
|
+
})
|
|
384
|
+
)}
|
|
385
|
+
{loop
|
|
386
|
+
? (
|
|
387
|
+
<>
|
|
388
|
+
{Array.from({ length: loopDuplicateCount }, (_, i) => i).map((
|
|
389
|
+
i,
|
|
390
|
+
) =>
|
|
391
|
+
children({
|
|
392
|
+
index: i,
|
|
393
|
+
item: data[i],
|
|
394
|
+
realIndex: data.length + i,
|
|
395
|
+
})
|
|
396
|
+
)}
|
|
397
|
+
</>
|
|
398
|
+
)
|
|
399
|
+
: null}
|
|
400
|
+
{bounceEndView}
|
|
401
|
+
</view>
|
|
402
|
+
</view>
|
|
403
|
+
</SwiperContext.Provider>
|
|
404
|
+
)
|
|
405
|
+
},
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
const MemoizedSwiper = memo(
|
|
409
|
+
Swiper,
|
|
410
|
+
(prevProps, nextProps) =>
|
|
411
|
+
comparePropsWithObject(prevProps, nextProps, ['modeConfig', 'bounces']),
|
|
412
|
+
) as typeof Swiper
|
|
413
|
+
|
|
414
|
+
export { MemoizedSwiper as Swiper }
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
.swiper-root {
|
|
2
|
+
display: linear;
|
|
3
|
+
linear-orientation: horizontal;
|
|
4
|
+
width: 100%;
|
|
5
|
+
height: 100%;
|
|
6
|
+
overflow: hidden;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.swiper-track {
|
|
10
|
+
display: linear;
|
|
11
|
+
linear-orientation: horizontal;
|
|
12
|
+
width: 100%;
|
|
13
|
+
height: 100%;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.bounce-start-item {
|
|
17
|
+
position: absolute;
|
|
18
|
+
left: 0;
|
|
19
|
+
transform: translate(-100%);
|
|
20
|
+
height: 100%;
|
|
21
|
+
width: max-content;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.bounce-start-item-RTL {
|
|
25
|
+
position: absolute;
|
|
26
|
+
inset-inline-start: 0;
|
|
27
|
+
transform: translate(100%);
|
|
28
|
+
height: 100%;
|
|
29
|
+
width: max-content;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.bounce-end-item {
|
|
33
|
+
position: absolute;
|
|
34
|
+
left: 100%;
|
|
35
|
+
height: 100%;
|
|
36
|
+
width: max-content;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.bounce-end-item-RTL {
|
|
40
|
+
position: absolute;
|
|
41
|
+
inset-inline-start: 100%;
|
|
42
|
+
height: 100%;
|
|
43
|
+
width: max-content;
|
|
44
|
+
}
|