@zerolith/a11y 0.1.0-alpha.1 → 0.1.0-alpha.10

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 CHANGED
@@ -19,9 +19,15 @@ createAnnouncer().announce("Saved");
19
19
  - `visuallyHidden` is the clip pattern for text meant for assistive technology only.
20
20
  - `mediaQuery`, `forcedColors`, and `reducedMotion` are media queries as signals.
21
21
 
22
- The guide is `docs/guide/accessibility.md` in the repository.
22
+ The guide is on the [docs site](https://zerolith.dev/guide/accessibility/).
23
23
 
24
- Status: pre-alpha, nothing published.
24
+ Status: alpha. Install with `npm install @zerolith/a11y@alpha`; the API may change before 1.0.
25
+
26
+ ## Documentation
27
+
28
+ - The [accessibility guide](https://zerolith.dev/guide/accessibility/) explains it with examples that run as tests.
29
+ - The [reference](https://zerolith.dev/reference/zerolith-a11y/) lists every export, generated from the source.
30
+ - New to zerolith? The [tutorial](https://zerolith.dev/learn/) builds an app step by step, and the [playground](https://zerolith.dev/playground/) runs code in the browser.
25
31
 
26
32
  ## License
27
33
 
@@ -16,7 +16,9 @@ export interface Announcer {
16
16
  }
17
17
  /**
18
18
  * Creates a pair of live regions, one polite and one assertive, appended to
19
- * the document body and hidden visually. Each announcement clears the
19
+ * the document body at once, so assistive technology knows them before the
20
+ * first message, and hidden visually by inline styles, which hold in any
21
+ * document `doc` names. Each announcement clears the
20
22
  * region and writes the message on the next task, so repeating a message
21
23
  * is read again.
22
24
  */
@@ -1 +1 @@
1
- {"version":3,"file":"announce.d.ts","sourceRoot":"","sources":["../src/announce.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,MAAM,EAAO,MAAM,UAAU,CAAC;AAE5C,iGAAiG;AACjG,eAAO,MAAM,cAAc,EAAE,MAE5B,CAAC;AAEF,sCAAsC;AACtC,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,WAAW,CAAC;AAEhD,sCAAsC;AACtC,MAAM,WAAW,SAAS;IAEtB,uFAAuF;IACvF,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IACzD,gCAAgC;IAChC,OAAO,IAAI,IAAI,CAAC;CACnB;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,GAAE,QAAmB,GAAG,SAAS,CAwCnE"}
1
+ {"version":3,"file":"announce.d.ts","sourceRoot":"","sources":["../src/announce.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,MAAM,EAAO,MAAM,UAAU,CAAC;AAE5C,iGAAiG;AACjG,eAAO,MAAM,cAAc,EAAE,MAE5B,CAAC;AAEF,sCAAsC;AACtC,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,WAAW,CAAC;AAEhD,sCAAsC;AACtC,MAAM,WAAW,SAAS;IAEtB,uFAAuF;IACvF,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IACzD,gCAAgC;IAChC,OAAO,IAAI,IAAI,CAAC;CACnB;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,GAAG,GAAE,QAAmB,GAAG,SAAS,CA+CnE"}
package/dist/announce.js CHANGED
@@ -9,7 +9,9 @@ export const visuallyHidden = css `
9
9
  `;
10
10
  /**
11
11
  * Creates a pair of live regions, one polite and one assertive, appended to
12
- * the document body and hidden visually. Each announcement clears the
12
+ * the document body at once, so assistive technology knows them before the
13
+ * first message, and hidden visually by inline styles, which hold in any
14
+ * document `doc` names. Each announcement clears the
13
15
  * region and writes the message on the next task, so repeating a message
14
16
  * is read again.
15
17
  */
@@ -23,23 +25,29 @@ export function createAnnouncer(doc = document) {
23
25
  element.setAttribute("aria-atomic", "true");
24
26
  element.setAttribute("role", politeness === "assertive" ? "alert" : "status");
25
27
  element.className = `zl-s ${visuallyHidden.scope}`;
28
+ element.setAttribute("style", "position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;");
26
29
  doc.body.append(element);
27
30
  regions.set(politeness, element);
28
31
  }
29
32
  return element;
30
33
  };
31
- let timer;
34
+ region("polite");
35
+ region("assertive");
36
+ const timers = new Map();
32
37
  return {
33
38
  announce(message, politeness = "polite") {
34
39
  const element = region(politeness);
35
40
  element.textContent = "";
36
- clearTimeout(timer);
37
- timer = setTimeout(() => {
41
+ clearTimeout(timers.get(politeness));
42
+ timers.set(politeness, setTimeout(() => {
38
43
  element.textContent = message;
39
- }, 0);
44
+ }, 0));
40
45
  },
41
46
  dispose() {
42
- clearTimeout(timer);
47
+ for (const timer of timers.values()) {
48
+ clearTimeout(timer);
49
+ }
50
+ timers.clear();
43
51
  for (const element of regions.values()) {
44
52
  element.remove();
45
53
  }
@@ -1 +1 @@
1
- {"version":3,"file":"announce.js","sourceRoot":"","sources":["../src/announce.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAe,GAAG,EAAE,MAAM,UAAU,CAAC;AAE5C,iGAAiG;AACjG,MAAM,CAAC,MAAM,cAAc,GAAW,GAAG,CAAA;;CAExC,CAAC;AAcF;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,GAAG,GAAa,QAAQ;IAEpD,MAAM,OAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;IACnD,MAAM,MAAM,GAAG,CAAC,UAAsB,EAAe,EAAE;QAEnD,IAAI,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QACtC,IAAI,OAAO,KAAK,SAAS,EACzB,CAAC;YACG,OAAO,GAAG,GAAG,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;YACnC,OAAO,CAAC,YAAY,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;YAC9C,OAAO,CAAC,YAAY,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC;YAC5C,OAAO,CAAC,YAAY,CAAC,MAAM,EAA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C,EAAE,CAAC,CAAC,CAAC,CAAC;QACX,CAAC;QACD,OAAO;YAEH,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,EACnC,CAAC;gBACG,YAAY,CAAC,KAAK,CAAC,CAAC;YACxB,CAAC;YACD,MAAM,CAAC,KAAK,EAAE,CAAC;YACf,KAAK,MAAM,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE,EACtC,CAAC;gBACG,OAAO,CAAC,MAAM,EAAE,CAAC;YACrB,CAAC;YACD,OAAO,CAAC,KAAK,EAAE,CAAC;QACpB,CAAC;KACJ,CAAC;AACN,CAAC"}
package/dist/focus.d.ts CHANGED
@@ -18,6 +18,8 @@ export interface FocusTrapOptions {
18
18
  * A `ref` that keeps Tab and Shift+Tab inside the element, pulls focus back
19
19
  * when it lands outside, moves focus in once the element is in the document,
20
20
  * and returns it where it came from when the element's template is removed.
21
+ * With traps on several elements, as when a dialog opens a confirmation,
22
+ * only the newest acts until its template is removed.
21
23
  */
22
24
  export declare function focusTrap(options?: FocusTrapOptions): (element: Element) => void;
23
25
  /** Options for `rovingTabindex`. */
@@ -32,7 +34,8 @@ export interface RovingOptions {
32
34
  /**
33
35
  * A `ref` for a composite widget: exactly one item is in the Tab order, the
34
36
  * arrow keys, Home, and End move between items, and the item that was
35
- * selected or checked starts as the current one.
37
+ * selected or checked starts as the current one. Disabled, hidden, and
38
+ * inert items are skipped, as the children of a collapsed tree item are.
36
39
  */
37
40
  export declare function rovingTabindex(options?: RovingOptions): (element: Element) => void;
38
41
  /** How the element that has focus got it. */
@@ -1 +1 @@
1
- {"version":3,"file":"focus.d.ts","sourceRoot":"","sources":["../src/focus.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,KAAK,cAAc,EAAqB,MAAM,UAAU,CAAC;AAIlE,mGAAmG;AACnG,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,WAAW,EAAE,CAGvD;AAED,+BAA+B;AAC/B,MAAM,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IACvK,OAAO,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,KAAK,CAAC;IACvC,2FAA2F;IAC3F,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CA8FpF;AAED,oCAAoC;AACpC,MAAM,WAAW,aAAa;IAE1B,uGAAuG;IACvG,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,mHAAmH;IACnH,WAAW,CAAC,EAAE,YAAY,GAAG,UAAU,GAAG,MAAM,CAAC;IACjD,wEAAwE;IACxE,IAAI,CAAC,EAAE,OAAO,CAAC;CAClB;AAID;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,GAAE,aAAkB,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CA6FtF;AAED,6CAA6C;AAC7C,MAAM,MAAM,WAAW,GAAG,UAAU,GAAG,SAAS,GAAG,OAAO,GAAG,SAAS,CAAC;AAEvE,kCAAkC;AAClC,MAAM,WAAW,kBAAkB;IAE/B,wFAAwF;IACxF,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;IACpD,+CAA+C;IAC/C,OAAO,IAAI,IAAI,CAAC;CACnB;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,GAAG,GAAE,QAAmB,GAAG,kBAAkB,CA8CxE"}
package/dist/focus.js CHANGED
@@ -5,15 +5,33 @@
5
5
  * the element is created and clean up when its template is removed.
6
6
  */
7
7
  import { onDispose, signal } from "zerolith";
8
- const FOCUSABLE = "a[href], area[href], button:not([disabled]), input:not([disabled]):not([type=hidden]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"]), [contenteditable=\"true\"], audio[controls], video[controls], details > summary, iframe";
8
+ const FOCUSABLE = "a[href], area[href], button:not(:disabled), input:not(:disabled):not([type=hidden]), select:not(:disabled), textarea:not(:disabled), [tabindex]:not([tabindex=\"-1\"]):not(:disabled), [contenteditable=\"true\"], audio[controls], video[controls], details > summary, iframe";
9
+ /** True when an element is rendered, visible, and outside any `inert` subtree, so it can take focus. */
10
+ function reachable(element) {
11
+ return element.getClientRects().length > 0 && element.closest("[inert]") === null && (element.ownerDocument.defaultView ?? window).getComputedStyle(element).visibility === "visible";
12
+ }
13
+ /** True for a control that uses the arrow keys itself: a text field, a list box, or editable text. */
14
+ function editsText(target) {
15
+ if (!(target instanceof HTMLElement)) {
16
+ return false;
17
+ }
18
+ if (target instanceof HTMLInputElement) {
19
+ return !["button", "checkbox", "color", "file", "hidden", "image", "radio", "range", "reset", "submit"].includes(target.type);
20
+ }
21
+ return target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement || target.isContentEditable;
22
+ }
9
23
  /** The elements inside `root` that take Tab focus, in document order, skipping anything hidden. */
10
24
  export function focusables(root) {
11
- return [...root.querySelectorAll(FOCUSABLE)].filter((element) => element.getClientRects().length > 0 && element.closest("[inert]") === null);
25
+ return [...root.querySelectorAll(FOCUSABLE)].filter(reachable);
12
26
  }
27
+ /** The elements with an attached focus trap, newest last. Only the newest acts, so a dialog opened from another keeps focus, and removing it hands control back. */
28
+ const traps = [];
13
29
  /**
14
30
  * A `ref` that keeps Tab and Shift+Tab inside the element, pulls focus back
15
31
  * when it lands outside, moves focus in once the element is in the document,
16
32
  * and returns it where it came from when the element's template is removed.
33
+ * With traps on several elements, as when a dialog opens a confirmation,
34
+ * only the newest acts until its template is removed.
17
35
  */
18
36
  export function focusTrap(options = {}) {
19
37
  return (element) => {
@@ -30,6 +48,7 @@ export function focusTrap(options = {}) {
30
48
  return element;
31
49
  };
32
50
  let disposed = false;
51
+ traps.push(element);
33
52
  const initial = options.initial;
34
53
  const focusInitial = () => {
35
54
  if (disposed || !element.isConnected || initial === false) {
@@ -48,7 +67,7 @@ export function focusTrap(options = {}) {
48
67
  }
49
68
  const onKeydown = (event) => {
50
69
  const key = event;
51
- if (key.key !== "Tab") {
70
+ if (key.key !== "Tab" || traps.at(-1) !== element) {
52
71
  return;
53
72
  }
54
73
  const list = focusables(element);
@@ -69,7 +88,7 @@ export function focusTrap(options = {}) {
69
88
  }
70
89
  };
71
90
  const onFocusIn = (event) => {
72
- if (!element.contains(event.target)) {
91
+ if (traps.at(-1) === element && !element.contains(event.target)) {
73
92
  fallback().focus();
74
93
  }
75
94
  };
@@ -77,6 +96,10 @@ export function focusTrap(options = {}) {
77
96
  doc.addEventListener("focusin", onFocusIn);
78
97
  onDispose(() => {
79
98
  disposed = true;
99
+ const index = traps.lastIndexOf(element);
100
+ if (index !== -1) {
101
+ traps.splice(index, 1);
102
+ }
80
103
  element.removeEventListener("keydown", onKeydown);
81
104
  doc.removeEventListener("focusin", onFocusIn);
82
105
  if (options.restore !== false && previous !== null && previous.isConnected) {
@@ -89,24 +112,26 @@ const ROVING_ITEMS = "[data-roving], [role=\"tab\"], [role=\"menuitem\"], [role=
89
112
  /**
90
113
  * A `ref` for a composite widget: exactly one item is in the Tab order, the
91
114
  * arrow keys, Home, and End move between items, and the item that was
92
- * selected or checked starts as the current one.
115
+ * selected or checked starts as the current one. Disabled, hidden, and
116
+ * inert items are skipped, as the children of a collapsed tree item are.
93
117
  */
94
118
  export function rovingTabindex(options = {}) {
95
119
  const selector = options.items ?? ROVING_ITEMS;
96
120
  const orientation = options.orientation ?? "both";
97
121
  const loop = options.loop !== false;
98
122
  return (element) => {
99
- const items = () => [...element.querySelectorAll(selector)].filter((item) => !item.hasAttribute("disabled") && item.getAttribute("aria-disabled") !== "true");
123
+ const all = () => [...element.querySelectorAll(selector)];
124
+ const items = () => all().filter((item) => !item.hasAttribute("disabled") && item.getAttribute("aria-disabled") !== "true");
100
125
  let current;
101
126
  const apply = (next) => {
102
127
  const list = items();
103
128
  current = next !== undefined && list.includes(next) ? next : list.find((item) => item.getAttribute("aria-selected") === "true" || item.getAttribute("aria-checked") === "true") ?? list[0];
104
- for (const item of list) {
129
+ for (const item of all()) {
105
130
  item.tabIndex = item === current ? 0 : -1;
106
131
  }
107
132
  };
108
133
  const move = (step, edge) => {
109
- const list = items();
134
+ const list = items().filter(reachable);
110
135
  if (list.length === 0) {
111
136
  return;
112
137
  }
@@ -125,6 +150,9 @@ export function rovingTabindex(options = {}) {
125
150
  };
126
151
  const onKeydown = (event) => {
127
152
  const key = event;
153
+ if (editsText(key.target) && !items().includes(key.target)) {
154
+ return;
155
+ }
128
156
  const horizontal = orientation !== "vertical";
129
157
  const vertical = orientation !== "horizontal";
130
158
  if ((key.key === "ArrowRight" && horizontal) || (key.key === "ArrowDown" && vertical)) {
package/dist/focus.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"focus.js","sourceRoot":"","sources":["../src/focus.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAuB,SAAS,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElE,MAAM,SAAS,GAAG,qQAAqQ,CAAC;AAExR,mGAAmG;AACnG,MAAM,UAAU,UAAU,CAAC,IAAa;IAEpC,OAAO,CAAC,GAAG,IAAI,CAAC,gBAAgB,CAAc,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,IAAI,CAAC,CAAC;AAC9J,CAAC;AAWD;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,OAAO,GAAqB,EAAE;IAEpD,OAAO,CAAC,OAAO,EAAE,EAAE;QAEf,MAAM,GAAG,GAAG,OAAO,CAAC,aAAa,CAAC;QAClC,MAAM,QAAQ,GAAG,GAAG,CAAC,aAAa,YAAY,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC;QACrF,MAAM,QAAQ,GAAG,GAAgB,EAAE;YAE/B,MA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MAAM;QACN,OAAO;YAEH,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,GAAG,CAAC,mBAAmB,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;YACpD,GAAG,CAAC,mBAAmB,CAAC,aAAa,EAAE,aAAa,EAAE,IAAI,CAAC,CAAC;YAC5D,GAAG,CAAC,mBAAmB,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;YACpD,GAAG,CAAC,mBAAmB,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,CAAC,CAAC;YACtD,GAAG,CAAC,eAAe,CAAC,eAAe,CAAC,mBAAmB,CAAC,CAAC;QAC7D,CAAC;KACJ,CAAC;AACN,CAAC"}
@@ -0,0 +1,4 @@
1
+ import{css as e}from"zerolith";export const visuallyHidden=e`
2
+ :scope { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
3
+ `;export function createAnnouncer(e=document){let t=new Map,n=n=>{let r=t.get(n);return r===void 0&&(r=e.createElement(`div`),r.setAttribute(`aria-live`,n),r.setAttribute(`aria-atomic`,`true`),r.setAttribute(`role`,n===`assertive`?`alert`:`status`),r.className=`zl-s ${visuallyHidden.scope}`,r.setAttribute(`style`,`position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;`),e.body.append(r),t.set(n,r)),r};n(`polite`),n(`assertive`);let r=new Map;return{announce(e,t=`polite`){let i=n(t);i.textContent=``,clearTimeout(r.get(t)),r.set(t,setTimeout(()=>{i.textContent=e},0))},dispose(){for(let e of r.values())clearTimeout(e);r.clear();for(let e of t.values())e.remove();t.clear()}}}
4
+ //# sourceMappingURL=announce.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"announce.js","sources":["../../src/announce.ts"],"sourcesContent":["/**\n * The live announcer and the visually hidden style it uses: text for\n * assistive technology that takes no space on screen.\n */\nimport { type Styles, css } from \"zerolith\";\n\n/** The clip pattern that hides an element visually while assistive technology still reads it. */\nexport const visuallyHidden: Styles = css`\n :scope { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }\n`;\n\n/** How urgently a message is read. */\nexport type Politeness = \"polite\" | \"assertive\";\n\n/** What `createAnnouncer` returns. */\nexport interface Announcer\n{\n /** Queues a message for the live region; `assertive` interrupts what is being read. */\n announce(message: string, politeness?: Politeness): void;\n /** Removes the live regions. */\n dispose(): void;\n}\n\n/**\n * Creates a pair of live regions, one polite and one assertive, appended to\n * the document body at once, so assistive technology knows them before the\n * first message, and hidden visually by inline styles, which hold in any\n * document `doc` names. Each announcement clears the\n * region and writes the message on the next task, so repeating a message\n * is read again.\n */\nexport function createAnnouncer(doc: Document = document): Announcer\n{\n const regions = new Map<Politeness, HTMLElement>();\n const region = (politeness: Politeness): HTMLElement =>\n {\n let element = regions.get(politeness);\n if (element === undefined)\n {\n element = doc.createElement(\"div\");\n element.setAttribute(\"aria-live\", politeness);\n element.setAttribute(\"aria-atomic\", \"true\");\n element.setAttribute(\"role\", politeness === \"assertive\" ? \"alert\" : \"status\");\n element.className = `zl-s ${visuallyHidden.scope}`;\n element.setAttribute(\"style\", \"position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;\");\n doc.body.append(element);\n regions.set(politeness, element);\n }\n return element;\n };\n region(\"polite\");\n region(\"assertive\");\n const timers = new Map<Politeness, ReturnType<typeof setTimeout>>();\n return {\n announce(message: string, politeness: Politeness = \"polite\"): void\n {\n const element = region(politeness);\n element.textContent = \"\";\n clearTimeout(timers.get(politeness));\n timers.set(politeness, setTimeout(() =>\n {\n element.textContent = message;\n }, 0));\n },\n dispose(): void\n {\n for (const timer of timers.values())\n {\n clearTimeout(timer);\n }\n timers.clear();\n for (const element of regions.values())\n {\n element.remove();\n }\n regions.clear();\n }\n };\n}\n"],"names":["regions","region","element","politeness","doc","timers","message","timer"],"mappings":"AAIA,OAAsB,aAAW,WAGjC,OAAO,MAAM,eAAyB,CAAG;;EAwBzC,OAAM,SAAU,gBAAgB,EAAgB,SAAQ,CAEpD,IAAMA,EAAU,IAAI,IACdC,EAAU,GAAuC,CAEnD,IAAIC,EAAUF,EAAQ,IAAIG,CAAU,EAYpC,OAXID,IAAY,SAEZ,EAAUE,EAAI,cAAc,KAAK,EACjCF,EAAQ,aAAa,YAAaC,CAAU,EAC5CD,EAAQ,aAAa,cAAe,MAAM,EAC1CA,EAAQ,aAAa,OAAQC,IAAe,YAAc,QAAU,QAAQ,EAC5E,EAAQ,UAAY,QAAQ,eAAe,QAC3CD,EAAQ,aAAa,QAAS,iJAAiJ,EAC/KE,EAAI,KAAK,OAAOF,CAAO,EACvBF,EAAQ,IAAIG,EAAYD,CAAO,GAE5BA,CACX,EACAD,EAAO,QAAQ,EACfA,EAAO,WAAW,EAClB,IAAMI,EAAS,IAAI,IACnB,MAAO,CACH,SAAS,EAAiB,EAAyB,SAAQ,CAEvD,IAAMH,EAAUD,EAAOE,CAAU,EACjC,EAAQ,YAAc,GACtB,aAAaE,EAAO,IAAIF,CAAU,CAAC,EACnCE,EAAO,IAAIF,EAAY,eAAgB,CAEnC,EAAQ,YAAcG,CAC1B,EAAG,CAAC,CAAC,CACT,EACA,SAAO,CAEH,IAAK,IAAMC,KAASF,EAAO,OAAM,EAE7B,aAAaE,CAAK,EAEtBF,EAAO,MAAK,EACZ,IAAK,IAAMH,KAAWF,EAAQ,OAAM,EAEhCE,EAAQ,OAAM,EAElBF,EAAQ,MAAK,CACjB,CACH,CACL"}
@@ -0,0 +1,2 @@
1
+ import{onDispose as e,signal as t}from"zerolith";function n(e){return e.getClientRects().length>0&&e.closest(`[inert]`)===null&&(e.ownerDocument.defaultView??window).getComputedStyle(e).visibility===`visible`}function r(e){return e instanceof HTMLElement?e instanceof HTMLInputElement?![`button`,`checkbox`,`color`,`file`,`hidden`,`image`,`radio`,`range`,`reset`,`submit`].includes(e.type):e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement||e.isContentEditable:!1}export function focusables(e){return[...e.querySelectorAll(`a[href], area[href], button:not(:disabled), input:not(:disabled):not([type=hidden]), select:not(:disabled), textarea:not(:disabled), [tabindex]:not([tabindex="-1"]):not(:disabled), [contenteditable="true"], audio[controls], video[controls], details > summary, iframe`)].filter(n)}const i=[];export function focusTrap(t={}){return n=>{let r=n.ownerDocument,a=r.activeElement instanceof HTMLElement?r.activeElement:null,o=()=>{let e=focusables(n)[0];return e===void 0?(n.hasAttribute(`tabindex`)||n.setAttribute(`tabindex`,`-1`),n):e},s=!1;i.push(n);let c=t.initial,l=()=>{!s&&n.isConnected&&c!==!1&&(typeof c==`string`?n.querySelector(c)??o():c??o()).focus()};c!==!1&&(n.isConnected?l():queueMicrotask(l));let u=e=>{let t=e;if(t.key!==`Tab`||i.at(-1)!==n)return;let a=focusables(n),o=a[0],s=a.at(-1);if(o===void 0||s===void 0){t.preventDefault();return}let c=r.activeElement;t.shiftKey&&(c===o||!n.contains(c))?(t.preventDefault(),s.focus()):t.shiftKey||c!==s&&n.contains(c)||(t.preventDefault(),o.focus())},d=e=>{i.at(-1)===n&&!n.contains(e.target)&&o().focus()};n.addEventListener(`keydown`,u),r.addEventListener(`focusin`,d),e(()=>{s=!0;let e=i.lastIndexOf(n);e!==-1&&i.splice(e,1),n.removeEventListener(`keydown`,u),r.removeEventListener(`focusin`,d),t.restore!==!1&&a!==null&&a.isConnected&&a.focus()})}}export function rovingTabindex(t={}){let i=t.items??`[data-roving], [role="tab"], [role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"], [role="option"], [role="radio"], [role="treeitem"], [role="gridcell"]`,a=t.orientation??`both`,o=t.loop!==!1;return t=>{let s=()=>[...t.querySelectorAll(i)],c=()=>s().filter(e=>!e.hasAttribute(`disabled`)&&e.getAttribute(`aria-disabled`)!==`true`),l,u=e=>{let t=c();l=e!==void 0&&t.includes(e)?e:t.find(e=>e.getAttribute(`aria-selected`)===`true`||e.getAttribute(`aria-checked`)===`true`)??t[0];for(let e of s())e.tabIndex=e===l?0:-1},d=(e,t)=>{let r=c().filter(n);if(r.length===0)return;let i=t===`first`?0:t===`last`?r.length-1:r.indexOf(l??r[0])+e;if(i<0||i>=r.length){if(!o)return;i=(i+r.length)%r.length}let a=r[i];a!==void 0&&(u(a),a.focus())},f=e=>{let t=e;if(r(t.target)&&!c().includes(t.target))return;let n=a!==`vertical`,i=a!==`horizontal`;if(t.key===`ArrowRight`&&n||t.key===`ArrowDown`&&i)d(1);else if(t.key===`ArrowLeft`&&n||t.key===`ArrowUp`&&i)d(-1);else if(t.key===`Home`)d(0,`first`);else if(t.key===`End`)d(0,`last`);else return;t.preventDefault()},p=e=>{let n=e.target.closest(i);n!==null&&t.contains(n)&&u(n)};u(void 0);let m=new MutationObserver(()=>u(l));m.observe(t,{childList:!0,subtree:!0}),t.addEventListener(`keydown`,f),t.addEventListener(`focusin`,p),e(()=>{m.disconnect(),t.removeEventListener(`keydown`,f),t.removeEventListener(`focusin`,p)})}}export function focusOrigin(e=document){let n=t(null),r,i,a=e=>{r=e,clearTimeout(i),i=setTimeout(()=>{r=void 0},650)},o=()=>a(`keyboard`),s=e=>a(e.pointerType===`touch`?`touch`:`pointer`),c=()=>{let t=r??`program`;n.set(t),e.documentElement.setAttribute(`data-focus-origin`,t)},l=t=>{t.relatedTarget===null&&(n.set(null),e.documentElement.removeAttribute(`data-focus-origin`))};return e.addEventListener(`keydown`,o,!0),e.addEventListener(`pointerdown`,s,!0),e.addEventListener(`focusin`,c,!0),e.addEventListener(`focusout`,l,!0),{origin:n,dispose(){clearTimeout(i),e.removeEventListener(`keydown`,o,!0),e.removeEventListener(`pointerdown`,s,!0),e.removeEventListener(`focusin`,c,!0),e.removeEventListener(`focusout`,l,!0),e.documentElement.removeAttribute(`data-focus-origin`)}}}
2
+ //# sourceMappingURL=focus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"focus.js","sources":["../../src/focus.ts"],"sourcesContent":["/**\n * Focus management: the trap a dialog needs, the roving tabindex a composite\n * widget needs, and a monitor that tells keyboard focus from pointer focus.\n * The trap and the roving tabindex are `ref` callbacks, so they attach when\n * the element is created and clean up when its template is removed.\n */\nimport { type ReadableSignal, onDispose, signal } from \"zerolith\";\n\nconst FOCUSABLE = \"a[href], area[href], button:not(:disabled), input:not(:disabled):not([type=hidden]), select:not(:disabled), textarea:not(:disabled), [tabindex]:not([tabindex=\\\"-1\\\"]):not(:disabled), [contenteditable=\\\"true\\\"], audio[controls], video[controls], details > summary, iframe\";\n\n/** True when an element is rendered, visible, and outside any `inert` subtree, so it can take focus. */\nfunction reachable(element: Element): boolean\n{\n return element.getClientRects().length > 0 && element.closest(\"[inert]\") === null && (element.ownerDocument.defaultView ?? window).getComputedStyle(element).visibility === \"visible\";\n}\n\n/** True for a control that uses the arrow keys itself: a text field, a list box, or editable text. */\nfunction editsText(target: EventTarget | null): boolean\n{\n if (!(target instanceof HTMLElement))\n {\n return false;\n }\n if (target instanceof HTMLInputElement)\n {\n return ![\"button\", \"checkbox\", \"color\", \"file\", \"hidden\", \"image\", \"radio\", \"range\", \"reset\", \"submit\"].includes(target.type);\n }\n return target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement || target.isContentEditable;\n}\n\n/** The elements inside `root` that take Tab focus, in document order, skipping anything hidden. */\nexport function focusables(root: Element): HTMLElement[]\n{\n return [...root.querySelectorAll<HTMLElement>(FOCUSABLE)].filter(reachable);\n}\n\n/** The elements with an attached focus trap, newest last. Only the newest acts, so a dialog opened from another keeps focus, and removing it hands control back. */\nconst traps: Element[] = [];\n\n/** Options for `focusTrap`. */\nexport interface FocusTrapOptions\n{\n /** What receives focus on attach: a selector inside the element, an element, or false to leave focus alone. Defaults to the first focusable, or the element itself. */\n initial?: string | HTMLElement | false;\n /** Returns focus to the element that had it before the trap attached. Defaults to true. */\n restore?: boolean;\n}\n\n/**\n * A `ref` that keeps Tab and Shift+Tab inside the element, pulls focus back\n * when it lands outside, moves focus in once the element is in the document,\n * and returns it where it came from when the element's template is removed.\n * With traps on several elements, as when a dialog opens a confirmation,\n * only the newest acts until its template is removed.\n */\nexport function focusTrap(options: FocusTrapOptions = {}): (element: Element) => void\n{\n return (element) =>\n {\n const doc = element.ownerDocument;\n const previous = doc.activeElement instanceof HTMLElement ? doc.activeElement : null;\n const fallback = (): HTMLElement =>\n {\n const first = focusables(element)[0];\n if (first !== undefined)\n {\n return first;\n }\n if (!element.hasAttribute(\"tabindex\"))\n {\n element.setAttribute(\"tabindex\", \"-1\");\n }\n return element as HTMLElement;\n };\n let disposed = false;\n traps.push(element);\n const initial = options.initial;\n const focusInitial = (): void =>\n {\n if (disposed || !element.isConnected || initial === false)\n {\n return;\n }\n const target = typeof initial === \"string\" ? element.querySelector<HTMLElement>(initial) ?? fallback() : initial ?? fallback();\n target.focus();\n };\n if (initial !== false)\n {\n if (element.isConnected)\n {\n focusInitial();\n }\n else\n {\n queueMicrotask(focusInitial);\n }\n }\n const onKeydown = (event: Event): void =>\n {\n const key = event as KeyboardEvent;\n if (key.key !== \"Tab\" || traps.at(-1) !== element)\n {\n return;\n }\n const list = focusables(element);\n const first = list[0];\n const last = list.at(-1);\n if (first === undefined || last === undefined)\n {\n key.preventDefault();\n return;\n }\n const active = doc.activeElement;\n if (key.shiftKey && (active === first || !element.contains(active)))\n {\n key.preventDefault();\n last.focus();\n }\n else if (!key.shiftKey && (active === last || !element.contains(active)))\n {\n key.preventDefault();\n first.focus();\n }\n };\n const onFocusIn = (event: Event): void =>\n {\n if (traps.at(-1) === element && !element.contains(event.target as Node))\n {\n fallback().focus();\n }\n };\n element.addEventListener(\"keydown\", onKeydown);\n doc.addEventListener(\"focusin\", onFocusIn);\n onDispose(() =>\n {\n disposed = true;\n const index = traps.lastIndexOf(element);\n if (index !== -1)\n {\n traps.splice(index, 1);\n }\n element.removeEventListener(\"keydown\", onKeydown);\n doc.removeEventListener(\"focusin\", onFocusIn);\n if (options.restore !== false && previous !== null && previous.isConnected)\n {\n previous.focus();\n }\n });\n };\n}\n\n/** Options for `rovingTabindex`. */\nexport interface RovingOptions\n{\n /** The selector of the items. Defaults to the ARIA roles of composite widgets plus `[data-roving]`. */\n items?: string;\n /** Which arrow keys move: `horizontal` (left and right), `vertical` (up and down), or `both`. Defaults to both. */\n orientation?: \"horizontal\" | \"vertical\" | \"both\";\n /** Wraps from the last item to the first and back. Defaults to true. */\n loop?: boolean;\n}\n\nconst ROVING_ITEMS = \"[data-roving], [role=\\\"tab\\\"], [role=\\\"menuitem\\\"], [role=\\\"menuitemcheckbox\\\"], [role=\\\"menuitemradio\\\"], [role=\\\"option\\\"], [role=\\\"radio\\\"], [role=\\\"treeitem\\\"], [role=\\\"gridcell\\\"]\";\n\n/**\n * A `ref` for a composite widget: exactly one item is in the Tab order, the\n * arrow keys, Home, and End move between items, and the item that was\n * selected or checked starts as the current one. Disabled, hidden, and\n * inert items are skipped, as the children of a collapsed tree item are.\n */\nexport function rovingTabindex(options: RovingOptions = {}): (element: Element) => void\n{\n const selector = options.items ?? ROVING_ITEMS;\n const orientation = options.orientation ?? \"both\";\n const loop = options.loop !== false;\n return (element) =>\n {\n const all = (): HTMLElement[] => [...element.querySelectorAll<HTMLElement>(selector)];\n const items = (): HTMLElement[] => all().filter((item) => !item.hasAttribute(\"disabled\") && item.getAttribute(\"aria-disabled\") !== \"true\");\n let current: HTMLElement | undefined;\n const apply = (next: HTMLElement | undefined): void =>\n {\n const list = items();\n current = next !== undefined && list.includes(next) ? next : list.find((item) => item.getAttribute(\"aria-selected\") === \"true\" || item.getAttribute(\"aria-checked\") === \"true\") ?? list[0];\n for (const item of all())\n {\n item.tabIndex = item === current ? 0 : -1;\n }\n };\n const move = (step: number, edge?: \"first\" | \"last\"): void =>\n {\n const list = items().filter(reachable);\n if (list.length === 0)\n {\n return;\n }\n let index = edge === \"first\" ? 0 : edge === \"last\" ? list.length - 1 : list.indexOf(current ?? list[0] as HTMLElement) + step;\n if (index < 0 || index >= list.length)\n {\n if (!loop)\n {\n return;\n }\n index = (index + list.length) % list.length;\n }\n const target = list[index];\n if (target !== undefined)\n {\n apply(target);\n target.focus();\n }\n };\n const onKeydown = (event: Event): void =>\n {\n const key = event as KeyboardEvent;\n if (editsText(key.target) && !items().includes(key.target as HTMLElement))\n {\n return;\n }\n const horizontal = orientation !== \"vertical\";\n const vertical = orientation !== \"horizontal\";\n if ((key.key === \"ArrowRight\" && horizontal) || (key.key === \"ArrowDown\" && vertical))\n {\n move(1);\n }\n else if ((key.key === \"ArrowLeft\" && horizontal) || (key.key === \"ArrowUp\" && vertical))\n {\n move(-1);\n }\n else if (key.key === \"Home\")\n {\n move(0, \"first\");\n }\n else if (key.key === \"End\")\n {\n move(0, \"last\");\n }\n else\n {\n return;\n }\n key.preventDefault();\n };\n const onFocusIn = (event: Event): void =>\n {\n const target = (event.target as Element).closest<HTMLElement>(selector);\n if (target !== null && element.contains(target))\n {\n apply(target);\n }\n };\n apply(undefined);\n const observer = new MutationObserver(() => apply(current));\n observer.observe(element, { childList: true, subtree: true });\n element.addEventListener(\"keydown\", onKeydown);\n element.addEventListener(\"focusin\", onFocusIn);\n onDispose(() =>\n {\n observer.disconnect();\n element.removeEventListener(\"keydown\", onKeydown);\n element.removeEventListener(\"focusin\", onFocusIn);\n });\n };\n}\n\n/** How the element that has focus got it. */\nexport type FocusOrigin = \"keyboard\" | \"pointer\" | \"touch\" | \"program\";\n\n/** What `focusOrigin` returns. */\nexport interface FocusOriginMonitor\n{\n /** The origin of the current focus, or null while nothing in the document has focus. */\n readonly origin: ReadableSignal<FocusOrigin | null>;\n /** Removes the listeners and the attribute. */\n dispose(): void;\n}\n\n/**\n * Watches how focus arrives, the way Angular CDK's focus monitor does: a key\n * pressed before focus moves means keyboard, a pointer pressed means pointer\n * or touch, anything else means program. The origin is a signal and a\n * `data-focus-origin` attribute on the root element, so a stylesheet can\n * show focus rings for keyboard users only: `[data-focus-origin=\"keyboard\"] :focus { outline: ... }`.\n */\nexport function focusOrigin(doc: Document = document): FocusOriginMonitor\n{\n const origin = signal<FocusOrigin | null>(null);\n let last: FocusOrigin | undefined;\n let timer: ReturnType<typeof setTimeout> | undefined;\n const remember = (value: FocusOrigin): void =>\n {\n last = value;\n clearTimeout(timer);\n timer = setTimeout(() =>\n {\n last = undefined;\n }, 650);\n };\n const onKeydown = (): void => remember(\"keyboard\");\n const onPointerdown = (event: Event): void => remember((event as PointerEvent).pointerType === \"touch\" ? \"touch\" : \"pointer\");\n const onFocusIn = (): void =>\n {\n const value = last ?? \"program\";\n origin.set(value);\n doc.documentElement.setAttribute(\"data-focus-origin\", value);\n };\n const onFocusOut = (event: Event): void =>\n {\n if ((event as FocusEvent).relatedTarget === null)\n {\n origin.set(null);\n doc.documentElement.removeAttribute(\"data-focus-origin\");\n }\n };\n doc.addEventListener(\"keydown\", onKeydown, true);\n doc.addEventListener(\"pointerdown\", onPointerdown, true);\n doc.addEventListener(\"focusin\", onFocusIn, true);\n doc.addEventListener(\"focusout\", onFocusOut, true);\n return {\n origin,\n dispose(): void\n {\n clearTimeout(timer);\n doc.removeEventListener(\"keydown\", onKeydown, true);\n doc.removeEventListener(\"pointerdown\", onPointerdown, true);\n doc.removeEventListener(\"focusin\", onFocusIn, true);\n doc.removeEventListener(\"focusout\", onFocusOut, true);\n doc.documentElement.removeAttribute(\"data-focus-origin\");\n }\n };\n}\n"],"names":["reachable","element","editsText","target","root","traps","doc","previous","fallback","first","disposed","initial","options","focusInitial","onKeydown","key","event","list","last","active","onFocusIn","onDispose","index","selector","orientation","loop","all","items","item","current","apply","next","move","edge","step","horizontal","vertical","observer","origin","signal","timer","remember","value","onPointerdown","onFocusOut"],"mappings":"AAMA,OAA8B,eAAW,gBAAc,WAKvD,SAASA,EAAU,EAAgB,CAE/B,OAAOC,EAAQ,eAAc,CAAE,CAAC,OAAS,GAAKA,EAAQ,QAAQ,SAAS,IAAM,OAASA,EAAQ,cAAc,aAAe,OAAM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BAAoB,WAAYsC,EAAY,EAAI,EACpDtC,EAAI,gBAAgB,gBAAgB,mBAAmB,CAC3D,CACH,CACL"}
@@ -0,0 +1,2 @@
1
+ export{focusOrigin,focusTrap,focusables,rovingTabindex}from"./focus.js";export{createAnnouncer,visuallyHidden}from"./announce.js";export{forcedColors,mediaQuery,reducedMotion}from"./media.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["/**\n * @zerolith/a11y: the accessibility primitives every component library\n * rebuilds, as functions over the DOM. The public surface is assembled here.\n */\nexport type { FocusOrigin, FocusOriginMonitor, FocusTrapOptions, RovingOptions } from \"./focus.js\";\nexport { focusOrigin, focusTrap, focusables, rovingTabindex } from \"./focus.js\";\n\nexport type { Announcer, Politeness } from \"./announce.js\";\nexport { createAnnouncer, visuallyHidden } from \"./announce.js\";\n\nexport { forcedColors, mediaQuery, reducedMotion } from \"./media.js\";\n"],"names":[],"mappings":"AAKA,OAAS,YAAa,UAAW,WAAY,mBAAsB,aAGnE,OAAS,gBAAiB,mBAAsB,gBAEhD,OAAS,aAAc,WAAY,kBAAqB"}
@@ -0,0 +1,2 @@
1
+ import{signal as e}from"zerolith";const t=new Map;export function mediaQuery(n){let r=t.get(n);if(r===void 0){if(typeof matchMedia>`u`)r=e(!1);else{let t=matchMedia(n),i=e(t.matches);t.addEventListener(`change`,e=>i.set(e.matches)),r=i}t.set(n,r)}return r}export function forcedColors(){return mediaQuery(`(forced-colors: active)`)}export function reducedMotion(){return mediaQuery(`(prefers-reduced-motion: reduce)`)}
2
+ //# sourceMappingURL=media.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"media.js","sources":["../../src/media.ts"],"sourcesContent":["/**\n * Media queries as signals, for the preferences a component should follow:\n * forced colors and reduced motion first.\n */\nimport { type ReadableSignal, signal } from \"zerolith\";\n\nconst queries = new Map<string, ReadableSignal<boolean>>();\n\n/** A media query as a signal that follows the query's matches; false wherever `matchMedia` does not exist, such as on the server. */\nexport function mediaQuery(query: string): ReadableSignal<boolean>\n{\n let existing = queries.get(query);\n if (existing === undefined)\n {\n if (typeof matchMedia === \"undefined\")\n {\n existing = signal(false);\n }\n else\n {\n const list = matchMedia(query);\n const matches = signal(list.matches);\n list.addEventListener(\"change\", (event) => matches.set(event.matches));\n existing = matches;\n }\n queries.set(query, existing);\n }\n return existing;\n}\n\n/** True while the user agent is in forced colors mode, such as Windows high contrast, where author colors are replaced by system ones. */\nexport function forcedColors(): ReadableSignal<boolean>\n{\n return mediaQuery(\"(forced-colors: active)\");\n}\n\n/** True while the user has asked for reduced motion. */\nexport function reducedMotion(): ReadableSignal<boolean>\n{\n return mediaQuery(\"(prefers-reduced-motion: reduce)\");\n}\n"],"names":["queries","existing","query","signal","list","matches","event"],"mappings":"AAIA,OAA8B,gBAAc,WAE5C,MAAMA,EAAU,IAAI,IAGpB,OAAM,SAAU,WAAW,EAAa,CAEpC,IAAIC,EAAWD,EAAQ,IAAIE,CAAK,EAChC,GAAID,IAAa,OACjB,CACI,GAAI,OAAO,WAAe,IAEtB,EAAWE,EAAO,EAAK,MAG3B,CACI,IAAMC,EAAO,WAAWF,CAAK,EACvBG,EAAUF,EAAOC,EAAK,OAAO,EACnCA,EAAK,iBAAiB,SAAW,GAAUC,EAAQ,IAAIC,EAAM,OAAO,CAAC,EACrE,EAAWD,CACf,CACAL,EAAQ,IAAIE,EAAOD,CAAQ,CAC/B,CACA,OAAOA,CACX,CAGA,OAAM,SAAU,cAAY,CAExB,OAAO,WAAW,yBAAyB,CAC/C,CAGA,OAAM,SAAU,eAAa,CAEzB,OAAO,WAAW,kCAAkC,CACxD"}
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@zerolith/a11y",
3
- "version": "0.1.0-alpha.1",
3
+ "version": "0.1.0-alpha.10",
4
4
  "description": "Accessibility primitives for zerolith: focus trap, roving tabindex, live announcer, focus origin, visually hidden styles, and media query signals, the pieces every component library rebuilds",
5
5
  "license": "Apache-2.0",
6
6
  "author": "molexxxx",
7
- "homepage": "https://github.com/molexxxx/zerolith#readme",
7
+ "homepage": "https://zerolith.dev/",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "git+https://github.com/molexxxx/zerolith.git",
@@ -28,7 +28,8 @@
28
28
  "exports": {
29
29
  ".": {
30
30
  "types": "./dist/index.d.ts",
31
- "default": "./dist/index.js"
31
+ "development": "./dist/index.js",
32
+ "default": "./dist/production/index.js"
32
33
  },
33
34
  "./package.json": "./package.json"
34
35
  },
@@ -39,12 +40,12 @@
39
40
  "README.md"
40
41
  ],
41
42
  "scripts": {
42
- "build": "tsc -p tsconfig.build.json"
43
+ "build": "tsc -p tsconfig.build.json && node ../../scripts/production.ts"
43
44
  },
44
45
  "peerDependencies": {
45
- "zerolith": "^0.1.0-alpha.1"
46
+ "zerolith": "^0.1.0-alpha.10"
46
47
  },
47
48
  "devDependencies": {
48
- "zerolith": "^0.1.0-alpha.1"
49
+ "zerolith": "^0.1.0-alpha.10"
49
50
  }
50
51
  }
package/src/announce.ts CHANGED
@@ -23,7 +23,9 @@ export interface Announcer
23
23
 
24
24
  /**
25
25
  * Creates a pair of live regions, one polite and one assertive, appended to
26
- * the document body and hidden visually. Each announcement clears the
26
+ * the document body at once, so assistive technology knows them before the
27
+ * first message, and hidden visually by inline styles, which hold in any
28
+ * document `doc` names. Each announcement clears the
27
29
  * region and writes the message on the next task, so repeating a message
28
30
  * is read again.
29
31
  */
@@ -40,26 +42,33 @@ export function createAnnouncer(doc: Document = document): Announcer
40
42
  element.setAttribute("aria-atomic", "true");
41
43
  element.setAttribute("role", politeness === "assertive" ? "alert" : "status");
42
44
  element.className = `zl-s ${visuallyHidden.scope}`;
45
+ element.setAttribute("style", "position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;");
43
46
  doc.body.append(element);
44
47
  regions.set(politeness, element);
45
48
  }
46
49
  return element;
47
50
  };
48
- let timer: ReturnType<typeof setTimeout> | undefined;
51
+ region("polite");
52
+ region("assertive");
53
+ const timers = new Map<Politeness, ReturnType<typeof setTimeout>>();
49
54
  return {
50
55
  announce(message: string, politeness: Politeness = "polite"): void
51
56
  {
52
57
  const element = region(politeness);
53
58
  element.textContent = "";
54
- clearTimeout(timer);
55
- timer = setTimeout(() =>
59
+ clearTimeout(timers.get(politeness));
60
+ timers.set(politeness, setTimeout(() =>
56
61
  {
57
62
  element.textContent = message;
58
- }, 0);
63
+ }, 0));
59
64
  },
60
65
  dispose(): void
61
66
  {
62
- clearTimeout(timer);
67
+ for (const timer of timers.values())
68
+ {
69
+ clearTimeout(timer);
70
+ }
71
+ timers.clear();
63
72
  for (const element of regions.values())
64
73
  {
65
74
  element.remove();
package/src/focus.ts CHANGED
@@ -6,14 +6,37 @@
6
6
  */
7
7
  import { type ReadableSignal, onDispose, signal } from "zerolith";
8
8
 
9
- const FOCUSABLE = "a[href], area[href], button:not([disabled]), input:not([disabled]):not([type=hidden]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"]), [contenteditable=\"true\"], audio[controls], video[controls], details > summary, iframe";
9
+ const FOCUSABLE = "a[href], area[href], button:not(:disabled), input:not(:disabled):not([type=hidden]), select:not(:disabled), textarea:not(:disabled), [tabindex]:not([tabindex=\"-1\"]):not(:disabled), [contenteditable=\"true\"], audio[controls], video[controls], details > summary, iframe";
10
+
11
+ /** True when an element is rendered, visible, and outside any `inert` subtree, so it can take focus. */
12
+ function reachable(element: Element): boolean
13
+ {
14
+ return element.getClientRects().length > 0 && element.closest("[inert]") === null && (element.ownerDocument.defaultView ?? window).getComputedStyle(element).visibility === "visible";
15
+ }
16
+
17
+ /** True for a control that uses the arrow keys itself: a text field, a list box, or editable text. */
18
+ function editsText(target: EventTarget | null): boolean
19
+ {
20
+ if (!(target instanceof HTMLElement))
21
+ {
22
+ return false;
23
+ }
24
+ if (target instanceof HTMLInputElement)
25
+ {
26
+ return !["button", "checkbox", "color", "file", "hidden", "image", "radio", "range", "reset", "submit"].includes(target.type);
27
+ }
28
+ return target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement || target.isContentEditable;
29
+ }
10
30
 
11
31
  /** The elements inside `root` that take Tab focus, in document order, skipping anything hidden. */
12
32
  export function focusables(root: Element): HTMLElement[]
13
33
  {
14
- return [...root.querySelectorAll<HTMLElement>(FOCUSABLE)].filter((element) => element.getClientRects().length > 0 && element.closest("[inert]") === null);
34
+ return [...root.querySelectorAll<HTMLElement>(FOCUSABLE)].filter(reachable);
15
35
  }
16
36
 
37
+ /** The elements with an attached focus trap, newest last. Only the newest acts, so a dialog opened from another keeps focus, and removing it hands control back. */
38
+ const traps: Element[] = [];
39
+
17
40
  /** Options for `focusTrap`. */
18
41
  export interface FocusTrapOptions
19
42
  {
@@ -27,6 +50,8 @@ export interface FocusTrapOptions
27
50
  * A `ref` that keeps Tab and Shift+Tab inside the element, pulls focus back
28
51
  * when it lands outside, moves focus in once the element is in the document,
29
52
  * and returns it where it came from when the element's template is removed.
53
+ * With traps on several elements, as when a dialog opens a confirmation,
54
+ * only the newest acts until its template is removed.
30
55
  */
31
56
  export function focusTrap(options: FocusTrapOptions = {}): (element: Element) => void
32
57
  {
@@ -48,6 +73,7 @@ export function focusTrap(options: FocusTrapOptions = {}): (element: Element) =>
48
73
  return element as HTMLElement;
49
74
  };
50
75
  let disposed = false;
76
+ traps.push(element);
51
77
  const initial = options.initial;
52
78
  const focusInitial = (): void =>
53
79
  {
@@ -72,7 +98,7 @@ export function focusTrap(options: FocusTrapOptions = {}): (element: Element) =>
72
98
  const onKeydown = (event: Event): void =>
73
99
  {
74
100
  const key = event as KeyboardEvent;
75
- if (key.key !== "Tab")
101
+ if (key.key !== "Tab" || traps.at(-1) !== element)
76
102
  {
77
103
  return;
78
104
  }
@@ -98,7 +124,7 @@ export function focusTrap(options: FocusTrapOptions = {}): (element: Element) =>
98
124
  };
99
125
  const onFocusIn = (event: Event): void =>
100
126
  {
101
- if (!element.contains(event.target as Node))
127
+ if (traps.at(-1) === element && !element.contains(event.target as Node))
102
128
  {
103
129
  fallback().focus();
104
130
  }
@@ -108,6 +134,11 @@ export function focusTrap(options: FocusTrapOptions = {}): (element: Element) =>
108
134
  onDispose(() =>
109
135
  {
110
136
  disposed = true;
137
+ const index = traps.lastIndexOf(element);
138
+ if (index !== -1)
139
+ {
140
+ traps.splice(index, 1);
141
+ }
111
142
  element.removeEventListener("keydown", onKeydown);
112
143
  doc.removeEventListener("focusin", onFocusIn);
113
144
  if (options.restore !== false && previous !== null && previous.isConnected)
@@ -134,7 +165,8 @@ const ROVING_ITEMS = "[data-roving], [role=\"tab\"], [role=\"menuitem\"], [role=
134
165
  /**
135
166
  * A `ref` for a composite widget: exactly one item is in the Tab order, the
136
167
  * arrow keys, Home, and End move between items, and the item that was
137
- * selected or checked starts as the current one.
168
+ * selected or checked starts as the current one. Disabled, hidden, and
169
+ * inert items are skipped, as the children of a collapsed tree item are.
138
170
  */
139
171
  export function rovingTabindex(options: RovingOptions = {}): (element: Element) => void
140
172
  {
@@ -143,20 +175,21 @@ export function rovingTabindex(options: RovingOptions = {}): (element: Element)
143
175
  const loop = options.loop !== false;
144
176
  return (element) =>
145
177
  {
146
- const items = (): HTMLElement[] => [...element.querySelectorAll<HTMLElement>(selector)].filter((item) => !item.hasAttribute("disabled") && item.getAttribute("aria-disabled") !== "true");
178
+ const all = (): HTMLElement[] => [...element.querySelectorAll<HTMLElement>(selector)];
179
+ const items = (): HTMLElement[] => all().filter((item) => !item.hasAttribute("disabled") && item.getAttribute("aria-disabled") !== "true");
147
180
  let current: HTMLElement | undefined;
148
181
  const apply = (next: HTMLElement | undefined): void =>
149
182
  {
150
183
  const list = items();
151
184
  current = next !== undefined && list.includes(next) ? next : list.find((item) => item.getAttribute("aria-selected") === "true" || item.getAttribute("aria-checked") === "true") ?? list[0];
152
- for (const item of list)
185
+ for (const item of all())
153
186
  {
154
187
  item.tabIndex = item === current ? 0 : -1;
155
188
  }
156
189
  };
157
190
  const move = (step: number, edge?: "first" | "last"): void =>
158
191
  {
159
- const list = items();
192
+ const list = items().filter(reachable);
160
193
  if (list.length === 0)
161
194
  {
162
195
  return;
@@ -180,6 +213,10 @@ export function rovingTabindex(options: RovingOptions = {}): (element: Element)
180
213
  const onKeydown = (event: Event): void =>
181
214
  {
182
215
  const key = event as KeyboardEvent;
216
+ if (editsText(key.target) && !items().includes(key.target as HTMLElement))
217
+ {
218
+ return;
219
+ }
183
220
  const horizontal = orientation !== "vertical";
184
221
  const vertical = orientation !== "horizontal";
185
222
  if ((key.key === "ArrowRight" && horizontal) || (key.key === "ArrowDown" && vertical))