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.
Files changed (2) hide show
  1. package/README.md +33 -6
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # React use drag [![npm](https://img.shields.io/npm/v/react-use-drag.svg)](https://www.npmjs.com/package/react-use-drag) ![npm type definitions](https://img.shields.io/npm/types/react-use-drag.svg)
2
2
 
3
- Drag interactions made easier. Try interactive [CodeSandbox demo](https://codesandbox.io/s/react-use-drag?file=/src/App.js).
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
  ![UI example](https://raw.githubusercontent.com/FilipChalupa/react-use-drag/HEAD/screencast.gif)
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 drag = useDrag()
20
-
21
- // @TODO
22
-
23
- return <button>Drag me</button>
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
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-use-drag",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Drag interactions made easier.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",