@hypen-space/web 0.4.980 → 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/README.md +20 -13
- 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 +90 -103
- 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 -2
- package/dist/canvas/index.js +3 -2
- package/dist/canvas/index.js.map +1 -1
- package/dist/canvas/layout.js +68 -98
- package/dist/canvas/layout.js.map +1 -1
- package/dist/canvas/paint.d.ts +16 -3
- package/dist/canvas/paint.js +449 -110
- package/dist/canvas/paint.js.map +1 -1
- package/dist/canvas/renderer.d.ts +28 -4
- package/dist/canvas/renderer.js +201 -81
- 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 +44 -18
- package/dist/canvas/text.js.map +1 -1
- package/dist/canvas/types.d.ts +10 -1
- package/dist/canvas/utils.d.ts +0 -23
- package/dist/canvas/utils.js +3 -67
- package/dist/canvas/utils.js.map +1 -1
- package/dist/canvas/variants.d.ts +52 -0
- package/dist/canvas/variants.js +156 -0
- package/dist/canvas/variants.js.map +1 -0
- 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 +36 -8
- package/dist/dom/applicators/events.js.map +1 -1
- package/dist/dom/applicators/font.d.ts +11 -6
- package/dist/dom/applicators/font.js +16 -27
- package/dist/dom/applicators/font.js.map +1 -1
- package/dist/dom/applicators/index.js +51 -45
- package/dist/dom/applicators/index.js.map +1 -1
- package/dist/dom/canvas/index.js +2 -2
- package/dist/dom/canvas/index.js.map +1 -1
- package/dist/dom/components/app.js +2 -2
- package/dist/dom/components/app.js.map +1 -1
- package/dist/dom/components/audio.js +2 -2
- package/dist/dom/components/audio.js.map +1 -1
- package/dist/dom/components/avatar.js +3 -4
- package/dist/dom/components/avatar.js.map +1 -1
- package/dist/dom/components/badge.js +3 -4
- package/dist/dom/components/badge.js.map +1 -1
- package/dist/dom/components/button.js +2 -2
- package/dist/dom/components/button.js.map +1 -1
- package/dist/dom/components/card.js +2 -2
- package/dist/dom/components/card.js.map +1 -1
- package/dist/dom/components/center.js +2 -2
- package/dist/dom/components/center.js.map +1 -1
- package/dist/dom/components/checkbox.js +4 -5
- package/dist/dom/components/checkbox.js.map +1 -1
- package/dist/dom/components/column.js +2 -2
- package/dist/dom/components/column.js.map +1 -1
- package/dist/dom/components/container.js +2 -2
- package/dist/dom/components/container.js.map +1 -1
- package/dist/dom/components/divider.js +2 -2
- package/dist/dom/components/divider.js.map +1 -1
- package/dist/dom/components/grid.js +10 -10
- package/dist/dom/components/grid.js.map +1 -1
- package/dist/dom/components/heading.js +4 -6
- package/dist/dom/components/heading.js.map +1 -1
- package/dist/dom/components/hypenapp.d.ts +25 -1
- package/dist/dom/components/hypenapp.js +215 -247
- package/dist/dom/components/hypenapp.js.map +1 -1
- package/dist/dom/components/icon.js +4 -5
- package/dist/dom/components/icon.js.map +1 -1
- package/dist/dom/components/image.js +2 -2
- package/dist/dom/components/image.js.map +1 -1
- package/dist/dom/components/index.d.ts +2 -2
- package/dist/dom/components/index.js +9 -2
- package/dist/dom/components/index.js.map +1 -1
- package/dist/dom/components/input.js +2 -2
- package/dist/dom/components/input.js.map +1 -1
- package/dist/dom/components/link.js +2 -2
- package/dist/dom/components/link.js.map +1 -1
- package/dist/dom/components/list.js +2 -2
- package/dist/dom/components/list.js.map +1 -1
- package/dist/dom/components/paragraph.js +3 -4
- package/dist/dom/components/paragraph.js.map +1 -1
- package/dist/dom/components/progressbar.js +3 -3
- package/dist/dom/components/progressbar.js.map +1 -1
- package/dist/dom/components/route.js +2 -2
- package/dist/dom/components/route.js.map +1 -1
- package/dist/dom/components/router.js +2 -2
- package/dist/dom/components/router.js.map +1 -1
- package/dist/dom/components/row.js +10 -10
- package/dist/dom/components/row.js.map +1 -1
- package/dist/dom/components/select.js +3 -4
- package/dist/dom/components/select.js.map +1 -1
- package/dist/dom/components/slider.js +2 -2
- package/dist/dom/components/slider.js.map +1 -1
- package/dist/dom/components/spacer.js +2 -2
- package/dist/dom/components/spacer.js.map +1 -1
- package/dist/dom/components/spinner.js +27 -13
- package/dist/dom/components/spinner.js.map +1 -1
- package/dist/dom/components/stack.js +10 -10
- package/dist/dom/components/stack.js.map +1 -1
- package/dist/dom/components/switch.js +5 -6
- package/dist/dom/components/switch.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/text.js +4 -7
- package/dist/dom/components/text.js.map +1 -1
- package/dist/dom/components/textarea.js +2 -2
- package/dist/dom/components/textarea.js.map +1 -1
- package/dist/dom/components/video.js +2 -2
- package/dist/dom/components/video.js.map +1 -1
- 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/debug.js +6 -8
- package/dist/dom/debug.js.map +1 -1
- package/dist/dom/index.d.ts +0 -1
- package/dist/dom/index.js +0 -1
- package/dist/dom/index.js.map +1 -1
- 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 +400 -66
- 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/dist/variants.d.ts +90 -0
- package/dist/variants.js +206 -0
- package/dist/variants.js.map +1 -0
- package/package.json +2 -2
- package/src/canvas/QUICKSTART.md +16 -11
- package/src/canvas/README.md +63 -34
- 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 +104 -115
- package/src/canvas/focus.ts +216 -0
- package/src/canvas/index.ts +8 -9
- package/src/canvas/layout.ts +68 -103
- package/src/canvas/paint.ts +509 -109
- package/src/canvas/renderer.ts +239 -99
- package/src/canvas/selection.ts +52 -210
- package/src/canvas/text-geometry.ts +311 -0
- package/src/canvas/text.ts +45 -18
- package/src/canvas/types.ts +28 -1
- package/src/canvas/utils.ts +3 -69
- package/src/canvas/variants.ts +172 -0
- package/src/dom/README.md +8 -7
- package/src/dom/a11y-styles.ts +65 -0
- package/src/dom/applicators/aria.ts +41 -0
- package/src/dom/applicators/events.ts +39 -7
- package/src/dom/applicators/font.ts +16 -29
- package/src/dom/applicators/index.ts +61 -46
- package/src/dom/canvas/index.ts +2 -2
- package/src/dom/components/app.ts +2 -2
- package/src/dom/components/audio.ts +2 -2
- package/src/dom/components/avatar.ts +3 -4
- package/src/dom/components/badge.ts +3 -4
- package/src/dom/components/button.ts +2 -2
- package/src/dom/components/card.ts +2 -2
- package/src/dom/components/center.ts +2 -2
- package/src/dom/components/checkbox.ts +4 -5
- package/src/dom/components/column.ts +2 -2
- package/src/dom/components/container.ts +2 -2
- package/src/dom/components/divider.ts +2 -2
- package/src/dom/components/grid.ts +10 -10
- package/src/dom/components/heading.ts +4 -6
- package/src/dom/components/hypenapp.ts +246 -274
- package/src/dom/components/icon.ts +4 -5
- package/src/dom/components/image.ts +2 -2
- package/src/dom/components/index.ts +10 -3
- package/src/dom/components/input.ts +2 -2
- package/src/dom/components/link.ts +2 -2
- package/src/dom/components/list.ts +2 -2
- package/src/dom/components/paragraph.ts +3 -4
- package/src/dom/components/progressbar.ts +3 -3
- package/src/dom/components/route.ts +2 -2
- package/src/dom/components/router.ts +2 -2
- package/src/dom/components/row.ts +10 -10
- package/src/dom/components/select.ts +3 -4
- package/src/dom/components/slider.ts +2 -2
- package/src/dom/components/spacer.ts +2 -2
- package/src/dom/components/spinner.ts +34 -16
- package/src/dom/components/stack.ts +10 -10
- package/src/dom/components/switch.ts +5 -6
- package/src/dom/components/tabs.ts +76 -0
- package/src/dom/components/text.ts +4 -7
- package/src/dom/components/textarea.ts +2 -2
- package/src/dom/components/video.ts +2 -2
- package/src/dom/components/visuallyhidden.ts +30 -0
- package/src/dom/debug.ts +6 -8
- package/src/dom/index.ts +0 -8
- package/src/dom/operability.ts +312 -0
- package/src/dom/renderer.ts +457 -66
- package/src/dom/route-focus.ts +98 -0
- package/src/dom/semantics.ts +199 -0
- package/src/variants.ts +249 -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/dist/client.d.ts +0 -36
- package/dist/client.js +0 -47
- package/dist/client.js.map +0 -1
- package/src/canvas/input.ts +0 -251
- package/src/client.ts +0 -88
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route-change focus management.
|
|
3
|
+
*
|
|
4
|
+
* When the Router swaps routes, keyboard and screen-reader focus is left on
|
|
5
|
+
* an element that just left the document — the user is stranded and AT
|
|
6
|
+
* announces nothing. This module defines the focus contract for navigation
|
|
7
|
+
* (design Open Question #4) and the DOM renderer applies it after each patch
|
|
8
|
+
* batch:
|
|
9
|
+
*
|
|
10
|
+
* 1. A batch is a **navigation** when it contains at least one `detach` (the
|
|
11
|
+
* leaving route, unlinked but kept alive by the Router cache) and at least
|
|
12
|
+
* one incoming subtree root (an `attach` of a cached route, or a fresh
|
|
13
|
+
* `create`+`insert` route build).
|
|
14
|
+
* 2. On navigation, focus moves to — in order of preference:
|
|
15
|
+
* a. the element that held focus when the incoming subtree was detached
|
|
16
|
+
* (**focus restore** on cached re-entry), if it is still inside the
|
|
17
|
+
* subtree;
|
|
18
|
+
* b. the subtree's first heading (`h1`–`h6` / `role="heading"`) or `main`
|
|
19
|
+
* landmark — the "start of the new page" for AT users;
|
|
20
|
+
* c. the subtree root itself.
|
|
21
|
+
* Non-natively-focusable targets get `tabindex="-1"` (programmatic focus
|
|
22
|
+
* only — they do not join the Tab order).
|
|
23
|
+
* 3. Focus-restore memory is keyed by the detached subtree's root NodeId and
|
|
24
|
+
* is dropped the moment a `remove` arrives for that id (Router LRU
|
|
25
|
+
* eviction) — **focus restore never targets an evicted NodeId**, the
|
|
26
|
+
* invariant this contract exists to guarantee.
|
|
27
|
+
*
|
|
28
|
+
* The contract is opt-out: `DOMRendererOptions.routeFocus: "off"` disables it
|
|
29
|
+
* for apps that own focus management themselves.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/** Tags that are natively focusable without a tabindex. */
|
|
33
|
+
const NATIVELY_FOCUSABLE = new Set(["A", "BUTTON", "INPUT", "TEXTAREA", "SELECT"]);
|
|
34
|
+
|
|
35
|
+
/** Heading hosts (`<h1>`–`<h6>`). */
|
|
36
|
+
const HEADING_TAGS = new Set(["H1", "H2", "H3", "H4", "H5", "H6"]);
|
|
37
|
+
|
|
38
|
+
type ElementLike = {
|
|
39
|
+
tagName?: string;
|
|
40
|
+
children?: ArrayLike<unknown>;
|
|
41
|
+
getAttribute?(name: string): string | null;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const matches = (el: ElementLike, pred: (el: ElementLike) => boolean): boolean => {
|
|
45
|
+
try {
|
|
46
|
+
return pred(el);
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const isHeading = (el: ElementLike): boolean =>
|
|
53
|
+
HEADING_TAGS.has(el.tagName?.toUpperCase() ?? "") ||
|
|
54
|
+
el.getAttribute?.("role") === "heading";
|
|
55
|
+
|
|
56
|
+
const isMain = (el: ElementLike): boolean =>
|
|
57
|
+
el.tagName?.toUpperCase() === "MAIN" || el.getAttribute?.("role") === "main";
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Depth-first search over `children` (works on both real DOM collections and
|
|
61
|
+
* test-double arrays) for the first element matching `pred`.
|
|
62
|
+
*/
|
|
63
|
+
function findFirst(root: ElementLike, pred: (el: ElementLike) => boolean): ElementLike | null {
|
|
64
|
+
const children = Array.from((root.children ?? []) as ArrayLike<ElementLike>);
|
|
65
|
+
for (const child of children) {
|
|
66
|
+
if (matches(child, pred)) return child;
|
|
67
|
+
const nested = findFirst(child, pred);
|
|
68
|
+
if (nested) return nested;
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Pick the element that should receive focus inside a freshly-shown route
|
|
75
|
+
* subtree: first heading, else first `main` landmark, else the root itself.
|
|
76
|
+
*/
|
|
77
|
+
export function findRouteFocusTarget(root: HTMLElement): HTMLElement {
|
|
78
|
+
const heading = findFirst(root as ElementLike, isHeading);
|
|
79
|
+
if (heading) return heading as HTMLElement;
|
|
80
|
+
const main = findFirst(root as ElementLike, isMain);
|
|
81
|
+
if (main) return main as HTMLElement;
|
|
82
|
+
return root;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Move focus to `target`, granting it programmatic focusability
|
|
87
|
+
* (`tabindex="-1"`) when it is neither natively focusable nor already
|
|
88
|
+
* carrying a tabindex. `-1` keeps it out of the Tab order — this is a
|
|
89
|
+
* navigation landing point, not a new tab stop.
|
|
90
|
+
*/
|
|
91
|
+
export function focusRouteTarget(target: HTMLElement): void {
|
|
92
|
+
const tag = target.tagName?.toUpperCase() ?? "";
|
|
93
|
+
const hasTabindex = target.getAttribute?.("tabindex") != null;
|
|
94
|
+
if (!NATIVELY_FOCUSABLE.has(tag) && !hasTabindex) {
|
|
95
|
+
target.setAttribute?.("tabindex", "-1");
|
|
96
|
+
}
|
|
97
|
+
target.focus?.();
|
|
98
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accessibility semantics → DOM.
|
|
3
|
+
*
|
|
4
|
+
* The engine derives a platform-neutral {@link Semantics} block (role, heading
|
|
5
|
+
* level, busy, and in later phases name/state/hidden/…) and ships it on
|
|
6
|
+
* `create` patches. This module is the DOM translation: it maps that block
|
|
7
|
+
* onto ARIA / native HTML.
|
|
8
|
+
*
|
|
9
|
+
* Guiding rule: **let native HTML do the work; never emit a redundant or
|
|
10
|
+
* misplaced role.** Most Hypen components render a semantic native element
|
|
11
|
+
* whose tag already conveys the correct role (`<button>`, `<a>`, `<p>`,
|
|
12
|
+
* `<img>`, `<input>`, `<select>`, `<textarea>`, `<h1>`–`<h6>`). Some render a
|
|
13
|
+
* wrapper around the real control (`Checkbox`/`Switch` are a `<label>` around a
|
|
14
|
+
* nested `<input>`), where putting the role on the wrapper would be flat wrong.
|
|
15
|
+
*
|
|
16
|
+
* So we only apply an explicit `role` when the host is a *generic* container
|
|
17
|
+
* (`<div>`/`<span>`) that conveys nothing on its own — which is exactly the
|
|
18
|
+
* handful of cases that need it (`Spinner`, `ProgressBar`). Everywhere else the
|
|
19
|
+
* native element is already correct, and the {@link Semantics} block still
|
|
20
|
+
* travels to the non-DOM renderers (Canvas shadow, iOS, Android) that lack
|
|
21
|
+
* native semantics.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import type { Semantics } from "@hypen-space/core/types";
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The implicit ARIA role each native host tag already conveys. An engine
|
|
28
|
+
* role equal to the implicit one is never applied (redundant-role smell:
|
|
29
|
+
* `role="button"` on `<button>`), but a *different* engine role is an
|
|
30
|
+
* intentional override and IS applied — `role="tab"` on a `<button>` host,
|
|
31
|
+
* `role="combobox"` on an `<input>` (the ARIA 1.2 combobox pattern).
|
|
32
|
+
*
|
|
33
|
+
* `LABEL` is special-cased below: it never takes a role at all, because
|
|
34
|
+
* Checkbox/Switch render a `<label>` *wrapper* whose role belongs to the
|
|
35
|
+
* nested `<input>` — putting it on the wrapper would be flat wrong.
|
|
36
|
+
*/
|
|
37
|
+
const IMPLICIT_ROLE: Record<string, string> = {
|
|
38
|
+
A: "link",
|
|
39
|
+
P: "paragraph",
|
|
40
|
+
IMG: "img",
|
|
41
|
+
INPUT: "textbox",
|
|
42
|
+
TEXTAREA: "textbox",
|
|
43
|
+
SELECT: "listbox",
|
|
44
|
+
BUTTON: "button",
|
|
45
|
+
H1: "heading",
|
|
46
|
+
H2: "heading",
|
|
47
|
+
H3: "heading",
|
|
48
|
+
H4: "heading",
|
|
49
|
+
H5: "heading",
|
|
50
|
+
H6: "heading",
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Attributes this module set on an element on its previous pass, so a
|
|
55
|
+
* re-apply (a reactive `setSemantics` patch) can clear exactly what it — and
|
|
56
|
+
* only it — owns. Tracking per element instead of blanket-removing every
|
|
57
|
+
* possible ARIA attribute keeps the `.aria()` escape hatch's attributes (and
|
|
58
|
+
* anything else outside this module) untouched.
|
|
59
|
+
*/
|
|
60
|
+
const appliedAttrs = new WeakMap<HTMLElement, string[]>();
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Apply derived accessibility semantics to a DOM element.
|
|
64
|
+
*
|
|
65
|
+
* Called at create (first paint) and again on every `setSemantics` patch
|
|
66
|
+
* with the node's complete re-resolved block. Idempotent, and **clearing**:
|
|
67
|
+
* an attribute this module set on a previous pass that the new block no
|
|
68
|
+
* longer produces is removed (a name reverting from explicit to derived
|
|
69
|
+
* must drop its stale `aria-label`, a cleared `.expanded` its
|
|
70
|
+
* `aria-expanded`). No-op when `semantics` was never present, so
|
|
71
|
+
* non-semantic nodes pay nothing.
|
|
72
|
+
*/
|
|
73
|
+
export function applySemantics(element: HTMLElement, semantics?: Semantics): void {
|
|
74
|
+
if (!semantics && !appliedAttrs.has(element)) return;
|
|
75
|
+
|
|
76
|
+
// Everything this pass wants set, collected first so the diff against the
|
|
77
|
+
// previous pass is a plain list comparison.
|
|
78
|
+
const next: Array<[string, string]> = [];
|
|
79
|
+
|
|
80
|
+
if (semantics) {
|
|
81
|
+
if (semantics.hidden) {
|
|
82
|
+
// Decorative: remove from the accessibility tree entirely. Nothing
|
|
83
|
+
// else applies once hidden.
|
|
84
|
+
next.push(["aria-hidden", "true"]);
|
|
85
|
+
} else {
|
|
86
|
+
// tagName is uppercase in real DOM; normalize so the lookup is robust
|
|
87
|
+
// against test doubles that preserve the as-created case.
|
|
88
|
+
const tag = element.tagName?.toUpperCase();
|
|
89
|
+
|
|
90
|
+
if (
|
|
91
|
+
semantics.role &&
|
|
92
|
+
tag !== "LABEL" &&
|
|
93
|
+
IMPLICIT_ROLE[tag] !== semantics.role
|
|
94
|
+
) {
|
|
95
|
+
// Applied when the host conveys nothing (div/span) or when the
|
|
96
|
+
// engine role intentionally overrides the tag's implicit role
|
|
97
|
+
// (`role="tab"` on a <button>). Skipped when redundant, and always
|
|
98
|
+
// skipped on <label> wrappers (the role belongs to the nested input).
|
|
99
|
+
next.push(["role", semantics.role]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// An explicit author label overrides visible content, so it is applied
|
|
103
|
+
// as aria-label even on native elements. A *derived* name is
|
|
104
|
+
// deliberately not applied — the browser already exposes it from the
|
|
105
|
+
// visible content.
|
|
106
|
+
if (semantics.nameExplicit && semantics.name) {
|
|
107
|
+
next.push(["aria-label", semantics.name]);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (semantics.description) {
|
|
111
|
+
next.push(["aria-description", semantics.description]);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Self-state relationship attributes.
|
|
115
|
+
if (semantics.expanded !== undefined) {
|
|
116
|
+
next.push(["aria-expanded", String(semantics.expanded)]);
|
|
117
|
+
}
|
|
118
|
+
if (semantics.pressed !== undefined) {
|
|
119
|
+
next.push(["aria-pressed", String(semantics.pressed)]);
|
|
120
|
+
}
|
|
121
|
+
if (semantics.selected !== undefined) {
|
|
122
|
+
next.push(["aria-selected", String(semantics.selected)]);
|
|
123
|
+
}
|
|
124
|
+
if (semantics.current) {
|
|
125
|
+
next.push(["aria-current", semantics.current]);
|
|
126
|
+
}
|
|
127
|
+
if (semantics.checked !== undefined) {
|
|
128
|
+
next.push(["aria-checked", String(semantics.checked)]);
|
|
129
|
+
}
|
|
130
|
+
if (semantics.invalid !== undefined) {
|
|
131
|
+
// Reactive form validity — kept live via `setSemantics` re-emits;
|
|
132
|
+
// clearing (block drops the field) removes the attribute below.
|
|
133
|
+
next.push(["aria-invalid", String(semantics.invalid)]);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Cross-node relationships by author-supplied id. DOM-only /
|
|
137
|
+
// web-leaning: these id-reference relationships have no equivalent in
|
|
138
|
+
// the string-hint native accessibility APIs, so they do not travel to
|
|
139
|
+
// Canvas / iOS / Android. See the guide's "Platform support" section
|
|
140
|
+
// (hypen-docs/content/docs/guide/accessibility.mdx).
|
|
141
|
+
if (semantics.id) {
|
|
142
|
+
// The anchor the references below resolve against — a real DOM id.
|
|
143
|
+
next.push(["id", semantics.id]);
|
|
144
|
+
}
|
|
145
|
+
if (semantics.dir) {
|
|
146
|
+
// Base text direction → the native HTML dir attribute (not ARIA).
|
|
147
|
+
// Managed like `id`: cleared when a later block drops it.
|
|
148
|
+
next.push(["dir", semantics.dir]);
|
|
149
|
+
}
|
|
150
|
+
if (semantics.controls) {
|
|
151
|
+
next.push(["aria-controls", semantics.controls]);
|
|
152
|
+
}
|
|
153
|
+
if (semantics.describedby) {
|
|
154
|
+
next.push(["aria-describedby", semantics.describedby]);
|
|
155
|
+
}
|
|
156
|
+
if (semantics.labelledby) {
|
|
157
|
+
next.push(["aria-labelledby", semantics.labelledby]);
|
|
158
|
+
}
|
|
159
|
+
if (semantics.activeDescendant) {
|
|
160
|
+
// Reactive roving-focus pointer — arrives initially on `create` and
|
|
161
|
+
// stays live via `setSemantics` re-emits as the bound state changes.
|
|
162
|
+
next.push(["aria-activedescendant", semantics.activeDescendant]);
|
|
163
|
+
}
|
|
164
|
+
if (semantics.owns) {
|
|
165
|
+
next.push(["aria-owns", semantics.owns]);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (semantics.busy) {
|
|
169
|
+
next.push(["aria-busy", "true"]);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (semantics.live) {
|
|
173
|
+
// Live-region politeness — engine-validated ("polite" | "assertive").
|
|
174
|
+
next.push(["aria-live", semantics.live]);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Clear what the previous pass set but this one doesn't.
|
|
180
|
+
const previous = appliedAttrs.get(element);
|
|
181
|
+
if (previous) {
|
|
182
|
+
const keep = new Set(next.map(([name]) => name));
|
|
183
|
+
for (const name of previous) {
|
|
184
|
+
if (!keep.has(name)) {
|
|
185
|
+
element.removeAttribute(name);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
for (const [name, value] of next) {
|
|
191
|
+
element.setAttribute(name, value);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (next.length > 0) {
|
|
195
|
+
appliedAttrs.set(element, next.map(([name]) => name));
|
|
196
|
+
} else {
|
|
197
|
+
appliedAttrs.delete(element);
|
|
198
|
+
}
|
|
199
|
+
}
|
package/src/variants.ts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared variant (responsive breakpoint + interaction state) support.
|
|
3
|
+
*
|
|
4
|
+
* The engine emits applicator props using the canonical key format:
|
|
5
|
+
*
|
|
6
|
+
* <camelBase><variant?><argSuffix>
|
|
7
|
+
*
|
|
8
|
+
* - camelBase: camelCase applicator name, e.g. "padding", "backgroundColor"
|
|
9
|
+
* - variant?: "@<bp>" and/or ":<state>", in that order if combined.
|
|
10
|
+
* bp in { sm, md, lg, xl, 2xl }
|
|
11
|
+
* state in { hover, focus, active, disabled,
|
|
12
|
+
* focus-visible, focus-within }
|
|
13
|
+
* combined example: "backgroundColor@md:hover" (BOTH md AND hover)
|
|
14
|
+
* - argSuffix: ".<index>" (almost always ".0") or ".<name>" for named args.
|
|
15
|
+
*
|
|
16
|
+
* Examples emitted by the engine:
|
|
17
|
+
* "padding.0", "padding@md.0", "backgroundColor:hover.0",
|
|
18
|
+
* "backgroundColor@md:hover.0".
|
|
19
|
+
*
|
|
20
|
+
* IMPORTANT: the variant marker sits BETWEEN the base name and the ".0" arg
|
|
21
|
+
* suffix. A lookup that forgets the ".0" (e.g. building just "padding@md")
|
|
22
|
+
* MISSES the real key. Always parse the variant out of the full key.
|
|
23
|
+
*
|
|
24
|
+
* Both the DOM applicator path and the Canvas renderer import this module so
|
|
25
|
+
* breakpoint pixels, state names, and precedence stay byte-compatible across
|
|
26
|
+
* renderers. The DOM renderer leans on the CSS engine for actual resolution
|
|
27
|
+
* (media queries + pseudo-classes), so it only needs PARSE_* here; Canvas does
|
|
28
|
+
* its own paint/layout and therefore also uses the runtime resolver.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Tailwind breakpoint min-widths (px). Order matters: ascending min-width.
|
|
33
|
+
* Mirrors the DOM `BREAKPOINTS` table and the iOS reference.
|
|
34
|
+
*/
|
|
35
|
+
export const BREAKPOINTS: Record<string, number> = {
|
|
36
|
+
sm: 640,
|
|
37
|
+
md: 768,
|
|
38
|
+
lg: 1024,
|
|
39
|
+
xl: 1280,
|
|
40
|
+
"2xl": 1536,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** Breakpoint names in ascending min-width order (sm < md < lg < xl < 2xl). */
|
|
44
|
+
export const BREAKPOINT_ORDER: readonly string[] = ["sm", "md", "lg", "xl", "2xl"];
|
|
45
|
+
|
|
46
|
+
/** Interaction-state names recognised in variant keys (same set as the DOM). */
|
|
47
|
+
export const VALID_STATES: readonly string[] = [
|
|
48
|
+
"hover",
|
|
49
|
+
"focus",
|
|
50
|
+
"active",
|
|
51
|
+
"disabled",
|
|
52
|
+
"focus-visible",
|
|
53
|
+
"focus-within",
|
|
54
|
+
];
|
|
55
|
+
const VALID_STATE_SET = new Set(VALID_STATES);
|
|
56
|
+
|
|
57
|
+
/** Parsed shape of a single prop key. */
|
|
58
|
+
export interface ParsedVariantKey {
|
|
59
|
+
/** camelCase base applicator name, e.g. "backgroundColor". */
|
|
60
|
+
base: string;
|
|
61
|
+
/** Breakpoint name (sm/md/lg/xl/2xl) or null if none. */
|
|
62
|
+
breakpoint: string | null;
|
|
63
|
+
/** Interaction state name or null if none. */
|
|
64
|
+
state: string | null;
|
|
65
|
+
/** Arg suffix without the leading dot (e.g. "0", "to"), or null if none. */
|
|
66
|
+
arg: string | null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Parse a prop key into `{ base, breakpoint, state, arg }`.
|
|
71
|
+
*
|
|
72
|
+
* Splits the arg suffix first (the LAST `.<token>`), then peels the variant
|
|
73
|
+
* markers (`@bp` and/or `:state`) off the remainder. An unknown breakpoint or
|
|
74
|
+
* state is left as part of the base name (caller decides what to do); this
|
|
75
|
+
* matches the DOM behaviour where an invalid `@invalid` / `:invalid` simply
|
|
76
|
+
* never matches a real variant.
|
|
77
|
+
*
|
|
78
|
+
* Examples:
|
|
79
|
+
* "padding.0" -> { base:"padding", breakpoint:null, state:null, arg:"0" }
|
|
80
|
+
* "padding@md.0" -> { base:"padding", breakpoint:"md", state:null, arg:"0" }
|
|
81
|
+
* "backgroundColor:hover.0" -> { base:"backgroundColor", breakpoint:null, state:"hover", arg:"0" }
|
|
82
|
+
* "backgroundColor@md:hover.0" -> { base:"backgroundColor", breakpoint:"md", state:"hover", arg:"0" }
|
|
83
|
+
* "backgroundColor@md:hover" -> { base:"backgroundColor", breakpoint:"md", state:"hover", arg:null }
|
|
84
|
+
*/
|
|
85
|
+
export function parseVariantKey(key: string): ParsedVariantKey {
|
|
86
|
+
let work = key;
|
|
87
|
+
let arg: string | null = null;
|
|
88
|
+
|
|
89
|
+
// Peel the arg suffix: the segment after the LAST dot, but only if that dot
|
|
90
|
+
// comes AFTER any variant markers (so we never split inside "focus-within"
|
|
91
|
+
// — that has no dot — and never treat "padding" alone as having an arg).
|
|
92
|
+
const lastDot = work.lastIndexOf(".");
|
|
93
|
+
if (lastDot > 0) {
|
|
94
|
+
arg = work.slice(lastDot + 1);
|
|
95
|
+
work = work.slice(0, lastDot);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Peel the state marker (":state") off the end of the remainder.
|
|
99
|
+
let state: string | null = null;
|
|
100
|
+
const colon = work.indexOf(":");
|
|
101
|
+
if (colon !== -1) {
|
|
102
|
+
const candidate = work.slice(colon + 1);
|
|
103
|
+
if (VALID_STATE_SET.has(candidate)) {
|
|
104
|
+
state = candidate;
|
|
105
|
+
work = work.slice(0, colon);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Peel the breakpoint marker ("@bp").
|
|
110
|
+
let breakpoint: string | null = null;
|
|
111
|
+
const at = work.indexOf("@");
|
|
112
|
+
if (at !== -1) {
|
|
113
|
+
const candidate = work.slice(at + 1);
|
|
114
|
+
if (candidate in BREAKPOINTS) {
|
|
115
|
+
breakpoint = candidate;
|
|
116
|
+
work = work.slice(0, at);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return { base: work, breakpoint, state, arg };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The set of interaction states currently active on a node. */
|
|
124
|
+
export interface ActiveStates {
|
|
125
|
+
hover?: boolean;
|
|
126
|
+
focus?: boolean;
|
|
127
|
+
active?: boolean;
|
|
128
|
+
disabled?: boolean;
|
|
129
|
+
"focus-visible"?: boolean;
|
|
130
|
+
"focus-within"?: boolean;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Precedence rank for a parsed variant (higher wins). Combines the breakpoint
|
|
135
|
+
* tier and the state tier into a single sortable number.
|
|
136
|
+
*
|
|
137
|
+
* Precedence (low -> high), later overrides earlier:
|
|
138
|
+
* base
|
|
139
|
+
* < breakpoints ascending (sm < md < lg < xl < 2xl)
|
|
140
|
+
* < disabled < hover < focus < active
|
|
141
|
+
*
|
|
142
|
+
* State always outranks any breakpoint (a `:hover` override beats a `@2xl`
|
|
143
|
+
* sizing override on the same prop), matching the iOS reference where state
|
|
144
|
+
* overrides are merged AFTER the responsive base. Within the same state tier,
|
|
145
|
+
* a higher breakpoint still breaks the tie so `@md:hover` beats `:hover`.
|
|
146
|
+
*/
|
|
147
|
+
const STATE_RANK: Record<string, number> = {
|
|
148
|
+
disabled: 1,
|
|
149
|
+
hover: 2,
|
|
150
|
+
// focus-visible / focus-within are focus-flavoured states: they share the
|
|
151
|
+
// `focus` precedence slot (NOT base/0) so they outrank hover and lose to
|
|
152
|
+
// active, matching the engine's `state_rank` in portable/variant.rs.
|
|
153
|
+
focus: 3,
|
|
154
|
+
"focus-visible": 3,
|
|
155
|
+
"focus-within": 3,
|
|
156
|
+
active: 4,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// State tier must be weighted by a value strictly greater than the largest
|
|
160
|
+
// possible breakpoint tier so the bp tiebreak can never leak across state
|
|
161
|
+
// bands (mirrors the engine's STATE_STEP > max-breakpoint invariant). Derived
|
|
162
|
+
// from BREAKPOINT_ORDER.length so adding breakpoints can't silently break it.
|
|
163
|
+
const STATE_WEIGHT = BREAKPOINT_ORDER.length + 1;
|
|
164
|
+
|
|
165
|
+
function variantRank(breakpoint: string | null, state: string | null): number {
|
|
166
|
+
const bpTier = breakpoint ? BREAKPOINT_ORDER.indexOf(breakpoint) + 1 : 0; // 0..len
|
|
167
|
+
const stateTier = state ? STATE_RANK[state] ?? 0 : 0; // 0..4
|
|
168
|
+
// State tier dominates; breakpoint tier breaks ties within a state tier.
|
|
169
|
+
return stateTier * STATE_WEIGHT + bpTier;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Is a parsed variant currently applicable, given the viewport width and the
|
|
174
|
+
* active interaction states? A combined key requires BOTH conditions.
|
|
175
|
+
*/
|
|
176
|
+
function variantApplies(
|
|
177
|
+
parsed: ParsedVariantKey,
|
|
178
|
+
width: number,
|
|
179
|
+
states: ActiveStates,
|
|
180
|
+
): boolean {
|
|
181
|
+
if (parsed.breakpoint) {
|
|
182
|
+
const minWidth = BREAKPOINTS[parsed.breakpoint];
|
|
183
|
+
if (minWidth === undefined || width < minWidth) return false;
|
|
184
|
+
}
|
|
185
|
+
if (parsed.state) {
|
|
186
|
+
if (!states[parsed.state as keyof ActiveStates]) return false;
|
|
187
|
+
}
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Resolve variant-bearing props against the current viewport width and active
|
|
193
|
+
* interaction states, producing a flat map of `base -> winning value`.
|
|
194
|
+
*
|
|
195
|
+
* Input keys are the already-normalised flat applicator keys (variant markers
|
|
196
|
+
* present, arg suffix already stripped by the applicator-grouping pass), e.g.
|
|
197
|
+
* `{ padding: 8, "padding@md": 16, "backgroundColor:hover": "red" }`.
|
|
198
|
+
*
|
|
199
|
+
* Only bases that actually have at least one applicable variant appear in the
|
|
200
|
+
* result with an overridden value; a base with only its plain entry resolves to
|
|
201
|
+
* that plain value. The caller merges the result over the node's base props.
|
|
202
|
+
*/
|
|
203
|
+
export function resolveVariantProps(
|
|
204
|
+
props: Record<string, unknown>,
|
|
205
|
+
width: number,
|
|
206
|
+
states: ActiveStates,
|
|
207
|
+
): Record<string, unknown> {
|
|
208
|
+
// base -> { value, rank }
|
|
209
|
+
const winners = new Map<string, { value: unknown; rank: number }>();
|
|
210
|
+
|
|
211
|
+
for (const key of Object.keys(props)) {
|
|
212
|
+
const parsed = parseVariantKey(key);
|
|
213
|
+
// Only consider entries that carry a variant marker. Plain keys (no bp/state)
|
|
214
|
+
// are the base and are handled implicitly via rank 0 below so a base with a
|
|
215
|
+
// single applicable variant still falls back to base when the variant is off.
|
|
216
|
+
if (!parsed.breakpoint && !parsed.state) {
|
|
217
|
+
// Seed the base value at rank 0 so variant lookups have a fallback and so
|
|
218
|
+
// a base that ALSO has variants resolves correctly when none apply.
|
|
219
|
+
const existing = winners.get(parsed.base);
|
|
220
|
+
if (!existing || existing.rank === 0) {
|
|
221
|
+
winners.set(parsed.base, { value: props[key], rank: 0 });
|
|
222
|
+
}
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (!variantApplies(parsed, width, states)) continue;
|
|
227
|
+
|
|
228
|
+
const rank = variantRank(parsed.breakpoint, parsed.state);
|
|
229
|
+
const existing = winners.get(parsed.base);
|
|
230
|
+
if (!existing || rank >= existing.rank) {
|
|
231
|
+
winners.set(parsed.base, { value: props[key], rank });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const out: Record<string, unknown> = {};
|
|
236
|
+
for (const [base, win] of winners) {
|
|
237
|
+
out[base] = win.value;
|
|
238
|
+
}
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** True if any key in `props` carries a variant marker (`@bp` or `:state`). */
|
|
243
|
+
export function hasVariantProps(props: Record<string, unknown>): boolean {
|
|
244
|
+
for (const key of Object.keys(props)) {
|
|
245
|
+
const parsed = parseVariantKey(key);
|
|
246
|
+
if (parsed.breakpoint || parsed.state) return true;
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
package/dist/canvas/input.d.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Input Overlay System
|
|
3
|
-
*
|
|
4
|
-
* Handle text input using DOM overlay elements
|
|
5
|
-
*/
|
|
6
|
-
import type { VirtualNode } from "./types.js";
|
|
7
|
-
/** Minimal engine interface for dispatching bind actions */
|
|
8
|
-
interface BindEngine {
|
|
9
|
-
dispatchAction(name: string, payload?: unknown): void;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Input Overlay Manager
|
|
13
|
-
*/
|
|
14
|
-
export declare class InputOverlay {
|
|
15
|
-
private container;
|
|
16
|
-
private overlay;
|
|
17
|
-
private focusedNode;
|
|
18
|
-
private onChangeCallback;
|
|
19
|
-
private bindPath;
|
|
20
|
-
private engine;
|
|
21
|
-
constructor(container: HTMLElement | null);
|
|
22
|
-
/**
|
|
23
|
-
* Show input overlay for a node.
|
|
24
|
-
*
|
|
25
|
-
* Type comparison is case-insensitive — the engine emits `"Input"` /
|
|
26
|
-
* `"Textarea"`, but the overlay branch was looking for lowercase
|
|
27
|
-
* `"textarea"` and always created an `<input>` (so multi-line typing
|
|
28
|
-
* couldn't enter newlines and the visible caret was the wrong height).
|
|
29
|
-
*/
|
|
30
|
-
showInput(node: VirtualNode, canvasBounds: DOMRect, onChange: (value: string) => void, engine?: BindEngine | null): void;
|
|
31
|
-
/**
|
|
32
|
-
* Hide input overlay
|
|
33
|
-
*/
|
|
34
|
-
hideInput(): void;
|
|
35
|
-
/**
|
|
36
|
-
* Update overlay position
|
|
37
|
-
*/
|
|
38
|
-
updatePosition(node: VirtualNode, canvasBounds: DOMRect): void;
|
|
39
|
-
/**
|
|
40
|
-
* Style overlay to match canvas node
|
|
41
|
-
*/
|
|
42
|
-
private styleOverlay;
|
|
43
|
-
/**
|
|
44
|
-
* Position overlay over canvas node.
|
|
45
|
-
*
|
|
46
|
-
* `bounds` is in canvas-local CSS pixels (the layout coordinate space
|
|
47
|
-
* the renderer paints into after `ctx.scale(dpr, dpr)`). The overlay is
|
|
48
|
-
* mounted on `document.body`, so we add `canvasBounds.left/top`
|
|
49
|
-
* (page coords from `getBoundingClientRect`) to land at the right
|
|
50
|
-
* absolute position regardless of where the canvas sits on the page.
|
|
51
|
-
* Adding `window.scrollX/Y` keeps the overlay pinned to the canvas
|
|
52
|
-
* even when the page scrolls between focus and reposition.
|
|
53
|
-
*/
|
|
54
|
-
private positionOverlay;
|
|
55
|
-
/**
|
|
56
|
-
* Handle input event
|
|
57
|
-
*/
|
|
58
|
-
private onInput;
|
|
59
|
-
/**
|
|
60
|
-
* Handle blur event
|
|
61
|
-
*/
|
|
62
|
-
private onBlur;
|
|
63
|
-
/**
|
|
64
|
-
* Handle keyboard events
|
|
65
|
-
*/
|
|
66
|
-
private onKeyDown;
|
|
67
|
-
/**
|
|
68
|
-
* Check if input is currently shown
|
|
69
|
-
*/
|
|
70
|
-
isShown(): boolean;
|
|
71
|
-
/**
|
|
72
|
-
* Get current focused node
|
|
73
|
-
*/
|
|
74
|
-
getFocusedNode(): VirtualNode | null;
|
|
75
|
-
}
|
|
76
|
-
export {};
|