1
1
import React , { useRef , useEffect } from 'react'
2
2
import clamp from 'lodash-es/clamp'
3
3
import { useSprings , animated } from 'react-spring'
4
- import { useGesture } from 'react-with -gesture'
4
+ import { useDrag } from 'react-use -gesture'
5
5
import './styles.css'
6
6
7
7
const pages = [
@@ -25,8 +25,8 @@ export default function Viewpager() {
25
25
set ( i => ( { x : i * width . current , sc : 1 , display : 'block' } ) )
26
26
} , [ ] )
27
27
28
- const bind = useGesture (
29
- ( { down, delta : [ xDelta ] , direction : [ xDir ] , distance, cancel } ) => {
28
+ const bind = useDrag (
29
+ ( { down, movement : [ xMovement ] , direction : [ xDir ] , distance, cancel } ) => {
30
30
if ( down && distance > width . current / 2 )
31
31
cancel (
32
32
( index . current = clamp (
@@ -38,7 +38,7 @@ export default function Viewpager() {
38
38
set ( i => {
39
39
if ( i < index . current - 1 && i > index . current + 1 )
40
40
return { display : 'none' }
41
- const x = ( i - index . current ) * width . current + ( down ? xDelta : 0 )
41
+ const x = ( i - index . current ) * width . current + ( down ? xMovement : 0 )
42
42
const sc = down ? 1 - distance / width . current / 2 : 1
43
43
return { x, sc, display : 'block' }
44
44
} )
@@ -49,13 +49,7 @@ export default function Viewpager() {
49
49
ref = { r => r && ( width . current = r . getBoundingClientRect ( ) . width ) }
50
50
className = "viewpager-main" >
51
51
{ props . map ( ( { x, display, sc } , i ) => (
52
- < animated . div
53
- { ...bind ( ) }
54
- key = { i }
55
- style = { {
56
- display,
57
- transform : x . to ( x => `translate3d(${ x } px,0,0)` ) ,
58
- } } >
52
+ < animated . div { ...bind ( ) } key = { i } style = { { display, x } } >
59
53
< animated . div
60
54
style = { {
61
55
transform : sc . to ( s => `scale(${ s } )` ) ,
0 commit comments