Skip to content

Commit a63c1a5

Browse files
author
Heising
committed
chore: replace react-spring from motion
1 parent d304753 commit a63c1a5

File tree

3 files changed

+50
-46
lines changed

3 files changed

+50
-46
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,21 @@
154154
"tdesign-site-components": "^0.15.6",
155155
"tdesign-theme-generator": "^1.1.0",
156156
"typescript": "5.2.2",
157-
"vite": "^5.4.7",
157+
"vite": "^2.7.0",
158158
"vite-plugin-pwa": "^0.11.11",
159159
"vite-plugin-tdoc": "^2.0.0",
160160
"vitest": "^2.1.9",
161161
"workbox-precaching": "^6.3.0"
162162
},
163163
"dependencies": {
164164
"@popperjs/core": "^2.11.8",
165-
"@react-spring/web": "^9.7.5",
166165
"@use-gesture/react": "^10.2.10",
167166
"ahooks": "^3.1.9",
168167
"classnames": "^2.3.1",
169168
"dayjs": "^1.11.13",
170169
"hoist-non-react-statics": "^3.3.2",
171170
"lodash-es": "^4.17.21",
172-
"react-spring": "9.6.1",
171+
"motion": "^12.6.3",
173172
"react-transition-group": "^4.4.2",
174173
"smoothscroll-polyfill": "^0.4.4",
175174
"tdesign-icons-react": "^0.4.4",

src/picker/PickerItem.tsx

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { FC, useRef, memo, useCallback, useContext, useMemo } from 'react
22
import { useDebounceEffect } from 'ahooks';
33
import { isUndefined } from 'lodash-es';
44
import { useDrag } from '@use-gesture/react';
5-
import { useSpring, animated } from '@react-spring/web';
5+
import { useSpring, motion } from 'motion/react';
66
import useDefaultProps from 'tdesign-mobile-react/hooks/useDefaultProps';
77
import useConfig from '../hooks/useConfig';
88
import nearest from '../_util/nearest';
@@ -52,13 +52,17 @@ const PickerItem: FC<PickerItemProps> = memo((props) => {
5252
.map((_, index) => index * itemHeight - yCompensation);
5353
}, [count, getControlItemHeight, getControlYCompensation]);
5454

55-
const [{ y }, api] = useSpring(() => ({
56-
from: { y: 0 },
57-
config: {
58-
tension: 400,
59-
mass: 0.8,
60-
},
61-
}));
55+
// const [{ y }, api] = useSpring(() => ({
56+
// from: { y: 0 },
57+
// config: {
58+
// tension: 400,
59+
// mass: 0.8,
60+
// },
61+
// }));
62+
63+
const y = useSpring(0, {
64+
stiffness: 5,
65+
});
6266

6367
const srollTo = useCallback(
6468
(index: number, immediate = false) => {
@@ -69,9 +73,14 @@ const PickerItem: FC<PickerItemProps> = memo((props) => {
6973
} else if (index >= offsetYList.length) {
7074
nextIndex = offsetYList.length - 1;
7175
}
72-
api.start({ y: offsetYList[nextIndex], immediate });
76+
const offsetY = offsetYList[nextIndex];
77+
if (immediate) {
78+
y.jump(-offsetY);
79+
} else {
80+
y.set(-offsetY);
81+
}
7382
},
74-
[api, getOffsetYList],
83+
[getOffsetYList, y],
7584
);
7685

7786
const handleChange = (value: number | string) => {
@@ -87,11 +96,11 @@ const PickerItem: FC<PickerItemProps> = memo((props) => {
8796
if (currentIndex >= 0) {
8897
srollTo(currentIndex, isUndefined(lastIndexRef.current));
8998
} else {
90-
api.start({ y: 0 });
99+
y.set(0);
91100
}
92101
lastIndexRef.current = currentIndex;
93102
},
94-
[api, currentIndex, srollTo],
103+
[y, currentIndex, srollTo],
95104
{ wait: 100 },
96105
);
97106

@@ -109,23 +118,19 @@ const PickerItem: FC<PickerItemProps> = memo((props) => {
109118
const nextIndex = offsetYList.findIndex((item) => item === nextOffsetY);
110119
if (isUndefined(value)) {
111120
lastIndexRef.current = nextIndex;
112-
api.start({
113-
to: async (next) => {
114-
await next({ y: nextOffsetY });
115-
handleChange(options[nextIndex].value);
116-
},
117-
});
121+
y.set(-nextOffsetY);
122+
handleChange(options[nextIndex].value);
118123
} else {
119124
// 受控模式,onChange回调后等待value更新,如果不更新回退到原处
120125
handleChange(options[nextIndex].value);
121126
setTimeout(() => {
122127
if (lastIndex === lastIndexRef.current) {
123-
api.start({ y: offsetYList[lastIndex] || 0 });
128+
y.set(-offsetYList[lastIndex] || 0);
124129
}
125130
}, 100);
126131
}
127132
} else {
128-
api.start({ y: offsetY });
133+
y.set(-offsetY);
129134
}
130135
},
131136
{
@@ -150,17 +155,17 @@ const PickerItem: FC<PickerItemProps> = memo((props) => {
150155
},
151156
},
152157
);
153-
158+
console.log('y', y);
154159
return withNativeProps(
155160
props,
156161
<div className={name} ref={rootRef}>
157-
<animated.div ref={controlRef} className={`${name}__wrapper`} style={{ y: y.to((y) => -y) }}>
162+
<motion.div ref={controlRef} className={`${name}__wrapper`} style={{ y }}>
158163
{options.map((item) => (
159164
<div key={item.value} className={`${name}__item`}>
160165
{(formatter && formatter(item)) || item.label}
161166
</div>
162167
))}
163-
</animated.div>
168+
</motion.div>
164169
</div>,
165170
);
166171
});

src/swipe-cell/SwipeCell.tsx

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isArray, isBoolean } from 'lodash-es';
44
import classNames from 'classnames';
55
import { useClickAway } from 'ahooks';
66
import { useDrag } from '@use-gesture/react';
7-
import { useSpring, animated } from '@react-spring/web';
7+
import { useSpring, motion } from 'motion/react';
88
import parseTNode from 'tdesign-mobile-react/_util/parseTNode';
99
import nearest from '../_util/nearest';
1010
import withNativeProps from '../_util/withNativeProps';
@@ -82,16 +82,17 @@ const SwipeCell = forwardRef<SwipeCellRef, SwipeCellProps>((originProps, ref) =>
8282
return 0;
8383
};
8484

