Skip to content

Commit 03376f8

Browse files
authored
perf: ⚡️ 优化数组拖动动画 array-items/array-table (#15)
1 parent c0913a1 commit 03376f8

File tree

1 file changed

+34
-7
lines changed
  • packages/components/src/__builtins__

1 file changed

+34
-7
lines changed

packages/components/src/__builtins__/sort.tsx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '@dnd-kit/sortable'
77
import { CSS } from '@dnd-kit/utilities'
88
import { ReactFC } from '@formily/reactive-react'
9-
import React, { createContext, useContext } from 'react'
9+
import React, { createContext, useContext, useMemo } from 'react'
1010

1111
export interface ISortableContainerProps {
1212
list: any[]
@@ -76,7 +76,7 @@ export function SortableElement<T extends React.HTMLAttributes<HTMLElement>>(
7676
const sortable = useSortable({
7777
id: index + 1,
7878
})
79-
const { setNodeRef, transform, isDragging } = sortable
79+
const { setNodeRef, transform, transition, isDragging } = sortable
8080
if (transform) {
8181
switch (lockAxis) {
8282
case 'x':
@@ -90,11 +90,38 @@ export function SortableElement<T extends React.HTMLAttributes<HTMLElement>>(
9090
break
9191
}
9292
}
93-
const style = {
94-
...props.style,
95-
transform: CSS.Transform.toString(transform),
96-
zIndex: isDragging ? 999 : 0,
97-
}
93+
94+
const style = useMemo(() => {
95+
const itemStyle: React.CSSProperties = {
96+
position: 'relative',
97+
touchAction: 'none',
98+
zIndex: 1,
99+
transform: `translate3d(${transform?.x || 0}px, ${
100+
transform?.y || 0
101+
}px, 0)`,
102+
transition: `${transform ? 'all 200ms ease' : ''}`,
103+
}
104+
const dragStyle = {
105+
transition,
106+
opacity: '0.8',
107+
transform: `translate3d(${transform?.x || 0}px, ${
108+
transform?.y || 0
109+
}px, 0)`,
110+
}
111+
112+
const computedStyle = isDragging
113+
? {
114+
...itemStyle,
115+
...dragStyle,
116+
...props.style,
117+
}
118+
: {
119+
...itemStyle,
120+
...props.style,
121+
}
122+
123+
return computedStyle
124+
}, [isDragging, transform, transition, props.style])
98125

99126
return (
100127
<SortableItemContext.Provider value={sortable}>

0 commit comments

Comments
 (0)