Skip to content

Set event.composed and event.composedPath() for shadow DOM #98

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions ts/drag-drop-touch-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function newForwardableEvent(
const evt = new Event(type, {
bubbles: true,
cancelable: true,
composed: target.shadowRoot?.mode === "open",
}) as unknown as Mutable<MouseEvent, "button" | "which" | "buttons"> & {
readonly defaultPrevented: boolean;
},
Expand All @@ -68,6 +69,19 @@ export function newForwardableEvent(
copyProps(evt, srcEvent, _kbdProps);
copyProps(evt, touch, _ptProps);
setOffsetAndLayerProps(evt, target);
// set composedPath for shadow DOM
if (evt.composed) {
evt.composedPath = () => {
const pt = pointFrom(srcEvent);
const el = target.shadowRoot?.elementFromPoint(pt.x, pt.y);
const getPath = (e?: Element | null, path: Element[] = []) => {
if (!e) return path;
path.unshift(e);
return getPath(e.parentElement, path);
};
return getPath(el);
};
}
return evt;
}

Expand Down