@remit/ui 0.0.181 → 0.0.183
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/components/bottom-sheet.stories.tsx +56 -0
- package/src/components/bottom-sheet.tsx +3 -2
- 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/self-update-progress-overlay.tsx +7 -48
- package/src/components/slide-panel.tsx +7 -1
- package/src/index.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-suggest-list.test.ts +69 -0
- package/src/lib/use-suggest-list.ts +46 -28
- package/src/lib/use-initial-focus.test.ts +0 -78
- package/src/lib/use-initial-focus.ts +0 -36
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { AlertOctagon, Check, Loader2 } from "lucide-react";
|
|
2
|
-
import {
|
|
2
|
+
import { useRef } from "react";
|
|
3
3
|
import { cn } from "../lib/cn.js";
|
|
4
4
|
import { useOverlayScope } from "../lib/overlay-scope.js";
|
|
5
|
+
import { useModalFocus } from "../lib/use-modal-focus.js";
|
|
5
6
|
import { Button } from "./button.js";
|
|
6
7
|
import {
|
|
7
8
|
type UpdatePhase,
|
|
@@ -11,51 +12,6 @@ import {
|
|
|
11
12
|
|
|
12
13
|
const phaseOrder: UpdatePhase[] = ["preparing", "restarting", "reconnecting"];
|
|
13
14
|
|
|
14
|
-
const FOCUSABLE =
|
|
15
|
-
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* A screen whose purpose is to stop interaction has to stop it for the
|
|
19
|
-
* keyboard too. Focus moves in on mount and Tab cycles inside, so nothing
|
|
20
|
-
* behind the overlay can be reached while the server is gone.
|
|
21
|
-
*/
|
|
22
|
-
function useBlockingFocus(ref: RefObject<HTMLDivElement | null>) {
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
const container = ref.current;
|
|
25
|
-
if (!container) return;
|
|
26
|
-
|
|
27
|
-
const focusables = () =>
|
|
28
|
-
Array.from(container.querySelectorAll<HTMLElement>(FOCUSABLE));
|
|
29
|
-
|
|
30
|
-
(focusables()[0] ?? container).focus();
|
|
31
|
-
|
|
32
|
-
const onKeyDown = (event: KeyboardEvent) => {
|
|
33
|
-
if (event.key !== "Tab") return;
|
|
34
|
-
const items = focusables();
|
|
35
|
-
if (items.length === 0) {
|
|
36
|
-
event.preventDefault();
|
|
37
|
-
container.focus();
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
const first = items[0];
|
|
41
|
-
const last = items[items.length - 1];
|
|
42
|
-
const active = document.activeElement;
|
|
43
|
-
if (event.shiftKey && (active === first || !container.contains(active))) {
|
|
44
|
-
event.preventDefault();
|
|
45
|
-
last.focus();
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (!event.shiftKey && (active === last || !container.contains(active))) {
|
|
49
|
-
event.preventDefault();
|
|
50
|
-
first.focus();
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
window.addEventListener("keydown", onKeyDown, true);
|
|
55
|
-
return () => window.removeEventListener("keydown", onKeyDown, true);
|
|
56
|
-
}, [ref]);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
15
|
const overlayShell =
|
|
60
16
|
"fixed inset-0 z-50 flex flex-col items-center justify-center gap-6 bg-canvas p-6 text-center outline-none";
|
|
61
17
|
|
|
@@ -76,7 +32,10 @@ export function SelfUpdateProgressOverlay({
|
|
|
76
32
|
elapsedSeconds,
|
|
77
33
|
}: SelfUpdateProgressOverlayProps) {
|
|
78
34
|
const ref = useRef<HTMLDivElement>(null);
|
|
79
|
-
|
|
35
|
+
// A screen whose purpose is to stop interaction has to stop it for the
|
|
36
|
+
// keyboard too: it has nothing to Tab to, so focus rests on the screen itself
|
|
37
|
+
// and stays there while the server is gone.
|
|
38
|
+
useModalFocus(ref, true);
|
|
80
39
|
// Nothing to answer: the update is running and there is no way out of it, so
|
|
81
40
|
// every shortcut is contained rather than acting on a mailbox that is gone.
|
|
82
41
|
useOverlayScope({ id: "self-update", open: true });
|
|
@@ -163,7 +122,7 @@ export function SelfUpdateUnreachableScreen({
|
|
|
163
122
|
onRetryConnection,
|
|
164
123
|
}: SelfUpdateUnreachableScreenProps) {
|
|
165
124
|
const ref = useRef<HTMLDivElement>(null);
|
|
166
|
-
|
|
125
|
+
useModalFocus(ref, true);
|
|
167
126
|
const minutes = Math.max(1, Math.round(elapsedSeconds / 60));
|
|
168
127
|
|
|
169
128
|
return (
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { X } from "lucide-react";
|
|
2
|
-
import type
|
|
2
|
+
import { type ReactNode, useRef } from "react";
|
|
3
3
|
import { cn } from "../lib/cn.js";
|
|
4
4
|
import { useOverlayScope } from "../lib/overlay-scope.js";
|
|
5
|
+
import { useModalFocus } from "../lib/use-modal-focus.js";
|
|
5
6
|
|
|
6
7
|
/* ------------------------------------------------------------------ */
|
|
7
8
|
/* SlidePanel: right-edge slide-over for a focused sub-task (editing */
|
|
@@ -37,6 +38,9 @@ export function SlidePanel({
|
|
|
37
38
|
answers: { back: onClose },
|
|
38
39
|
});
|
|
39
40
|
|
|
41
|
+
const panelRef = useRef<HTMLDivElement>(null);
|
|
42
|
+
useModalFocus(panelRef, isOpen);
|
|
43
|
+
|
|
40
44
|
return (
|
|
41
45
|
<>
|
|
42
46
|
{/* Click-to-dismiss scrim: a pointer shortcut for the header's Close
|
|
@@ -52,6 +56,7 @@ export function SlidePanel({
|
|
|
52
56
|
/>
|
|
53
57
|
|
|
54
58
|
<div
|
|
59
|
+
ref={panelRef}
|
|
55
60
|
className={cn(
|
|
56
61
|
"safe-area-frame fixed top-0 right-0 z-50 flex h-full w-full flex-col border-l border-line bg-canvas shadow-xl sm:w-[400px] sm:max-w-[90vw]",
|
|
57
62
|
"transform transition-transform duration-200 ease-out",
|
|
@@ -62,6 +67,7 @@ export function SlidePanel({
|
|
|
62
67
|
aria-hidden={!isOpen}
|
|
63
68
|
inert={!isOpen}
|
|
64
69
|
aria-labelledby="slide-panel-title"
|
|
70
|
+
tabIndex={-1}
|
|
65
71
|
>
|
|
66
72
|
<div className="flex h-14 shrink-0 items-center justify-between border-b border-line px-4">
|
|
67
73
|
<h2 id="slide-panel-title" className="font-semibold">
|
package/src/index.ts
CHANGED
|
@@ -1133,7 +1133,6 @@ export {
|
|
|
1133
1133
|
type SuggestKeyState,
|
|
1134
1134
|
suggestKeyAction,
|
|
1135
1135
|
} from "./lib/suggest-keys.js";
|
|
1136
|
-
export { useInitialFocus } from "./lib/use-initial-focus.js";
|
|
1137
1136
|
export { type ListCursor, useListCursor } from "./lib/use-list-cursor.js";
|
|
1138
1137
|
export {
|
|
1139
1138
|
type ListKeyboard,
|
|
@@ -1146,6 +1145,7 @@ export {
|
|
|
1146
1145
|
useLongPress,
|
|
1147
1146
|
} from "./lib/use-long-press.js";
|
|
1148
1147
|
export { useMatchMedia } from "./lib/use-match-media.js";
|
|
1148
|
+
export { useModalFocus } from "./lib/use-modal-focus.js";
|
|
1149
1149
|
export {
|
|
1150
1150
|
MESSAGE_ROW_SELECTOR,
|
|
1151
1151
|
useRenderedRowIds,
|
package/src/lib/overlay-scope.ts
CHANGED
|
@@ -70,6 +70,18 @@ let entries: ScopeEntry[] = [];
|
|
|
70
70
|
let renderedOverlays = 0;
|
|
71
71
|
const nextDepth = (): number => ++renderedOverlays;
|
|
72
72
|
|
|
73
|
+
/**
|
|
74
|
+
* This surface's position in the React tree, as the stack orders it.
|
|
75
|
+
*
|
|
76
|
+
* Shared with the focus trap, which needs the same ordering for the same
|
|
77
|
+
* reason: registration order puts an inner surface underneath the one
|
|
78
|
+
* containing it, and a sibling raised later underneath the one already up.
|
|
79
|
+
*/
|
|
80
|
+
export function useOverlayDepth(): number {
|
|
81
|
+
const [depth] = useState(nextDepth);
|
|
82
|
+
return depth;
|
|
83
|
+
}
|
|
84
|
+
|
|
73
85
|
const byDepth = (a: ScopeEntry, b: ScopeEntry): number => a.depth - b.depth;
|
|
74
86
|
|
|
75
87
|
/**
|
|
@@ -192,7 +204,7 @@ export function useOverlayScope({
|
|
|
192
204
|
|
|
193
205
|
// Numbered during the first render, where React is still going parent before
|
|
194
206
|
// child — the one moment nesting is legible from inside a hook.
|
|
195
|
-
const
|
|
207
|
+
const depth = useOverlayDepth();
|
|
196
208
|
|
|
197
209
|
useEffect(() => {
|
|
198
210
|
if (!open) return;
|
|
@@ -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
|
+
});
|