Created
August 31, 2020 08:27
-
-
Save ilomon10/00182b3ce8f90ca3e94f2fa28ba8148b to your computer and use it in GitHub Desktop.
Drag React Component with Handle
This file contains 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
import React, { useState, useCallback } from 'react'; | |
const Draggable = ({ | |
children | |
}) => { | |
const [isDrag, setIsDrag] = useState(false); | |
const onDragEndHandler = useCallback((e) => { | |
setIsDrag(false); | |
}, []); | |
const onMouseDownHandler = useCallback((e) => { | |
setIsDrag(true); | |
}, []); | |
const onMouseUpHandler = useCallback((e) => { | |
setIsDrag(false); | |
}, []); | |
return children({ | |
isDrag, | |
onDragEndHandler, | |
onMouseDownHandler, | |
onMouseUpHandler | |
}); | |
} | |
export default Draggable; |
This file contains 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
import React from 'react'; | |
import Draggable from 'draggable.js'; | |
const Component = () => { | |
return ( | |
<Draggable> | |
{({isDrag, onDragEndHandler, onMouseDownHandler, onMouseUpHandler})=>( | |
<div className="its a parent" | |
draggable={isDrag} | |
onDragEnd={onDragEndHandler}> | |
<div className="its a handle" | |
onMouseDown={onMouseDownHandler} | |
onMouseUp={onMouseUpHandler}>Drag Me!</div> | |
<div className="its a content">Lorem ipsum dolor sit amet</div> | |
</div> | |
)} | |
</Draggable> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment