Skip to content

Instantly share code, notes, and snippets.

@EmrysMyrddin
Created July 28, 2020 09:53
Show Gist options
  • Save EmrysMyrddin/6cc577ca9245a7c48e7f0b895df2366a to your computer and use it in GitHub Desktop.
Save EmrysMyrddin/6cc577ca9245a7c48e7f0b895df2366a to your computer and use it in GitHub Desktop.
const DnD = () => {
const [draging, setDraging] = useState(false)
const handleDragStart = useCallback(() => {
setDraging(true)
// [Record starting point]
}, [setDraging])
const handleDragMove = useCallback(() => {
// [Calc the current position]
}, [])
const handleDragComplete = useCallback(() => {
setDraging(false)
// [Resize the element]
}, [setDraging])
useEffect(() => {
if(draging) {
document.addEventListener('mousemove', handleDragMove)
document.addEventListener('mouseup', handleDragComplete)
}
return () => {
document.removeEventListener('mousemove', handleDragMove)
document.removeEventListener('mouseup', handleDragComplete)
}
}, [draging, handleDragMove, handleDragComplete])
return <div onMouseDown={handleDragStart} />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment