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

Solution for dragEnter and dragLeave being fired on child elements #9

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 4 additions & 2 deletions src/lib/actions/filedrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function getInputElement(node: HTMLElement, { input }: FileDropOptions): HTMLInp
return inputs.item(0) as HTMLInputElement;
}

let elementDragCounter = 0;
export const filedrop = function (node: HTMLElement, opts?: FileDropOptions): Action {
function dispatch<K extends keyof Events, T extends Events[K] = Events[K]>(typ: K, detail: T): void {
node.dispatchEvent(new CustomEvent<T>(typ, { detail }));
Expand Down Expand Up @@ -226,7 +227,7 @@ export const filedrop = function (node: HTMLElement, opts?: FileDropOptions): Ac

async function handleDragEnter(ev: DragEvent) {
isDraggingFiles = isEventWithFiles(ev);
if (!isDraggingFiles) {
if (!isDraggingFiles && 0 < elementDragCounter++) {
return;
}
isDraggingFiles = true;
Expand All @@ -243,7 +244,7 @@ export const filedrop = function (node: HTMLElement, opts?: FileDropOptions): Ac

async function handleDragLeave(ev: DragEvent) {
isDraggingFiles = isEventWithFiles(ev);
if (!isDraggingFiles) {
if (!isDraggingFiles && 0 < --elementDragCounter) {
return;
}
const files = await extractFilesFromEvent(ev);
Expand Down Expand Up @@ -278,6 +279,7 @@ export const filedrop = function (node: HTMLElement, opts?: FileDropOptions): Ac
if (!isDraggingFiles) {
return;
}
elementDragCounter = 0;
if (isNode(ev.target) && (node.isEqualNode(ev.target) || node.contains(ev.target))) {
// let it bubble
return;
Expand Down