Skip to content

Commit f3bdde8

Browse files
committed
+
1 parent 324b8fe commit f3bdde8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/utils/dom.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
destroy,
2828
registerDestructor,
2929
} from './glimmer/destroyable';
30-
import { api } from '@/utils/dom-api';
30+
import { api, getDocument } from '@/utils/dom-api';
3131
import {
3232
isFn,
3333
isPrimitive,
@@ -86,25 +86,25 @@ let delegatedEvents: Record<string, WeakMap<HTMLElement, (e: Event) => void>> =
8686

8787
function handleDelegatedEvent(e: Event) {
8888
let target = e.target as HTMLElement;
89-
let maxDepth = 3;
90-
while (target && target !== document.body && maxDepth > 0) {
91-
maxDepth--;
89+
const body = getDocument().body;
90+
while (target && target !== body) {
9291
if (delegatedEvents.click.has(target)) {
9392
break;
9493
}
9594
target = target.parentElement!;
9695
}
96+
if (!target.isConnected) {
97+
return;
98+
}
9799
const fn = delegatedEvents.click.get(target);
98100
if (fn) {
99101
fn(e);
100102
}
101103
}
102104

103-
if (!IN_SSR_ENV) {
104-
Object.keys(delegatedEvents).forEach((name) => {
105-
document.addEventListener(name, handleDelegatedEvent);
106-
});
107-
}
105+
Object.keys(delegatedEvents).forEach((name) => {
106+
api.addEventListener(getDocument(), name, handleDelegatedEvent);
107+
});
108108

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

0 commit comments

Comments
 (0)