Created
July 28, 2020 09:53
-
-
Save EmrysMyrddin/6cc577ca9245a7c48e7f0b895df2366a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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