0.3.2 • Published 11 months ago

react-use-drag v0.3.2

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

React use drag npm npm type definitions

Drag interactions made easier. Try interactive CodeSandbox demo.

UI example

Installation

npm install react-use-drag

How to use

import { useDrag } from 'react-use-drag'

const App = () => {
	const [position, setPosition] = useState({ x: 0, y: 0 })
	const [positionOffset, setPositionOffset] = useState({ x: 0, y: 0 })
	const onRelativePositionChange = useCallback((x, y) => {
		setPositionOffset({ x, y })
	}, [])
	const onStart = useCallback(() => {
		console.log('Dragging has started')
	}, [])
	const onEnd = useCallback((x, y) => {
		setPosition((position) => ({
			x: position.x + x,
			y: position.y + y,
		}))
		setPositionOffset({ x: 0, y: 0 })
	}, [])
	const { elementProps, isMoving } = useDrag({
		onRelativePositionChange,
		onStart,
		onEnd,
	})

	return (
		<button
			className="draggable"
			style={{
				translate: `translate(${position.x}px ${position.y}px)`,
			}}
			{...elementProps}
		>
			{isMoving ? '🚶' : '🧍'}
		</button>
	)
}
0.3.0

11 months ago

0.3.2

11 months ago

0.3.1

11 months ago

0.2.0

1 year ago

1.2.0

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

0.1.0

2 years ago

0.0.1

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.3

2 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago