Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed May 4, 2024
1 parent 7dd8ea7 commit 47e13b6
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
destroy,
registerDestructor,
} from './glimmer/destroyable';
import { api } from '@/utils/dom-api';
import { api, getDocument } from '@/utils/dom-api';
import {
isFn,
isPrimitive,
Expand Down Expand Up @@ -86,25 +86,25 @@ let delegatedEvents: Record<string, WeakMap<HTMLElement, (e: Event) => void>> =

function handleDelegatedEvent(e: Event) {
let target = e.target as HTMLElement;
let maxDepth = 3;
while (target && target !== document.body && maxDepth > 0) {
maxDepth--;
const body = getDocument().body;
while (target && target !== body) {
if (delegatedEvents.click.has(target)) {
break;
}
target = target.parentElement!;
}
if (!target.isConnected) {
return;
}
const fn = delegatedEvents.click.get(target);
if (fn) {
fn(e);
}
}

if (!IN_SSR_ENV) {
Object.keys(delegatedEvents).forEach((name) => {
document.addEventListener(name, handleDelegatedEvent);
});
}
Object.keys(delegatedEvents).forEach((name) => {
api.addEventListener(getDocument(), name, handleDelegatedEvent);
});

export function $_delegateEvent(element: HTMLElement, name: string, fn: (e: Event) => void) {
delegatedEvents[name].set(element, fn);
Expand Down

0 comments on commit 47e13b6

Please sign in to comment.