@figliolia/drag-detector
Version:
Mouse and Touch driven drag detection for DOM elements
28 lines (27 loc) • 806 B
JavaScript
import { useEffect } from "react";
import { useController } from "@figliolia/react-hooks";
import { DragDetector } from "./DragDetector.js";
/**
* useDragDetector
*
* Sets up a `DragDetector` instance with the specified options
* and returns it. When the hook unmounts, the `DragDetector`
* instance is automatically destroyed
*
* To attach your drag detector instance to a DOM node:
* ```tsx
* const detector = useDragDetector(options);
*
* <div className='my-element' {...detector.bindings} />
* ```
*/
export const useDragDetector = (options) => {
const detector = useController(new DragDetector(options));
useEffect(() => {
detector.setOptions(options);
return () => {
detector.destroy();
};
}, [options, detector]);
return detector;
};