@remit/ui 0.0.180 → 0.0.182
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/package.json +1 -1
- package/src/category-presentation.test.ts +76 -0
- package/src/category-presentation.ts +147 -0
- package/src/components/app-shell-types.ts +4 -63
- package/src/components/app-shell.tsx +2 -5
- package/src/components/bottom-sheet.stories.tsx +56 -0
- package/src/components/bottom-sheet.tsx +3 -2
- package/src/components/brief-section.tsx +1 -0
- package/src/components/brief-sections.render.test.ts +3 -3
- package/src/components/brief-sections.stories.tsx +2 -5
- package/src/components/brief-sections.tsx +8 -22
- package/src/components/category-badge.render.test.ts +21 -9
- package/src/components/category-badge.stories.tsx +3 -2
- package/src/components/category-badge.tsx +5 -39
- package/src/components/category-presentation.stories.tsx +2 -6
- package/src/components/confirm-dialog.tsx +10 -20
- package/src/components/dialog.stories.tsx +156 -0
- package/src/components/dialog.tsx +3 -2
- package/src/components/flow-screen.focus.test.ts +107 -0
- package/src/components/flow-screen.tsx +101 -75
- package/src/components/intelligence-panel.render.test.ts +21 -3
- package/src/components/intelligence-panel.tsx +6 -2
- package/src/components/message-row.render.test.ts +2 -1
- package/src/components/message-row.tsx +3 -2
- package/src/components/self-update-progress-overlay.tsx +7 -48
- package/src/components/slide-panel.tsx +7 -1
- package/src/filter-presets.ts +4 -24
- package/src/index.ts +14 -9
- package/src/lib/brief-filter-query.ts +1 -1
- package/src/lib/brief-filters.ts +1 -1
- package/src/lib/overlay-scope.ts +13 -1
- package/src/lib/use-modal-focus.test.ts +520 -0
- package/src/lib/use-modal-focus.ts +158 -0
- package/src/lib/use-initial-focus.test.ts +0 -78
- package/src/lib/use-initial-focus.ts +0 -36
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import "@remit/test-dom";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
4
|
+
import { act, createElement, useRef } from "react";
|
|
5
|
+
import { createRoot, type Root } from "react-dom/client";
|
|
6
|
+
import { useModalFocus } from "./use-modal-focus.js";
|
|
7
|
+
|
|
8
|
+
let root: Root;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
const container = document.getElementById("root") as unknown as HTMLElement;
|
|
12
|
+
container.innerHTML = "";
|
|
13
|
+
root = createRoot(container);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
act(() => root.unmount());
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
function Panel({ open }: { open: boolean }) {
|
|
21
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
22
|
+
useModalFocus(ref, open);
|
|
23
|
+
return createElement(
|
|
24
|
+
"div",
|
|
25
|
+
{ ref },
|
|
26
|
+
createElement("span", null, "not focusable"),
|
|
27
|
+
createElement("button", { type: "button" }, "First action"),
|
|
28
|
+
createElement("button", { type: "button" }, "Second action"),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const tab = (shiftKey = false): KeyboardEvent => {
|
|
33
|
+
const event = new KeyboardEvent("keydown", {
|
|
34
|
+
key: "Tab",
|
|
35
|
+
shiftKey,
|
|
36
|
+
bubbles: true,
|
|
37
|
+
cancelable: true,
|
|
38
|
+
});
|
|
39
|
+
(document.activeElement ?? document).dispatchEvent(event);
|
|
40
|
+
return event;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const buttonNamed = (name: string): HTMLElement => {
|
|
44
|
+
const match = Array.from(document.querySelectorAll("button")).find(
|
|
45
|
+
(el) => el.textContent === name,
|
|
46
|
+
);
|
|
47
|
+
assert.ok(match, `no button named ${name}`);
|
|
48
|
+
return match;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
describe("useModalFocus", () => {
|
|
52
|
+
it("focuses the first focusable descendant once open", () => {
|
|
53
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
54
|
+
assert.equal(document.activeElement?.textContent, "First action");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("leaves focus on the body while closed", () => {
|
|
58
|
+
act(() => root.render(createElement(Panel, { open: false })));
|
|
59
|
+
assert.equal(document.activeElement, document.body);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("moves focus in when an already-mounted panel opens", () => {
|
|
63
|
+
act(() => root.render(createElement(Panel, { open: false })));
|
|
64
|
+
assert.equal(document.activeElement, document.body);
|
|
65
|
+
|
|
66
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
67
|
+
assert.equal(document.activeElement?.textContent, "First action");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("returns focus to the trigger once the panel closes", () => {
|
|
71
|
+
const trigger = document.createElement("button");
|
|
72
|
+
trigger.textContent = "Open panel";
|
|
73
|
+
document.body.appendChild(trigger);
|
|
74
|
+
trigger.focus();
|
|
75
|
+
assert.equal(document.activeElement, trigger);
|
|
76
|
+
|
|
77
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
78
|
+
assert.equal(document.activeElement?.textContent, "First action");
|
|
79
|
+
|
|
80
|
+
act(() => root.render(createElement(Panel, { open: false })));
|
|
81
|
+
assert.equal(document.activeElement, trigger);
|
|
82
|
+
|
|
83
|
+
trigger.remove();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("does not throw restoring focus to a trigger that left the DOM", () => {
|
|
87
|
+
const trigger = document.createElement("button");
|
|
88
|
+
document.body.appendChild(trigger);
|
|
89
|
+
trigger.focus();
|
|
90
|
+
|
|
91
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
92
|
+
trigger.remove();
|
|
93
|
+
|
|
94
|
+
act(() => root.render(createElement(Panel, { open: false })));
|
|
95
|
+
assert.notEqual(document.activeElement, trigger);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("wraps Tab off the last focusable back to the first", () => {
|
|
99
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
100
|
+
buttonNamed("Second action").focus();
|
|
101
|
+
|
|
102
|
+
const event = tab();
|
|
103
|
+
|
|
104
|
+
assert.equal(event.defaultPrevented, true);
|
|
105
|
+
assert.equal(document.activeElement, buttonNamed("First action"));
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("wraps Shift+Tab off the first focusable back to the last", () => {
|
|
109
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
110
|
+
assert.equal(document.activeElement, buttonNamed("First action"));
|
|
111
|
+
|
|
112
|
+
const event = tab(true);
|
|
113
|
+
|
|
114
|
+
assert.equal(event.defaultPrevented, true);
|
|
115
|
+
assert.equal(document.activeElement, buttonNamed("Second action"));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("leaves Tab alone in the middle of the panel, so the browser moves on", () => {
|
|
119
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
120
|
+
assert.equal(document.activeElement, buttonNamed("First action"));
|
|
121
|
+
|
|
122
|
+
const event = tab();
|
|
123
|
+
|
|
124
|
+
assert.equal(event.defaultPrevented, false);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* An error banner and the fatal-error overlay paint above every modal from
|
|
129
|
+
* outside its subtree, and a trap that reclaims every Tab strands them
|
|
130
|
+
* (#970). Focus already outside the surface is somebody else's.
|
|
131
|
+
*/
|
|
132
|
+
it("leaves focus outside the panel alone", () => {
|
|
133
|
+
const outside = document.createElement("button");
|
|
134
|
+
outside.textContent = "Behind the panel";
|
|
135
|
+
document.body.appendChild(outside);
|
|
136
|
+
|
|
137
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
138
|
+
outside.focus();
|
|
139
|
+
|
|
140
|
+
const event = tab();
|
|
141
|
+
|
|
142
|
+
assert.equal(event.defaultPrevented, false);
|
|
143
|
+
assert.equal(document.activeElement, outside);
|
|
144
|
+
outside.remove();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("leaves focus outside the panel alone on Shift+Tab too", () => {
|
|
148
|
+
const outside = document.createElement("button");
|
|
149
|
+
outside.textContent = "Behind the panel";
|
|
150
|
+
document.body.appendChild(outside);
|
|
151
|
+
|
|
152
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
153
|
+
outside.focus();
|
|
154
|
+
|
|
155
|
+
const event = tab(true);
|
|
156
|
+
|
|
157
|
+
assert.equal(event.defaultPrevented, false);
|
|
158
|
+
assert.equal(document.activeElement, outside);
|
|
159
|
+
outside.remove();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Clicking the title of a dialog — a region no control owns — leaves focus on
|
|
164
|
+
* the body, and the next Tab has to land back inside the surface rather than
|
|
165
|
+
* on whatever sits behind it (#1204).
|
|
166
|
+
*/
|
|
167
|
+
it("pulls focus back in when it is resting on nothing", () => {
|
|
168
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
169
|
+
(document.activeElement as HTMLElement).blur();
|
|
170
|
+
assert.equal(document.activeElement, document.body);
|
|
171
|
+
|
|
172
|
+
const event = tab();
|
|
173
|
+
|
|
174
|
+
assert.equal(event.defaultPrevented, true);
|
|
175
|
+
assert.equal(document.activeElement, buttonNamed("First action"));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("pulls focus resting on nothing back to the last control on Shift+Tab", () => {
|
|
179
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
180
|
+
(document.activeElement as HTMLElement).blur();
|
|
181
|
+
|
|
182
|
+
const event = tab(true);
|
|
183
|
+
|
|
184
|
+
assert.equal(event.defaultPrevented, true);
|
|
185
|
+
assert.equal(document.activeElement, buttonNamed("Second action"));
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("stops trapping once the panel closes", () => {
|
|
189
|
+
const outside = document.createElement("button");
|
|
190
|
+
outside.textContent = "Behind the panel";
|
|
191
|
+
document.body.appendChild(outside);
|
|
192
|
+
|
|
193
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
194
|
+
act(() => root.render(createElement(Panel, { open: false })));
|
|
195
|
+
outside.focus();
|
|
196
|
+
|
|
197
|
+
const event = tab();
|
|
198
|
+
|
|
199
|
+
assert.equal(event.defaultPrevented, false);
|
|
200
|
+
assert.equal(document.activeElement, outside);
|
|
201
|
+
outside.remove();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The self-update overlay has no control to offer and still has to stop the
|
|
206
|
+
* keyboard: focus rests on the surface itself and Tab moves nowhere.
|
|
207
|
+
*/
|
|
208
|
+
it("holds Tab still when the panel has nothing to focus", () => {
|
|
209
|
+
function Empty() {
|
|
210
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
211
|
+
useModalFocus(ref, true);
|
|
212
|
+
return createElement(
|
|
213
|
+
"div",
|
|
214
|
+
{ ref, tabIndex: -1 },
|
|
215
|
+
"nothing focusable here",
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
act(() => root.render(createElement(Empty)));
|
|
219
|
+
assert.equal(document.activeElement?.textContent, "nothing focusable here");
|
|
220
|
+
|
|
221
|
+
const event = tab();
|
|
222
|
+
|
|
223
|
+
assert.equal(event.defaultPrevented, true);
|
|
224
|
+
assert.equal(document.activeElement?.textContent, "nothing focusable here");
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("takes focus resting on nothing onto a panel with no control of its own", () => {
|
|
228
|
+
function Empty() {
|
|
229
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
230
|
+
useModalFocus(ref, true);
|
|
231
|
+
return createElement(
|
|
232
|
+
"div",
|
|
233
|
+
{ ref, tabIndex: -1 },
|
|
234
|
+
"nothing focusable here",
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
act(() => root.render(createElement(Empty)));
|
|
238
|
+
(document.activeElement as HTMLElement).blur();
|
|
239
|
+
assert.equal(document.activeElement, document.body);
|
|
240
|
+
|
|
241
|
+
const event = tab();
|
|
242
|
+
|
|
243
|
+
assert.equal(event.defaultPrevented, true);
|
|
244
|
+
assert.equal(document.activeElement?.textContent, "nothing focusable here");
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* A browser reports `tabIndex` -1 for an editing host and for an iframe, so a
|
|
249
|
+
* ring that trusts the property drops them: the control before the editor
|
|
250
|
+
* becomes the surface's last stop and Tab wraps away from it.
|
|
251
|
+
*/
|
|
252
|
+
it("counts a rich-text body as a stop in the ring", () => {
|
|
253
|
+
function WithEditor() {
|
|
254
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
255
|
+
useModalFocus(ref, true);
|
|
256
|
+
return createElement(
|
|
257
|
+
"div",
|
|
258
|
+
{ ref },
|
|
259
|
+
createElement("button", { type: "button" }, "First action"),
|
|
260
|
+
createElement("div", {
|
|
261
|
+
contentEditable: true,
|
|
262
|
+
"aria-label": "Message body",
|
|
263
|
+
}),
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
act(() => root.render(createElement(WithEditor)));
|
|
267
|
+
buttonNamed("First action").focus();
|
|
268
|
+
|
|
269
|
+
const event = tab();
|
|
270
|
+
|
|
271
|
+
assert.equal(event.defaultPrevented, false);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("counts an embedded frame as a stop in the ring", () => {
|
|
275
|
+
function WithFrame() {
|
|
276
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
277
|
+
useModalFocus(ref, true);
|
|
278
|
+
return createElement(
|
|
279
|
+
"div",
|
|
280
|
+
{ ref },
|
|
281
|
+
createElement("button", { type: "button" }, "First action"),
|
|
282
|
+
createElement("iframe", { title: "Message body" }),
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
act(() => root.render(createElement(WithFrame)));
|
|
286
|
+
buttonNamed("First action").focus();
|
|
287
|
+
|
|
288
|
+
const event = tab();
|
|
289
|
+
|
|
290
|
+
assert.equal(event.defaultPrevented, false);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("skips a disabled control when deciding where the edges are", () => {
|
|
294
|
+
function WithDisabled() {
|
|
295
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
296
|
+
useModalFocus(ref, true);
|
|
297
|
+
return createElement(
|
|
298
|
+
"div",
|
|
299
|
+
{ ref },
|
|
300
|
+
createElement("button", { type: "button" }, "First action"),
|
|
301
|
+
createElement("button", { type: "button" }, "Second action"),
|
|
302
|
+
createElement("button", { type: "button", disabled: true }, "Disabled"),
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
act(() => root.render(createElement(WithDisabled)));
|
|
306
|
+
buttonNamed("Second action").focus();
|
|
307
|
+
|
|
308
|
+
const event = tab();
|
|
309
|
+
|
|
310
|
+
assert.equal(event.defaultPrevented, true);
|
|
311
|
+
assert.equal(document.activeElement, buttonNamed("First action"));
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* A control the eye cannot see is not the surface's last one: leaving it in
|
|
316
|
+
* the ring makes the wrap a no-op — `focus()` on a hidden element does
|
|
317
|
+
* nothing — and Tab walks out of the surface from there.
|
|
318
|
+
*/
|
|
319
|
+
it("skips controls a reader cannot see or press", () => {
|
|
320
|
+
function WithHidden() {
|
|
321
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
322
|
+
useModalFocus(ref, true);
|
|
323
|
+
return createElement(
|
|
324
|
+
"div",
|
|
325
|
+
{ ref },
|
|
326
|
+
createElement("button", { type: "button" }, "First action"),
|
|
327
|
+
createElement("button", { type: "button" }, "Second action"),
|
|
328
|
+
createElement(
|
|
329
|
+
"button",
|
|
330
|
+
{ type: "button", "aria-disabled": "true" },
|
|
331
|
+
"Stated off",
|
|
332
|
+
),
|
|
333
|
+
createElement("button", { type: "button", hidden: true }, "Hidden"),
|
|
334
|
+
createElement(
|
|
335
|
+
"button",
|
|
336
|
+
{ type: "button", style: { display: "none" } },
|
|
337
|
+
"Gone",
|
|
338
|
+
),
|
|
339
|
+
createElement(
|
|
340
|
+
"button",
|
|
341
|
+
{ type: "button", style: { visibility: "hidden" } },
|
|
342
|
+
"Invisible",
|
|
343
|
+
),
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
act(() => root.render(createElement(WithHidden)));
|
|
347
|
+
buttonNamed("Second action").focus();
|
|
348
|
+
|
|
349
|
+
const event = tab();
|
|
350
|
+
|
|
351
|
+
assert.equal(event.defaultPrevented, true);
|
|
352
|
+
assert.equal(document.activeElement, buttonNamed("First action"));
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* A dialog raised over a surface that is already up takes the ring with it,
|
|
357
|
+
* so the ring stays what a pointer can reach.
|
|
358
|
+
*/
|
|
359
|
+
it("hands the ring to a modal opened inside the panel", () => {
|
|
360
|
+
function WithNested() {
|
|
361
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
362
|
+
useModalFocus(ref, true);
|
|
363
|
+
return createElement(
|
|
364
|
+
"div",
|
|
365
|
+
{ ref },
|
|
366
|
+
createElement("button", { type: "button" }, "Under the backdrop"),
|
|
367
|
+
createElement(
|
|
368
|
+
"div",
|
|
369
|
+
{ role: "dialog", "aria-modal": "true", "aria-label": "Confirm" },
|
|
370
|
+
createElement("button", { type: "button" }, "Confirm"),
|
|
371
|
+
createElement("button", { type: "button" }, "Cancel"),
|
|
372
|
+
),
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
act(() => root.render(createElement(WithNested)));
|
|
376
|
+
assert.equal(document.activeElement?.textContent, "Confirm");
|
|
377
|
+
|
|
378
|
+
buttonNamed("Under the backdrop").focus();
|
|
379
|
+
tab();
|
|
380
|
+
|
|
381
|
+
assert.equal(document.activeElement?.textContent, "Confirm");
|
|
382
|
+
|
|
383
|
+
buttonNamed("Cancel").focus();
|
|
384
|
+
tab();
|
|
385
|
+
|
|
386
|
+
assert.equal(document.activeElement?.textContent, "Confirm");
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Two surfaces can be up at once — the narrow-width nav drawer with a dialog
|
|
392
|
+
* raised over it (#1204) — and Tab belongs to the one on top. Listener order
|
|
393
|
+
* says the opposite: the surface that registered first sees the key first, and
|
|
394
|
+
* a trap that answers it drags focus out from under the dialog above it.
|
|
395
|
+
*/
|
|
396
|
+
describe("useModalFocus with a second surface above it", () => {
|
|
397
|
+
function TwoSurfaces({ topOpen }: { topOpen: boolean }) {
|
|
398
|
+
const under = useRef<HTMLDivElement>(null);
|
|
399
|
+
const over = useRef<HTMLDivElement>(null);
|
|
400
|
+
useModalFocus(under, true);
|
|
401
|
+
useModalFocus(over, topOpen);
|
|
402
|
+
return createElement(
|
|
403
|
+
"div",
|
|
404
|
+
null,
|
|
405
|
+
createElement(
|
|
406
|
+
"div",
|
|
407
|
+
{ ref: under },
|
|
408
|
+
createElement("button", { type: "button" }, "Drawer first"),
|
|
409
|
+
createElement("button", { type: "button" }, "Drawer last"),
|
|
410
|
+
),
|
|
411
|
+
topOpen
|
|
412
|
+
? createElement(
|
|
413
|
+
"div",
|
|
414
|
+
{ ref: over },
|
|
415
|
+
createElement("button", { type: "button" }, "Dialog first"),
|
|
416
|
+
createElement("button", { type: "button" }, "Dialog last"),
|
|
417
|
+
)
|
|
418
|
+
: null,
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
it("wraps within the top surface and leaves the one under it silent", () => {
|
|
423
|
+
act(() => root.render(createElement(TwoSurfaces, { topOpen: true })));
|
|
424
|
+
assert.equal(document.activeElement?.textContent, "Dialog first");
|
|
425
|
+
|
|
426
|
+
tab(true);
|
|
427
|
+
|
|
428
|
+
assert.equal(document.activeElement?.textContent, "Dialog last");
|
|
429
|
+
|
|
430
|
+
tab();
|
|
431
|
+
|
|
432
|
+
assert.equal(document.activeElement?.textContent, "Dialog first");
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it("keeps the surface underneath from wrapping at its own edge", () => {
|
|
436
|
+
act(() => root.render(createElement(TwoSurfaces, { topOpen: true })));
|
|
437
|
+
buttonNamed("Drawer last").focus();
|
|
438
|
+
|
|
439
|
+
const event = tab();
|
|
440
|
+
|
|
441
|
+
assert.equal(event.defaultPrevented, false);
|
|
442
|
+
assert.equal(document.activeElement?.textContent, "Drawer last");
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("hands Tab back to the surface underneath once the top one closes", () => {
|
|
446
|
+
act(() => root.render(createElement(TwoSurfaces, { topOpen: true })));
|
|
447
|
+
act(() => root.render(createElement(TwoSurfaces, { topOpen: false })));
|
|
448
|
+
buttonNamed("Drawer last").focus();
|
|
449
|
+
|
|
450
|
+
const event = tab();
|
|
451
|
+
|
|
452
|
+
assert.equal(event.defaultPrevented, true);
|
|
453
|
+
assert.equal(document.activeElement?.textContent, "Drawer first");
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* The nested pair, where both surfaces contain the focused control and the
|
|
458
|
+
* outer one would otherwise answer first over a ring of its own.
|
|
459
|
+
*/
|
|
460
|
+
it("answers from the inner surface when one is nested in the other", () => {
|
|
461
|
+
function Nested() {
|
|
462
|
+
const outer = useRef<HTMLDivElement>(null);
|
|
463
|
+
const inner = useRef<HTMLDivElement>(null);
|
|
464
|
+
useModalFocus(outer, true);
|
|
465
|
+
useModalFocus(inner, true);
|
|
466
|
+
return createElement(
|
|
467
|
+
"div",
|
|
468
|
+
{ ref: outer },
|
|
469
|
+
createElement("button", { type: "button" }, "Outer action"),
|
|
470
|
+
createElement(
|
|
471
|
+
"div",
|
|
472
|
+
{ ref: inner },
|
|
473
|
+
createElement("button", { type: "button" }, "Inner first"),
|
|
474
|
+
createElement("button", { type: "button" }, "Inner last"),
|
|
475
|
+
),
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
act(() => root.render(createElement(Nested)));
|
|
479
|
+
buttonNamed("Inner last").focus();
|
|
480
|
+
|
|
481
|
+
tab();
|
|
482
|
+
|
|
483
|
+
assert.equal(document.activeElement?.textContent, "Inner first");
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* The same pair with the mount order reversed: the inner surface is a
|
|
488
|
+
* component of its own, so React commits its effect first and the listener
|
|
489
|
+
* registered first is the top one's. Depth has to pick the answer either way,
|
|
490
|
+
* or the test only ever sees registration order agreeing with it.
|
|
491
|
+
*/
|
|
492
|
+
it("answers from the inner surface when that one mounts first", () => {
|
|
493
|
+
function Inner() {
|
|
494
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
495
|
+
useModalFocus(ref, true);
|
|
496
|
+
return createElement(
|
|
497
|
+
"div",
|
|
498
|
+
{ ref },
|
|
499
|
+
createElement("button", { type: "button" }, "Inner first"),
|
|
500
|
+
createElement("button", { type: "button" }, "Inner last"),
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
function Outer() {
|
|
504
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
505
|
+
useModalFocus(ref, true);
|
|
506
|
+
return createElement(
|
|
507
|
+
"div",
|
|
508
|
+
{ ref },
|
|
509
|
+
createElement("button", { type: "button" }, "Outer action"),
|
|
510
|
+
createElement(Inner),
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
act(() => root.render(createElement(Outer)));
|
|
514
|
+
buttonNamed("Inner last").focus();
|
|
515
|
+
|
|
516
|
+
tab();
|
|
517
|
+
|
|
518
|
+
assert.equal(document.activeElement?.textContent, "Inner first");
|
|
519
|
+
});
|
|
520
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { type RefObject, useEffect } from "react";
|
|
2
|
+
import { useOverlayDepth } from "./overlay-scope.js";
|
|
3
|
+
|
|
4
|
+
const FOCUSABLE_SELECTOR =
|
|
5
|
+
'a[href], button, input, select, textarea, iframe, [tabindex], [contenteditable]:not([contenteditable="false"])';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Everything the selector matches is in the tab order unless it takes itself
|
|
9
|
+
* out of it. A declared `tabindex` is the only reading of that which holds: a
|
|
10
|
+
* browser reports `tabIndex` -1 for an editing host and for an iframe, so
|
|
11
|
+
* trusting the property drops the rich-text body of a compose sheet from the
|
|
12
|
+
* ring and Tab walks past it.
|
|
13
|
+
*/
|
|
14
|
+
function isTabbable(element: HTMLElement): boolean {
|
|
15
|
+
if (Number(element.getAttribute("tabindex") ?? 0) < 0) return false;
|
|
16
|
+
if (element.hasAttribute("disabled")) return false;
|
|
17
|
+
if (element.getAttribute("aria-disabled") === "true") return false;
|
|
18
|
+
if (element.closest("[inert], [hidden], [aria-hidden='true']")) return false;
|
|
19
|
+
const style = getComputedStyle(element);
|
|
20
|
+
return style.display !== "none" && style.visibility !== "hidden";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The controls Tab may reach inside this surface, in the order it reaches them.
|
|
25
|
+
*
|
|
26
|
+
* A modal opened from inside the surface takes the ring with it: the
|
|
27
|
+
* intelligence pane's reclassify dialog renders under the drawer's own panel,
|
|
28
|
+
* so a ring scoped to the panel puts its Cancel last and leaves every control
|
|
29
|
+
* behind that dialog's backdrop in the cycle — Tab walks onto buttons no
|
|
30
|
+
* pointer can reach, which is the bug the trap exists to prevent (#747).
|
|
31
|
+
*/
|
|
32
|
+
function tabRing(container: HTMLElement): HTMLElement[] {
|
|
33
|
+
const nested = container.querySelectorAll<HTMLElement>(
|
|
34
|
+
'[role="dialog"][aria-modal="true"]',
|
|
35
|
+
);
|
|
36
|
+
const root = nested[nested.length - 1] ?? container;
|
|
37
|
+
return [...root.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)].filter(
|
|
38
|
+
isTabbable,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Open traps, by their position in the React tree. See {@link isTopTrap}. */
|
|
43
|
+
const openTraps = new Set<number>();
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether this surface is the innermost one trapping focus.
|
|
47
|
+
*
|
|
48
|
+
* Two surfaces can be up over overlapping DOM — the narrow-width nav drawer and
|
|
49
|
+
* a dialog raised over it, a confirmation opened from inside a drawer — and only
|
|
50
|
+
* the top one may answer Tab. Listener order cannot decide that: handlers run in
|
|
51
|
+
* the order their effects registered, so the surface that opened first takes the
|
|
52
|
+
* key and pulls focus out from under the one above it. Depth is the ordering
|
|
53
|
+
* that holds, and `overlay-scope` already hands it out.
|
|
54
|
+
*/
|
|
55
|
+
function isTopTrap(depth: number): boolean {
|
|
56
|
+
for (const other of openTraps) {
|
|
57
|
+
if (other > depth) return false;
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Focus another surface is holding, which this one leaves where it is: an error
|
|
64
|
+
* banner and the fatal-error overlay paint above every modal from outside its
|
|
65
|
+
* subtree, and a trap that reclaims every Tab strands them (#970).
|
|
66
|
+
*
|
|
67
|
+
* Focus resting nowhere is not that. Clicking the title of a dialog — a region
|
|
68
|
+
* no control owns — leaves it on the body, and a trap that reads that as
|
|
69
|
+
* somebody else's hands the next Tab to the surface behind the modal (#1204).
|
|
70
|
+
*/
|
|
71
|
+
function heldElsewhere(
|
|
72
|
+
container: HTMLElement,
|
|
73
|
+
active: Element | null,
|
|
74
|
+
): boolean {
|
|
75
|
+
if (!(active instanceof HTMLElement)) return false;
|
|
76
|
+
if (active === document.body || active === document.documentElement) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return !container.contains(active);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The keyboard contract of an `aria-modal` surface, in one hook: focus moves
|
|
84
|
+
* into `ref` when `open` turns true, Tab stays inside it while it is up, and
|
|
85
|
+
* focus goes back to whatever held it once the surface closes or leaves — the
|
|
86
|
+
* trigger can have gone with it, so the restore is guarded.
|
|
87
|
+
*
|
|
88
|
+
* The trap is a Tab handler rather than `inert` on the background: these
|
|
89
|
+
* surfaces do not portal to the body, so there is no stable set of background
|
|
90
|
+
* nodes to mark. It wraps at the ring's ends and pulls in focus that sits inside
|
|
91
|
+
* the surface but outside the ring, under a nested dialog's backdrop (#973), or
|
|
92
|
+
* on nothing at all. Focus another surface holds is the one exemption, and it is
|
|
93
|
+
* narrow — see {@link heldElsewhere}.
|
|
94
|
+
*/
|
|
95
|
+
export function useModalFocus(
|
|
96
|
+
ref: RefObject<HTMLElement | null>,
|
|
97
|
+
open: boolean,
|
|
98
|
+
): void {
|
|
99
|
+
const depth = useOverlayDepth();
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (!open) return;
|
|
103
|
+
openTraps.add(depth);
|
|
104
|
+
return () => {
|
|
105
|
+
openTraps.delete(depth);
|
|
106
|
+
};
|
|
107
|
+
}, [open, depth]);
|
|
108
|
+
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
if (!open) return;
|
|
111
|
+
const restoreTo =
|
|
112
|
+
document.activeElement instanceof HTMLElement
|
|
113
|
+
? document.activeElement
|
|
114
|
+
: null;
|
|
115
|
+
const container = ref.current;
|
|
116
|
+
if (container) (tabRing(container)[0] ?? container).focus();
|
|
117
|
+
return () => {
|
|
118
|
+
if (restoreTo?.isConnected) restoreTo.focus();
|
|
119
|
+
};
|
|
120
|
+
}, [open, ref]);
|
|
121
|
+
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
if (!open) return;
|
|
124
|
+
|
|
125
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
126
|
+
if (event.key !== "Tab" || event.defaultPrevented) return;
|
|
127
|
+
if (!isTopTrap(depth)) return;
|
|
128
|
+
const container = ref.current;
|
|
129
|
+
if (!container) return;
|
|
130
|
+
const active = document.activeElement;
|
|
131
|
+
if (heldElsewhere(container, active)) return;
|
|
132
|
+
|
|
133
|
+
const ring = tabRing(container);
|
|
134
|
+
const first = ring[0];
|
|
135
|
+
const last = ring.at(-1);
|
|
136
|
+
if (!first || !last) {
|
|
137
|
+
event.preventDefault();
|
|
138
|
+
container.focus();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const inside = active instanceof HTMLElement && ring.includes(active);
|
|
142
|
+
if (event.shiftKey) {
|
|
143
|
+
if (!inside || active === first) {
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
last.focus();
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (!inside || active === last) {
|
|
150
|
+
event.preventDefault();
|
|
151
|
+
first.focus();
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
document.addEventListener("keydown", onKeyDown);
|
|
156
|
+
return () => document.removeEventListener("keydown", onKeyDown);
|
|
157
|
+
}, [open, ref, depth]);
|
|
158
|
+
}
|