From bb6fc60d9b126fec36520a7124d9164a8515abd9 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 24 Jan 2023 19:59:24 +0100 Subject: [PATCH] ensure `relatedTarget` is an `HTMLElement` Or in other words, Robin trust the type system... I was assuming that this was always an `HTMLElement` or `null` but that's not the case. Just using `e.relatedTarget` shows that `dataset` is not always available. --- .../@headlessui-react/src/components/focus-trap/focus-trap.tsx | 3 ++- .../@headlessui-vue/src/components/focus-trap/focus-trap.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/@headlessui-react/src/components/focus-trap/focus-trap.tsx b/packages/@headlessui-react/src/components/focus-trap/focus-trap.tsx index a0a60339f8..76fafe59c4 100644 --- a/packages/@headlessui-react/src/components/focus-trap/focus-trap.tsx +++ b/packages/@headlessui-react/src/components/focus-trap/focus-trap.tsx @@ -109,8 +109,9 @@ export let FocusTrap = Object.assign( let allContainers = new Set(containers?.current) allContainers.add(container) - let relatedTarget = e.relatedTarget as HTMLElement | null + let relatedTarget = e.relatedTarget if (!relatedTarget) return + if (!(relatedTarget instanceof HTMLElement)) return // Known guards, leave them alone! if (relatedTarget.dataset.headlessuiFocusGuard === 'true') { diff --git a/packages/@headlessui-vue/src/components/focus-trap/focus-trap.ts b/packages/@headlessui-vue/src/components/focus-trap/focus-trap.ts index faa6501426..23ac7fa4ef 100644 --- a/packages/@headlessui-vue/src/components/focus-trap/focus-trap.ts +++ b/packages/@headlessui-vue/src/components/focus-trap/focus-trap.ts @@ -111,8 +111,9 @@ export let FocusTrap = Object.assign( let allContainers = new Set(props.containers?.value) allContainers.add(container) - let relatedTarget = e.relatedTarget as HTMLElement | null + let relatedTarget = e.relatedTarget if (!relatedTarget) return + if (!(relatedTarget instanceof HTMLElement)) return // Known guards, leave them alone! if (relatedTarget.dataset.headlessuiFocusGuard === 'true') {