@barqjs/aria 0.0.0-stage → 0.2.0-next.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/LICENSE +21 -0
- package/README.md +304 -2
- package/package.json +437 -4
- package/src/autocomplete.tsx +581 -0
- package/src/breadcrumbs.tsx +297 -0
- package/src/button.tsx +468 -0
- package/src/calendar.tsx +2316 -0
- package/src/checkbox.tsx +732 -0
- package/src/collections.ts +1059 -0
- package/src/color.ts +2119 -0
- package/src/colorpicker.tsx +2023 -0
- package/src/colorswatchpicker.tsx +240 -0
- package/src/combobox.tsx +3003 -0
- package/src/command.tsx +1203 -0
- package/src/date.ts +674 -0
- package/src/datefield.tsx +2066 -0
- package/src/datepicker.tsx +907 -0
- package/src/dialog.tsx +1077 -0
- package/src/disclosure.tsx +711 -0
- package/src/dnd/clipboard.ts +125 -0
- package/src/dnd/collection.ts +1265 -0
- package/src/dnd/data.ts +409 -0
- package/src/dnd/drag-drop.ts +801 -0
- package/src/dnd/index.ts +133 -0
- package/src/dnd/manager.ts +639 -0
- package/src/dnd/strings.ts +1279 -0
- package/src/dnd/targets.ts +726 -0
- package/src/dnd/types.ts +212 -0
- package/src/dom.ts +703 -0
- package/src/drag-and-drop.tsx +669 -0
- package/src/drawer.ts +674 -0
- package/src/dropzone.tsx +208 -0
- package/src/file-trigger.tsx +103 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +1354 -0
- package/src/group.tsx +146 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1672 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +226 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +298 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +428 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +226 -0
- package/src/interactions/press.ts +915 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +140 -0
- package/src/link.tsx +477 -0
- package/src/listbox.tsx +1247 -0
- package/src/live.ts +208 -0
- package/src/menu.tsx +2383 -0
- package/src/menubar.tsx +579 -0
- package/src/navigation-tree.tsx +357 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +942 -0
- package/src/otp.ts +430 -0
- package/src/overlay-arrow.tsx +157 -0
- package/src/overlays.ts +1549 -0
- package/src/platform.ts +75 -0
- package/src/presence.ts +164 -0
- package/src/preview-trigger.tsx +582 -0
- package/src/progress-bar-context.ts +15 -0
- package/src/questionnaire.tsx +1717 -0
- package/src/radio.tsx +641 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1745 -0
- package/src/selection-indicator.tsx +74 -0
- package/src/selection.ts +1472 -0
- package/src/shared-element-transition.tsx +316 -0
- package/src/sheet.tsx +1461 -0
- package/src/slider.tsx +1137 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +212 -0
- package/src/table.tsx +2218 -0
- package/src/tabs.tsx +1127 -0
- package/src/tag.tsx +637 -0
- package/src/textarea.tsx +186 -0
- package/src/textfield.tsx +483 -0
- package/src/toast-components.tsx +589 -0
- package/src/toast.ts +658 -0
- package/src/toggle-button-group.tsx +175 -0
- package/src/toggle-input.tsx +86 -0
- package/src/toggle.ts +384 -0
- package/src/togglegroup.ts +365 -0
- package/src/tokenfield.tsx +1673 -0
- package/src/toolbar.tsx +184 -0
- package/src/tooltip.tsx +674 -0
- package/src/tree.tsx +2075 -0
- package/src/utils.ts +787 -0
- package/src/validation.ts +504 -0
- package/src/virtualizer.tsx +837 -0
package/src/focus.ts
ADDED
|
@@ -0,0 +1,1370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Focus containment, restoration and the ring.
|
|
3
|
+
*
|
|
4
|
+
* A modal dialog has to trap Tab inside itself, put focus back where it came
|
|
5
|
+
* from when it closes, and cope with being one of several nested traps. The
|
|
6
|
+
* platform offers `inert` for the first, nothing for the second, and nothing
|
|
7
|
+
* at all for the third; `inert` also cannot express "contained but with a
|
|
8
|
+
* child scope allowed to take over", which is what a menu inside a dialog is.
|
|
9
|
+
*
|
|
10
|
+
* So scopes form a tree of their own, parallel to the DOM, and the one that
|
|
11
|
+
* currently owns focus is a module-level fact rather than a per-scope one:
|
|
12
|
+
* a scope opened from inside another must become its child even when it
|
|
13
|
+
* renders through a portal somewhere else entirely.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
type Accessor,
|
|
18
|
+
createContext,
|
|
19
|
+
createEffect,
|
|
20
|
+
getContext,
|
|
21
|
+
getOwner,
|
|
22
|
+
install,
|
|
23
|
+
onSettled,
|
|
24
|
+
createSignal,
|
|
25
|
+
} from "@barqjs/core";
|
|
26
|
+
import { isServer } from "@barqjs/web";
|
|
27
|
+
import { tryCleanup } from "@barqjs/primitives/utils";
|
|
28
|
+
import {
|
|
29
|
+
activeElement,
|
|
30
|
+
contains,
|
|
31
|
+
isFocusable,
|
|
32
|
+
isTabbable,
|
|
33
|
+
isShadowDOMEnabled,
|
|
34
|
+
isShadowRoot,
|
|
35
|
+
ownerDocument,
|
|
36
|
+
ownerWindow,
|
|
37
|
+
targetElement,
|
|
38
|
+
TOP_LAYER_ATTRIBUTE,
|
|
39
|
+
} from "./dom.ts";
|
|
40
|
+
import { focusSafely } from "./interactions/focusable.ts";
|
|
41
|
+
import { focused, focusWithin } from "./interactions/focus-events.ts";
|
|
42
|
+
import { getInteractionModality, focusVisible } from "./interactions/modality.ts";
|
|
43
|
+
import { isAndroid, isChrome } from "./platform.ts";
|
|
44
|
+
import { access, mergeProps, type DOMProps, type MaybeAccessor } from "./utils.ts";
|
|
45
|
+
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
// The tree walker
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
export interface FocusWalkerOptions {
|
|
51
|
+
/** Start from here rather than from the currently focused element. */
|
|
52
|
+
from?: Element;
|
|
53
|
+
/** Only elements in the browser's Tab order. */
|
|
54
|
+
tabbable?: boolean;
|
|
55
|
+
/** Wrap around at the ends. */
|
|
56
|
+
wrap?: boolean;
|
|
57
|
+
/** An extra test the element must pass. */
|
|
58
|
+
accept?: (node: Element) => boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const ACCEPT = 1;
|
|
62
|
+
const REJECT = 2;
|
|
63
|
+
const SKIP = 3;
|
|
64
|
+
|
|
65
|
+
type Verdict = typeof ACCEPT | typeof REJECT | typeof SKIP;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The focus order under a root, walked by hand.
|
|
69
|
+
*
|
|
70
|
+
* The platform has `TreeWalker`, and it is not used, for two reasons. It stops
|
|
71
|
+
* at a shadow boundary, so a scope containing a custom element cannot find
|
|
72
|
+
* anything focusable inside it. And its `previousNode` is not implemented
|
|
73
|
+
* consistently: happy-dom returns the root rather than the previous sibling,
|
|
74
|
+
* which silently turns every Shift+Tab and every `focusPrevious` into a wrap.
|
|
75
|
+
* A suite running against it would report focus containment as working when
|
|
76
|
+
* backwards movement is broken.
|
|
77
|
+
*
|
|
78
|
+
* The traversal is the one the DOM standard specifies for `TreeWalker`, with
|
|
79
|
+
* `firstChild` and `parentNode` reading through open shadow roots when
|
|
80
|
+
* {@link enableShadowDOM} is on.
|
|
81
|
+
*/
|
|
82
|
+
export class FocusWalker {
|
|
83
|
+
currentNode: Node;
|
|
84
|
+
|
|
85
|
+
#root: Node;
|
|
86
|
+
#filter: (node: Node) => Verdict;
|
|
87
|
+
|
|
88
|
+
constructor(root: Node, filter: (node: Node) => Verdict) {
|
|
89
|
+
this.#root = root;
|
|
90
|
+
this.#filter = filter;
|
|
91
|
+
this.currentNode = root;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#firstChild(node: Node): Node | null {
|
|
95
|
+
if (isShadowDOMEnabled()) {
|
|
96
|
+
const shadow = (node as Element).shadowRoot;
|
|
97
|
+
if (shadow) return shadow.firstChild;
|
|
98
|
+
}
|
|
99
|
+
return node.firstChild;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
#lastChild(node: Node): Node | null {
|
|
103
|
+
if (isShadowDOMEnabled()) {
|
|
104
|
+
const shadow = (node as Element).shadowRoot;
|
|
105
|
+
if (shadow) return shadow.lastChild;
|
|
106
|
+
}
|
|
107
|
+
return node.lastChild;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
#parent(node: Node): Node | null {
|
|
111
|
+
const parent = node.parentNode;
|
|
112
|
+
if (parent === null) return null;
|
|
113
|
+
if (isShadowDOMEnabled() && isShadowRoot(parent)) return parent.host;
|
|
114
|
+
return parent;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
nextNode(): Node | null {
|
|
118
|
+
let node = this.currentNode;
|
|
119
|
+
let verdict: Verdict = ACCEPT;
|
|
120
|
+
|
|
121
|
+
for (;;) {
|
|
122
|
+
while (verdict !== REJECT) {
|
|
123
|
+
const child = this.#firstChild(node);
|
|
124
|
+
if (child === null) break;
|
|
125
|
+
node = child;
|
|
126
|
+
verdict = this.#filter(node);
|
|
127
|
+
if (verdict === ACCEPT) {
|
|
128
|
+
this.currentNode = node;
|
|
129
|
+
return node;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
let following: Node | null = null;
|
|
134
|
+
let at: Node | null = node;
|
|
135
|
+
while (at !== null) {
|
|
136
|
+
if (at === this.#root) return null;
|
|
137
|
+
const sibling = at.nextSibling;
|
|
138
|
+
if (sibling !== null) {
|
|
139
|
+
following = sibling;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
at = this.#parent(at);
|
|
143
|
+
}
|
|
144
|
+
if (following === null) return null;
|
|
145
|
+
|
|
146
|
+
node = following;
|
|
147
|
+
verdict = this.#filter(node);
|
|
148
|
+
if (verdict === ACCEPT) {
|
|
149
|
+
this.currentNode = node;
|
|
150
|
+
return node;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
previousNode(): Node | null {
|
|
156
|
+
let node = this.currentNode;
|
|
157
|
+
|
|
158
|
+
while (node !== this.#root) {
|
|
159
|
+
let sibling = node.previousSibling;
|
|
160
|
+
while (sibling !== null) {
|
|
161
|
+
node = sibling;
|
|
162
|
+
let verdict = this.#filter(node);
|
|
163
|
+
// Descend to the deepest acceptable node of this subtree: the previous
|
|
164
|
+
// element in focus order is the LAST one inside the previous sibling,
|
|
165
|
+
// not the sibling itself.
|
|
166
|
+
while (verdict !== REJECT) {
|
|
167
|
+
const child = this.#lastChild(node);
|
|
168
|
+
if (child === null) break;
|
|
169
|
+
node = child;
|
|
170
|
+
verdict = this.#filter(node);
|
|
171
|
+
}
|
|
172
|
+
if (verdict === ACCEPT) {
|
|
173
|
+
this.currentNode = node;
|
|
174
|
+
return node;
|
|
175
|
+
}
|
|
176
|
+
sibling = node.previousSibling;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const parent = this.#parent(node);
|
|
180
|
+
if (node === this.#root || parent === null) return null;
|
|
181
|
+
node = parent;
|
|
182
|
+
if (this.#filter(node) === ACCEPT) {
|
|
183
|
+
this.currentNode = node;
|
|
184
|
+
return node;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The last acceptable node under the root, or null. */
|
|
192
|
+
last(): Node | null {
|
|
193
|
+
const from = this.currentNode;
|
|
194
|
+
this.currentNode = this.#root;
|
|
195
|
+
let found: Node | null = null;
|
|
196
|
+
let node = this.nextNode();
|
|
197
|
+
while (node !== null) {
|
|
198
|
+
found = node;
|
|
199
|
+
node = this.nextNode();
|
|
200
|
+
}
|
|
201
|
+
if (found === null) this.currentNode = from;
|
|
202
|
+
return found;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function radiosInGroup(input: HTMLInputElement): HTMLInputElement[] {
|
|
207
|
+
const view = ownerWindow(input);
|
|
208
|
+
if (!input.form) {
|
|
209
|
+
return [
|
|
210
|
+
...ownerDocument(input).querySelectorAll<HTMLInputElement>(
|
|
211
|
+
`input[type="radio"][name="${CSS.escape(input.name)}"]`,
|
|
212
|
+
),
|
|
213
|
+
].filter((radio) => !radio.form);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// `namedItem` returns a RadioNodeList for two or more, a single Element for
|
|
217
|
+
// exactly one, and null for none.
|
|
218
|
+
const list = input.form.elements.namedItem(input.name);
|
|
219
|
+
if (list instanceof view.RadioNodeList) {
|
|
220
|
+
return [...list].filter((el): el is HTMLInputElement => el instanceof view.HTMLInputElement);
|
|
221
|
+
}
|
|
222
|
+
if (list instanceof view.HTMLInputElement) return [list];
|
|
223
|
+
return [];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Whether a radio is the one its group puts in the Tab order: the checked one,
|
|
228
|
+
* or the first when none is checked.
|
|
229
|
+
*/
|
|
230
|
+
function isTabbableRadio(input: HTMLInputElement): boolean {
|
|
231
|
+
if (input.checked) return true;
|
|
232
|
+
const group = radiosInGroup(input);
|
|
233
|
+
return group.length > 0 && !group.some((radio) => radio.checked);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** A walker over the focusable, or tabbable, elements under `root`. */
|
|
237
|
+
export function focusableWalker(
|
|
238
|
+
root: Element,
|
|
239
|
+
options?: FocusWalkerOptions,
|
|
240
|
+
scope?: Element[],
|
|
241
|
+
): FocusWalker {
|
|
242
|
+
const match = options?.tabbable === true ? isTabbable : isFocusable;
|
|
243
|
+
|
|
244
|
+
const walker = new FocusWalker(root, (node: Node): Verdict => {
|
|
245
|
+
if (node.nodeType !== Node.ELEMENT_NODE) return SKIP;
|
|
246
|
+
const element = node as Element;
|
|
247
|
+
|
|
248
|
+
// Everything inside the starting node is behind us.
|
|
249
|
+
if (contains(options?.from, element)) return REJECT;
|
|
250
|
+
|
|
251
|
+
if (
|
|
252
|
+
options?.tabbable === true &&
|
|
253
|
+
element.tagName === "INPUT" &&
|
|
254
|
+
element.getAttribute("type") === "radio"
|
|
255
|
+
) {
|
|
256
|
+
if (!isTabbableRadio(element as HTMLInputElement)) return REJECT;
|
|
257
|
+
const current = walker.currentNode as Element;
|
|
258
|
+
// Two radios of one group are one Tab stop.
|
|
259
|
+
if (
|
|
260
|
+
current.tagName === "INPUT" &&
|
|
261
|
+
(current as HTMLInputElement).type === "radio" &&
|
|
262
|
+
(current as HTMLInputElement).name === (element as HTMLInputElement).name
|
|
263
|
+
) {
|
|
264
|
+
return REJECT;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (
|
|
269
|
+
match(element) &&
|
|
270
|
+
(scope === undefined || isInScope(element, scope)) &&
|
|
271
|
+
(options?.accept === undefined || options.accept(element))
|
|
272
|
+
) {
|
|
273
|
+
return ACCEPT;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return SKIP;
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
if (options?.from !== undefined) walker.currentNode = options.from;
|
|
280
|
+
return walker;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ---------------------------------------------------------------------------
|
|
284
|
+
// The scope tree
|
|
285
|
+
// ---------------------------------------------------------------------------
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* The elements between one scope's sentinels.
|
|
289
|
+
*
|
|
290
|
+
* Computed on demand rather than collected once, because the extent of a scope
|
|
291
|
+
* is not fixed: a `Show` inside it swaps its content, and a list adds rows.
|
|
292
|
+
* A collected snapshot goes stale the moment either happens, and the scope
|
|
293
|
+
* then contains focus inside elements that are no longer there.
|
|
294
|
+
*
|
|
295
|
+
* `read` is the reactive form, so an effect binding listeners to these
|
|
296
|
+
* elements re-binds when the set changes; `current` is the plain read, for the
|
|
297
|
+
* scope-tree bookkeeping that must not subscribe to anything.
|
|
298
|
+
*/
|
|
299
|
+
class ScopeNodes {
|
|
300
|
+
#start: Element | null = null;
|
|
301
|
+
#end: Element | null = null;
|
|
302
|
+
|
|
303
|
+
// The sentinels arrive through refs, which run while the component that
|
|
304
|
+
// holds them is still being built.
|
|
305
|
+
#version = createSignal(0, { ownedWrite: true });
|
|
306
|
+
#observer: MutationObserver | null = null;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* The sentinels, and setting either one INVALIDATES the scope.
|
|
310
|
+
*
|
|
311
|
+
* They were plain fields, and the reactive read had no idea when they were
|
|
312
|
+
* filled in: `focusScope` creates its effects before the JSX that holds the
|
|
313
|
+
* sentinels is built, so the first read finds no scope, and nothing told it
|
|
314
|
+
* to look again. Under happy-dom the `watch()` bump happened to land after
|
|
315
|
+
* both refs had run; in a browser — where a portalled overlay's content is
|
|
316
|
+
* built a microtask later — it landed before, and the scope stayed empty for
|
|
317
|
+
* its whole life. An overlay then did not autofocus, did not contain Tab and
|
|
318
|
+
* did not close on Escape, with every test still green.
|
|
319
|
+
*/
|
|
320
|
+
get start(): Element | null {
|
|
321
|
+
return this.#start;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
set start(element: Element | null) {
|
|
325
|
+
if (this.#start === element) return;
|
|
326
|
+
this.#start = element;
|
|
327
|
+
this.bump();
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
get end(): Element | null {
|
|
331
|
+
return this.#end;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
set end(element: Element | null) {
|
|
335
|
+
if (this.#end === element) return;
|
|
336
|
+
this.#end = element;
|
|
337
|
+
this.bump();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
get current(): Element[] | null {
|
|
341
|
+
if (this.#start === null || this.#end === null) return null;
|
|
342
|
+
const between: Element[] = [];
|
|
343
|
+
let at = this.#start.nextSibling;
|
|
344
|
+
while (at !== null && at !== this.#end) {
|
|
345
|
+
if (at.nodeType === Node.ELEMENT_NODE) between.push(at as Element);
|
|
346
|
+
at = at.nextSibling;
|
|
347
|
+
}
|
|
348
|
+
return between;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
read(): Element[] | null {
|
|
352
|
+
this.#version[0]();
|
|
353
|
+
return this.current;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
bump(): void {
|
|
357
|
+
this.#version[1]((n) => n + 1);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Re-read when the sentinels' parent gains or loses children.
|
|
362
|
+
*
|
|
363
|
+
* The sentinels may have no parent yet. A PORTALLED scope's content is built
|
|
364
|
+
* on a microtask after its marker connects, so `onSettled` can run first — and
|
|
365
|
+
* a scope that gave up here never observed anything, never re-read itself,
|
|
366
|
+
* and stayed empty for its whole life. What that looked like: an overlay's
|
|
367
|
+
* `autoFocus` did nothing, `Escape` did nothing, and focus was not contained,
|
|
368
|
+
* in a real browser only. Under happy-dom the parent was already there, so
|
|
369
|
+
* every test passed.
|
|
370
|
+
*
|
|
371
|
+
* So it retries on a microtask until there is a parent to observe. BOUNDED:
|
|
372
|
+
* a scope whose sentinels never mount would otherwise queue a microtask that
|
|
373
|
+
* queues a microtask, which starves the event loop rather than failing.
|
|
374
|
+
*/
|
|
375
|
+
watch(): () => void {
|
|
376
|
+
if (typeof MutationObserver === "undefined") {
|
|
377
|
+
this.bump();
|
|
378
|
+
return () => {};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
let cancelled = false;
|
|
382
|
+
// One microtask is what a portal defers by. The rest is slack.
|
|
383
|
+
let attempts = 4;
|
|
384
|
+
|
|
385
|
+
const attach = (): void => {
|
|
386
|
+
if (cancelled) return;
|
|
387
|
+
const parent = this.start?.parentNode;
|
|
388
|
+
if (parent === null || parent === undefined) {
|
|
389
|
+
this.bump();
|
|
390
|
+
if (attempts-- > 0) queueMicrotask(attach);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
this.#observer = new MutationObserver(() => this.bump());
|
|
394
|
+
this.#observer.observe(parent, { childList: true });
|
|
395
|
+
this.bump();
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
attach();
|
|
399
|
+
|
|
400
|
+
return () => {
|
|
401
|
+
cancelled = true;
|
|
402
|
+
this.#observer?.disconnect();
|
|
403
|
+
this.#observer = null;
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
class ScopeNode {
|
|
409
|
+
nodes: ScopeNodes | null;
|
|
410
|
+
restoreTo: HTMLElement | SVGElement | undefined;
|
|
411
|
+
parent: ScopeNode | undefined;
|
|
412
|
+
children = new Set<ScopeNode>();
|
|
413
|
+
contain = false;
|
|
414
|
+
|
|
415
|
+
constructor(nodes: ScopeNodes | null) {
|
|
416
|
+
this.nodes = nodes;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
add(child: ScopeNode): void {
|
|
420
|
+
this.children.add(child);
|
|
421
|
+
child.parent = this;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
remove(child: ScopeNode): void {
|
|
425
|
+
this.children.delete(child);
|
|
426
|
+
child.parent = undefined;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
class ScopeTree {
|
|
431
|
+
root = new ScopeNode(null);
|
|
432
|
+
#byNodes = new Map<ScopeNodes | null, ScopeNode>();
|
|
433
|
+
|
|
434
|
+
constructor() {
|
|
435
|
+
this.#byNodes.set(null, this.root);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
get(nodes: ScopeNodes | null): ScopeNode | undefined {
|
|
439
|
+
return this.#byNodes.get(nodes);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
register(node: ScopeNode): void {
|
|
443
|
+
this.#byNodes.set(node.nodes, node);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
attach(nodes: ScopeNodes | null, parent: ScopeNodes | null, restoreTo?: HTMLElement): void {
|
|
447
|
+
const parentNode = this.#byNodes.get(parent ?? null);
|
|
448
|
+
if (parentNode === undefined) return;
|
|
449
|
+
const node = new ScopeNode(nodes);
|
|
450
|
+
parentNode.add(node);
|
|
451
|
+
this.#byNodes.set(nodes, node);
|
|
452
|
+
if (restoreTo !== undefined) node.restoreTo = restoreTo;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
remove(nodes: ScopeNodes | null): void {
|
|
456
|
+
if (nodes === null) return;
|
|
457
|
+
const node = this.#byNodes.get(nodes);
|
|
458
|
+
if (node === undefined) return;
|
|
459
|
+
|
|
460
|
+
// A sibling scope restoring focus INTO the scope being removed has to
|
|
461
|
+
// inherit this one's target instead, or focus lands on a detached node.
|
|
462
|
+
for (const other of this.walk()) {
|
|
463
|
+
if (
|
|
464
|
+
other !== node &&
|
|
465
|
+
node.restoreTo !== undefined &&
|
|
466
|
+
other.restoreTo !== undefined &&
|
|
467
|
+
node.nodes?.current !== undefined &&
|
|
468
|
+
node.nodes.current !== null &&
|
|
469
|
+
isInScope(other.restoreTo, node.nodes.current)
|
|
470
|
+
) {
|
|
471
|
+
other.restoreTo = node.restoreTo;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const parent = node.parent;
|
|
476
|
+
if (parent !== undefined) {
|
|
477
|
+
parent.remove(node);
|
|
478
|
+
for (const child of node.children) parent.add(child);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
this.#byNodes.delete(node.nodes);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
*walk(from: ScopeNode = this.root): Generator<ScopeNode> {
|
|
485
|
+
if (from.nodes !== null) yield from;
|
|
486
|
+
for (const child of from.children) yield* this.walk(child);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
clone(): ScopeTree {
|
|
490
|
+
const copy = new ScopeTree();
|
|
491
|
+
for (const node of this.walk()) {
|
|
492
|
+
copy.attach(
|
|
493
|
+
node.nodes,
|
|
494
|
+
node.parent?.nodes ?? null,
|
|
495
|
+
node.restoreTo as HTMLElement | undefined,
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
return copy;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const scopeTree = new ScopeTree();
|
|
503
|
+
let activeScope: ScopeNodes | null = null;
|
|
504
|
+
|
|
505
|
+
const RESTORE_EVENT = "barq-focus-scope-restore";
|
|
506
|
+
|
|
507
|
+
function isInScope(element: Element | null | undefined, scope: Element[] | null): boolean {
|
|
508
|
+
if (element === null || element === undefined || scope === null) return false;
|
|
509
|
+
return scope.some((node) => contains(node, element));
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function isInAnyChildScope(element: Element, from: ScopeNodes | null = null): boolean {
|
|
513
|
+
// The top layer is above every scope, so focus is always allowed there.
|
|
514
|
+
if (element instanceof Element && element.closest(`[${TOP_LAYER_ATTRIBUTE}]`) !== null) {
|
|
515
|
+
return true;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// `contains` covers child scopes that are DOM descendants; the walk covers
|
|
519
|
+
// the ones rendered through a portal.
|
|
520
|
+
for (const node of scopeTree.walk(scopeTree.get(from) ?? scopeTree.root)) {
|
|
521
|
+
if (node.nodes !== null && isInScope(element, node.nodes.current)) return true;
|
|
522
|
+
}
|
|
523
|
+
return false;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function isAncestorScope(ancestor: ScopeNodes | null, scope: ScopeNodes | null): boolean {
|
|
527
|
+
let parent = scopeTree.get(scope)?.parent;
|
|
528
|
+
while (parent !== undefined) {
|
|
529
|
+
if (parent.nodes === ancestor) return true;
|
|
530
|
+
parent = parent.parent;
|
|
531
|
+
}
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Whether this scope may act on a focus event, given which scope is active.
|
|
537
|
+
*
|
|
538
|
+
* A contained scope between the active one and this one owns focus, so an
|
|
539
|
+
* ancestor must not pull it out.
|
|
540
|
+
*/
|
|
541
|
+
function shouldContainFocus(scope: ScopeNodes): boolean {
|
|
542
|
+
let node = scopeTree.get(activeScope);
|
|
543
|
+
while (node !== undefined && node.nodes !== scope) {
|
|
544
|
+
if (node.contain) return false;
|
|
545
|
+
node = node.parent;
|
|
546
|
+
}
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function shouldRestoreFocus(scope: ScopeNodes): boolean {
|
|
551
|
+
let node = scopeTree.get(activeScope);
|
|
552
|
+
while (node !== undefined && node.nodes !== scope) {
|
|
553
|
+
if (node.restoreTo !== undefined) return false;
|
|
554
|
+
node = node.parent;
|
|
555
|
+
}
|
|
556
|
+
return node?.nodes === scope;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function focusElement(element: Element | null, scroll = false): void {
|
|
560
|
+
if (element === null) return;
|
|
561
|
+
try {
|
|
562
|
+
if (scroll) (element as HTMLElement).focus();
|
|
563
|
+
else focusSafely(element as HTMLElement);
|
|
564
|
+
} catch {
|
|
565
|
+
// A detached or non-focusable element; nothing to do.
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
function scopeRoot(scope: Element[]): Element {
|
|
570
|
+
return scope[0]?.parentElement as Element;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function firstInScope(scope: Element[], tabbable = true): Element | null {
|
|
574
|
+
const sentinel = scope[0]?.previousElementSibling;
|
|
575
|
+
if (sentinel === null || sentinel === undefined) return null;
|
|
576
|
+
|
|
577
|
+
let walker = focusableWalker(scopeRoot(scope), { tabbable }, scope);
|
|
578
|
+
walker.currentNode = sentinel;
|
|
579
|
+
let next = walker.nextNode();
|
|
580
|
+
|
|
581
|
+
// Nothing tabbable does not mean nothing focusable.
|
|
582
|
+
if (tabbable && next === null) {
|
|
583
|
+
walker = focusableWalker(scopeRoot(scope), { tabbable: false }, scope);
|
|
584
|
+
walker.currentNode = sentinel;
|
|
585
|
+
next = walker.nextNode();
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
return next as Element | null;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// ---------------------------------------------------------------------------
|
|
592
|
+
// The focus manager
|
|
593
|
+
// ---------------------------------------------------------------------------
|
|
594
|
+
|
|
595
|
+
export interface FocusManager {
|
|
596
|
+
focusNext(options?: FocusWalkerOptions): Element | null;
|
|
597
|
+
focusPrevious(options?: FocusWalkerOptions): Element | null;
|
|
598
|
+
focusFirst(options?: FocusWalkerOptions): Element | null;
|
|
599
|
+
focusLast(options?: FocusWalkerOptions): Element | null;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
interface FocusScopeContextValue {
|
|
603
|
+
manager: FocusManager;
|
|
604
|
+
node: ScopeNode;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
const FocusScopeContext = createContext<FocusScopeContextValue | null>(null);
|
|
608
|
+
|
|
609
|
+
/** The manager for the nearest enclosing {@link focusScope}. */
|
|
610
|
+
export function focusManager(): FocusManager | undefined {
|
|
611
|
+
return getContext(FocusScopeContext)?.manager;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function last(walker: FocusWalker): Element | null {
|
|
615
|
+
return walker.last() as Element | null;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
function managerForScope(nodes: ScopeNodes): FocusManager {
|
|
619
|
+
const scopeOf = (): Element[] | null => nodes.current;
|
|
620
|
+
|
|
621
|
+
return {
|
|
622
|
+
focusNext(options = {}) {
|
|
623
|
+
const scope = scopeOf();
|
|
624
|
+
if (scope === null || scope.length === 0) return null;
|
|
625
|
+
const { from, tabbable, wrap, accept } = options;
|
|
626
|
+
const start = from ?? activeElement(ownerDocument(scope[0]));
|
|
627
|
+
const sentinel = scope[0]?.previousElementSibling as Element;
|
|
628
|
+
const walker = focusableWalker(scopeRoot(scope), { tabbable, accept }, scope);
|
|
629
|
+
walker.currentNode = isInScope(start, scope) ? (start as Element) : sentinel;
|
|
630
|
+
let next = walker.nextNode() as Element | null;
|
|
631
|
+
if (next === null && wrap === true) {
|
|
632
|
+
walker.currentNode = sentinel;
|
|
633
|
+
next = walker.nextNode() as Element | null;
|
|
634
|
+
}
|
|
635
|
+
if (next !== null) focusElement(next, true);
|
|
636
|
+
return next;
|
|
637
|
+
},
|
|
638
|
+
|
|
639
|
+
focusPrevious(options = {}) {
|
|
640
|
+
const scope = scopeOf();
|
|
641
|
+
if (scope === null || scope.length === 0) return null;
|
|
642
|
+
const { from, tabbable, wrap, accept } = options;
|
|
643
|
+
const start = from ?? activeElement(ownerDocument(scope[0]));
|
|
644
|
+
const sentinel = scope[scope.length - 1]?.nextElementSibling as Element;
|
|
645
|
+
const walker = focusableWalker(scopeRoot(scope), { tabbable, accept }, scope);
|
|
646
|
+
walker.currentNode = isInScope(start, scope) ? (start as Element) : sentinel;
|
|
647
|
+
let previous = walker.previousNode() as Element | null;
|
|
648
|
+
if (previous === null && wrap === true) {
|
|
649
|
+
walker.currentNode = sentinel;
|
|
650
|
+
previous = walker.previousNode() as Element | null;
|
|
651
|
+
}
|
|
652
|
+
if (previous !== null) focusElement(previous, true);
|
|
653
|
+
return previous;
|
|
654
|
+
},
|
|
655
|
+
|
|
656
|
+
focusFirst(options = {}) {
|
|
657
|
+
const scope = scopeOf();
|
|
658
|
+
if (scope === null || scope.length === 0) return null;
|
|
659
|
+
const walker = focusableWalker(
|
|
660
|
+
scopeRoot(scope),
|
|
661
|
+
{ tabbable: options.tabbable, accept: options.accept },
|
|
662
|
+
scope,
|
|
663
|
+
);
|
|
664
|
+
walker.currentNode = scope[0]?.previousElementSibling as Element;
|
|
665
|
+
const next = walker.nextNode() as Element | null;
|
|
666
|
+
if (next !== null) focusElement(next, true);
|
|
667
|
+
return next;
|
|
668
|
+
},
|
|
669
|
+
|
|
670
|
+
focusLast(options = {}) {
|
|
671
|
+
const scope = scopeOf();
|
|
672
|
+
if (scope === null || scope.length === 0) return null;
|
|
673
|
+
const walker = focusableWalker(
|
|
674
|
+
scopeRoot(scope),
|
|
675
|
+
{ tabbable: options.tabbable, accept: options.accept },
|
|
676
|
+
scope,
|
|
677
|
+
);
|
|
678
|
+
walker.currentNode = scope[scope.length - 1]?.nextElementSibling as Element;
|
|
679
|
+
const previous = walker.previousNode() as Element | null;
|
|
680
|
+
if (previous !== null) focusElement(previous, true);
|
|
681
|
+
return previous;
|
|
682
|
+
},
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* A focus manager over one element's subtree, with no scope tree involved.
|
|
688
|
+
*
|
|
689
|
+
* For a composite widget moving focus among its own children — a toolbar, a
|
|
690
|
+
* tab list — where nothing needs containing or restoring.
|
|
691
|
+
*/
|
|
692
|
+
export function createFocusManager(
|
|
693
|
+
ref: MaybeAccessor<Element | null | undefined>,
|
|
694
|
+
defaults: FocusWalkerOptions = {},
|
|
695
|
+
): FocusManager {
|
|
696
|
+
const rootOf = (): Element | null => (access(ref) as Element | null) ?? null;
|
|
697
|
+
|
|
698
|
+
return {
|
|
699
|
+
focusNext(options = {}) {
|
|
700
|
+
const root = rootOf();
|
|
701
|
+
if (root === null) return null;
|
|
702
|
+
const {
|
|
703
|
+
from,
|
|
704
|
+
tabbable = defaults.tabbable,
|
|
705
|
+
wrap = defaults.wrap,
|
|
706
|
+
accept = defaults.accept,
|
|
707
|
+
} = options;
|
|
708
|
+
const start = from ?? activeElement(ownerDocument(root));
|
|
709
|
+
const walker = focusableWalker(root, { tabbable, accept });
|
|
710
|
+
if (contains(root, start)) walker.currentNode = start as Element;
|
|
711
|
+
let next = walker.nextNode() as Element | null;
|
|
712
|
+
if (next === null && wrap === true) {
|
|
713
|
+
walker.currentNode = root;
|
|
714
|
+
next = walker.nextNode() as Element | null;
|
|
715
|
+
}
|
|
716
|
+
if (next !== null) focusElement(next, true);
|
|
717
|
+
return next;
|
|
718
|
+
},
|
|
719
|
+
|
|
720
|
+
focusPrevious(options = {}) {
|
|
721
|
+
const root = rootOf();
|
|
722
|
+
if (root === null) return null;
|
|
723
|
+
const {
|
|
724
|
+
from,
|
|
725
|
+
tabbable = defaults.tabbable,
|
|
726
|
+
wrap = defaults.wrap,
|
|
727
|
+
accept = defaults.accept,
|
|
728
|
+
} = options;
|
|
729
|
+
const start = from ?? activeElement(ownerDocument(root));
|
|
730
|
+
const walker = focusableWalker(root, { tabbable, accept });
|
|
731
|
+
if (!contains(root, start)) {
|
|
732
|
+
const next = last(walker);
|
|
733
|
+
if (next !== null) focusElement(next, true);
|
|
734
|
+
return next;
|
|
735
|
+
}
|
|
736
|
+
walker.currentNode = start as Element;
|
|
737
|
+
let previous = walker.previousNode() as Element | null;
|
|
738
|
+
if (previous === null && wrap === true) {
|
|
739
|
+
walker.currentNode = root;
|
|
740
|
+
previous = last(walker);
|
|
741
|
+
}
|
|
742
|
+
if (previous !== null) focusElement(previous, true);
|
|
743
|
+
return previous;
|
|
744
|
+
},
|
|
745
|
+
|
|
746
|
+
focusFirst(options = {}) {
|
|
747
|
+
const root = rootOf();
|
|
748
|
+
if (root === null) return null;
|
|
749
|
+
const walker = focusableWalker(root, {
|
|
750
|
+
tabbable: options.tabbable ?? defaults.tabbable,
|
|
751
|
+
accept: options.accept ?? defaults.accept,
|
|
752
|
+
});
|
|
753
|
+
const next = walker.nextNode() as Element | null;
|
|
754
|
+
if (next !== null) focusElement(next, true);
|
|
755
|
+
return next;
|
|
756
|
+
},
|
|
757
|
+
|
|
758
|
+
focusLast(options = {}) {
|
|
759
|
+
const root = rootOf();
|
|
760
|
+
if (root === null) return null;
|
|
761
|
+
const walker = focusableWalker(root, {
|
|
762
|
+
tabbable: options.tabbable ?? defaults.tabbable,
|
|
763
|
+
accept: options.accept ?? defaults.accept,
|
|
764
|
+
});
|
|
765
|
+
const next = last(walker);
|
|
766
|
+
if (next !== null) focusElement(next, true);
|
|
767
|
+
return next;
|
|
768
|
+
},
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// ---------------------------------------------------------------------------
|
|
773
|
+
// focusScope
|
|
774
|
+
// ---------------------------------------------------------------------------
|
|
775
|
+
|
|
776
|
+
export interface FocusScopeOptions {
|
|
777
|
+
/** Keep Tab inside the scope. */
|
|
778
|
+
contain?: MaybeAccessor<boolean | undefined>;
|
|
779
|
+
/** Put focus back where it was when the scope goes away. */
|
|
780
|
+
restoreFocus?: MaybeAccessor<boolean | undefined>;
|
|
781
|
+
/** Focus the first tabbable element on mount. */
|
|
782
|
+
autoFocus?: MaybeAccessor<boolean | undefined>;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
export interface FocusScopeResult {
|
|
786
|
+
/** Ref for the sentinel before the scope's contents. */
|
|
787
|
+
startRef: (element: Element | null) => void;
|
|
788
|
+
/** Ref for the sentinel after the scope's contents. */
|
|
789
|
+
endRef: (element: Element | null) => void;
|
|
790
|
+
/** Programmatic focus movement within the scope. */
|
|
791
|
+
manager: FocusManager;
|
|
792
|
+
/** Re-read which elements lie between the sentinels. */
|
|
793
|
+
refresh: () => void;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* A region that owns focus.
|
|
798
|
+
*
|
|
799
|
+
* The two sentinels delimit it: everything between them is the scope, which is
|
|
800
|
+
* how a scope whose content is a fragment, or changes shape, still has an
|
|
801
|
+
* unambiguous extent. Render them hidden, immediately around the content.
|
|
802
|
+
*
|
|
803
|
+
* ```tsx
|
|
804
|
+
* const scope = focusScope({ contain: true, restoreFocus: true, autoFocus: true });
|
|
805
|
+
* <>
|
|
806
|
+
* <span hidden ref={scope.startRef} />
|
|
807
|
+
* {props.children}
|
|
808
|
+
* <span hidden ref={scope.endRef} />
|
|
809
|
+
* </>
|
|
810
|
+
* ```
|
|
811
|
+
*/
|
|
812
|
+
export function focusScope(options: FocusScopeOptions = {}): FocusScopeResult {
|
|
813
|
+
const nodes = new ScopeNodes();
|
|
814
|
+
const parentContext = getContext(FocusScopeContext);
|
|
815
|
+
const node = new ScopeNode(nodes);
|
|
816
|
+
const manager = managerForScope(nodes);
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* Where focus goes when the scope is disposed.
|
|
820
|
+
*
|
|
821
|
+
* Captured when the scope's content MOUNTS rather than when the hook is
|
|
822
|
+
* called, and the difference is the whole point: an overlay's scope is
|
|
823
|
+
* created with its component and opened much later, so reading focus here
|
|
824
|
+
* would record whatever had it at mount. A submenu opened from a menu item
|
|
825
|
+
* then restored focus to the MENU, not to the item that opened it.
|
|
826
|
+
*
|
|
827
|
+
* Still before any child with `autoFocus` can take focus away, because the
|
|
828
|
+
* sentinel's ref runs before the content between the sentinels is built.
|
|
829
|
+
*
|
|
830
|
+
* A signal, because the effect below copies it onto the scope node and has
|
|
831
|
+
* to run again when a scope that was created closed reopens.
|
|
832
|
+
*/
|
|
833
|
+
const [restoreTo, setRestoreTo] = createSignal<HTMLElement | SVGElement | null>(null, {
|
|
834
|
+
ownedWrite: true,
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
const stopEvent = (event: Event): void => event.stopPropagation();
|
|
838
|
+
|
|
839
|
+
if (isServer) {
|
|
840
|
+
return {
|
|
841
|
+
startRef: () => {},
|
|
842
|
+
endRef: () => {},
|
|
843
|
+
manager,
|
|
844
|
+
refresh: () => {},
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/**
|
|
849
|
+
* A scope that opens while another is active belongs UNDER that one, even
|
|
850
|
+
* when the context says otherwise: a dialog launched from a menu is a child
|
|
851
|
+
* of the menu's scope, not of whatever wraps its portal.
|
|
852
|
+
*
|
|
853
|
+
* Resolved twice — here, and again when the scope's content MOUNTS. An
|
|
854
|
+
* overlay's scope is created with its component and opened much later, so at
|
|
855
|
+
* this point `activeScope` is whatever was active when the component was
|
|
856
|
+
* built, which for a submenu is nothing at all. Getting it wrong puts the
|
|
857
|
+
* scope beside its parent rather than under it, and focus restoration then
|
|
858
|
+
* refuses to run because the disposing scope is not the active one.
|
|
859
|
+
*/
|
|
860
|
+
const parentOf = (): ScopeNode => {
|
|
861
|
+
let found = parentContext?.node ?? scopeTree.root;
|
|
862
|
+
if (
|
|
863
|
+
scopeTree.get(found.nodes) !== undefined &&
|
|
864
|
+
activeScope !== null &&
|
|
865
|
+
!isAncestorScope(activeScope, found.nodes)
|
|
866
|
+
) {
|
|
867
|
+
const active = scopeTree.get(activeScope);
|
|
868
|
+
if (active !== undefined) found = active;
|
|
869
|
+
}
|
|
870
|
+
return found;
|
|
871
|
+
};
|
|
872
|
+
|
|
873
|
+
parentOf().add(node);
|
|
874
|
+
scopeTree.register(node);
|
|
875
|
+
|
|
876
|
+
createEffect(
|
|
877
|
+
() => access(options.contain) === true,
|
|
878
|
+
(contain) => {
|
|
879
|
+
node.contain = contain;
|
|
880
|
+
},
|
|
881
|
+
);
|
|
882
|
+
|
|
883
|
+
const startRef = (element: Element | null): void => {
|
|
884
|
+
if (element !== null && nodes.start === null) {
|
|
885
|
+
setRestoreTo(activeElement(ownerDocument(element)) as HTMLElement | SVGElement | null);
|
|
886
|
+
const now = parentOf();
|
|
887
|
+
if (now !== node.parent && now !== node) {
|
|
888
|
+
node.parent?.remove(node);
|
|
889
|
+
now.add(node);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
nodes.start = element;
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
const endRef = (element: Element | null): void => {
|
|
896
|
+
nodes.end = element;
|
|
897
|
+
};
|
|
898
|
+
|
|
899
|
+
// The sentinels are siblings only once the fragment holding them has been
|
|
900
|
+
// inserted, which is after the refs have run.
|
|
901
|
+
onSettled(() => nodes.watch());
|
|
902
|
+
|
|
903
|
+
// A restore inside a nested scope must not reach this one.
|
|
904
|
+
createEffect(
|
|
905
|
+
() => nodes.read(),
|
|
906
|
+
(scope) => {
|
|
907
|
+
if (scope === null) return undefined;
|
|
908
|
+
for (const element of scope) element.addEventListener(RESTORE_EVENT, stopEvent);
|
|
909
|
+
return () => {
|
|
910
|
+
for (const element of scope) element.removeEventListener(RESTORE_EVENT, stopEvent);
|
|
911
|
+
};
|
|
912
|
+
},
|
|
913
|
+
);
|
|
914
|
+
|
|
915
|
+
// Published to the subtree, so a nested scope parents itself here and a
|
|
916
|
+
// descendant can reach this scope's manager.
|
|
917
|
+
const owner = getOwner();
|
|
918
|
+
if (owner !== null) {
|
|
919
|
+
const value: FocusScopeContextValue = { manager, node };
|
|
920
|
+
install(owner, FocusScopeContext, () => value);
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
// FIRST, so it runs LAST. Cleanups run in reverse registration order, and
|
|
924
|
+
// this one takes the scope out of the tree — which is what
|
|
925
|
+
// `restoreFocusOnDispose` reads to find where focus came from. Registered
|
|
926
|
+
// after it, it ran first, `scopeTree.get(nodes)` was already undefined, and
|
|
927
|
+
// the restore gave up every time. Nothing caught it: happy-dom leaves
|
|
928
|
+
// `activeElement` on a removed element, so the assertion passed on the
|
|
929
|
+
// element that was already focused.
|
|
930
|
+
tryCleanup(() => {
|
|
931
|
+
const parentScope = scopeTree.get(nodes)?.parent?.nodes ?? null;
|
|
932
|
+
if (
|
|
933
|
+
(nodes === activeScope || isAncestorScope(nodes, activeScope)) &&
|
|
934
|
+
(parentScope === null || scopeTree.get(parentScope) !== undefined)
|
|
935
|
+
) {
|
|
936
|
+
activeScope = parentScope;
|
|
937
|
+
}
|
|
938
|
+
scopeTree.remove(nodes);
|
|
939
|
+
});
|
|
940
|
+
|
|
941
|
+
containFocus(nodes, options);
|
|
942
|
+
restoreFocusOnDispose(nodes, options, restoreTo);
|
|
943
|
+
trackActiveScope(nodes, options);
|
|
944
|
+
autoFocusOnMount(nodes, options);
|
|
945
|
+
|
|
946
|
+
return { startRef, endRef, manager, refresh: () => nodes.bump() };
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
function containFocus(nodes: ScopeNodes, options: FocusScopeOptions): void {
|
|
950
|
+
let focusedNode: Element | undefined;
|
|
951
|
+
let frame: number | undefined;
|
|
952
|
+
|
|
953
|
+
createEffect(
|
|
954
|
+
() => [nodes.read(), access(options.contain)] as const,
|
|
955
|
+
([scope, contain]) => {
|
|
956
|
+
if (contain !== true) {
|
|
957
|
+
if (frame !== undefined) {
|
|
958
|
+
cancelAnimationFrame(frame);
|
|
959
|
+
frame = undefined;
|
|
960
|
+
}
|
|
961
|
+
return undefined;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
const doc = ownerDocument(scope?.[0]);
|
|
965
|
+
|
|
966
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
967
|
+
if (
|
|
968
|
+
event.key !== "Tab" ||
|
|
969
|
+
event.altKey ||
|
|
970
|
+
event.ctrlKey ||
|
|
971
|
+
event.metaKey ||
|
|
972
|
+
!shouldContainFocus(nodes) ||
|
|
973
|
+
event.isComposing
|
|
974
|
+
) {
|
|
975
|
+
return undefined;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
const current = activeElement(doc);
|
|
979
|
+
const live = nodes.current;
|
|
980
|
+
if (live === null || live.length === 0 || !isInScope(current, live)) return;
|
|
981
|
+
|
|
982
|
+
const walker = focusableWalker(scopeRoot(live), { tabbable: true }, live);
|
|
983
|
+
if (current === null) return;
|
|
984
|
+
walker.currentNode = current;
|
|
985
|
+
let next = (event.shiftKey ? walker.previousNode() : walker.nextNode()) as Element | null;
|
|
986
|
+
|
|
987
|
+
if (next === null) {
|
|
988
|
+
walker.currentNode = (
|
|
989
|
+
event.shiftKey
|
|
990
|
+
? live[live.length - 1]?.nextElementSibling
|
|
991
|
+
: live[0]?.previousElementSibling
|
|
992
|
+
) as Element;
|
|
993
|
+
next = (event.shiftKey ? walker.previousNode() : walker.nextNode()) as Element | null;
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
event.preventDefault();
|
|
997
|
+
if (next !== null) {
|
|
998
|
+
focusElement(next, true);
|
|
999
|
+
if (next instanceof ownerWindow(next).HTMLInputElement) next.select();
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
|
|
1003
|
+
const onFocus = (event: Event): void => {
|
|
1004
|
+
const target = targetElement(event);
|
|
1005
|
+
if (
|
|
1006
|
+
(activeScope === null || isAncestorScope(activeScope, nodes)) &&
|
|
1007
|
+
isInScope(target, nodes.current)
|
|
1008
|
+
) {
|
|
1009
|
+
// A child scope taking focus becomes the active one; moving out to an
|
|
1010
|
+
// ancestor is not allowed.
|
|
1011
|
+
activeScope = nodes;
|
|
1012
|
+
focusedNode = target ?? undefined;
|
|
1013
|
+
} else if (
|
|
1014
|
+
shouldContainFocus(nodes) &&
|
|
1015
|
+
target !== null &&
|
|
1016
|
+
!isInAnyChildScope(target, nodes)
|
|
1017
|
+
) {
|
|
1018
|
+
// Focus arrived from outside the scope entirely, e.g. the user tabbed
|
|
1019
|
+
// in from the browser's address bar.
|
|
1020
|
+
if (focusedNode !== undefined) focusElement(focusedNode);
|
|
1021
|
+
else if (activeScope?.current !== undefined && activeScope.current !== null) {
|
|
1022
|
+
focusElement(firstInScope(activeScope.current));
|
|
1023
|
+
}
|
|
1024
|
+
} else if (shouldContainFocus(nodes)) {
|
|
1025
|
+
focusedNode = target ?? undefined;
|
|
1026
|
+
}
|
|
1027
|
+
};
|
|
1028
|
+
|
|
1029
|
+
const onBlur = (event: Event): void => {
|
|
1030
|
+
// Firefox does not put focus back without the frame.
|
|
1031
|
+
if (frame !== undefined) cancelAnimationFrame(frame);
|
|
1032
|
+
frame = requestAnimationFrame(() => {
|
|
1033
|
+
// TalkBack on Chrome cannot move its virtual cursor out of a contained
|
|
1034
|
+
// scope, and coerces focus back forever. Focus is allowed to leave
|
|
1035
|
+
// there, which is narrower than it sounds: only that combination.
|
|
1036
|
+
const how = getInteractionModality();
|
|
1037
|
+
const skip = (how === "virtual" || how === null) && isAndroid() && isChrome();
|
|
1038
|
+
|
|
1039
|
+
const active = activeElement(doc);
|
|
1040
|
+
if (
|
|
1041
|
+
skip ||
|
|
1042
|
+
active === null ||
|
|
1043
|
+
!shouldContainFocus(nodes) ||
|
|
1044
|
+
isInAnyChildScope(active, nodes)
|
|
1045
|
+
) {
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
activeScope = nodes;
|
|
1050
|
+
const target = targetElement(event);
|
|
1051
|
+
if (target !== null && target.isConnected) {
|
|
1052
|
+
focusedNode = target;
|
|
1053
|
+
focusElement(focusedNode);
|
|
1054
|
+
} else if (activeScope.current !== null) {
|
|
1055
|
+
focusElement(firstInScope(activeScope.current));
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1058
|
+
};
|
|
1059
|
+
|
|
1060
|
+
doc.addEventListener("keydown", onKeyDown as EventListener, false);
|
|
1061
|
+
doc.addEventListener("focusin", onFocus, false);
|
|
1062
|
+
for (const element of scope ?? []) {
|
|
1063
|
+
element.addEventListener("focusin", onFocus, false);
|
|
1064
|
+
element.addEventListener("focusout", onBlur, false);
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
return () => {
|
|
1068
|
+
doc.removeEventListener("keydown", onKeyDown as EventListener, false);
|
|
1069
|
+
doc.removeEventListener("focusin", onFocus, false);
|
|
1070
|
+
for (const element of scope ?? []) {
|
|
1071
|
+
element.removeEventListener("focusin", onFocus, false);
|
|
1072
|
+
element.removeEventListener("focusout", onBlur, false);
|
|
1073
|
+
}
|
|
1074
|
+
if (frame !== undefined) cancelAnimationFrame(frame);
|
|
1075
|
+
};
|
|
1076
|
+
},
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
function trackActiveScope(nodes: ScopeNodes, options: FocusScopeOptions): void {
|
|
1081
|
+
createEffect(
|
|
1082
|
+
() => [nodes.read(), access(options.restoreFocus), access(options.contain)] as const,
|
|
1083
|
+
([scope, restoreFocus, contain]) => {
|
|
1084
|
+
// Containment and restoration each track this themselves.
|
|
1085
|
+
if (restoreFocus === true || contain === true) return undefined;
|
|
1086
|
+
|
|
1087
|
+
const doc = ownerDocument(scope?.[0]);
|
|
1088
|
+
|
|
1089
|
+
const onFocus = (event: Event): void => {
|
|
1090
|
+
const target = targetElement(event);
|
|
1091
|
+
if (target === null) return;
|
|
1092
|
+
if (isInScope(target, nodes.current)) activeScope = nodes;
|
|
1093
|
+
else if (!isInAnyChildScope(target)) activeScope = null;
|
|
1094
|
+
};
|
|
1095
|
+
|
|
1096
|
+
doc.addEventListener("focusin", onFocus, false);
|
|
1097
|
+
for (const element of scope ?? []) element.addEventListener("focusin", onFocus, false);
|
|
1098
|
+
|
|
1099
|
+
return () => {
|
|
1100
|
+
doc.removeEventListener("focusin", onFocus, false);
|
|
1101
|
+
for (const element of scope ?? []) element.removeEventListener("focusin", onFocus, false);
|
|
1102
|
+
};
|
|
1103
|
+
},
|
|
1104
|
+
);
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
function autoFocusOnMount(nodes: ScopeNodes, options: FocusScopeOptions): void {
|
|
1108
|
+
const [pending, setPending] = createSignal(access(options.autoFocus) === true);
|
|
1109
|
+
|
|
1110
|
+
createEffect(
|
|
1111
|
+
() => [nodes.read(), pending()] as const,
|
|
1112
|
+
([scope, isPending]) => {
|
|
1113
|
+
if (!isPending) return;
|
|
1114
|
+
if (scope === null || scope.length === 0) return;
|
|
1115
|
+
setPending(false);
|
|
1116
|
+
|
|
1117
|
+
activeScope = nodes;
|
|
1118
|
+
const doc = ownerDocument(scope[0]);
|
|
1119
|
+
if (!isInScope(activeElement(doc), scope)) focusElement(firstInScope(scope));
|
|
1120
|
+
},
|
|
1121
|
+
);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
function restoreFocusOnDispose(
|
|
1125
|
+
nodes: ScopeNodes,
|
|
1126
|
+
options: FocusScopeOptions,
|
|
1127
|
+
restoreTo: Accessor<HTMLElement | SVGElement | null>,
|
|
1128
|
+
): void {
|
|
1129
|
+
// A restoring scope that is not containing still has to know when it becomes
|
|
1130
|
+
// active, so it can decide whether the restore is its to perform.
|
|
1131
|
+
createEffect(
|
|
1132
|
+
() => [nodes.read(), access(options.restoreFocus), access(options.contain)] as const,
|
|
1133
|
+
([scope, restoreFocus, contain]) => {
|
|
1134
|
+
if (restoreFocus !== true || contain === true) return undefined;
|
|
1135
|
+
|
|
1136
|
+
const doc = ownerDocument(scope?.[0]);
|
|
1137
|
+
|
|
1138
|
+
const onFocus = (): void => {
|
|
1139
|
+
if (
|
|
1140
|
+
(activeScope === null || isAncestorScope(activeScope, nodes)) &&
|
|
1141
|
+
isInScope(activeElement(doc), nodes.current)
|
|
1142
|
+
) {
|
|
1143
|
+
activeScope = nodes;
|
|
1144
|
+
}
|
|
1145
|
+
};
|
|
1146
|
+
|
|
1147
|
+
doc.addEventListener("focusin", onFocus, false);
|
|
1148
|
+
for (const element of scope ?? []) element.addEventListener("focusin", onFocus, false);
|
|
1149
|
+
|
|
1150
|
+
return () => {
|
|
1151
|
+
doc.removeEventListener("focusin", onFocus, false);
|
|
1152
|
+
for (const element of scope ?? []) element.removeEventListener("focusin", onFocus, false);
|
|
1153
|
+
};
|
|
1154
|
+
},
|
|
1155
|
+
);
|
|
1156
|
+
|
|
1157
|
+
// Tabbing out of a restoring scope goes to what follows the element focus
|
|
1158
|
+
// came from, not to what follows the scope in the DOM. For an overlay in a
|
|
1159
|
+
// portal at the end of the body those are very different places.
|
|
1160
|
+
createEffect(
|
|
1161
|
+
() => [access(options.restoreFocus), access(options.contain)] as const,
|
|
1162
|
+
([restoreFocus, contain]) => {
|
|
1163
|
+
if (restoreFocus !== true || contain === true) return undefined;
|
|
1164
|
+
|
|
1165
|
+
const doc = ownerDocument(nodes.current?.[0]);
|
|
1166
|
+
|
|
1167
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
1168
|
+
if (
|
|
1169
|
+
event.key !== "Tab" ||
|
|
1170
|
+
event.altKey ||
|
|
1171
|
+
event.ctrlKey ||
|
|
1172
|
+
event.metaKey ||
|
|
1173
|
+
!shouldContainFocus(nodes) ||
|
|
1174
|
+
event.isComposing
|
|
1175
|
+
) {
|
|
1176
|
+
return undefined;
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
const current = doc.activeElement;
|
|
1180
|
+
if (current === null) return;
|
|
1181
|
+
if (!isInAnyChildScope(current, nodes) || !shouldRestoreFocus(nodes)) return;
|
|
1182
|
+
|
|
1183
|
+
const scopeNode = scopeTree.get(nodes);
|
|
1184
|
+
if (scopeNode === undefined) return;
|
|
1185
|
+
|
|
1186
|
+
let target = scopeNode.restoreTo;
|
|
1187
|
+
const walker = focusableWalker(doc.body, { tabbable: true });
|
|
1188
|
+
walker.currentNode = current;
|
|
1189
|
+
let next = (event.shiftKey ? walker.previousNode() : walker.nextNode()) as Element | null;
|
|
1190
|
+
|
|
1191
|
+
if (target === undefined || !target.isConnected || (target as Element) === doc.body) {
|
|
1192
|
+
target = undefined;
|
|
1193
|
+
scopeNode.restoreTo = undefined;
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
if ((next === null || !isInAnyChildScope(next, nodes)) && target !== undefined) {
|
|
1197
|
+
walker.currentNode = target;
|
|
1198
|
+
// Step over the scope itself, in case it immediately follows.
|
|
1199
|
+
do {
|
|
1200
|
+
next = (event.shiftKey ? walker.previousNode() : walker.nextNode()) as Element | null;
|
|
1201
|
+
} while (next !== null && isInAnyChildScope(next, nodes));
|
|
1202
|
+
|
|
1203
|
+
event.preventDefault();
|
|
1204
|
+
event.stopPropagation();
|
|
1205
|
+
if (next !== null) {
|
|
1206
|
+
focusElement(next, true);
|
|
1207
|
+
} else if (!isInAnyChildScope(target)) {
|
|
1208
|
+
// Leaving the outermost scope with nowhere to go: the browser chrome
|
|
1209
|
+
// is next, so give focus up entirely.
|
|
1210
|
+
(current as HTMLElement).blur();
|
|
1211
|
+
} else {
|
|
1212
|
+
focusElement(target, true);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
};
|
|
1216
|
+
|
|
1217
|
+
doc.addEventListener("keydown", onKeyDown as EventListener, true);
|
|
1218
|
+
return () => doc.removeEventListener("keydown", onKeyDown as EventListener, true);
|
|
1219
|
+
},
|
|
1220
|
+
);
|
|
1221
|
+
|
|
1222
|
+
createEffect(
|
|
1223
|
+
() => (access(options.restoreFocus) === true ? { value: restoreTo() } : null),
|
|
1224
|
+
(restoring) => {
|
|
1225
|
+
if (restoring === null) return;
|
|
1226
|
+
|
|
1227
|
+
const scopeNode = scopeTree.get(nodes);
|
|
1228
|
+
if (scopeNode === undefined) return;
|
|
1229
|
+
scopeNode.restoreTo = restoring.value ?? undefined;
|
|
1230
|
+
},
|
|
1231
|
+
);
|
|
1232
|
+
|
|
1233
|
+
/**
|
|
1234
|
+
* Put focus back where it came from.
|
|
1235
|
+
*
|
|
1236
|
+
* Runs when the scope is DISPOSED, which is why an overlay has to create its
|
|
1237
|
+
* scope with its content rather than beside it: `<Modal>` used to call
|
|
1238
|
+
* `focusScope` in the component body and render the sentinels inside a
|
|
1239
|
+
* `<Show>`, so closing removed the content and left the scope alive. Nothing
|
|
1240
|
+
* was disposed, nothing restored, and focus stayed on `<body>` with the
|
|
1241
|
+
* trigger unreachable — invisible under happy-dom, where removing the focused
|
|
1242
|
+
* element leaves `activeElement` where it was.
|
|
1243
|
+
*/
|
|
1244
|
+
const restore = (previous: Element[] | null): void => {
|
|
1245
|
+
if (access(options.restoreFocus) !== true) return;
|
|
1246
|
+
|
|
1247
|
+
const doc = ownerDocument(previous?.[0] ?? nodes.current?.[0]);
|
|
1248
|
+
const scopeNode = scopeTree.get(nodes);
|
|
1249
|
+
const target = scopeNode?.restoreTo;
|
|
1250
|
+
const active = activeElement(doc);
|
|
1251
|
+
|
|
1252
|
+
if (
|
|
1253
|
+
target === undefined ||
|
|
1254
|
+
!(
|
|
1255
|
+
(active !== null && isInAnyChildScope(active, nodes)) ||
|
|
1256
|
+
(active === doc.body && shouldRestoreFocus(nodes))
|
|
1257
|
+
)
|
|
1258
|
+
) {
|
|
1259
|
+
return;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
// The tree may be about to lose these nodes, so the search runs against a
|
|
1263
|
+
// copy taken now.
|
|
1264
|
+
const frozen = scopeTree.clone();
|
|
1265
|
+
requestAnimationFrame(() => {
|
|
1266
|
+
// Only when focus actually fell to the body. Anything else means
|
|
1267
|
+
// something deliberately moved it.
|
|
1268
|
+
if (doc.activeElement !== doc.body) return;
|
|
1269
|
+
|
|
1270
|
+
let at = frozen.get(nodes);
|
|
1271
|
+
while (at !== undefined) {
|
|
1272
|
+
if (at.restoreTo !== undefined && at.restoreTo.isConnected) {
|
|
1273
|
+
restoreFocusTo(at.restoreTo);
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
at = at.parent;
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
at = frozen.get(nodes);
|
|
1280
|
+
while (at !== undefined) {
|
|
1281
|
+
if (
|
|
1282
|
+
at.nodes?.current !== undefined &&
|
|
1283
|
+
at.nodes.current !== null &&
|
|
1284
|
+
scopeTree.get(at.nodes) !== undefined
|
|
1285
|
+
) {
|
|
1286
|
+
const first = firstInScope(at.nodes.current, true);
|
|
1287
|
+
if (first !== null) {
|
|
1288
|
+
restoreFocusTo(first as HTMLElement);
|
|
1289
|
+
return;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
at = at.parent;
|
|
1293
|
+
}
|
|
1294
|
+
});
|
|
1295
|
+
};
|
|
1296
|
+
|
|
1297
|
+
tryCleanup(() => restore(null));
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
/**
|
|
1301
|
+
* Restore focus, announcing it first.
|
|
1302
|
+
*
|
|
1303
|
+
* A virtualised collection reuses its DOM nodes, so the element focus came
|
|
1304
|
+
* from may still exist while representing a different item. The event lets an
|
|
1305
|
+
* ancestor cancel and put focus somewhere it chooses instead.
|
|
1306
|
+
*/
|
|
1307
|
+
function restoreFocusTo(element: HTMLElement | SVGElement): void {
|
|
1308
|
+
const proceed = element.dispatchEvent(
|
|
1309
|
+
new CustomEvent(RESTORE_EVENT, { bubbles: true, cancelable: true }),
|
|
1310
|
+
);
|
|
1311
|
+
if (proceed) focusElement(element);
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
/** The context value a focus scope publishes, for nested scopes to find. */
|
|
1315
|
+
export { FocusScopeContext };
|
|
1316
|
+
|
|
1317
|
+
// ---------------------------------------------------------------------------
|
|
1318
|
+
// focusRing
|
|
1319
|
+
// ---------------------------------------------------------------------------
|
|
1320
|
+
|
|
1321
|
+
export interface FocusRingOptions {
|
|
1322
|
+
/** Watch the whole subtree rather than the element itself. */
|
|
1323
|
+
within?: MaybeAccessor<boolean | undefined>;
|
|
1324
|
+
/** Whether the element is a text input, where only Tab and Escape ring it. */
|
|
1325
|
+
isTextInput?: MaybeAccessor<boolean | undefined>;
|
|
1326
|
+
autoFocus?: MaybeAccessor<boolean | undefined>;
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
export interface FocusRingResult {
|
|
1330
|
+
focusProps: DOMProps;
|
|
1331
|
+
/** Whether the element has focus at all. */
|
|
1332
|
+
isFocused: Accessor<boolean>;
|
|
1333
|
+
/** Whether a ring should be drawn: focused, and not reached by pointer. */
|
|
1334
|
+
isFocusVisible: Accessor<boolean>;
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
/**
|
|
1338
|
+
* Whether to draw a focus ring.
|
|
1339
|
+
*
|
|
1340
|
+
* `:focus-visible` cannot answer this for a widget: it applies to the focused
|
|
1341
|
+
* element, and the ring usually belongs on a wrapper; and its heuristic for a
|
|
1342
|
+
* text input rings on every keystroke.
|
|
1343
|
+
*
|
|
1344
|
+
* ```tsx
|
|
1345
|
+
* const { focusProps, isFocusVisible } = focusRing();
|
|
1346
|
+
* <button {...focusProps} data-focus-visible={isFocusVisible} />
|
|
1347
|
+
* ```
|
|
1348
|
+
*/
|
|
1349
|
+
export function focusRing(options: FocusRingOptions = {}): FocusRingResult {
|
|
1350
|
+
const within = (): boolean => access(options.within) === true;
|
|
1351
|
+
|
|
1352
|
+
const visible = focusVisible({
|
|
1353
|
+
isTextInput: access(options.isTextInput),
|
|
1354
|
+
autoFocus: access(options.autoFocus),
|
|
1355
|
+
});
|
|
1356
|
+
|
|
1357
|
+
// Both are created and both are gated, rather than one being chosen here:
|
|
1358
|
+
// `within` may be an accessor, and a props object chosen at call time could
|
|
1359
|
+
// not follow it.
|
|
1360
|
+
const element = focused({ isDisabled: within });
|
|
1361
|
+
const subtree = focusWithin({ isDisabled: () => !within() });
|
|
1362
|
+
|
|
1363
|
+
const isFocused = (): boolean => (within() ? subtree.isFocusWithin() : element.isFocused());
|
|
1364
|
+
|
|
1365
|
+
return {
|
|
1366
|
+
isFocused,
|
|
1367
|
+
isFocusVisible: () => isFocused() && visible(),
|
|
1368
|
+
focusProps: mergeProps(element.focusProps, subtree.focusWithinProps),
|
|
1369
|
+
};
|
|
1370
|
+
}
|