Skip to content

Commit d0888b0

Browse files
KairuiLiulkrthecrypticaceRobinMalfait
authored
Add FocusTrap event listeners once document has loaded (#2389)
* feat: addEventListener on document loaded * Refactor * Fix import * Update changelog * use function instead of arrow function * make callback in `onDocumentReady` mandatory --------- Co-authored-by: lkr <[email protected]> Co-authored-by: Jordan Pittman <[email protected]> Co-authored-by: Robin Malfait <[email protected]>
1 parent d55c77e commit d0888b0

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Fix focus styles showing up when using the mouse ([#2347](https://github.com/tailwindlabs/headlessui/pull/2347))
1313
- Fix "Can't perform a React state update on an unmounted component." when using the `Transition` component ([#2374](https://github.com/tailwindlabs/headlessui/pull/2374))
14+
- Add `FocusTrap` event listeners once document has loaded ([#2389](https://github.com/tailwindlabs/headlessui/pull/2389))
1415

1516
### Added
1617

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { useEventListener } from '../../hooks/use-event-listener'
2424
import { microTask } from '../../utils/micro-task'
2525
import { useWatch } from '../../hooks/use-watch'
2626
import { useDisposables } from '../../hooks/use-disposables'
27+
import { onDocumentReady } from '../../utils/document-ready'
2728

2829
type Containers =
2930
// Lazy resolved containers
@@ -211,7 +212,7 @@ export let FocusTrap = Object.assign(FocusTrapRoot, {
211212
// ---
212213

213214
let history: HTMLElement[] = []
214-
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
215+
onDocumentReady(() => {
215216
function handle(e: Event) {
216217
if (!(e.target instanceof HTMLElement)) return
217218
if (e.target === document.body) return
@@ -231,7 +232,7 @@ if (typeof window !== 'undefined' && typeof document !== 'undefined') {
231232
document.body.addEventListener('click', handle, { capture: true })
232233
document.body.addEventListener('mousedown', handle, { capture: true })
233234
document.body.addEventListener('focus', handle, { capture: true })
234-
}
235+
})
235236

236237
function useRestoreElement(enabled: boolean = true) {
237238
let localHistory = useRef<HTMLElement[]>(history.slice())
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function onDocumentReady(cb: () => void) {
2+
function check() {
3+
if (document.readyState === 'loading') return
4+
cb()
5+
document.removeEventListener('DOMContentLoaded', check)
6+
}
7+
8+
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
9+
document.addEventListener('DOMContentLoaded', check)
10+
check()
11+
}
12+
}

packages/@headlessui-vue/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Fix focus styles showing up when using the mouse ([#2347](https://github.com/tailwindlabs/headlessui/pull/2347))
1313
- Disable `ComboboxInput` when its `Combobox` is disabled ([#2375](https://github.com/tailwindlabs/headlessui/pull/2375))
14+
- Add `FocusTrap` event listeners once document has loaded ([#2389](https://github.com/tailwindlabs/headlessui/pull/2389))
1415

1516
### Added
1617

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { useTabDirection, Direction as TabDirection } from '../../hooks/use-tab-
2222
import { getOwnerDocument } from '../../utils/owner'
2323
import { useEventListener } from '../../hooks/use-event-listener'
2424
import { microTask } from '../../utils/micro-task'
25+
import { onDocumentReady } from '../../utils/document-ready'
2526

2627
type Containers =
2728
// Lazy resolved containers
@@ -209,7 +210,7 @@ export let FocusTrap = Object.assign(
209210
)
210211

211212
let history: HTMLElement[] = []
212-
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
213+
onDocumentReady(() => {
213214
function handle(e: Event) {
214215
if (!(e.target instanceof HTMLElement)) return
215216
if (e.target === document.body) return
@@ -229,7 +230,7 @@ if (typeof window !== 'undefined' && typeof document !== 'undefined') {
229230
document.body.addEventListener('click', handle, { capture: true })
230231
document.body.addEventListener('mousedown', handle, { capture: true })
231232
document.body.addEventListener('focus', handle, { capture: true })
232-
}
233+
})
233234

234235
function useRestoreElement(enabled: Ref<boolean>) {
235236
let localHistory = ref<HTMLElement[]>(history.slice())
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function onDocumentReady(cb: () => void) {
2+
function check() {
3+
if (document.readyState === 'loading') return
4+
cb()
5+
document.removeEventListener('DOMContentLoaded', check)
6+
}
7+
8+
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
9+
document.addEventListener('DOMContentLoaded', check)
10+
check()
11+
}
12+
}

0 commit comments

Comments
 (0)