@hypen-space/web 0.5.2 → 0.5.3
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/dist/canvas/accessibility.d.ts +118 -11
- package/dist/canvas/accessibility.js +470 -108
- package/dist/canvas/accessibility.js.map +1 -1
- package/dist/canvas/dispatch.d.ts +24 -0
- package/dist/canvas/dispatch.js +62 -0
- package/dist/canvas/dispatch.js.map +1 -0
- package/dist/canvas/editing.d.ts +178 -0
- package/dist/canvas/editing.js +590 -0
- package/dist/canvas/editing.js.map +1 -0
- package/dist/canvas/events.d.ts +28 -23
- package/dist/canvas/events.js +76 -128
- package/dist/canvas/events.js.map +1 -1
- package/dist/canvas/focus.d.ts +78 -0
- package/dist/canvas/focus.js +182 -0
- package/dist/canvas/focus.js.map +1 -0
- package/dist/canvas/index.d.ts +3 -1
- package/dist/canvas/index.js +3 -1
- package/dist/canvas/index.js.map +1 -1
- package/dist/canvas/paint.d.ts +16 -3
- package/dist/canvas/paint.js +283 -41
- package/dist/canvas/paint.js.map +1 -1
- package/dist/canvas/renderer.d.ts +20 -1
- package/dist/canvas/renderer.js +181 -50
- package/dist/canvas/renderer.js.map +1 -1
- package/dist/canvas/selection.js +31 -177
- package/dist/canvas/selection.js.map +1 -1
- package/dist/canvas/text-geometry.d.ts +72 -0
- package/dist/canvas/text-geometry.js +244 -0
- package/dist/canvas/text-geometry.js.map +1 -0
- package/dist/canvas/text.js +42 -9
- package/dist/canvas/text.js.map +1 -1
- package/dist/canvas/types.d.ts +7 -1
- package/dist/canvas/variants.d.ts +4 -1
- package/dist/canvas/variants.js +22 -3
- package/dist/canvas/variants.js.map +1 -1
- package/dist/dom/a11y-styles.d.ts +20 -0
- package/dist/dom/a11y-styles.js +61 -0
- package/dist/dom/a11y-styles.js.map +1 -0
- package/dist/dom/applicators/aria.d.ts +19 -0
- package/dist/dom/applicators/aria.js +36 -0
- package/dist/dom/applicators/aria.js.map +1 -0
- package/dist/dom/applicators/events.d.ts +7 -0
- package/dist/dom/applicators/events.js +35 -8
- package/dist/dom/applicators/events.js.map +1 -1
- package/dist/dom/applicators/index.js +4 -0
- package/dist/dom/applicators/index.js.map +1 -1
- package/dist/dom/components/hypenapp.d.ts +25 -1
- package/dist/dom/components/hypenapp.js +213 -245
- package/dist/dom/components/hypenapp.js.map +1 -1
- package/dist/dom/components/index.js +7 -0
- package/dist/dom/components/index.js.map +1 -1
- package/dist/dom/components/spinner.js +24 -10
- package/dist/dom/components/spinner.js.map +1 -1
- package/dist/dom/components/tabs.d.ts +27 -0
- package/dist/dom/components/tabs.js +69 -0
- package/dist/dom/components/tabs.js.map +1 -0
- package/dist/dom/components/visuallyhidden.d.ts +9 -0
- package/dist/dom/components/visuallyhidden.js +26 -0
- package/dist/dom/components/visuallyhidden.js.map +1 -0
- package/dist/dom/operability.d.ts +100 -0
- package/dist/dom/operability.js +298 -0
- package/dist/dom/operability.js.map +1 -0
- package/dist/dom/renderer.d.ts +98 -5
- package/dist/dom/renderer.js +398 -63
- package/dist/dom/renderer.js.map +1 -1
- package/dist/dom/route-focus.d.ts +42 -0
- package/dist/dom/route-focus.js +88 -0
- package/dist/dom/route-focus.js.map +1 -0
- package/dist/dom/semantics.d.ts +35 -0
- package/dist/dom/semantics.js +184 -0
- package/dist/dom/semantics.js.map +1 -0
- package/package.json +2 -2
- package/src/canvas/QUICKSTART.md +4 -4
- package/src/canvas/README.md +55 -29
- package/src/canvas/accessibility.ts +507 -115
- package/src/canvas/dispatch.ts +78 -0
- package/src/canvas/editing.ts +697 -0
- package/src/canvas/events.ts +89 -142
- package/src/canvas/focus.ts +216 -0
- package/src/canvas/index.ts +8 -1
- package/src/canvas/paint.ts +339 -42
- package/src/canvas/renderer.ts +218 -70
- package/src/canvas/selection.ts +52 -210
- package/src/canvas/text-geometry.ts +311 -0
- package/src/canvas/text.ts +43 -9
- package/src/canvas/types.ts +14 -1
- package/src/canvas/variants.ts +24 -5
- package/src/dom/a11y-styles.ts +65 -0
- package/src/dom/applicators/aria.ts +41 -0
- package/src/dom/applicators/events.ts +38 -7
- package/src/dom/applicators/index.ts +5 -0
- package/src/dom/components/hypenapp.ts +244 -272
- package/src/dom/components/index.ts +7 -0
- package/src/dom/components/spinner.ts +31 -13
- package/src/dom/components/tabs.ts +76 -0
- package/src/dom/components/visuallyhidden.ts +30 -0
- package/src/dom/operability.ts +312 -0
- package/src/dom/renderer.ts +455 -63
- package/src/dom/route-focus.ts +98 -0
- package/src/dom/semantics.ts +199 -0
- package/dist/canvas/input.d.ts +0 -76
- package/dist/canvas/input.js +0 -207
- package/dist/canvas/input.js.map +0 -1
- package/src/canvas/input.ts +0 -251
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composite-widget hosts: Tabs / Tab / TabPanel / Option.
|
|
3
|
+
*
|
|
4
|
+
* These exist so the composite element types render *focusable, styleable*
|
|
5
|
+
* hosts instead of falling through to the unknown-component fallback (a
|
|
6
|
+
* `display: contents` div, which cannot reliably hold keyboard focus). The
|
|
7
|
+
* accessibility roles come from the engine-derived Semantics block
|
|
8
|
+
* (tablist/tab/tabpanel/option), not from these handlers; keyboard roving is
|
|
9
|
+
* wired by the renderer when it sees `role="tablist"` (see operability.ts).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ComponentHandler } from "./index.js";
|
|
13
|
+
|
|
14
|
+
/** Tabs → a plain row container; role="tablist" arrives via semantics. */
|
|
15
|
+
export const tabsHandler: ComponentHandler = {
|
|
16
|
+
create(): HTMLElement {
|
|
17
|
+
const el = document.createElement("div");
|
|
18
|
+
el.dataset.hypenType = "tabs";
|
|
19
|
+
el.style.display = "flex";
|
|
20
|
+
el.style.flexDirection = "row";
|
|
21
|
+
return el;
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Tab → a native `<button>` so focus and Enter/Space activation come free;
|
|
27
|
+
* the engine's `role="tab"` overrides the implicit button role (see the
|
|
28
|
+
* IMPLICIT_ROLE override logic in semantics.ts).
|
|
29
|
+
*/
|
|
30
|
+
export const tabHandler: ComponentHandler = {
|
|
31
|
+
create(): HTMLElement {
|
|
32
|
+
const el = document.createElement("button");
|
|
33
|
+
el.setAttribute("type", "button");
|
|
34
|
+
el.dataset.hypenType = "tab";
|
|
35
|
+
return el;
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
applyProps(el: HTMLElement, props: Record<string, any>): void {
|
|
39
|
+
const text = props["0"] ?? props.text;
|
|
40
|
+
if (text !== undefined) {
|
|
41
|
+
el.dataset.textTemplate = String(text);
|
|
42
|
+
el.textContent = String(text);
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* TabPanel → a focusable region (`tabindex=0`) so a Tab keypress from the
|
|
49
|
+
* tablist lands inside the panel even when it has no focusable content
|
|
50
|
+
* (WAI-ARIA APG recommendation).
|
|
51
|
+
*/
|
|
52
|
+
export const tabPanelHandler: ComponentHandler = {
|
|
53
|
+
create(): HTMLElement {
|
|
54
|
+
const el = document.createElement("div");
|
|
55
|
+
el.dataset.hypenType = "tabpanel";
|
|
56
|
+
el.tabIndex = 0;
|
|
57
|
+
return el;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Option → a plain div host; role="option" arrives via semantics. */
|
|
62
|
+
export const optionHandler: ComponentHandler = {
|
|
63
|
+
create(): HTMLElement {
|
|
64
|
+
const el = document.createElement("div");
|
|
65
|
+
el.dataset.hypenType = "option";
|
|
66
|
+
return el;
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
applyProps(el: HTMLElement, props: Record<string, any>): void {
|
|
70
|
+
const text = props["0"] ?? props.text;
|
|
71
|
+
if (text !== undefined) {
|
|
72
|
+
el.dataset.textTemplate = String(text);
|
|
73
|
+
el.textContent = String(text);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VisuallyHidden Component
|
|
3
|
+
*
|
|
4
|
+
* Renders content that is invisible on screen but remains in the accessibility
|
|
5
|
+
* tree for screen readers — the standard "sr-only" pattern. Useful for giving
|
|
6
|
+
* an icon-only control readable text, announcing context, etc.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ComponentHandler } from "./index.js";
|
|
10
|
+
|
|
11
|
+
export const visuallyHiddenHandler: ComponentHandler = {
|
|
12
|
+
create(): HTMLElement {
|
|
13
|
+
const el = document.createElement("span");
|
|
14
|
+
el.dataset.hypenType = "visuallyhidden";
|
|
15
|
+
|
|
16
|
+
// Standard visually-hidden (sr-only) styles: removed from the visual
|
|
17
|
+
// layout but still read by assistive technology.
|
|
18
|
+
el.style.position = "absolute";
|
|
19
|
+
el.style.width = "1px";
|
|
20
|
+
el.style.height = "1px";
|
|
21
|
+
el.style.padding = "0";
|
|
22
|
+
el.style.margin = "-1px";
|
|
23
|
+
el.style.overflow = "hidden";
|
|
24
|
+
el.style.clip = "rect(0, 0, 0, 0)";
|
|
25
|
+
el.style.whiteSpace = "nowrap";
|
|
26
|
+
el.style.border = "0";
|
|
27
|
+
|
|
28
|
+
return el;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keyboard operability for actionable elements.
|
|
3
|
+
*
|
|
4
|
+
* Native interactive elements (`<button>`, `<a href>`) are keyboard-operable
|
|
5
|
+
* for free — the browser activates them on Enter/Space. But Hypen's actionable
|
|
6
|
+
* `Card` renders a plain `<div>`, which is neither focusable nor
|
|
7
|
+
* keyboard-activatable. This module closes that gap: a non-native actionable
|
|
8
|
+
* host is given a `button` role, made focusable (`tabindex=0`), and wired so
|
|
9
|
+
* Enter/Space dispatch the same action as a click.
|
|
10
|
+
*
|
|
11
|
+
* This is intentionally client-side: focus and key handling are synchronous
|
|
12
|
+
* interactions the streaming engine cannot mediate.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { triggerElementAction } from "./applicators/events.js";
|
|
16
|
+
import { getMeta, setMeta } from "./element-data.js";
|
|
17
|
+
import { focusRouteTarget } from "./route-focus.js";
|
|
18
|
+
|
|
19
|
+
/** Host tags that the browser already makes keyboard-operable. */
|
|
20
|
+
const NATIVE_INTERACTIVE = new Set(["BUTTON", "A"]);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Make a non-native actionable element keyboard-operable. Idempotent across
|
|
24
|
+
* re-renders (guarded by a dataset flag). No-op for native interactive tags.
|
|
25
|
+
*/
|
|
26
|
+
export function makeKeyboardActivatable(element: HTMLElement, actionValue: unknown): void {
|
|
27
|
+
const tag = element.tagName?.toUpperCase();
|
|
28
|
+
if (tag && NATIVE_INTERACTIVE.has(tag)) return;
|
|
29
|
+
|
|
30
|
+
// The keydown listener reads the action from element meta, so a later
|
|
31
|
+
// re-apply with a different action retargets keyboard activation exactly
|
|
32
|
+
// as it does the click listener — no stale action, no second listener.
|
|
33
|
+
setMeta(element, "kbdAction", actionValue);
|
|
34
|
+
|
|
35
|
+
// Wire the keydown listener only once.
|
|
36
|
+
if (element.dataset?.hypenKbd) return;
|
|
37
|
+
if (element.dataset) element.dataset.hypenKbd = "1";
|
|
38
|
+
|
|
39
|
+
// Announce as a button, make it reachable, and activate on Enter/Space.
|
|
40
|
+
element.setAttribute("role", "button");
|
|
41
|
+
element.tabIndex = 0;
|
|
42
|
+
|
|
43
|
+
element.addEventListener("keydown", (event: Event) => {
|
|
44
|
+
const key = (event as KeyboardEvent).key;
|
|
45
|
+
if (key === "Enter" || key === " " || key === "Spacebar") {
|
|
46
|
+
// Space would otherwise scroll the page.
|
|
47
|
+
event.preventDefault?.();
|
|
48
|
+
triggerElementAction(element, getMeta(element, "kbdAction"));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** CSS selector for the elements that can hold keyboard focus. */
|
|
54
|
+
const FOCUSABLE_SELECTOR = "a,button,input,textarea,select,[tabindex]";
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Pick the focus target for a Tab keypress within a trapped container, wrapping
|
|
58
|
+
* at the ends.
|
|
59
|
+
*
|
|
60
|
+
* Pure over the ordered list of focusable descendants so it is testable without
|
|
61
|
+
* a real DOM: given the currently-focused element (`active`) and the Shift
|
|
62
|
+
* state, returns the element that should receive focus next, or `null` when
|
|
63
|
+
* there is nothing to focus. Tab past the last element wraps to the first;
|
|
64
|
+
* Shift+Tab past the first wraps to the last. When focus is outside the set,
|
|
65
|
+
* Tab lands on the first element and Shift+Tab on the last.
|
|
66
|
+
*/
|
|
67
|
+
export function nextTrapFocus<T>(focusable: T[], active: T | null, shift: boolean): T | null {
|
|
68
|
+
if (focusable.length === 0) return null;
|
|
69
|
+
const first = focusable[0]!;
|
|
70
|
+
const last = focusable[focusable.length - 1]!;
|
|
71
|
+
const index = active == null ? -1 : focusable.indexOf(active);
|
|
72
|
+
|
|
73
|
+
if (index === -1) return shift ? last : first;
|
|
74
|
+
if (shift) return index === 0 ? last : focusable[index - 1]!;
|
|
75
|
+
return index === focusable.length - 1 ? first : focusable[index + 1]!;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Pick the tab that should receive focus for a keypress inside a roving
|
|
80
|
+
* tablist (WAI-ARIA APG "Tabs" keyboard contract). Pure over the ordered tab
|
|
81
|
+
* list so it is testable without a real DOM: Arrow Right/Down move to the
|
|
82
|
+
* next tab (wrapping), Arrow Left/Up to the previous (wrapping), Home/End to
|
|
83
|
+
* the first/last. Any other key — or an empty list — returns `null` (leave
|
|
84
|
+
* focus alone).
|
|
85
|
+
*/
|
|
86
|
+
export function nextRovingFocus<T>(tabs: T[], active: T | null, key: string): T | null {
|
|
87
|
+
if (tabs.length === 0) return null;
|
|
88
|
+
const first = tabs[0]!;
|
|
89
|
+
const last = tabs[tabs.length - 1]!;
|
|
90
|
+
if (key === "Home") return first;
|
|
91
|
+
if (key === "End") return last;
|
|
92
|
+
|
|
93
|
+
const forward = key === "ArrowRight" || key === "ArrowDown";
|
|
94
|
+
const backward = key === "ArrowLeft" || key === "ArrowUp";
|
|
95
|
+
if (!forward && !backward) return null;
|
|
96
|
+
|
|
97
|
+
const index = active == null ? -1 : tabs.indexOf(active);
|
|
98
|
+
if (index === -1) return first;
|
|
99
|
+
if (forward) return index === tabs.length - 1 ? first : tabs[index + 1]!;
|
|
100
|
+
return index === 0 ? last : tabs[index - 1]!;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Pick the item typeahead should move focus to (WAI-ARIA APG first-character
|
|
105
|
+
* navigation). Pure over the ordered item list so it is testable without a
|
|
106
|
+
* real DOM: the search is a case-insensitive `startsWith` over `textOf(item)`,
|
|
107
|
+
* starting from the item AFTER `active` and wrapping; `active` itself is the
|
|
108
|
+
* last candidate, so a query the current item already matches keeps focus in
|
|
109
|
+
* place (multi-char accumulation refining onto the same item). Returns `null`
|
|
110
|
+
* when nothing matches or the query is empty. Query accumulation and its
|
|
111
|
+
* timeout live at the caller.
|
|
112
|
+
*/
|
|
113
|
+
export function nextTypeaheadFocus<T>(
|
|
114
|
+
items: T[],
|
|
115
|
+
active: T | null,
|
|
116
|
+
query: string,
|
|
117
|
+
textOf: (item: T) => string,
|
|
118
|
+
): T | null {
|
|
119
|
+
if (items.length === 0 || query.length === 0) return null;
|
|
120
|
+
const q = query.toLowerCase();
|
|
121
|
+
const start = active == null ? -1 : items.indexOf(active);
|
|
122
|
+
for (let step = 1; step <= items.length; step++) {
|
|
123
|
+
const item = items[(start + step + items.length) % items.length]!;
|
|
124
|
+
if (textOf(item).trim().toLowerCase().startsWith(q)) return item;
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Pause after which an accumulated typeahead query starts over. */
|
|
130
|
+
const TYPEAHEAD_RESET_MS = 500;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Shared roving-tabindex installer for composite widgets (tablist, listbox):
|
|
134
|
+
* arrow keys move focus among the `itemSelector` descendants (wrapping),
|
|
135
|
+
* Home/End jump to the ends, and only the focused item stays in the page Tab
|
|
136
|
+
* order (`tabindex=0`; the rest drop to `-1`) so a single Tab keypress leaves
|
|
137
|
+
* the widget. Printable characters accumulate into a short-lived typeahead
|
|
138
|
+
* query (reset after a 500ms pause, tracked by timestamp — no timers) that
|
|
139
|
+
* roves to the next item whose text starts with the query. Space is excluded:
|
|
140
|
+
* it activates the focused item, and stealing it would break native Button
|
|
141
|
+
* hosts. Activation stays with the item's own click/Enter handling —
|
|
142
|
+
* selection is app state, not focus state. Idempotent across re-renders,
|
|
143
|
+
* guarded by a dataset flag.
|
|
144
|
+
*/
|
|
145
|
+
function installRovingWidget(container: HTMLElement, itemSelector: string): void {
|
|
146
|
+
if (container.dataset?.hypenRoving) return;
|
|
147
|
+
if (container.dataset) container.dataset.hypenRoving = "1";
|
|
148
|
+
|
|
149
|
+
const itemsOf = (): HTMLElement[] =>
|
|
150
|
+
Array.from(container.querySelectorAll(itemSelector)) as unknown as HTMLElement[];
|
|
151
|
+
|
|
152
|
+
const rove = (target: HTMLElement, items: HTMLElement[]): void => {
|
|
153
|
+
for (const item of items) {
|
|
154
|
+
item.tabIndex = item === target ? 0 : -1;
|
|
155
|
+
}
|
|
156
|
+
target.focus?.();
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
let typeahead = "";
|
|
160
|
+
let typeaheadAt = 0;
|
|
161
|
+
|
|
162
|
+
container.addEventListener("keydown", (event: Event) => {
|
|
163
|
+
const keyEvent = event as KeyboardEvent;
|
|
164
|
+
const key = keyEvent.key;
|
|
165
|
+
const items = itemsOf();
|
|
166
|
+
const focused = (container.ownerDocument?.activeElement ?? null) as HTMLElement | null;
|
|
167
|
+
const active = focused && items.includes(focused) ? focused : null;
|
|
168
|
+
|
|
169
|
+
let target = nextRovingFocus(items, active, key);
|
|
170
|
+
|
|
171
|
+
if (
|
|
172
|
+
!target &&
|
|
173
|
+
key.length === 1 &&
|
|
174
|
+
key !== " " &&
|
|
175
|
+
!keyEvent.ctrlKey &&
|
|
176
|
+
!keyEvent.metaKey &&
|
|
177
|
+
!keyEvent.altKey
|
|
178
|
+
) {
|
|
179
|
+
const now = Date.now();
|
|
180
|
+
if (now - typeaheadAt > TYPEAHEAD_RESET_MS) typeahead = "";
|
|
181
|
+
typeaheadAt = now;
|
|
182
|
+
typeahead += key;
|
|
183
|
+
target = nextTypeaheadFocus(items, active, typeahead, (item) => item.textContent ?? "");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (target) {
|
|
187
|
+
event.preventDefault?.();
|
|
188
|
+
rove(target, items);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// First entry into the widget establishes the roving state (focused item
|
|
193
|
+
// reachable, siblings parked at -1) without needing any keydown first.
|
|
194
|
+
container.addEventListener("focusin", (event: Event) => {
|
|
195
|
+
const items = itemsOf();
|
|
196
|
+
const focused = (event as FocusEvent).target as HTMLElement | null;
|
|
197
|
+
if (focused && items.includes(focused)) {
|
|
198
|
+
for (const item of items) {
|
|
199
|
+
item.tabIndex = item === focused ? 0 : -1;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Install the WAI-ARIA APG "Tabs" keyboard contract on a tablist container:
|
|
207
|
+
* roving tabindex over the `role="tab"` descendants plus first-character
|
|
208
|
+
* typeahead (see `installRovingWidget`).
|
|
209
|
+
*/
|
|
210
|
+
export function makeRovingTablist(container: HTMLElement): void {
|
|
211
|
+
installRovingWidget(container, '[role="tab"]');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Install the WAI-ARIA APG "Listbox" keyboard contract on a listbox
|
|
216
|
+
* container: roving tabindex over the `role="option"` descendants plus
|
|
217
|
+
* first-character typeahead (see `installRovingWidget`). No-op for a native
|
|
218
|
+
* `<select>` host (Hypen's `Select`), where the browser already owns arrow
|
|
219
|
+
* and typeahead behaviour.
|
|
220
|
+
*/
|
|
221
|
+
export function makeRovingListbox(container: HTMLElement): void {
|
|
222
|
+
if (container.tagName?.toUpperCase() === "SELECT") return;
|
|
223
|
+
installRovingWidget(container, '[role="option"]');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Install a focus trap on a dialog-like container: Tab / Shift+Tab cycle focus
|
|
228
|
+
* among the container's focusable descendants and wrap at the ends. Closing is
|
|
229
|
+
* app state: Escape is handled by `installDialogEscape` only when the dialog
|
|
230
|
+
* declares an `onClose` action. Idempotent across re-renders, guarded by a
|
|
231
|
+
* dataset flag.
|
|
232
|
+
*/
|
|
233
|
+
export function makeFocusTrap(container: HTMLElement): void {
|
|
234
|
+
if (container.dataset?.hypenTrap) return;
|
|
235
|
+
if (container.dataset) container.dataset.hypenTrap = "1";
|
|
236
|
+
|
|
237
|
+
container.addEventListener("keydown", (event: Event) => {
|
|
238
|
+
if ((event as KeyboardEvent).key !== "Tab") return;
|
|
239
|
+
const focusable = Array.from(
|
|
240
|
+
container.querySelectorAll(FOCUSABLE_SELECTOR),
|
|
241
|
+
) as unknown as HTMLElement[];
|
|
242
|
+
const active = (container.ownerDocument?.activeElement ?? null) as HTMLElement | null;
|
|
243
|
+
const target = nextTrapFocus(focusable, active, (event as KeyboardEvent).shiftKey === true);
|
|
244
|
+
if (target) {
|
|
245
|
+
event.preventDefault?.();
|
|
246
|
+
target.focus?.();
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Tags that can hold focus without a tabindex — the DFS twin of `FOCUSABLE_SELECTOR`. */
|
|
252
|
+
const FOCUSABLE_TAGS = new Set(["A", "BUTTON", "INPUT", "TEXTAREA", "SELECT"]);
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* First focusable descendant of `root` in document order, or `null`. Matches
|
|
256
|
+
* the focus trap's element set (`FOCUSABLE_SELECTOR`) but walks `children`
|
|
257
|
+
* recursively instead of `querySelectorAll`, so it works on hosts without
|
|
258
|
+
* selector support (test doubles) — document order is exactly child order.
|
|
259
|
+
*/
|
|
260
|
+
export function findFirstFocusable(root: HTMLElement): HTMLElement | null {
|
|
261
|
+
const children = Array.from((root.children ?? []) as unknown as ArrayLike<HTMLElement>);
|
|
262
|
+
for (const child of children) {
|
|
263
|
+
const tag = child.tagName?.toUpperCase() ?? "";
|
|
264
|
+
if (FOCUSABLE_TAGS.has(tag) || child.getAttribute?.("tabindex") != null) return child;
|
|
265
|
+
const nested = findFirstFocusable(child);
|
|
266
|
+
if (nested) return nested;
|
|
267
|
+
}
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Dialog auto-focus on mount (WAI-ARIA APG "Dialog (Modal)"): focus the
|
|
273
|
+
* dialog's first focusable descendant, else the dialog itself (granted
|
|
274
|
+
* programmatic focusability by `focusRouteTarget`). Returns the element that
|
|
275
|
+
* held focus before the dialog opened, so the caller can restore it on close.
|
|
276
|
+
*/
|
|
277
|
+
export function focusDialogOnOpen(dialog: HTMLElement): HTMLElement | null {
|
|
278
|
+
const doc = dialog.ownerDocument ?? (typeof document !== "undefined" ? document : null);
|
|
279
|
+
const previous = (doc?.activeElement ?? null) as HTMLElement | null;
|
|
280
|
+
focusRouteTarget(findFirstFocusable(dialog) ?? dialog);
|
|
281
|
+
return previous;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Restore focus to the element that had it before a dialog opened. Skipped
|
|
286
|
+
* when the opener is gone: no longer connected to the document, or living
|
|
287
|
+
* inside the (now closed) dialog itself.
|
|
288
|
+
*/
|
|
289
|
+
export function restoreDialogFocus(dialog: HTMLElement, opener: HTMLElement | null): void {
|
|
290
|
+
if (!opener) return;
|
|
291
|
+
if (dialog === opener || dialog.contains?.(opener)) return;
|
|
292
|
+
if ((opener as { isConnected?: boolean }).isConnected === false) return;
|
|
293
|
+
opener.focus?.();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Escape-to-close for a dialog that declares an `onClose` action: Escape
|
|
298
|
+
* dispatches the action; the actual close (state flip, unmount) stays with
|
|
299
|
+
* the app. A dialog without `onClose` gets no Escape behaviour — the renderer
|
|
300
|
+
* cannot know how to close it. Idempotent across re-renders, guarded by a
|
|
301
|
+
* dataset flag.
|
|
302
|
+
*/
|
|
303
|
+
export function installDialogEscape(dialog: HTMLElement, onCloseValue: unknown): void {
|
|
304
|
+
if (dialog.dataset?.hypenDialogEsc) return;
|
|
305
|
+
if (dialog.dataset) dialog.dataset.hypenDialogEsc = "1";
|
|
306
|
+
|
|
307
|
+
dialog.addEventListener("keydown", (event: Event) => {
|
|
308
|
+
if ((event as KeyboardEvent).key !== "Escape") return;
|
|
309
|
+
event.preventDefault?.();
|
|
310
|
+
triggerElementAction(dialog, onCloseValue);
|
|
311
|
+
});
|
|
312
|
+
}
|