Skip to content

Commit 6f205f0

Browse files
authored
Fix crash when reading headlessuiFocusGuard of relatedTarget in the FocusTrap component (#2203)
* 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. * update changelog
1 parent 1d94d15 commit 6f205f0

File tree

4 files changed

+6
-4
lines changed

4 files changed

+6
-4
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Fix `failed to removeChild on Node` bug ([#2164](https://github.com/tailwindlabs/headlessui/pull/2164))
1818
- Don’t overwrite classes during SSR when rendering fragments ([#2173](https://github.com/tailwindlabs/headlessui/pull/2173))
1919
- Improve `Combobox` accessibility ([#2153](https://github.com/tailwindlabs/headlessui/pull/2153))
20+
- Fix crash when reading `headlessuiFocusGuard` of `relatedTarget` in the `FocusTrap` component ([#2203](https://github.com/tailwindlabs/headlessui/pull/2203))
2021

2122
## [1.7.7] - 2022-12-16
2223

packages/@headlessui-react/src/components/focus-trap/focus-trap.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ export let FocusTrap = Object.assign(
109109
let allContainers = new Set(containers?.current)
110110
allContainers.add(container)
111111

112-
let relatedTarget = e.relatedTarget as HTMLElement | null
113-
if (!relatedTarget) return
112+
let relatedTarget = e.relatedTarget
113+
if (!(relatedTarget instanceof HTMLElement)) return
114114

115115
// Known guards, leave them alone!
116116
if (relatedTarget.dataset.headlessuiFocusGuard === 'true') {

packages/@headlessui-vue/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- Fix `Tab` key with non focusable elements in `Popover.Panel` ([#2147](https://github.com/tailwindlabs/headlessui/pull/2147))
1919
- Don’t overwrite classes during SSR when rendering fragments ([#2173](https://github.com/tailwindlabs/headlessui/pull/2173))
2020
- Improve `Combobox` accessibility ([#2153](https://github.com/tailwindlabs/headlessui/pull/2153))
21+
- Fix crash when reading `headlessuiFocusGuard` of `relatedTarget` in the `FocusTrap` component ([#2203](https://github.com/tailwindlabs/headlessui/pull/2203))
2122

2223
## [1.7.7] - 2022-12-16
2324

packages/@headlessui-vue/src/components/focus-trap/focus-trap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ export let FocusTrap = Object.assign(
111111
let allContainers = new Set(props.containers?.value)
112112
allContainers.add(container)
113113

114-
let relatedTarget = e.relatedTarget as HTMLElement | null
115-
if (!relatedTarget) return
114+
let relatedTarget = e.relatedTarget
115+
if (!(relatedTarget instanceof HTMLElement)) return
116116

117117
// Known guards, leave them alone!
118118
if (relatedTarget.dataset.headlessuiFocusGuard === 'true') {

0 commit comments

Comments
 (0)