85-
const [{ x }, api] = useSpring(
86-
() => ({
87-
x: 0,
88-
config: { tension: 200, friction: 30 },
89-
}),
90-
[],
91-
);
85+
const x = useSpring(0, {
86+
stiffness: 200,
87+
damping: 30,
88+
});
9289

9390
const close = (immediate = false) => {
94-
api.start({ x: 0, immediate });
91+
if (immediate) {
92+
x.jump(0);
93+
} else {
94+
x.set(0);
95+
}
9596
onChange();
9697
if (curSure.content) {
9798
setTimeout(() => {
@@ -105,11 +106,13 @@ const SwipeCell = forwardRef<SwipeCellRef, SwipeCellProps>((originProps, ref) =>
105106
};
106107

107108
const expand = (side: SideType = 'right', immediate = false) => {
108-
const x = getSideOffsetX(side);
109-
api.start({
110-
x,
111-
immediate,
112-
});
109+
const offsetX = getSideOffsetX(side);
110+
if (immediate) {
111+
x.jump(offsetX);
112+
} else {
113+
x.set(offsetX);
114+
}
115+
113116
onChange(side);
114117
};
115118

@@ -145,10 +148,7 @@ const SwipeCell = forwardRef<SwipeCellRef, SwipeCellProps>((originProps, ref) =>
145148
ctx.dragging = false;
146149
});
147150
} else {
148-
api.start({
149-
x: offsetX,
150-
immediate: true,
151-
});
151+
x.jump(offsetX);
152152
}
153153
},
154154
{
@@ -272,7 +272,7 @@ const SwipeCell = forwardRef<SwipeCellRef, SwipeCellProps>((originProps, ref) =>
272272
}
273273
}}
274274
>
275-
<animated.div className={`${swipeCellClass}__wrapper`} style={{ x }}>
275+
<motion.div className={`${swipeCellClass}__wrapper`} style={{ x }}>
276276
{left && (
277277
<div className={`${swipeCellClass}__left`} ref={leftRef}>
278278
{renderSureContent()}
@@ -286,7 +286,7 @@ const SwipeCell = forwardRef<SwipeCellRef, SwipeCellProps>((originProps, ref) =>
286286
{renderActions(right as any, 'right')}
287287
</div>
288288
)}
289-
</animated.div>
289+
</motion.div>
290290
</div>,
291291
);
292292
});

0 commit comments

Comments
 (0)