Skip to content

Commit

Permalink
Move logic into consuming hook
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Aug 23, 2024
1 parent 8ee6d06 commit 1cc8c84
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,23 @@ export default function useBlockDropZone( {
200
);

/**
* Checks if the given element is an insertion point.
*
* @param {EventTarget|null} targetToCheck - The element to check.
* @param {Document} ownerDocument - The owner document of the element.
* @return {boolean} True if the element is a insertion point, false otherwise.
*/
function isInsertionPoint( targetToCheck, ownerDocument ) {
const { defaultView } = ownerDocument;

return !! (
defaultView &&
targetToCheck instanceof defaultView.HTMLElement &&
targetToCheck.dataset.isInsertionPoint
);
}

return useDropZone( {
dropZoneElement,
isDisabled,
Expand All @@ -525,7 +542,18 @@ export default function useBlockDropZone( {
// https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget
throttled( event, event.currentTarget.ownerDocument );
},
onDragLeave() {
onDragLeave( event ) {
const { ownerDocument } = event.currentTarget;

// If the drag event is leaving the drop zone and entering an insertion point,
// do not hide the insertion point as it is conceptually within the dropzone.
if (
isInsertionPoint( event.relatedTarget, ownerDocument ) ||
isInsertionPoint( event.target, ownerDocument )
) {
return;
}

throttled.cancel();
hideInsertionPoint();
},
Expand Down
27 changes: 0 additions & 27 deletions packages/compose/src/hooks/use-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,6 @@ export default function useDropZone( {
return false;
}

/**
* Checks if the given element is an insertion point.
*
* @param {EventTarget|null} targetToCheck - The element to check.
* @return {boolean} True if the element is a insertion point, false otherwise.
*/
function isInsertionPoint( targetToCheck ) {
const { defaultView } = ownerDocument;

return !! (
defaultView &&
targetToCheck instanceof defaultView.HTMLElement &&
targetToCheck.dataset.isInsertionPoint
);
}

function maybeDragStart( /** @type {DragEvent} */ event ) {
if ( isDragging ) {
return;
Expand Down Expand Up @@ -187,17 +171,6 @@ export default function useDropZone( {
return;
}

// If we're moving in/out of an insertion point then don't trigger
// the onDragLeave event. This is to prevent the dropzone from
// trigger any unwanted side effects when the user is likely to
// be moving the cursor quickly over the insertion point.
if (
isInsertionPoint( event.relatedTarget ) ||
isInsertionPoint( event.target )
) {
return;
}

if ( onDragLeaveRef.current ) {
onDragLeaveRef.current( event );
}
Expand Down

0 comments on commit 1cc8c84

Please sign in to comment.