Open History to see the last commit
1interface PickScope<E> {
2 elementsFromPoint(x: number, y: number): ReadonlyArray<E>;
3 contains(node: NoInfer<E>): boolean;
4}
5
6// Document hit-testing retargets shadow content to its host, so descend into open roots.
7// ShadowRoot.elementsFromPoint still lists the host and the rest of the page (the overlay
8// too), so only the root's own elements count. Closed roots expose no shadowRoot.
9export function pickElementAtPoint<E extends { readonly shadowRoot: PickScope<E> | null }>(
10 scope: PickScope<E>,
11 x: number,
12 y: number,
13 isExcluded: (element: E) => boolean,
14): E | null {
15 for (const candidate of scope.elementsFromPoint(x, y)) {
16 if (!scope.contains(candidate) || isExcluded(candidate)) continue;
17 const inner =
18 candidate.shadowRoot && pickElementAtPoint(candidate.shadowRoot, x, y, isExcluded);
19 return inner ?? candidate;
20 }
21 return null;
22}