# useDrag A hook that receives a reference to an HTML Element (using React's useRef) and enables it to be dragged.\ The hook returns a boolean value indicating whether the element is currently being dragged or not. ### Why? 💡 - takes care of attaching drag-related event listeners to the specified target - takes care of emoving the listener when the component is unmounted. - allow to easily implement draggable business logic ### Basic Usage: Provide a DOM ref as first parameter to `useDrag` ```jsx harmony import { useRef } from 'react'; import useDrag from 'beautiful-react-hooks/useDrag'; const MyComponent = () => { const ref = useRef(); const isDragged = useDrag(ref); return (
Draggable item... {isDragged && is being dragged}
); }; ``` ### Drag image: ```jsx harmony import { useRef } from 'react'; import useDrag from 'beautiful-react-hooks/useDrag'; const MyComponent = () => { const ref = useRef(); const isDragged = useDrag(ref, { dragImage: 'https://beautifulinteractions.com/img/logo-colorful.svg', dragImageXOffset: 5, dragImageYOffset: 5, }); return (
Draggable item... {isDragged && is being dragged}
); }; ``` ### Data transfer: ```jsx harmony import { useRef } from 'react'; import useDrag from 'beautiful-react-hooks/useDrag'; const MyComponent = () => { const ref = useRef(); const isDragged = useDrag(ref, { transfer: { id: 'item-id', foo: 'bar' }, transferFormat: 'text/plain', }); return (
Draggable item... {isDragged && is being dragged}
); }; ``` ### Mastering the hook #### ✅ When to use - If you require basic drag-related business logic ### Types ```typescript static import { type RefObject } from 'react'; export interface UseDragOptions { dragImage?: string; dragImageXOffset?: number; dragImageYOffset?: number; transfer?: string | number | Record; transferFormat?: string; } declare const useDrag: (targetRef: RefObject, options?: UseDragOptions) => boolean; export default useDrag; ```