-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact.js
More file actions
95 lines (78 loc) · 2.96 KB
/
Copy pathreact.js
File metadata and controls
95 lines (78 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { mountFocusDebugger } from './overlay/focus.js'
import { mountNamesDebugger } from './overlay/names.js'
import { mountHeadingMapDebugger } from './overlay/headings.js'
import { mountTabStopsDebugger } from './overlay/tabstops.js'
import { mountDebugLauncher } from './overlay/launcher.js'
import { mountDebugHelp } from './overlay/help.js'
import { mountDeployBanner } from './overlay/banner.js'
const IS_DEV = !!globalThis.ROGERS_DEV
// React is passed in by the caller — no top-level React import.
// Usage: import { createComponents } from '@a11yfred/rogers/react'
// const { FocusDebugger } = createComponents(React)
export function createComponents({ useEffect, useRef }) {
function useOverlay(mountFn, enabled, deps) {
useEffect(() => {
if (!IS_DEV || !enabled) return
const overlay = mountFn()
return () => overlay.destroy()
// deps passed by caller
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps)
}
function FocusDebugger({ enabled = true }) {
useOverlay(() => mountFocusDebugger(), enabled, [enabled])
return null
}
function NamesDebugger({ enabled = true }) {
useOverlay(() => mountNamesDebugger(), enabled, [enabled])
return null
}
function HeadingMapDebugger({ enabled = false }) {
useOverlay(() => mountHeadingMapDebugger(), enabled, [enabled])
return null
}
function TabStopsDebugger({ enabled = false }) {
useOverlay(() => mountTabStopsDebugger(), enabled, [enabled])
return null
}
function DebugLauncher({ enabled = false, position = 'bottom-right', onCommand, customSections = [] }) {
useOverlay(
() => mountDebugLauncher({ position, onCommand, customSections }),
enabled,
[enabled, position, onCommand, customSections],
)
return null
}
function DebugHelp({ open, onClose, customCommands = [] }) {
const panelRef = useRef(null)
useEffect(() => {
if (!IS_DEV) return
const panel = mountDebugHelp({ onClose, customCommands })
panelRef.current = panel
return () => { panel.destroy(); panelRef.current = null }
// customCommands is a stable reference from caller
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [onClose])
useEffect(() => {
if (!IS_DEV) return
if (open) panelRef.current?.open()
else panelRef.current?.close()
}, [open])
return null
}
function DeployBanner({ target }) {
const bannerRef = useRef(null)
useEffect(() => {
if (!IS_DEV) return
const banner = mountDeployBanner(target)
bannerRef.current = banner
return () => { banner.destroy(); bannerRef.current = null }
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
useEffect(() => {
bannerRef.current?.setTarget(target)
}, [target])
return null
}
return { FocusDebugger, NamesDebugger, HeadingMapDebugger, TabStopsDebugger, DebugLauncher, DebugHelp, DeployBanner }
}