UNPKG

@wordpress/block-library

Version:
1 lines • 2.6 kB
{"version":3,"file":"use-is-dragging-within.cjs","names":["useState"],"sources":["../../../src/navigation-link/shared/use-is-dragging-within.js"],"sourcesContent":["import { useState, useEffect } from '@wordpress/element';\n\n/**\n * A React hook to determine if it's dragging within the target element.\n *\n * @typedef {import('@wordpress/element').RefObject} RefObject\n *\n * @param {RefObject<HTMLElement>} elementRef The target elementRef object.\n *\n * @return {boolean} Is dragging within the target element.\n */\nexport const useIsDraggingWithin = ( elementRef ) => {\n\tconst [ isDraggingWithin, setIsDraggingWithin ] = useState( false );\n\n\tuseEffect( () => {\n\t\tconst { ownerDocument } = elementRef.current;\n\n\t\tfunction handleDragStart( event ) {\n\t\t\t// Check the first time when the dragging starts.\n\t\t\thandleDragEnter( event );\n\t\t}\n\n\t\t// Set to false whenever the user cancel the drag event by either releasing the mouse or press Escape.\n\t\tfunction handleDragEnd() {\n\t\t\tsetIsDraggingWithin( false );\n\t\t}\n\n\t\tfunction handleDragEnter( event ) {\n\t\t\t// Check if the current target is inside the item element.\n\t\t\tif ( elementRef.current.contains( event.target ) ) {\n\t\t\t\tsetIsDraggingWithin( true );\n\t\t\t} else {\n\t\t\t\tsetIsDraggingWithin( false );\n\t\t\t}\n\t\t}\n\n\t\t// Bind these events to the document to catch all drag events.\n\t\t// Ideally, we can also use `event.relatedTarget`, but sadly that\n\t\t// doesn't work in Safari.\n\t\townerDocument.addEventListener( 'dragstart', handleDragStart );\n\t\townerDocument.addEventListener( 'dragend', handleDragEnd );\n\t\townerDocument.addEventListener( 'dragenter', handleDragEnter );\n\n\t\treturn () => {\n\t\t\townerDocument.removeEventListener( 'dragstart', handleDragStart );\n\t\t\townerDocument.removeEventListener( 'dragend', handleDragEnd );\n\t\t\townerDocument.removeEventListener( 'dragenter', handleDragEnter );\n\t\t};\n\t}, [ elementRef ] );\n\n\treturn isDraggingWithin;\n};\n"],"mappings":";;;;;;;;;;;;;;;;AAWA,MAAa,uBAAwB,eAAgB;CACpD,MAAM,CAAE,kBAAkB,wBAAA,GAAwBA,mBAAAA,SAAAA,CAAU,KAAM;CAElE,CAAA,GAAA,mBAAA,UAAA,OAAiB;EAChB,MAAM,EAAE,kBAAkB,WAAW;EAErC,SAAS,gBAAiB,OAAQ;GAEjC,gBAAiB,KAAM;EACxB;EAGA,SAAS,gBAAgB;GACxB,oBAAqB,KAAM;EAC5B;EAEA,SAAS,gBAAiB,OAAQ;GAEjC,IAAK,WAAW,QAAQ,SAAU,MAAM,MAAO,GAC9C,oBAAqB,IAAK;QAE1B,oBAAqB,KAAM;EAE7B;EAKA,cAAc,iBAAkB,aAAa,eAAgB;EAC7D,cAAc,iBAAkB,WAAW,aAAc;EACzD,cAAc,iBAAkB,aAAa,eAAgB;EAE7D,aAAa;GACZ,cAAc,oBAAqB,aAAa,eAAgB;GAChE,cAAc,oBAAqB,WAAW,aAAc;GAC5D,cAAc,oBAAqB,aAAa,eAAgB;EACjE;CACD,GAAG,CAAE,UAAW,CAAE;CAElB,OAAO;AACR"}