Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useDropZone: Ensure drag event targets HTMLElement #34272

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions packages/compose/src/hooks/use-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,24 @@ export default function useDropZone( {
/**
* Checks if an element is in the drop zone.
*
* @param {HTMLElement|null} elementToCheck
* @param {EventTarget|null} targetToCheck
*
* @return {boolean} True if in drop zone, false if not.
*/
function isElementInZone( elementToCheck ) {
function isElementInZone( targetToCheck ) {
const { defaultView } = ownerDocument;
if (
! elementToCheck ||
! element.contains( elementToCheck )
! targetToCheck ||
! defaultView ||
! ( targetToCheck instanceof defaultView.HTMLElement ) ||
! element.contains( targetToCheck )
) {
return false;
}

/** @type {HTMLElement|null} */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it still be null? It looks like the previous if statement would rule that possibility out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! The answer is yes, there needs to be margin for null for the subsequent do/while loop. Otherwise, TS complains at elementToCheck = elementToCheck.parentElement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, I missed that it's a let and not a const. 👍

let elementToCheck = targetToCheck;

do {
if ( elementToCheck.dataset.isDropZone ) {
return elementToCheck === element;
Expand Down Expand Up @@ -155,11 +161,7 @@ export default function useDropZone( {
// leaving the drop zone, which means the `relatedTarget`
// (element that has been entered) should be outside the drop
// zone.
if (
isElementInZone(
/** @type {HTMLElement|null} */ ( event.relatedTarget )
)
) {
if ( isElementInZone( event.relatedTarget ) ) {
return;
}

Expand Down