@zerolith/a11y 0.1.0-alpha.9 → 1.0.1
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 +3 -2
- package/dist/focus.d.ts +6 -1
- package/dist/focus.d.ts.map +1 -1
- package/dist/focus.js +24 -5
- package/dist/focus.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/inert.d.ts +22 -0
- package/dist/inert.d.ts.map +1 -0
- package/dist/inert.js +89 -0
- package/dist/inert.js.map +1 -0
- package/dist/production/focus.js +1 -1
- package/dist/production/focus.js.map +1 -1
- package/dist/production/index.js +1 -1
- package/dist/production/index.js.map +1 -1
- package/dist/production/inert.js +2 -0
- package/dist/production/inert.js.map +1 -0
- package/package.json +3 -3
- package/src/focus.ts +30 -5
- package/src/index.ts +2 -0
- package/src/inert.ts +110 -0
package/README.md
CHANGED
|
@@ -12,7 +12,8 @@ html`<span class=${visuallyHidden}>Opens in a new window</span>`;
|
|
|
12
12
|
createAnnouncer().announce("Saved");
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
- `focusTrap` keeps Tab inside an element, moves focus in on attach, and returns it on removal.
|
|
15
|
+
- `focusTrap` keeps Tab inside an element, moves focus in on attach, and returns it on removal; on a native `<dialog>` it acts while the dialog is open.
|
|
16
|
+
- `inertOutside` makes everything outside an element inert, for a drawer or a menu, and returns what undoes it.
|
|
16
17
|
- `rovingTabindex` makes one item of a composite widget tabbable and moves between items with the arrow keys, Home, and End.
|
|
17
18
|
- `createAnnouncer` speaks messages through a live region.
|
|
18
19
|
- `focusOrigin` tells keyboard focus from pointer focus, as a signal and as a `data-focus-origin` attribute on the root element for styling.
|
|
@@ -21,7 +22,7 @@ createAnnouncer().announce("Saved");
|
|
|
21
22
|
|
|
22
23
|
The guide is on the [docs site](https://zerolith.dev/guide/accessibility/).
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
Install with `npm install @zerolith/a11y`. The API is frozen at 1.0: a breaking change comes only in a major, after a deprecation notice, and the [support policy](https://github.com/molexxxx/zerolith/blob/main/docs/policy/support.md) says how long a line gets fixes.
|
|
25
26
|
|
|
26
27
|
## Documentation
|
|
27
28
|
|
package/dist/focus.d.ts
CHANGED
|
@@ -19,7 +19,12 @@ export interface FocusTrapOptions {
|
|
|
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
21
|
* With traps on several elements, as when a dialog opens a confirmation,
|
|
22
|
-
* only the newest acts until its template is removed.
|
|
22
|
+
* only the newest acts until its template is removed. A trap acts while
|
|
23
|
+
* its element is rendered: a closed native `<dialog>`, or a drawer that is
|
|
24
|
+
* `hidden`, neither acts nor stands in the way of an older trap, and its
|
|
25
|
+
* initial focus waits until the element is rendered as well, while
|
|
26
|
+
* `restore` runs when the template is removed; `showModal()` makes the
|
|
27
|
+
* page behind a dialog inert itself and moves focus in and back on its own.
|
|
23
28
|
*/
|
|
24
29
|
export declare function focusTrap(options?: FocusTrapOptions): (element: Element) => void;
|
|
25
30
|
/** Options for `rovingTabindex`. */
|
package/dist/focus.d.ts.map
CHANGED
|
@@ -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;AAwBlE,mGAAmG;AACnG,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,WAAW,EAAE,CAGvD;
|
|
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;AAwBlE,mGAAmG;AACnG,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,WAAW,EAAE,CAGvD;AAyBD,+BAA+B;AAC/B,MAAM,WAAW,gBAAgB;IAE7B,uKAAuK;IACvK,OAAO,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,KAAK,CAAC;IACvC,2FAA2F;IAC3F,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;;;;;;;;;GAWG;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
|
@@ -24,14 +24,33 @@ function editsText(target) {
|
|
|
24
24
|
export function focusables(root) {
|
|
25
25
|
return [...root.querySelectorAll(FOCUSABLE)].filter(reachable);
|
|
26
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. */
|
|
27
|
+
/** The elements with an attached focus trap, newest last. Only the newest that holds focus acts, so a dialog opened from another keeps focus, and removing or closing it hands control back. */
|
|
28
28
|
const traps = [];
|
|
29
|
+
/** True while a trapped element can hold focus: it is in the document and rendered, which a closed `<dialog>`, a `hidden` drawer, or anything under `display: none` is not. */
|
|
30
|
+
function holds(element) {
|
|
31
|
+
return element.isConnected && element.getClientRects().length > 0;
|
|
32
|
+
}
|
|
33
|
+
/** The trap that acts: the newest whose element holds focus. */
|
|
34
|
+
function acting() {
|
|
35
|
+
for (let index = traps.length - 1; index >= 0; index--) {
|
|
36
|
+
const element = traps[index];
|
|
37
|
+
if (holds(element)) {
|
|
38
|
+
return element;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
29
43
|
/**
|
|
30
44
|
* A `ref` that keeps Tab and Shift+Tab inside the element, pulls focus back
|
|
31
45
|
* when it lands outside, moves focus in once the element is in the document,
|
|
32
46
|
* and returns it where it came from when the element's template is removed.
|
|
33
47
|
* With traps on several elements, as when a dialog opens a confirmation,
|
|
34
|
-
* only the newest acts until its template is removed.
|
|
48
|
+
* only the newest acts until its template is removed. A trap acts while
|
|
49
|
+
* its element is rendered: a closed native `<dialog>`, or a drawer that is
|
|
50
|
+
* `hidden`, neither acts nor stands in the way of an older trap, and its
|
|
51
|
+
* initial focus waits until the element is rendered as well, while
|
|
52
|
+
* `restore` runs when the template is removed; `showModal()` makes the
|
|
53
|
+
* page behind a dialog inert itself and moves focus in and back on its own.
|
|
35
54
|
*/
|
|
36
55
|
export function focusTrap(options = {}) {
|
|
37
56
|
return (element) => {
|
|
@@ -51,7 +70,7 @@ export function focusTrap(options = {}) {
|
|
|
51
70
|
traps.push(element);
|
|
52
71
|
const initial = options.initial;
|
|
53
72
|
const focusInitial = () => {
|
|
54
|
-
if (disposed || !element
|
|
73
|
+
if (disposed || !holds(element) || initial === false) {
|
|
55
74
|
return;
|
|
56
75
|
}
|
|
57
76
|
const target = typeof initial === "string" ? element.querySelector(initial) ?? fallback() : initial ?? fallback();
|
|
@@ -67,7 +86,7 @@ export function focusTrap(options = {}) {
|
|
|
67
86
|
}
|
|
68
87
|
const onKeydown = (event) => {
|
|
69
88
|
const key = event;
|
|
70
|
-
if (key.key !== "Tab" ||
|
|
89
|
+
if (key.key !== "Tab" || acting() !== element) {
|
|
71
90
|
return;
|
|
72
91
|
}
|
|
73
92
|
const list = focusables(element);
|
|
@@ -88,7 +107,7 @@ export function focusTrap(options = {}) {
|
|
|
88
107
|
}
|
|
89
108
|
};
|
|
90
109
|
const onFocusIn = (event) => {
|
|
91
|
-
if (
|
|
110
|
+
if (acting() === element && !element.contains(event.target)) {
|
|
92
111
|
fallback().focus();
|
|
93
112
|
}
|
|
94
113
|
};
|
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,gRAAgR,CAAC;AAEnS,wGAAwG;AACxG,SAAS,SAAS,CAAC,OAAgB;IAE/B,OAAO,OAAO,CAAC,cAAc,EAAE,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,IAAI,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,WAAW,IAAI,MAAM,CAAC,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,UAAU,KAAK,SAAS,CAAC;AAC1L,CAAC;AAED,sGAAsG;AACtG,SAAS,SAAS,CAAC,MAA0B;IAEzC,IAAI,CAAC,CAAC,MAAM,YAAY,WAAW,CAAC,EACpC,CAAC;QACG,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,IAAI,MAAM,YAAY,gBAAgB,EACtC,CAAC;QACG,OAAO,CAAC,CAAC,QAAQ,EAAE,UAAU,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,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"}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export type { FocusOrigin, FocusOriginMonitor, FocusTrapOptions, RovingOptions } from "./focus.js";
|
|
6
6
|
export { focusOrigin, focusTrap, focusables, rovingTabindex } from "./focus.js";
|
|
7
|
+
export { inertOutside } from "./inert.js";
|
|
7
8
|
export type { Announcer, Politeness } from "./announce.js";
|
|
8
9
|
export { createAnnouncer, visuallyHidden } from "./announce.js";
|
|
9
10
|
export { forcedColors, mediaQuery, reducedMotion } from "./media.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACnG,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEhF,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEhE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACnG,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEhF,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEhE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { focusOrigin, focusTrap, focusables, rovingTabindex } from "./focus.js";
|
|
2
|
+
export { inertOutside } from "./inert.js";
|
|
2
3
|
export { createAnnouncer, visuallyHidden } from "./announce.js";
|
|
3
4
|
export { forcedColors, mediaQuery, reducedMotion } from "./media.js";
|
|
4
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEhF,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG1C,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEhE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/inert.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything outside one element made inert, for a drawer, a menu, or a
|
|
3
|
+
* dialog that is not a native modal: the page behind it takes no focus,
|
|
4
|
+
* no clicks, and no reading by assistive technology until it is given
|
|
5
|
+
* back.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Makes everything outside `element` inert and returns what undoes it: the
|
|
9
|
+
* siblings of the element and of each of its ancestors up to the body get
|
|
10
|
+
* the `inert` attribute, through a shadow tree's host where the element is
|
|
11
|
+
* in one, and the returned function takes it back. An element inert on its
|
|
12
|
+
* own is left as it is, and one two calls hold stays inert until the last
|
|
13
|
+
* lets go, so two drawers at once undo only what each added. Returned
|
|
14
|
+
* from an effect that reads the open state, it holds while the drawer is
|
|
15
|
+
* open and lets go when it closes or the effect is disposed. An element
|
|
16
|
+
* not yet in the page is held once it is, by the next microtask, as a
|
|
17
|
+
* `ref` creates it before its template is inserted. A native `<dialog>`
|
|
18
|
+
* opened with `showModal()` makes the page behind it inert itself and
|
|
19
|
+
* needs none of this.
|
|
20
|
+
*/
|
|
21
|
+
export declare function inertOutside(element: Element): () => void;
|
|
22
|
+
//# sourceMappingURL=inert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inert.d.ts","sourceRoot":"","sources":["../src/inert.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAwBH;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,IAAI,CAkEzD"}
|
package/dist/inert.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything outside one element made inert, for a drawer, a menu, or a
|
|
3
|
+
* dialog that is not a native modal: the page behind it takes no focus,
|
|
4
|
+
* no clicks, and no reading by assistive technology until it is given
|
|
5
|
+
* back.
|
|
6
|
+
*/
|
|
7
|
+
/** How many holds each element is under, so the attribute leaves only with the last; an element inert on its own, with the attribute and no hold, is never touched. */
|
|
8
|
+
const holds = new WeakMap();
|
|
9
|
+
/** The siblings of `node` and the element the walk continues from: its parent element, or for a node at the top of a shadow tree the tree's host, whose siblings come next. */
|
|
10
|
+
function above(node) {
|
|
11
|
+
const parent = node.parentNode;
|
|
12
|
+
if (parent === null) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
if (parent.nodeType === 1) {
|
|
16
|
+
return { siblings: parent.children, next: parent };
|
|
17
|
+
}
|
|
18
|
+
if (parent.nodeType === 11 && "host" in parent) {
|
|
19
|
+
return { siblings: parent.children, next: parent.host };
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Makes everything outside `element` inert and returns what undoes it: the
|
|
25
|
+
* siblings of the element and of each of its ancestors up to the body get
|
|
26
|
+
* the `inert` attribute, through a shadow tree's host where the element is
|
|
27
|
+
* in one, and the returned function takes it back. An element inert on its
|
|
28
|
+
* own is left as it is, and one two calls hold stays inert until the last
|
|
29
|
+
* lets go, so two drawers at once undo only what each added. Returned
|
|
30
|
+
* from an effect that reads the open state, it holds while the drawer is
|
|
31
|
+
* open and lets go when it closes or the effect is disposed. An element
|
|
32
|
+
* not yet in the page is held once it is, by the next microtask, as a
|
|
33
|
+
* `ref` creates it before its template is inserted. A native `<dialog>`
|
|
34
|
+
* opened with `showModal()` makes the page behind it inert itself and
|
|
35
|
+
* needs none of this.
|
|
36
|
+
*/
|
|
37
|
+
export function inertOutside(element) {
|
|
38
|
+
const held = [];
|
|
39
|
+
let released = false;
|
|
40
|
+
const hold = () => {
|
|
41
|
+
if (released || !element.isConnected) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const body = element.ownerDocument.body;
|
|
45
|
+
let node = element;
|
|
46
|
+
while (node !== body) {
|
|
47
|
+
const step = above(node);
|
|
48
|
+
if (step === null) {
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
for (const sibling of step.siblings) {
|
|
52
|
+
if (sibling === node) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const count = holds.get(sibling) ?? 0;
|
|
56
|
+
if (count === 0) {
|
|
57
|
+
if (sibling.hasAttribute("inert")) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
sibling.setAttribute("inert", "");
|
|
61
|
+
}
|
|
62
|
+
holds.set(sibling, count + 1);
|
|
63
|
+
held.push(sibling);
|
|
64
|
+
}
|
|
65
|
+
node = step.next;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
if (element.isConnected) {
|
|
69
|
+
hold();
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
queueMicrotask(hold);
|
|
73
|
+
}
|
|
74
|
+
return () => {
|
|
75
|
+
released = true;
|
|
76
|
+
for (const sibling of held) {
|
|
77
|
+
const count = (holds.get(sibling) ?? 1) - 1;
|
|
78
|
+
if (count === 0) {
|
|
79
|
+
holds.delete(sibling);
|
|
80
|
+
sibling.removeAttribute("inert");
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
holds.set(sibling, count);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
held.length = 0;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=inert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inert.js","sourceRoot":"","sources":["../src/inert.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,uKAAuK;AACvK,MAAM,KAAK,GAAG,IAAI,OAAO,EAAmB,CAAC;AAE7C,+KAA+K;AAC/K,SAAS,KAAK,CAAC,IAAU;IAErB,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC;IAC/B,IAAI,MAAM,KAAK,IAAI,EACnB,CAAC;QACG,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,IAAI,MAAM,CAAC,QAAQ,KAAK,CAAC,E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CAAC,CAAC;gBACtC,CAAC;gBACD,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;gBAC9B,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACvB,CAAC;YACD,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACrB,CAAC;IACL,CAAC,CAAC;IACF,IAAI,OAAO,CAAC,WAAW,EACvB,CAAC;QACG,IAAI,EAAE,CAAC;IACX,CAAC;SAED,CAAC;QACG,cAAc,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IACD,OAAO,GAAG,EAAE;QAER,QAAQ,GAAG,IAAI,CAAC;QAChB,KAAK,MAAM,OAAO,IAAI,IAAI,EAC1B,CAAC;YACG,MAAM,KAAK,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;YAC5C,IAAI,KAAK,KAAK,CAAC,EACf,CAAC;gBACG,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;gBACtB,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;YACrC,CAAC;iBAED,CAAC;gBACG,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC9B,CAAC;QACL,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IACpB,CAAC,CAAC;AACN,CAAC"}
|
package/dist/production/focus.js
CHANGED
|
@@ -1,2 +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,
|
|
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=[];function a(e){return e.isConnected&&e.getClientRects().length>0}function o(){for(let e=i.length-1;e>=0;e--){let t=i[e];if(a(t))return t}}export function focusTrap(t={}){return n=>{let r=n.ownerDocument,s=r.activeElement instanceof HTMLElement?r.activeElement:null,c=()=>{let e=focusables(n)[0];return e===void 0?(n.hasAttribute(`tabindex`)||n.setAttribute(`tabindex`,`-1`),n):e},l=!1;i.push(n);let u=t.initial,d=()=>{!l&&a(n)&&u!==!1&&(typeof u==`string`?n.querySelector(u)??c():u??c()).focus()};u!==!1&&(n.isConnected?d():queueMicrotask(d));let f=e=>{let t=e;if(t.key!==`Tab`||o()!==n)return;let i=focusables(n),a=i[0],s=i.at(-1);if(a===void 0||s===void 0){t.preventDefault();return}let c=r.activeElement;t.shiftKey&&(c===a||!n.contains(c))?(t.preventDefault(),s.focus()):t.shiftKey||c!==s&&n.contains(c)||(t.preventDefault(),a.focus())},p=e=>{o()===n&&!n.contains(e.target)&&c().focus()};n.addEventListener(`keydown`,f),r.addEventListener(`focusin`,p),e(()=>{l=!0;let e=i.lastIndexOf(n);e!==-1&&i.splice(e,1),n.removeEventListener(`keydown`,f),r.removeEventListener(`focusin`,p),t.restore!==!1&&s!==null&&s.isConnected&&s.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
2
|
//# sourceMappingURL=focus.js.map
|
|
@@ -1 +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,CAAE,iBAAiBA,CAAO,CAAC,CAAC,aAAe,SAChL,CAGA,SAASC,EAAU,EAA0B,CAUzC,OARMC,aAAkB,YAIpBA,aAAkB,iBAEX,CAAC,CAAC,SAAU,WAAY,QAAS,OAAQ,SAAU,QAAS,QAAS,QAAS,QAAS,QAAQ,CAAC,CAAC,SAASA,EAAO,IAAI,EAEzHA,aAAkB,qBAAuBA,aAAkB,mBAAqBA,EAAO,kBANnF,EAOf,CAGA,OAAM,SAAU,WAAW,EAAa,CAEpC,MAAO,CAAC,GAAGC,EAAK,iBAA8B,4QAAS,CAAC,CAAC,CAAC,OAAOJ,CAAS,CAC9E,CAGA,MAAMK,EAAmB,CAAA,EAkBzB,OAAM,SAAU,UAAU,EAA4B,CAAA,EAAE,CAEpD,MAAQ,IAAW,CAEf,IAAMC,EAAML,EAAQ,cACdM,EAAWD,EAAI,yBAAyB,YAAcA,EAAI,cAAgB,KAC1EE,MAA6B,CAE/B,IAAMC,EAAQ,WAAWR,CAAO,CAAC,CAAC,GASlC,OARIQ,IAAU,QAITR,EAAQ,aAAa,UAAU,GAEhCA,EAAQ,aAAa,WAAY,IAAI,EAElCA,GANIQ,CAOf,EACIC,EAAW,GACfL,EAAM,KAAKJ,CA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GAAsB,CAEjC5B,EAAqB,gBAAkB,OAExCsB,EAAO,IAAI,IAAI,EACfhC,EAAI,gBAAgB,gBAAgB,mBAAmB,EAE/D,EAKA,OAJAA,EAAI,iBAAiB,UAAWQ,EAAW,EAAI,EAC/CR,EAAI,iBAAiB,cAAeqC,EAAe,EAAI,EACvDrC,EAAI,iBAAiB,UAAWc,EAAW,EAAI,EAC/Cd,EAAI,iBAAiB,WAAYsC,EAAY,EAAI,EAC1C,CACH,SACA,SAAO,CAEH,aAAaJ,CAAK,EAClBlC,EAAI,oBAAoB,UAAWQ,EAAW,EAAI,EAClDR,EAAI,oBAAoB,cAAeqC,EAAe,EAAI,EAC1DrC,EAAI,oBAAoB,UAAWc,EAAW,EAAI,EAClDd,EAAI,oBAAoB,WAAYsC,EAAY,EAAI,EACpDtC,EAAI,gBAAgB,gBAAgB,mBAAmB,CAC3D,CACH,CACL"}
|
|
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 that holds focus acts, so a dialog opened from another keeps focus, and removing or closing it hands control back. */\nconst traps: Element[] = [];\n\n/** True while a trapped element can hold focus: it is in the document and rendered, which a closed `<dialog>`, a `hidden` drawer, or anything under `display: none` is not. */\nfunction holds(element: Element): boolean\n{\n return element.isConnected && element.getClientRects().length > 0;\n}\n\n/** The trap that acts: the newest whose element holds focus. */\nfunction acting(): Element | undefined\n{\n for (let index = traps.length - 1; index >= 0; index--)\n {\n const element = traps[index] as Element;\n if (holds(element))\n {\n return element;\n }\n }\n return undefined;\n}\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. A trap acts while\n * its element is rendered: a closed native `<dialog>`, or a drawer that is\n * `hidden`, neither acts nor stands in the way of an older trap, and its\n * initial focus waits until the element is rendered as well, while\n * `restore` runs when the template is removed; `showModal()` makes the\n * page behind a dialog inert itself and moves focus in and back on its own.\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 || !holds(element) || 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\" || acting() !== 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 (acting() === 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","holds","acting","index","doc","previous","fallback","first","disposed","initial","options","focusInitial","onKeydown","key","event","list","last","active","onFocusIn","onDispose","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,CAAE,iBAAiBA,CAAO,CAAC,CAAC,aAAe,SAChL,CAGA,SAASC,EAAU,EAA0B,CAUzC,OARMC,aAAkB,YAIpBA,aAAkB,iBAEX,CAAC,CAAC,SAAU,WAAY,QAAS,OAAQ,SAAU,QAAS,QAAS,QAAS,QAAS,QAAQ,CAAC,CAAC,SAASA,EAAO,IAAI,EAEzHA,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EAAI,oBAAoB,WAAYqC,EAAY,EAAI,EACpDrC,EAAI,gBAAgB,gBAAgB,mBAAmB,CAC3D,CACH,CACL"}
|
package/dist/production/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export{focusOrigin,focusTrap,focusables,rovingTabindex}from"./focus.js";export{createAnnouncer,visuallyHidden}from"./announce.js";export{forcedColors,mediaQuery,reducedMotion}from"./media.js";
|
|
1
|
+
export{focusOrigin,focusTrap,focusables,rovingTabindex}from"./focus.js";export{inertOutside}from"./inert.js";export{createAnnouncer,visuallyHidden}from"./announce.js";export{forcedColors,mediaQuery,reducedMotion}from"./media.js";
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +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,
|
|
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 { inertOutside } from \"./inert.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,aAEnE,OAAS,iBAAoB,aAG7B,OAAS,gBAAiB,mBAAsB,gBAEhD,OAAS,aAAc,WAAY,kBAAqB"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=new WeakMap;function t(e){let t=e.parentNode;return t===null?null:t.nodeType===1?{siblings:t.children,next:t}:t.nodeType===11&&`host`in t?{siblings:t.children,next:t.host}:null}export function inertOutside(n){let r=[],i=!1,a=()=>{if(i||!n.isConnected)return;let a=n.ownerDocument.body,o=n;for(;o!==a;){let n=t(o);if(n===null)break;for(let t of n.siblings){if(t===o)continue;let n=e.get(t)??0;if(n===0){if(t.hasAttribute(`inert`))continue;t.setAttribute(`inert`,``)}e.set(t,n+1),r.push(t)}o=n.next}};return n.isConnected?a():queueMicrotask(a),()=>{i=!0;for(let t of r){let n=(e.get(t)??1)-1;n===0?(e.delete(t),t.removeAttribute(`inert`)):e.set(t,n)}r.length=0}}
|
|
2
|
+
//# sourceMappingURL=inert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inert.js","sources":["../../src/inert.ts"],"sourcesContent":["/**\n * Everything outside one element made inert, for a drawer, a menu, or a\n * dialog that is not a native modal: the page behind it takes no focus,\n * no clicks, and no reading by assistive technology until it is given\n * back.\n */\n\n/** How many holds each element is under, so the attribute leaves only with the last; an element inert on its own, with the attribute and no hold, is never touched. */\nconst holds = new WeakMap<Element, number>();\n\n/** The siblings of `node` and the element the walk continues from: its parent element, or for a node at the top of a shadow tree the tree's host, whose siblings come next. */\nfunction above(node: Node): { siblings: Iterable<Element>; next: Element } | null\n{\n const parent = node.parentNode;\n if (parent === null)\n {\n return null;\n }\n if (parent.nodeType === 1)\n {\n return { siblings: (parent as Element).children, next: parent as Element };\n }\n if (parent.nodeType === 11 && \"host\" in parent)\n {\n return { siblings: (parent as ShadowRoot).children, next: (parent as ShadowRoot).host };\n }\n return null;\n}\n\n/**\n * Makes everything outside `element` inert and returns what undoes it: the\n * siblings of the element and of each of its ancestors up to the body get\n * the `inert` attribute, through a shadow tree's host where the element is\n * in one, and the returned function takes it back. An element inert on its\n * own is left as it is, and one two calls hold stays inert until the last\n * lets go, so two drawers at once undo only what each added. Returned\n * from an effect that reads the open state, it holds while the drawer is\n * open and lets go when it closes or the effect is disposed. An element\n * not yet in the page is held once it is, by the next microtask, as a\n * `ref` creates it before its template is inserted. A native `<dialog>`\n * opened with `showModal()` makes the page behind it inert itself and\n * needs none of this.\n */\nexport function inertOutside(element: Element): () => void\n{\n const held: Element[] = [];\n let released = false;\n const hold = (): void =>\n {\n if (released || !element.isConnected)\n {\n return;\n }\n const body = element.ownerDocument.body;\n let node: Element = element;\n while (node !== body)\n {\n const step = above(node);\n if (step === null)\n {\n break;\n }\n for (const sibling of step.siblings)\n {\n if (sibling === node)\n {\n continue;\n }\n const count = holds.get(sibling) ?? 0;\n if (count === 0)\n {\n if (sibling.hasAttribute(\"inert\"))\n {\n continue;\n }\n sibling.setAttribute(\"inert\", \"\");\n }\n holds.set(sibling, count + 1);\n held.push(sibling);\n }\n node = step.next;\n }\n };\n if (element.isConnected)\n {\n hold();\n }\n else\n {\n queueMicrotask(hold);\n }\n return () =>\n {\n released = true;\n for (const sibling of held)\n {\n const count = (holds.get(sibling) ?? 1) - 1;\n if (count === 0)\n {\n holds.delete(sibling);\n sibling.removeAttribute(\"inert\");\n }\n else\n {\n holds.set(sibling, count);\n }\n }\n held.length = 0;\n };\n}\n"],"names":["holds","above","parent","node","held","released","hold","element","body","step","sibling","count"],"mappings":"AAQA,MAAMA,EAAQ,IAAI,QAGlB,SAASC,EAAM,EAAU,CAErB,IAAMC,EAASC,EAAK,WAapB,OAZID,IAAW,KAEJ,KAEPA,EAAO,WAAa,EAEb,CAAE,SAAWA,EAAmB,SAAU,KAAMA,CAAiB,EAExEA,EAAO,WAAa,IAAM,SAAUA,EAE7B,CAAE,SAAWA,EAAsB,SAAU,KAAOA,EAAsB,IAAI,EAElF,IACX,CAgBA,OAAM,SAAU,aAAa,EAAgB,CAEzC,IAAME,EAAkB,CAAA,EACpBC,EAAW,GACTC,MAAkB,CAEpB,GAAID,GAAY,CAACE,EAAQ,YAErB,OAEJ,IAAMC,EAAOD,EAAQ,cAAc,KAC/BJ,EAAgBI,EACpB,KAAOJ,IAASK,GAChB,CACI,IAAMC,EAAOR,EAAME,CAAI,EACvB,GAAIM,IAAS,KAET,MAEJ,IAAK,IAAMC,KAAWD,EAAK,SAC3B,CACI,GAAIC,IAAYP,EAEZ,SAEJ,IAAMQ,EAAQX,EAAM,IAAIU,CAAO,GAAK,EACpC,GAAIC,IAAU,EACd,CACI,GAAID,EAAQ,aAAa,OAAO,EAE5B,SAEJA,EAAQ,aAAa,QAAS,EAAE,CACpC,CACAV,EAAM,IAAIU,EAASC,EAAQ,CAAC,EAC5BP,EAAK,KAAKM,CAAO,CACrB,CACA,EAAOD,EAAK,IAChB,CACJ,EASA,OARIF,EAAQ,YAERD,EAAI,EAIJ,eAAeA,CAAI,MAEX,CAER,EAAW,GACX,IAAK,IAAMI,KAAWN,EACtB,CACI,IAAMO,GAASX,EAAM,IAAIU,CAAO,GAAK,GAAK,EACtCC,IAAU,GAEVX,EAAM,OAAOU,CAAO,EACpBA,EAAQ,gBAAgB,OAAO,GAI/BV,EAAM,IAAIU,EAASC,CAAK,CAEhC,CACA,EAAK,OAAS,CAClB,CACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zerolith/a11y",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.1",
|
|
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",
|
|
@@ -43,9 +43,9 @@
|
|
|
43
43
|
"build": "tsc -p tsconfig.build.json && node ../../scripts/production.ts"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"zerolith": "^
|
|
46
|
+
"zerolith": "^1.0.1"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"zerolith": "^
|
|
49
|
+
"zerolith": "^1.0.1"
|
|
50
50
|
}
|
|
51
51
|
}
|
package/src/focus.ts
CHANGED
|
@@ -34,9 +34,29 @@ export function focusables(root: Element): HTMLElement[]
|
|
|
34
34
|
return [...root.querySelectorAll<HTMLElement>(FOCUSABLE)].filter(reachable);
|
|
35
35
|
}
|
|
36
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. */
|
|
37
|
+
/** The elements with an attached focus trap, newest last. Only the newest that holds focus acts, so a dialog opened from another keeps focus, and removing or closing it hands control back. */
|
|
38
38
|
const traps: Element[] = [];
|
|
39
39
|
|
|
40
|
+
/** True while a trapped element can hold focus: it is in the document and rendered, which a closed `<dialog>`, a `hidden` drawer, or anything under `display: none` is not. */
|
|
41
|
+
function holds(element: Element): boolean
|
|
42
|
+
{
|
|
43
|
+
return element.isConnected && element.getClientRects().length > 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The trap that acts: the newest whose element holds focus. */
|
|
47
|
+
function acting(): Element | undefined
|
|
48
|
+
{
|
|
49
|
+
for (let index = traps.length - 1; index >= 0; index--)
|
|
50
|
+
{
|
|
51
|
+
const element = traps[index] as Element;
|
|
52
|
+
if (holds(element))
|
|
53
|
+
{
|
|
54
|
+
return element;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
40
60
|
/** Options for `focusTrap`. */
|
|
41
61
|
export interface FocusTrapOptions
|
|
42
62
|
{
|
|
@@ -51,7 +71,12 @@ export interface FocusTrapOptions
|
|
|
51
71
|
* when it lands outside, moves focus in once the element is in the document,
|
|
52
72
|
* and returns it where it came from when the element's template is removed.
|
|
53
73
|
* With traps on several elements, as when a dialog opens a confirmation,
|
|
54
|
-
* only the newest acts until its template is removed.
|
|
74
|
+
* only the newest acts until its template is removed. A trap acts while
|
|
75
|
+
* its element is rendered: a closed native `<dialog>`, or a drawer that is
|
|
76
|
+
* `hidden`, neither acts nor stands in the way of an older trap, and its
|
|
77
|
+
* initial focus waits until the element is rendered as well, while
|
|
78
|
+
* `restore` runs when the template is removed; `showModal()` makes the
|
|
79
|
+
* page behind a dialog inert itself and moves focus in and back on its own.
|
|
55
80
|
*/
|
|
56
81
|
export function focusTrap(options: FocusTrapOptions = {}): (element: Element) => void
|
|
57
82
|
{
|
|
@@ -77,7 +102,7 @@ export function focusTrap(options: FocusTrapOptions = {}): (element: Element) =>
|
|
|
77
102
|
const initial = options.initial;
|
|
78
103
|
const focusInitial = (): void =>
|
|
79
104
|
{
|
|
80
|
-
if (disposed || !element
|
|
105
|
+
if (disposed || !holds(element) || initial === false)
|
|
81
106
|
{
|
|
82
107
|
return;
|
|
83
108
|
}
|
|
@@ -98,7 +123,7 @@ export function focusTrap(options: FocusTrapOptions = {}): (element: Element) =>
|
|
|
98
123
|
const onKeydown = (event: Event): void =>
|
|
99
124
|
{
|
|
100
125
|
const key = event as KeyboardEvent;
|
|
101
|
-
if (key.key !== "Tab" ||
|
|
126
|
+
if (key.key !== "Tab" || acting() !== element)
|
|
102
127
|
{
|
|
103
128
|
return;
|
|
104
129
|
}
|
|
@@ -124,7 +149,7 @@ export function focusTrap(options: FocusTrapOptions = {}): (element: Element) =>
|
|
|
124
149
|
};
|
|
125
150
|
const onFocusIn = (event: Event): void =>
|
|
126
151
|
{
|
|
127
|
-
if (
|
|
152
|
+
if (acting() === element && !element.contains(event.target as Node))
|
|
128
153
|
{
|
|
129
154
|
fallback().focus();
|
|
130
155
|
}
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
export type { FocusOrigin, FocusOriginMonitor, FocusTrapOptions, RovingOptions } from "./focus.js";
|
|
6
6
|
export { focusOrigin, focusTrap, focusables, rovingTabindex } from "./focus.js";
|
|
7
7
|
|
|
8
|
+
export { inertOutside } from "./inert.js";
|
|
9
|
+
|
|
8
10
|
export type { Announcer, Politeness } from "./announce.js";
|
|
9
11
|
export { createAnnouncer, visuallyHidden } from "./announce.js";
|
|
10
12
|
|
package/src/inert.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Everything outside one element made inert, for a drawer, a menu, or a
|
|
3
|
+
* dialog that is not a native modal: the page behind it takes no focus,
|
|
4
|
+
* no clicks, and no reading by assistive technology until it is given
|
|
5
|
+
* back.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** How many holds each element is under, so the attribute leaves only with the last; an element inert on its own, with the attribute and no hold, is never touched. */
|
|
9
|
+
const holds = new WeakMap<Element, number>();
|
|
10
|
+
|
|
11
|
+
/** The siblings of `node` and the element the walk continues from: its parent element, or for a node at the top of a shadow tree the tree's host, whose siblings come next. */
|
|
12
|
+
function above(node: Node): { siblings: Iterable<Element>; next: Element } | null
|
|
13
|
+
{
|
|
14
|
+
const parent = node.parentNode;
|
|
15
|
+
if (parent === null)
|
|
16
|
+
{
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
if (parent.nodeType === 1)
|
|
20
|
+
{
|
|
21
|
+
return { siblings: (parent as Element).children, next: parent as Element };
|
|
22
|
+
}
|
|
23
|
+
if (parent.nodeType === 11 && "host" in parent)
|
|
24
|
+
{
|
|
25
|
+
return { siblings: (parent as ShadowRoot).children, next: (parent as ShadowRoot).host };
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Makes everything outside `element` inert and returns what undoes it: the
|
|
32
|
+
* siblings of the element and of each of its ancestors up to the body get
|
|
33
|
+
* the `inert` attribute, through a shadow tree's host where the element is
|
|
34
|
+
* in one, and the returned function takes it back. An element inert on its
|
|
35
|
+
* own is left as it is, and one two calls hold stays inert until the last
|
|
36
|
+
* lets go, so two drawers at once undo only what each added. Returned
|
|
37
|
+
* from an effect that reads the open state, it holds while the drawer is
|
|
38
|
+
* open and lets go when it closes or the effect is disposed. An element
|
|
39
|
+
* not yet in the page is held once it is, by the next microtask, as a
|
|
40
|
+
* `ref` creates it before its template is inserted. A native `<dialog>`
|
|
41
|
+
* opened with `showModal()` makes the page behind it inert itself and
|
|
42
|
+
* needs none of this.
|
|
43
|
+
*/
|
|
44
|
+
export function inertOutside(element: Element): () => void
|
|
45
|
+
{
|
|
46
|
+
const held: Element[] = [];
|
|
47
|
+
let released = false;
|
|
48
|
+
const hold = (): void =>
|
|
49
|
+
{
|
|
50
|
+
if (released || !element.isConnected)
|
|
51
|
+
{
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const body = element.ownerDocument.body;
|
|
55
|
+
let node: Element = element;
|
|
56
|
+
while (node !== body)
|
|
57
|
+
{
|
|
58
|
+
const step = above(node);
|
|
59
|
+
if (step === null)
|
|
60
|
+
{
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
for (const sibling of step.siblings)
|
|
64
|
+
{
|
|
65
|
+
if (sibling === node)
|
|
66
|
+
{
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const count = holds.get(sibling) ?? 0;
|
|
70
|
+
if (count === 0)
|
|
71
|
+
{
|
|
72
|
+
if (sibling.hasAttribute("inert"))
|
|
73
|
+
{
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
sibling.setAttribute("inert", "");
|
|
77
|
+
}
|
|
78
|
+
holds.set(sibling, count + 1);
|
|
79
|
+
held.push(sibling);
|
|
80
|
+
}
|
|
81
|
+
node = step.next;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
if (element.isConnected)
|
|
85
|
+
{
|
|
86
|
+
hold();
|
|
87
|
+
}
|
|
88
|
+
else
|
|
89
|
+
{
|
|
90
|
+
queueMicrotask(hold);
|
|
91
|
+
}
|
|
92
|
+
return () =>
|
|
93
|
+
{
|
|
94
|
+
released = true;
|
|
95
|
+
for (const sibling of held)
|
|
96
|
+
{
|
|
97
|
+
const count = (holds.get(sibling) ?? 1) - 1;
|
|
98
|
+
if (count === 0)
|
|
99
|
+
{
|
|
100
|
+
holds.delete(sibling);
|
|
101
|
+
sibling.removeAttribute("inert");
|
|
102
|
+
}
|
|
103
|
+
else
|
|
104
|
+
{
|
|
105
|
+
holds.set(sibling, count);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
held.length = 0;
|
|
109
|
+
};
|
|
110
|
+
}
|