@cat-factory/app 0.137.2 → 0.138.0

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.
@@ -1,72 +0,0 @@
1
- import { nextTick, onScopeDispose, watch, type Ref } from 'vue'
2
-
3
- /**
4
- * Lightweight focus management for a modal surface (the screenshot review windows + the
5
- * shared lightbox). While `active`, it:
6
- * · moves focus into the container on open (so keyboard / screen-reader users land inside
7
- * the dialog instead of staying on the background),
8
- * · traps Tab / Shift+Tab within the container's focusable elements, and
9
- * · restores focus to whatever was focused before, on close.
10
- *
11
- * Nested surfaces (a lightbox opened over a review window) hand off cleanly because each
12
- * caller scopes its own `active` — the window passes `open && !lightboxOpen`, so exactly one
13
- * trap is live at a time and they never fight over Tab.
14
- */
15
- export function useFocusTrap(container: Ref<HTMLElement | null>, active: Ref<boolean>): void {
16
- let previouslyFocused: HTMLElement | null = null
17
-
18
- function focusables(): HTMLElement[] {
19
- const root = container.value
20
- if (!root) return []
21
- const nodes = root.querySelectorAll<HTMLElement>(
22
- 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])',
23
- )
24
- // Skip elements that aren't actually rendered (e.g. inside a `v-if`/`hidden` branch).
25
- return Array.from(nodes).filter(
26
- (el) => el.offsetParent !== null || el === document.activeElement,
27
- )
28
- }
29
-
30
- function onKeydown(e: KeyboardEvent): void {
31
- if (!active.value || e.key !== 'Tab') return
32
- const els = focusables()
33
- if (!els.length) {
34
- e.preventDefault()
35
- container.value?.focus()
36
- return
37
- }
38
- const first = els[0]!
39
- const last = els[els.length - 1]!
40
- const current = document.activeElement as HTMLElement | null
41
- const inside = !!container.value?.contains(current)
42
- if (e.shiftKey) {
43
- if (!inside || current === first) {
44
- e.preventDefault()
45
- last.focus()
46
- }
47
- } else if (!inside || current === last) {
48
- e.preventDefault()
49
- first.focus()
50
- }
51
- }
52
-
53
- watch(
54
- active,
55
- (on) => {
56
- if (on) {
57
- previouslyFocused = document.activeElement as HTMLElement | null
58
- window.addEventListener('keydown', onKeydown, true)
59
- void nextTick(() => {
60
- ;(focusables()[0] ?? container.value)?.focus()
61
- })
62
- } else {
63
- window.removeEventListener('keydown', onKeydown, true)
64
- previouslyFocused?.focus?.()
65
- previouslyFocused = null
66
- }
67
- },
68
- { immediate: true },
69
- )
70
-
71
- onScopeDispose(() => window.removeEventListener('keydown', onKeydown, true))
72
- }