1- import React , { useRef } from 'react'
1+ import * as React from 'react'
22import { useSprings , animated } from '@react-spring/web'
33import { useDrag } from 'react-use-gesture'
44import clamp from 'lodash.clamp'
55import swap from 'lodash-move'
66
77import styles from './styles.module.css'
88
9- const fn = ( order : number [ ] , active = false , originalIndex = 0 , curIndex = 0 , y = 0 ) => ( index : number ) =>
10- active && index === originalIndex
11- ? {
12- y : curIndex * 50 + y ,
13- scale : 1.1 ,
14- zIndex : 1 ,
15- shadow : 15 ,
16- immediate : ( key : string ) => key === 'y' || key === 'zIndex' ,
17- }
18- : {
19- y : order . indexOf ( index ) * 50 ,
20- scale : 1 ,
21- zIndex : 0 ,
22- shadow : 1 ,
23- immediate : false ,
24- }
9+ const fn =
10+ ( order : number [ ] , active = false , originalIndex = 0 , curIndex = 0 , y = 0 ) =>
11+ ( index : number ) =>
12+ active && index === originalIndex
13+ ? {
14+ y : curIndex * 50 + y ,
15+ scale : 1.1 ,
16+ zIndex : 1 ,
17+ shadow : 15 ,
18+ immediate : ( key : string ) => key === 'y' || key === 'zIndex' ,
19+ }
20+ : {
21+ y : order . indexOf ( index ) * 50 ,
22+ scale : 1 ,
23+ zIndex : 0 ,
24+ shadow : 1 ,
25+ immediate : false ,
26+ }
2527
2628function DraggableList ( { items } : { items : string [ ] } ) {
27- const order = useRef ( items . map ( ( _ , index ) => index ) ) // Store indicies as a local ref, this represents the item order
29+ const order = React . useRef ( items . map ( ( _ , index ) => index ) ) // Store indicies as a local ref, this represents the item order
2830 const [ springs , api ] = useSprings ( items . length , fn ( order . current ) ) // Create springs, each corresponds to an item, controlling its transform, scale, etc.
2931 const bind = useDrag ( ( { args : [ originalIndex ] , active, movement : [ , y ] } ) => {
3032 const curIndex = order . current . indexOf ( originalIndex )
@@ -44,9 +46,9 @@ function DraggableList({ items }: { items: string[] }) {
4446 boxShadow : shadow . to ( s => `rgba(0, 0, 0, 0.15) 0px ${ s } px ${ 2 * s } px 0px` ) ,
4547 y,
4648 scale,
47- } }
48- children = { items [ i ] }
49- / >
49+ } } >
50+ { items [ i ] }
51+ </ animated . div >
5052 ) ) }
5153 </ div >
5254 )
0 commit comments