UNPKG

@gravity-ui/uikit

Version:

Gravity UI base styling and components

35 lines (34 loc) 1.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useResizeHandlers = useResizeHandlers; const tslib_1 = require("tslib"); const React = tslib_1.__importStar(require("react")); function useResizeHandlers({ onStart, onMove, onEnd, arrangement = 'horizontal', }) { const startRef = React.useRef(0); const handleMove = React.useCallback((evt) => { const current = arrangement === 'horizontal' ? evt.clientX : evt.clientY; onMove(startRef.current - current); }, [arrangement, onMove]); const handleUp = React.useCallback((evt) => { window.removeEventListener('pointermove', handleMove); onEnd(startRef.current - (arrangement === 'horizontal' ? evt.clientX : evt.clientY), evt); }, [arrangement, handleMove, onEnd]); const onPointerDown = React.useCallback((e) => { e.preventDefault(); const axisPos = arrangement === 'horizontal' ? e.clientX : e.clientY; startRef.current = axisPos; onStart(); window.addEventListener('pointermove', handleMove); window.addEventListener('pointerup', handleUp, { once: true }); }, [onStart, arrangement, handleMove, handleUp]); React.useEffect(() => { return () => { window.removeEventListener('pointermove', handleMove); window.removeEventListener('pointerup', handleUp); }; }, [handleMove, handleUp]); return { onPointerDown, }; } //# sourceMappingURL=useResizeHandlers.js.map