react-use-drag 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# React use drag [](https://www.npmjs.com/package/react-use-drag) 
|
|
2
2
|
|
|
3
|
-
Drag interactions made easier. Try interactive [CodeSandbox demo](https://codesandbox.io/
|
|
3
|
+
Drag interactions made easier. Try interactive [CodeSandbox demo](https://codesandbox.io/p/sandbox/react-use-drag-trjpqp?file=%2Fsrc%2FApp.js).
|
|
4
4
|
|
|
5
5
|

|
|
6
6
|
|
|
@@ -16,10 +16,37 @@ npm install react-use-drag
|
|
|
16
16
|
import { useDrag } from 'react-use-drag'
|
|
17
17
|
|
|
18
18
|
const App = () => {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
const [position, setPosition] = useState({ x: 0, y: 0 })
|
|
20
|
+
const [positionOffset, setPositionOffset] = useState({ x: 0, y: 0 })
|
|
21
|
+
const onRelativePositionChange = useCallback((x, y) => {
|
|
22
|
+
setPositionOffset({ x, y })
|
|
23
|
+
}, [])
|
|
24
|
+
const onStart = useCallback(() => {
|
|
25
|
+
console.log('Dragging has started')
|
|
26
|
+
}, [])
|
|
27
|
+
const onEnd = useCallback((x, y) => {
|
|
28
|
+
setPosition((position) => ({
|
|
29
|
+
x: position.x + x,
|
|
30
|
+
y: position.y + y,
|
|
31
|
+
}))
|
|
32
|
+
setPositionOffset({ x: 0, y: 0 })
|
|
33
|
+
}, [])
|
|
34
|
+
const { elementProps, isMoving } = useDrag({
|
|
35
|
+
onRelativePositionChange,
|
|
36
|
+
onStart,
|
|
37
|
+
onEnd,
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<button
|
|
42
|
+
className="draggable"
|
|
43
|
+
style={{
|
|
44
|
+
translate: `translate(${position.x}px ${position.y}px)`,
|
|
45
|
+
}}
|
|
46
|
+
{...elementProps}
|
|
47
|
+
>
|
|
48
|
+
{isMoving ? '🚶' : '🧍'}
|
|
49
|
+
</button>
|
|
50
|
+
)
|
|
24
51
|
}
|
|
25
52
|
```
|