diff --git a/components/bl-dashlet-component/src/helpers.js b/components/bl-dashlet-component/src/helpers.js index 17a682ac9..bb844ccfb 100644 --- a/components/bl-dashlet-component/src/helpers.js +++ b/components/bl-dashlet-component/src/helpers.js @@ -1,27 +1,23 @@ -import { useRef, useState, useEffect, useCallback } from 'react'; +import { useCallback, useEffect, useRef, useState } from 'react'; export const StyleVariants = { 'default' : '', - 'alternative': 'alternative' + 'alternative': 'alternative', }; export const ContextBlockItemTypes = { LINK : 'link', - ACTION: 'action' + ACTION: 'action', }; export const useDraggable = ({ onDrag, rootRef, initialPosition, dragging }) => { - if (!dragging) { - return [null]; - } - const [pressed, setPressed] = useState(false); const position = useRef({ x: initialPosition.x, y: initialPosition.y }); const ref = useRef(); const unsubscribe = useRef(); - const legacyRef = useCallback((elem) => { + const legacyRef = useCallback(elem => { ref.current = elem; if (unsubscribe.current) { @@ -44,46 +40,48 @@ export const useDraggable = ({ onDrag, rootRef, initialPosition, dragging }) => }, []); useEffect(() => { - if (!pressed) { - return; - } - - const handleMouseMove = throttle((event) => { - if (!rootRef.current || !position.current) { + if (dragging) { + if (!pressed) { return; } - const pos = position.current; - const elem = rootRef.current; + const handleMouseMove = throttle(event => { + if (!rootRef.current || !position.current) { + return; + } - position.current = onDrag({ - x: pos.x + event.movementX, - y: pos.y + event.movementY - }); + const pos = position.current; + const elem = rootRef.current; - elem.style.transform = `translate(${ pos.x }px, ${ pos.y }px)`; - }); + position.current = onDrag({ + x: pos.x + event.movementX, + y: pos.y + event.movementY, + }); - const handleMouseUp = (e) => { - e.target.style.userSelect = 'auto'; - setPressed(false); - }; + elem.style.transform = `translate(${ pos.x }px, ${ pos.y }px)`; + }); - document.addEventListener('mousemove', handleMouseMove); - document.addEventListener('mouseup', handleMouseUp); + const handleMouseUp = e => { + e.target.style.userSelect = 'auto'; + setPressed(false); + }; - return () => { - handleMouseMove.cancel(); + document.addEventListener('mousemove', handleMouseMove); + document.addEventListener('mouseup', handleMouseUp); - document.removeEventListener('mousemove', handleMouseMove); - document.removeEventListener('mouseup', handleMouseUp); - }; - }, [pressed, onDrag]); + return () => { + handleMouseMove.cancel(); + + document.removeEventListener('mousemove', handleMouseMove); + document.removeEventListener('mouseup', handleMouseUp); + }; + } + }, [pressed, onDrag, dragging]); return [legacyRef, pressed]; }; -const throttle = (f) => { +const throttle = f => { let token = null, lastArgs = null; const invoke = () => { @@ -117,6 +115,6 @@ export const getPosition = (ref, coords) => { y: Math.max( (0 - rootOffsetTop), Math.min(parentClientHeight - refClientHeight, coords.y) - ) + ), }; };