hlmnhkl-ui 0.22.0 → 0.22.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/internal/focus.ts +12 -0
package/package.json
CHANGED
package/src/internal/focus.ts
CHANGED
|
@@ -48,6 +48,9 @@ export function useFocusTrap(
|
|
|
48
48
|
const first = initialFocus?.current ?? list.find((el) => !el.hasAttribute('data-skip-initial-focus')) ?? list[0] ?? root
|
|
49
49
|
first.focus({ preventScroll: true })
|
|
50
50
|
|
|
51
|
+
// Safari's plain Tab skips buttons, so it can jump from the middle of the layer to the page behind,
|
|
52
|
+
// or off the page. A Tab that lands outside goes back to the end it wrapped past.
|
|
53
|
+
let check = 0
|
|
51
54
|
const onKey = (e: KeyboardEvent) => {
|
|
52
55
|
if (e.key !== 'Tab') return
|
|
53
56
|
const items = focusables(root)
|
|
@@ -65,10 +68,19 @@ export function useFocusTrap(
|
|
|
65
68
|
} else if (!e.shiftKey && current === lastEl) {
|
|
66
69
|
e.preventDefault()
|
|
67
70
|
firstEl.focus()
|
|
71
|
+
} else {
|
|
72
|
+
const back = e.shiftKey
|
|
73
|
+
clearTimeout(check)
|
|
74
|
+
check = window.setTimeout(() => {
|
|
75
|
+
if (root.contains(document.activeElement)) return
|
|
76
|
+
const now = focusables(root)
|
|
77
|
+
now[back ? now.length - 1 : 0]?.focus({ preventScroll: true })
|
|
78
|
+
})
|
|
68
79
|
}
|
|
69
80
|
}
|
|
70
81
|
root.addEventListener('keydown', onKey)
|
|
71
82
|
return () => {
|
|
83
|
+
clearTimeout(check)
|
|
72
84
|
root.removeEventListener('keydown', onKey)
|
|
73
85
|
// Give focus back only if it is still in the layer, so a click elsewhere keeps its own focus.
|
|
74
86
|
const now = document.activeElement
|