diff --git a/src/app/utils/dom.ts b/src/app/utils/dom.ts index 3ced8f57f..81f4a8087 100644 --- a/src/app/utils/dom.ts +++ b/src/app/utils/dom.ts @@ -1,6 +1,16 @@ export const targetFromEvent = (evt: Event, selector: string): Element | undefined => { const targets = evt.composedPath() as Element[]; - return targets.find((target) => target.matches?.(selector)); + if (targets.length > 0) { + return targets.find((target) => target.matches?.(selector)); + } + // composedPath() is empty when the event is no longer dispatching (e.g. inside a + // portal-within-portal in React 19). Walk up the DOM from evt.target instead. + let el = evt.target instanceof Element ? evt.target : null; + while (el) { + if (el.matches(selector)) return el; + el = el.parentElement; + } + return undefined; }; export const editableActiveElement = (): boolean =>