Skip to content

Commit 34129e0

Browse files
committed
Merge branch 'pull-21' into pull-750
2 parents 4c86f1e + eb69b7e commit 34129e0

File tree

11 files changed

+138
-36
lines changed

11 files changed

+138
-36
lines changed

components/examples-hooks.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ return <animated.div style={{ transform }} children="Slide">`,
7474
link: 'https://codesandbox.io/embed/r5qmj8m6lq',
7575
tags: ['useSprings'],
7676
},
77+
{
78+
name: 'hooks/inertia',
79+
title: 'Inertia',
80+
tags: ['useSprings'],
81+
},
82+
{
83+
name: 'hooks/clamp-bounce',
84+
title: 'Clamp bounce',
85+
tags: ['useSprings'],
86+
},
7787
{
7888
name: 'hooks/mouse-parallax',
7989
title: 'Mouse parallax',

demos/hooks/card-flick/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState } from 'react'
22
import { useSprings, animated, to } from 'react-spring'
3-
import { useGesture } from 'react-with-gesture'
3+
import { useDrag } from 'react-use-gesture'
44
import './styles.css'
55

66
const cards = [
@@ -36,12 +36,11 @@ export default function Deck() {
3636
}))
3737

3838
// Create a gesture, we're interested in down-state, delta (current-pos - click-pos), direction and velocity
39-
const bind = useGesture(
39+
const bind = useDrag(
4040
({
4141
args: [index],
4242
down,
43-
delta: [xDelta],
44-
distance,
43+
movement: [xMove],
4544
direction: [xDir],
4645
velocity,
4746
}) => {
@@ -57,9 +56,9 @@ export default function Deck() {
5756
if (index !== i) return
5857
const isGone = gone.has(index)
5958
// When a card is gone it flys out left or right, otherwise it's either dragged to delta, or goes back to zero
60-
const x = isGone ? (200 + window.innerWidth) * dir : down ? xDelta : 0
59+
const x = isGone ? (200 + window.innerWidth) * dir : down ? xMove : 0
6160
// How much the card tilts, flicking it harder makes it rotate faster
62-
const rot = xDelta / 100 + (isGone ? dir * 10 * velocity : 0)
61+
const rot = xMove / 100 + (isGone ? dir * 10 * velocity : 0)
6362
// Active cards lift up a bit
6463
const scale = down ? 1.1 : 1
6564
return {

demos/hooks/clamp-bounce/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react'
2+
import { useSpring, animated } from 'react-spring'
3+
import './styles.css'
4+
5+
export default function ClampBounce() {
6+
const [toggle, setToggle] = React.useState(false)
7+
const { y } = useSpring({
8+
y: toggle ? 250 : -250,
9+
config: { tension: 120, friction: 12, clamp: 2 },
10+
})
11+
12+
return (
13+
<div className="clamp-bounce">
14+
<animated.div onClick={() => setToggle(t => !t)} style={{ y }} />
15+
</div>
16+
)
17+
}

demos/hooks/clamp-bounce/styles.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.clamp-bounce {
2+
display: flex;
3+
align-items: center;
4+
justify-content: center;
5+
background: #ecede7;
6+
height: 100%;
7+
}
8+
9+
.clamp-bounce > div {
10+
width: 80px;
11+
height: 80px;
12+
background: hotpink;
13+
border-radius: 16px;
14+
}

demos/hooks/draggable-list/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useRef } from 'react'
22
import clamp from 'lodash/clamp'
3-
import { useGesture } from 'react-with-gesture'
3+
import { useDrag } from 'react-use-gesture'
44
import { useSprings, animated, to } from 'react-spring'
55
import './styles.css'
66

@@ -35,7 +35,7 @@ export default function DraggableList({
3535
const [springs, setSprings] = useSprings(items.length, fn(order.current))
3636

3737
// Preps a gesture handler which returns drag-deltas, touched/clicked state, etc.
38-
const bind = useGesture(({ args: [originalIndex], down, delta: [, y] }) => {
38+
const bind = useDrag(({ args: [originalIndex], down, movement: [, y] }) => {
3939
// Bunch of math to calculate current row and new order, it's unavoidable ¯\_(ツ)_/¯
4040
const curIndex = order.current.indexOf(originalIndex)
4141
const curRow = clamp(
@@ -60,10 +60,8 @@ export default function DraggableList({
6060
boxShadow: shadow.to(
6161
s => `rgba(0, 0, 0, 0.2) 0px ${s}px ${2 * s}px 0px`
6262
),
63-
transform: to(
64-
[y, scale],
65-
(y, s) => `translate3d(0,${y}px,0) scale(${s})`
66-
),
63+
y,
64+
scale,
6765
}}
6866
children={items[i]}
6967
{...bind(i)}

demos/hooks/gestures-pager/index.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useRef, useEffect } from 'react'
22
import clamp from 'lodash-es/clamp'
33
import { useSprings, animated } from 'react-spring'
4-
import { useGesture } from 'react-with-gesture'
4+
import { useDrag } from 'react-use-gesture'
55
import './styles.css'
66

77
const pages = [
@@ -25,8 +25,8 @@ export default function Viewpager() {
2525
set(i => ({ x: i * width.current, sc: 1, display: 'block' }))
2626
}, [])
2727

28-
const bind = useGesture(
29-
({ down, delta: [xDelta], direction: [xDir], distance, cancel }) => {
28+
const bind = useDrag(
29+
({ down, movement: [xMovement], direction: [xDir], distance, cancel }) => {
3030
if (down && distance > width.current / 2)
3131
cancel(
3232
(index.current = clamp(
@@ -38,7 +38,7 @@ export default function Viewpager() {
3838
set(i => {
3939
if (i < index.current - 1 && i > index.current + 1)
4040
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)
4242
const sc = down ? 1 - distance / width.current / 2 : 1
4343
return { x, sc, display: 'block' }
4444
})
@@ -49,13 +49,7 @@ export default function Viewpager() {
4949
ref={r => r && (width.current = r.getBoundingClientRect().width)}
5050
className="viewpager-main">
5151
{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 }}>
5953
<animated.div
6054
style={{
6155
transform: sc.to(s => `scale(${s})`),

demos/hooks/gestures-pull/index.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
import React, { useState, useRef, useEffect } from 'react'
22
import clamp from 'lodash-es/clamp'
3-
import { useGesture } from 'react-with-gesture'
3+
import { useDrag } from 'react-use-gesture'
44
import { useSpring, animated } from 'react-spring'
55
import { add, scale } from 'vec-la'
66
import './styles.css'
77

88
function Pull() {
9-
const [{ xy }, set] = useSpring(() => ({ xy: [0, 0] }))
10-
const bind = useGesture(({ down, delta, velocity }) => {
9+
const [{ x, y }, set] = useSpring(() => ({ x: 0, y: 0 }))
10+
const bind = useDrag(({ down, movement: [mx, my], velocity }) => {
1111
velocity = Math.max(1, velocity)
12-
console.log(delta, velocity)
1312
set({
14-
xy: down ? delta : [0, 0],
13+
x: down ? mx : 0,
14+
y: down ? my : 0,
1515
config: { mass: velocity, tension: 500 * velocity, friction: 50 },
1616
})
1717
})
18-
return (
19-
<animated.div
20-
{...bind()}
21-
style={{
22-
transform: xy.to((x, y) => `translate3d(${x}px,${y}px,0)`),
23-
}}
24-
/>
25-
)
18+
return <animated.div {...bind()} style={{ x, y }} />
2619
}
2720

2821
export default function App() {

demos/hooks/inertia/index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react'
2+
import { useSpring, animated } from 'react-spring'
3+
import { useDrag } from 'react-use-gesture'
4+
import './styles.css'
5+
6+
const [min, max] = [-250, 250]
7+
8+
export default function Inertia() {
9+
const [{ y }, set] = useSpring(() => ({ y: 0 }))
10+
11+
const bind = useDrag(
12+
({ down, movement: [, dy], vxvy: [, vy], memo = y.getValue() }) => {
13+
if (down) set({ y: dy + memo, onFrame: () => {}, immediate: true })
14+
else inertia(dy + memo + 1, vy)
15+
return memo
16+
}
17+
)
18+
19+
const springBounce = React.useCallback(
20+
velocity => {
21+
set({
22+
y: velocity > 0 ? max : min,
23+
onFrame: () => {}, // <-- this is annoying :)
24+
config: { velocity: velocity * 3 },
25+
})
26+
},
27+
[set]
28+
)
29+
30+
const inertia = React.useCallback(
31+
(position, velocity) => {
32+
set({
33+
to: async (next, stop) => {
34+
await next({
35+
y: position,
36+
onFrame: async v => {
37+
const vel = y.lastVelocity
38+
39+
if ((v.y > max && vel > 0) || (v.y < min && vel < 0)) {
40+
stop()
41+
springBounce(vel)
42+
}
43+
},
44+
config: { decay: true, velocity },
45+
})
46+
},
47+
})
48+
},
49+
[y, set, springBounce]
50+
)
51+
// Now we're just mapping the animated values to our view, that's it. Btw, this component only renders once. :-)
52+
return (
53+
<div className="inertia">
54+
<animated.div {...bind()} style={{ y }} />
55+
</div>
56+
)
57+
}

demos/hooks/inertia/styles.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.inertia {
2+
display: flex;
3+
align-items: center;
4+
justify-content: center;
5+
background: #ecede7;
6+
height: 100%;
7+
}
8+
9+
.inertia > div {
10+
width: 80px;
11+
height: 80px;
12+
background: hotpink;
13+
border-radius: 16px;
14+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"react-feather": "^1.1.6",
7676
"react-loadable": "^5.5.0",
7777
"react-spring": "^8.0.5",
78+
"react-use-gesture": "^6.0.0-beta.3",
7879
"react-with-gesture": "^4.0.2",
7980
"resize-observer-polyfill": "^1.5.1",
8081
"styled-components": "^4.1.3",

0 commit comments

Comments
 (0)