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
This commit is contained in:
Robin Malfait
2023-01-24 20:08:38 +01:00
committed by GitHub
parent 1d94d15c79
commit 6f205f07d2
4 changed files with 6 additions and 4 deletions
+1
View File
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix `failed to removeChild on Node` bug ([#2164](https://github.com/tailwindlabs/headlessui/pull/2164))
- Dont overwrite classes during SSR when rendering fragments ([#2173](https://github.com/tailwindlabs/headlessui/pull/2173))
- Improve `Combobox` accessibility ([#2153](https://github.com/tailwindlabs/headlessui/pull/2153))
- Fix crash when reading `headlessuiFocusGuard` of `relatedTarget` in the `FocusTrap` component ([#2203](https://github.com/tailwindlabs/headlessui/pull/2203))
## [1.7.7] - 2022-12-16
@@ -109,8 +109,8 @@ export let FocusTrap = Object.assign(
let allContainers = new Set(containers?.current)
allContainers.add(container)
let relatedTarget = e.relatedTarget as HTMLElement | null
if (!relatedTarget) return
let relatedTarget = e.relatedTarget
if (!(relatedTarget instanceof HTMLElement)) return
// Known guards, leave them alone!
if (relatedTarget.dataset.headlessuiFocusGuard === 'true') {
+1
View File
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix `Tab` key with non focusable elements in `Popover.Panel` ([#2147](https://github.com/tailwindlabs/headlessui/pull/2147))
- Dont overwrite classes during SSR when rendering fragments ([#2173](https://github.com/tailwindlabs/headlessui/pull/2173))
- Improve `Combobox` accessibility ([#2153](https://github.com/tailwindlabs/headlessui/pull/2153))
- Fix crash when reading `headlessuiFocusGuard` of `relatedTarget` in the `FocusTrap` component ([#2203](https://github.com/tailwindlabs/headlessui/pull/2203))
## [1.7.7] - 2022-12-16
@@ -111,8 +111,8 @@ export let FocusTrap = Object.assign(
let allContainers = new Set(props.containers?.value)
allContainers.add(container)
let relatedTarget = e.relatedTarget as HTMLElement | null
if (!relatedTarget) return
let relatedTarget = e.relatedTarget
if (!(relatedTarget instanceof HTMLElement)) return
// Known guards, leave them alone!
if (relatedTarget.dataset.headlessuiFocusGuard === 'true') {