@figliolia/drag-detector
Version:
Mouse and Touch driven drag detection for DOM elements
32 lines (31 loc) • 1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDragDetector = void 0;
const react_1 = require("react");
const react_hooks_1 = require("@figliolia/react-hooks");
const DragDetector_1 = require("./DragDetector");
/**
* 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} />
* ```
*/
const useDragDetector = (options) => {
const detector = (0, react_hooks_1.useController)(new DragDetector_1.DragDetector(options));
(0, react_1.useEffect)(() => {
detector.setOptions(options);
return () => {
detector.destroy();
};
}, [options, detector]);
return detector;
};
exports.useDragDetector = useDragDetector;