@remit/ui 0.0.181 → 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/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-initial-focus.test.ts +0 -78
- package/src/lib/use-initial-focus.ts +0 -36
package/package.json
CHANGED
|
@@ -96,3 +96,59 @@ export const MirrorsDialogSemantics: Story = {
|
|
|
96
96
|
await expect(canvas.queryByRole("dialog")).toBeNull();
|
|
97
97
|
},
|
|
98
98
|
};
|
|
99
|
+
|
|
100
|
+
function TrapDemo() {
|
|
101
|
+
const [open, setOpen] = useState(true);
|
|
102
|
+
return (
|
|
103
|
+
<div className="relative h-full overflow-hidden bg-surface">
|
|
104
|
+
<div className="px-row-inset py-3">
|
|
105
|
+
<Button variant="secondary" onClick={() => setOpen(true)}>
|
|
106
|
+
Open sheet
|
|
107
|
+
</Button>
|
|
108
|
+
</div>
|
|
109
|
+
<BottomSheet
|
|
110
|
+
open={open}
|
|
111
|
+
onClose={() => setOpen(false)}
|
|
112
|
+
label="Action sheet"
|
|
113
|
+
>
|
|
114
|
+
<div className="flex flex-col gap-3 px-row-inset py-6">
|
|
115
|
+
<Button variant="secondary" onClick={() => setOpen(false)}>
|
|
116
|
+
Rename
|
|
117
|
+
</Button>
|
|
118
|
+
<Button variant="primary" onClick={() => setOpen(false)}>
|
|
119
|
+
Got it
|
|
120
|
+
</Button>
|
|
121
|
+
</div>
|
|
122
|
+
</BottomSheet>
|
|
123
|
+
</div>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The sheet claims `aria-modal`, so the page behind it is hidden from a screen
|
|
129
|
+
* reader and the keyboard has to agree: Tab off the last control wraps to the
|
|
130
|
+
* first, Shift+Tab off the first wraps to the last, and neither reaches the
|
|
131
|
+
* button behind the scrim.
|
|
132
|
+
*/
|
|
133
|
+
export const TrapsTabAtBothEdges: Story = {
|
|
134
|
+
render: () => <TrapDemo />,
|
|
135
|
+
play: async ({ canvasElement }) => {
|
|
136
|
+
const canvas = within(canvasElement);
|
|
137
|
+
const first = canvas.getByRole("button", { name: "Rename" });
|
|
138
|
+
const last = canvas.getByRole("button", { name: "Got it" });
|
|
139
|
+
const opener = canvas.getByRole("button", { name: "Open sheet" });
|
|
140
|
+
|
|
141
|
+
await expect(first).toHaveFocus();
|
|
142
|
+
|
|
143
|
+
await userEvent.tab();
|
|
144
|
+
await expect(last).toHaveFocus();
|
|
145
|
+
|
|
146
|
+
await userEvent.tab();
|
|
147
|
+
await expect(first).toHaveFocus();
|
|
148
|
+
await expect(opener).not.toHaveFocus();
|
|
149
|
+
|
|
150
|
+
await userEvent.tab({ shift: true });
|
|
151
|
+
await expect(last).toHaveFocus();
|
|
152
|
+
await expect(opener).not.toHaveFocus();
|
|
153
|
+
},
|
|
154
|
+
};
|
|
@@ -2,7 +2,7 @@ import type { ReactNode } from "react";
|
|
|
2
2
|
import { useCallback, useLayoutEffect, useRef, useState } from "react";
|
|
3
3
|
import { cn } from "../lib/cn.js";
|
|
4
4
|
import { useOverlayScope } from "../lib/overlay-scope.js";
|
|
5
|
-
import {
|
|
5
|
+
import { useModalFocus } from "../lib/use-modal-focus.js";
|
|
6
6
|
|
|
7
7
|
const SNAP_MS = 320;
|
|
8
8
|
const SNAP_EASE = "cubic-bezier(0.32, 0.9, 0.3, 1)";
|
|
@@ -44,7 +44,7 @@ export function BottomSheet({
|
|
|
44
44
|
const [height, setHeight] = useState(HEIGHT_FALLBACK);
|
|
45
45
|
const [drag, setDrag] = useState<number | null>(null);
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
useModalFocus(contentRef, open);
|
|
48
48
|
|
|
49
49
|
useLayoutEffect(() => {
|
|
50
50
|
const el = sheetRef.current;
|
|
@@ -157,6 +157,7 @@ export function BottomSheet({
|
|
|
157
157
|
</div>
|
|
158
158
|
<div
|
|
159
159
|
ref={contentRef}
|
|
160
|
+
tabIndex={-1}
|
|
160
161
|
className="flex min-h-0 flex-1 flex-col overflow-hidden"
|
|
161
162
|
>
|
|
162
163
|
{children}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useRef } from "react";
|
|
2
2
|
import { cn } from "../lib/cn.js";
|
|
3
3
|
import { useOverlayScope } from "../lib/overlay-scope.js";
|
|
4
|
+
import { useModalFocus } from "../lib/use-modal-focus.js";
|
|
4
5
|
import { DialogBackdrop } from "./dialog-backdrop.js";
|
|
5
6
|
|
|
6
7
|
export interface ConfirmDialogProps {
|
|
@@ -23,8 +24,7 @@ export interface ConfirmDialogProps {
|
|
|
23
24
|
* primitive ships in the web client (only the bespoke KeyboardShortcutsModal
|
|
24
25
|
* and SlidePanel), so this is a small reusable one matching their Tailwind +
|
|
25
26
|
* overlay conventions. Esc cancels, the backdrop cancels, Cancel is focused on
|
|
26
|
-
* open, and
|
|
27
|
-
* stays within the dialog.
|
|
27
|
+
* open, and Tab stays inside the dialog.
|
|
28
28
|
*/
|
|
29
29
|
export const ConfirmDialog = ({
|
|
30
30
|
isOpen,
|
|
@@ -37,7 +37,7 @@ export const ConfirmDialog = ({
|
|
|
37
37
|
onConfirm,
|
|
38
38
|
onCancel,
|
|
39
39
|
}: ConfirmDialogProps) => {
|
|
40
|
-
const
|
|
40
|
+
const dialogRef = useRef<HTMLDivElement>(null);
|
|
41
41
|
|
|
42
42
|
useOverlayScope({
|
|
43
43
|
id: "confirm-dialog",
|
|
@@ -45,21 +45,10 @@ export const ConfirmDialog = ({
|
|
|
45
45
|
answers: { back: onCancel },
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
-
//
|
|
49
|
-
// a
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
useEffect(() => {
|
|
53
|
-
if (!isOpen) return;
|
|
54
|
-
const opener =
|
|
55
|
-
document.activeElement instanceof HTMLElement
|
|
56
|
-
? document.activeElement
|
|
57
|
-
: null;
|
|
58
|
-
cancelRef.current?.focus();
|
|
59
|
-
return () => {
|
|
60
|
-
if (opener?.isConnected) opener.focus();
|
|
61
|
-
};
|
|
62
|
-
}, [isOpen]);
|
|
48
|
+
// Cancel is the first control in the dialog, so the shared trap opens on it —
|
|
49
|
+
// a confirmation asks before it acts. Whoever opened the dialog gets the
|
|
50
|
+
// focus back when it closes.
|
|
51
|
+
useModalFocus(dialogRef, isOpen);
|
|
63
52
|
|
|
64
53
|
if (!isOpen) return null;
|
|
65
54
|
|
|
@@ -76,9 +65,11 @@ export const ConfirmDialog = ({
|
|
|
76
65
|
|
|
77
66
|
{/* Dialog */}
|
|
78
67
|
<div
|
|
68
|
+
ref={dialogRef}
|
|
79
69
|
role="dialog"
|
|
80
70
|
aria-modal="true"
|
|
81
71
|
aria-label={title}
|
|
72
|
+
tabIndex={-1}
|
|
82
73
|
className={cn(
|
|
83
74
|
"relative z-10 w-full max-w-sm",
|
|
84
75
|
"bg-surface border border-line rounded-sm shadow-lg",
|
|
@@ -92,7 +83,6 @@ export const ConfirmDialog = ({
|
|
|
92
83
|
|
|
93
84
|
<div className="mt-6 flex items-center justify-end gap-2">
|
|
94
85
|
<button
|
|
95
|
-
ref={cancelRef}
|
|
96
86
|
type="button"
|
|
97
87
|
onClick={onCancel}
|
|
98
88
|
className={cn(
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { expect, userEvent, within } from "storybook/test";
|
|
4
|
+
import { Button } from "./button.js";
|
|
5
|
+
import { Dialog } from "./dialog.js";
|
|
6
|
+
|
|
7
|
+
const meta: Meta<typeof Dialog> = {
|
|
8
|
+
title: "Components/Dialog",
|
|
9
|
+
component: Dialog,
|
|
10
|
+
parameters: { layout: "fullscreen" },
|
|
11
|
+
};
|
|
12
|
+
export default meta;
|
|
13
|
+
|
|
14
|
+
type Story = StoryObj<typeof Dialog>;
|
|
15
|
+
|
|
16
|
+
function Demo({ initialOpen = true }: { initialOpen?: boolean }) {
|
|
17
|
+
const [open, setOpen] = useState(initialOpen);
|
|
18
|
+
return (
|
|
19
|
+
<div className="h-dvh bg-canvas p-6">
|
|
20
|
+
<Button variant="secondary" onClick={() => setOpen(true)}>
|
|
21
|
+
Open dialog
|
|
22
|
+
</Button>
|
|
23
|
+
<Dialog open={open} onClose={() => setOpen(false)} title="Move message">
|
|
24
|
+
<div className="flex flex-col gap-3 p-4">
|
|
25
|
+
<input
|
|
26
|
+
aria-label="Folder"
|
|
27
|
+
placeholder="Folder"
|
|
28
|
+
className="h-9 rounded-md border border-line bg-surface px-2 text-sm text-fg"
|
|
29
|
+
/>
|
|
30
|
+
<div className="flex justify-end gap-2">
|
|
31
|
+
<Button variant="ghost" onClick={() => setOpen(false)}>
|
|
32
|
+
Cancel
|
|
33
|
+
</Button>
|
|
34
|
+
<Button onClick={() => setOpen(false)}>Move</Button>
|
|
35
|
+
</div>
|
|
36
|
+
</div>
|
|
37
|
+
</Dialog>
|
|
38
|
+
</div>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const Default: Story = {
|
|
43
|
+
render: () => <Demo />,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The dialog claims `aria-modal`, so the page behind it is hidden from a screen
|
|
48
|
+
* reader and the keyboard has to agree: focus opens on the first control, Tab
|
|
49
|
+
* off the last wraps to the first, Shift+Tab off the first wraps to the last,
|
|
50
|
+
* and neither reaches the button behind the scrim.
|
|
51
|
+
*/
|
|
52
|
+
export const TrapsTabAtBothEdges: Story = {
|
|
53
|
+
render: () => <Demo />,
|
|
54
|
+
play: async ({ canvasElement }) => {
|
|
55
|
+
const canvas = within(canvasElement);
|
|
56
|
+
const first = canvas.getByLabelText("Folder");
|
|
57
|
+
const last = canvas.getByRole("button", { name: "Move" });
|
|
58
|
+
const opener = canvas.getByRole("button", { name: "Open dialog" });
|
|
59
|
+
|
|
60
|
+
await expect(first).toHaveFocus();
|
|
61
|
+
|
|
62
|
+
await userEvent.tab();
|
|
63
|
+
await expect(canvas.getByRole("button", { name: "Cancel" })).toHaveFocus();
|
|
64
|
+
await userEvent.tab();
|
|
65
|
+
await expect(last).toHaveFocus();
|
|
66
|
+
|
|
67
|
+
await userEvent.tab();
|
|
68
|
+
await expect(first).toHaveFocus();
|
|
69
|
+
await expect(opener).not.toHaveFocus();
|
|
70
|
+
|
|
71
|
+
await userEvent.tab({ shift: true });
|
|
72
|
+
await expect(last).toHaveFocus();
|
|
73
|
+
await expect(opener).not.toHaveFocus();
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Escape dismisses the dialog and hands focus back to whatever opened it, so a
|
|
79
|
+
* keyboard user is never left parked on a surface that has gone.
|
|
80
|
+
*/
|
|
81
|
+
export const EscapeReturnsFocus: Story = {
|
|
82
|
+
render: () => <Demo initialOpen={false} />,
|
|
83
|
+
play: async ({ canvasElement }) => {
|
|
84
|
+
const canvas = within(canvasElement);
|
|
85
|
+
const opener = canvas.getByRole("button", { name: "Open dialog" });
|
|
86
|
+
|
|
87
|
+
await userEvent.click(opener);
|
|
88
|
+
await expect(canvas.getByLabelText("Folder")).toHaveFocus();
|
|
89
|
+
|
|
90
|
+
await userEvent.keyboard("{Escape}");
|
|
91
|
+
await expect(canvas.queryByRole("dialog")).toBeNull();
|
|
92
|
+
await expect(opener).toHaveFocus();
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
function StackedDemo() {
|
|
97
|
+
const [navOpen, setNavOpen] = useState(true);
|
|
98
|
+
const [moveOpen, setMoveOpen] = useState(true);
|
|
99
|
+
return (
|
|
100
|
+
<div className="h-dvh bg-canvas p-6">
|
|
101
|
+
<Dialog
|
|
102
|
+
open={navOpen}
|
|
103
|
+
onClose={() => setNavOpen(false)}
|
|
104
|
+
title="Folders"
|
|
105
|
+
anchor="left"
|
|
106
|
+
>
|
|
107
|
+
<nav className="flex flex-col gap-2 p-4">
|
|
108
|
+
<Button variant="ghost">Inbox</Button>
|
|
109
|
+
<Button variant="ghost">Archive</Button>
|
|
110
|
+
</nav>
|
|
111
|
+
</Dialog>
|
|
112
|
+
<Dialog
|
|
113
|
+
open={moveOpen}
|
|
114
|
+
onClose={() => setMoveOpen(false)}
|
|
115
|
+
title="Move message"
|
|
116
|
+
>
|
|
117
|
+
<div className="flex justify-end gap-2 p-4">
|
|
118
|
+
<Button variant="ghost" onClick={() => setMoveOpen(false)}>
|
|
119
|
+
Cancel
|
|
120
|
+
</Button>
|
|
121
|
+
<Button onClick={() => setMoveOpen(false)}>Move</Button>
|
|
122
|
+
</div>
|
|
123
|
+
</Dialog>
|
|
124
|
+
</div>
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* At narrow widths the nav drawer is a dialog too, so two `aria-modal` surfaces
|
|
130
|
+
* can be up at once. Tab belongs to the one on top: it cycles between Cancel and
|
|
131
|
+
* Move and never reaches the folder list behind it (#1204).
|
|
132
|
+
*/
|
|
133
|
+
export const TabStaysInTheTopSurface: Story = {
|
|
134
|
+
render: () => <StackedDemo />,
|
|
135
|
+
play: async ({ canvasElement }) => {
|
|
136
|
+
const canvas = within(canvasElement);
|
|
137
|
+
const cancel = canvas.getByRole("button", { name: "Cancel" });
|
|
138
|
+
const move = canvas.getByRole("button", { name: "Move" });
|
|
139
|
+
const inbox = canvas.getByRole("button", { name: "Inbox" });
|
|
140
|
+
const archive = canvas.getByRole("button", { name: "Archive" });
|
|
141
|
+
|
|
142
|
+
await expect(cancel).toHaveFocus();
|
|
143
|
+
|
|
144
|
+
await userEvent.tab();
|
|
145
|
+
await expect(move).toHaveFocus();
|
|
146
|
+
|
|
147
|
+
await userEvent.tab();
|
|
148
|
+
await expect(cancel).toHaveFocus();
|
|
149
|
+
await expect(inbox).not.toHaveFocus();
|
|
150
|
+
await expect(archive).not.toHaveFocus();
|
|
151
|
+
|
|
152
|
+
await userEvent.tab({ shift: true });
|
|
153
|
+
await expect(move).toHaveFocus();
|
|
154
|
+
await expect(archive).not.toHaveFocus();
|
|
155
|
+
},
|
|
156
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type ReactNode, useRef } from "react";
|
|
2
2
|
import { cn } from "../lib/cn.js";
|
|
3
3
|
import { useOverlayScope } from "../lib/overlay-scope.js";
|
|
4
|
-
import {
|
|
4
|
+
import { useModalFocus } from "../lib/use-modal-focus.js";
|
|
5
5
|
import { DialogBackdrop } from "./dialog-backdrop.js";
|
|
6
6
|
|
|
7
7
|
export interface DialogProps {
|
|
@@ -30,7 +30,7 @@ export function Dialog({
|
|
|
30
30
|
const dialogRef = useRef<HTMLDivElement>(null);
|
|
31
31
|
|
|
32
32
|
useOverlayScope({ id: "dialog", open, answers: { back: onClose } });
|
|
33
|
-
|
|
33
|
+
useModalFocus(dialogRef, open);
|
|
34
34
|
|
|
35
35
|
if (!open) return null;
|
|
36
36
|
|
|
@@ -54,6 +54,7 @@ export function Dialog({
|
|
|
54
54
|
role="dialog"
|
|
55
55
|
aria-modal="true"
|
|
56
56
|
aria-labelledby="dialog-title"
|
|
57
|
+
tabIndex={-1}
|
|
57
58
|
className={cn(
|
|
58
59
|
"relative z-10 overflow-hidden border-line bg-surface shadow-xl",
|
|
59
60
|
// The edge-anchored panels reach the device edges; the centered
|
|
@@ -0,0 +1,107 @@
|
|
|
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 } from "react";
|
|
5
|
+
import { createRoot, type Root } from "react-dom/client";
|
|
6
|
+
import { FlowScreen, type FlowScreenProps } from "./flow-screen.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
|
+
const screen = (anchor: FlowScreenProps["anchor"]) => {
|
|
21
|
+
// createElement never folds the children argument into the props type, so a
|
|
22
|
+
// component with required children needs the props cast.
|
|
23
|
+
const props: Omit<FlowScreenProps, "children"> = {
|
|
24
|
+
title: "Pick a calendar",
|
|
25
|
+
steps: ["Calendar"],
|
|
26
|
+
activeStep: 0,
|
|
27
|
+
onBack: () => undefined,
|
|
28
|
+
onExit: () => undefined,
|
|
29
|
+
anchor,
|
|
30
|
+
};
|
|
31
|
+
return createElement(
|
|
32
|
+
FlowScreen,
|
|
33
|
+
props as FlowScreenProps,
|
|
34
|
+
createElement("button", { type: "button" }, "Body action"),
|
|
35
|
+
);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const surface = (): HTMLElement => {
|
|
39
|
+
const element = document.querySelector<HTMLElement>("#root > *");
|
|
40
|
+
assert.ok(element, "no screen rendered");
|
|
41
|
+
return element;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const tab = (): KeyboardEvent => {
|
|
45
|
+
const event = new KeyboardEvent("keydown", {
|
|
46
|
+
key: "Tab",
|
|
47
|
+
bubbles: true,
|
|
48
|
+
cancelable: true,
|
|
49
|
+
});
|
|
50
|
+
(document.activeElement ?? document).dispatchEvent(event);
|
|
51
|
+
return event;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
describe("FlowScreen focus", () => {
|
|
55
|
+
it("takes the keyboard when it covers the window", () => {
|
|
56
|
+
act(() => root.render(screen("viewport")));
|
|
57
|
+
|
|
58
|
+
assert.equal(document.activeElement?.getAttribute("aria-label"), "Back");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The contained variant fills a pane that is already on screen rather than
|
|
63
|
+
* covering the app, so the controls around it stay reachable.
|
|
64
|
+
*/
|
|
65
|
+
it("leaves the keyboard alone when it fills a pane", () => {
|
|
66
|
+
const outside = document.createElement("button");
|
|
67
|
+
outside.textContent = "Beside the pane";
|
|
68
|
+
document.body.appendChild(outside);
|
|
69
|
+
outside.focus();
|
|
70
|
+
|
|
71
|
+
act(() => root.render(screen("container")));
|
|
72
|
+
assert.equal(document.activeElement, outside);
|
|
73
|
+
|
|
74
|
+
const event = tab();
|
|
75
|
+
|
|
76
|
+
assert.equal(event.defaultPrevented, false);
|
|
77
|
+
assert.equal(document.activeElement, outside);
|
|
78
|
+
outside.remove();
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* What the screen claims and what it does have to be the same statement: a
|
|
84
|
+
* surface that says the rest of the page is gone while Tab walks into it tells a
|
|
85
|
+
* screen-reader user the opposite of what the keyboard does (#1204).
|
|
86
|
+
*/
|
|
87
|
+
describe("FlowScreen semantics", () => {
|
|
88
|
+
it("is a modal dialog when it covers the window", () => {
|
|
89
|
+
act(() => root.render(screen("viewport")));
|
|
90
|
+
|
|
91
|
+
const element = surface();
|
|
92
|
+
assert.equal(element.getAttribute("role"), "dialog");
|
|
93
|
+
assert.equal(element.getAttribute("aria-modal"), "true");
|
|
94
|
+
assert.equal(element.getAttribute("aria-label"), "Pick a calendar");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
/** A labelled `section` is a region, which is what a pane's content is. */
|
|
98
|
+
it("is a labelled region when it fills a pane", () => {
|
|
99
|
+
act(() => root.render(screen("container")));
|
|
100
|
+
|
|
101
|
+
const element = surface();
|
|
102
|
+
assert.equal(element.tagName, "SECTION");
|
|
103
|
+
assert.equal(element.getAttribute("role"), null);
|
|
104
|
+
assert.equal(element.getAttribute("aria-modal"), null);
|
|
105
|
+
assert.equal(element.getAttribute("aria-label"), "Pick a calendar");
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -5,8 +5,9 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { ArrowLeft, X } from "lucide-react";
|
|
8
|
-
import type
|
|
8
|
+
import { type ReactNode, useRef } from "react";
|
|
9
9
|
import { cn } from "../lib/cn.js";
|
|
10
|
+
import { useModalFocus } from "../lib/use-modal-focus.js";
|
|
10
11
|
|
|
11
12
|
export interface FlowStepRailProps {
|
|
12
13
|
/** How many steps the flow walks. */
|
|
@@ -63,8 +64,13 @@ export interface FlowScreenProps {
|
|
|
63
64
|
|
|
64
65
|
/**
|
|
65
66
|
* The body is the only scrolling region, so back and the footer's controls
|
|
66
|
-
* never leave the screen.
|
|
67
|
-
*
|
|
67
|
+
* never leave the screen.
|
|
68
|
+
*
|
|
69
|
+
* The viewport variant covers what opened it, which is why it takes both the
|
|
70
|
+
* accessibility tree and the keyboard with it. The contained one is the content
|
|
71
|
+
* of a pane that is already on screen, so it claims neither: `aria-modal` there
|
|
72
|
+
* tells a screen reader the rest of the page is gone while Tab walks straight
|
|
73
|
+
* into it, which is the split the trap exists to close (#1204).
|
|
68
74
|
*/
|
|
69
75
|
export function FlowScreen({
|
|
70
76
|
title,
|
|
@@ -79,91 +85,111 @@ export function FlowScreen({
|
|
|
79
85
|
bodyFit = "scroll",
|
|
80
86
|
}: FlowScreenProps) {
|
|
81
87
|
const contained = anchor === "container";
|
|
82
|
-
|
|
88
|
+
const screenRef = useRef<HTMLElement>(null);
|
|
89
|
+
useModalFocus(screenRef, !contained);
|
|
90
|
+
const frame = cn(
|
|
91
|
+
"z-50 flex flex-col font-sans text-fg",
|
|
92
|
+
contained
|
|
93
|
+
? "absolute inset-0"
|
|
94
|
+
: "fixed inset-0 md:items-center md:justify-center md:bg-black/40 md:p-6",
|
|
95
|
+
);
|
|
96
|
+
const body = (
|
|
83
97
|
<div
|
|
84
|
-
role="dialog"
|
|
85
|
-
aria-modal="true"
|
|
86
|
-
aria-label={title}
|
|
87
98
|
className={cn(
|
|
88
|
-
"
|
|
89
|
-
contained
|
|
90
|
-
|
|
91
|
-
: "fixed inset-0 md:items-center md:justify-center md:bg-black/40 md:p-6",
|
|
99
|
+
"flex min-h-0 w-full flex-1 flex-col bg-canvas",
|
|
100
|
+
!contained &&
|
|
101
|
+
"md:h-[45rem] md:max-h-[calc(100dvh-3rem)] md:w-[35rem] md:max-w-[calc(100vw-3rem)] md:flex-none md:overflow-hidden md:rounded-xl md:border md:border-line md:shadow-lg",
|
|
92
102
|
)}
|
|
93
103
|
>
|
|
104
|
+
<header
|
|
105
|
+
className={cn(
|
|
106
|
+
"shrink-0 border-b border-line px-3 pb-2",
|
|
107
|
+
contained
|
|
108
|
+
? "pt-3"
|
|
109
|
+
: "pt-[calc(0.75rem+env(safe-area-inset-top,0px))] md:pt-3",
|
|
110
|
+
)}
|
|
111
|
+
>
|
|
112
|
+
<div className="flex items-center gap-1">
|
|
113
|
+
<button
|
|
114
|
+
type="button"
|
|
115
|
+
onClick={onBack}
|
|
116
|
+
aria-label="Back"
|
|
117
|
+
className="flex size-11 items-center justify-center rounded-md text-fg-muted"
|
|
118
|
+
>
|
|
119
|
+
<ArrowLeft className="size-5" />
|
|
120
|
+
</button>
|
|
121
|
+
<div className="min-w-0 flex-1 text-center">
|
|
122
|
+
<h1 className="truncate text-sm font-semibold">{title}</h1>
|
|
123
|
+
{subtitle && (
|
|
124
|
+
<p className="truncate text-2xs text-fg-muted">{subtitle}</p>
|
|
125
|
+
)}
|
|
126
|
+
</div>
|
|
127
|
+
<button
|
|
128
|
+
type="button"
|
|
129
|
+
onClick={onExit}
|
|
130
|
+
aria-label="Cancel"
|
|
131
|
+
className="flex size-11 items-center justify-center rounded-md text-fg-muted"
|
|
132
|
+
>
|
|
133
|
+
<X className="size-5" />
|
|
134
|
+
</button>
|
|
135
|
+
</div>
|
|
136
|
+
{steps.length > 1 && (
|
|
137
|
+
<div className="space-y-1 px-1 pt-2">
|
|
138
|
+
<FlowStepRail count={steps.length} activeStep={activeStep} />
|
|
139
|
+
<p className="text-2xs text-fg-subtle">
|
|
140
|
+
Step {activeStep + 1} of {steps.length} · {steps[activeStep]}
|
|
141
|
+
</p>
|
|
142
|
+
</div>
|
|
143
|
+
)}
|
|
144
|
+
</header>
|
|
145
|
+
|
|
94
146
|
<div
|
|
95
147
|
className={cn(
|
|
96
|
-
"
|
|
97
|
-
|
|
98
|
-
"md:h-[45rem] md:max-h-[calc(100dvh-3rem)] md:w-[35rem] md:max-w-[calc(100vw-3rem)] md:flex-none md:overflow-hidden md:rounded-xl md:border md:border-line md:shadow-lg",
|
|
148
|
+
"min-h-0 flex-1",
|
|
149
|
+
bodyFit === "fill" ? "overflow-hidden" : "overflow-y-auto px-4 py-4",
|
|
99
150
|
)}
|
|
100
151
|
>
|
|
101
|
-
|
|
152
|
+
{children}
|
|
153
|
+
</div>
|
|
154
|
+
|
|
155
|
+
{footer && (
|
|
156
|
+
<footer
|
|
102
157
|
className={cn(
|
|
103
|
-
"shrink-0 border-
|
|
158
|
+
"shrink-0 border-t border-line px-4 py-3",
|
|
104
159
|
contained
|
|
105
|
-
? "
|
|
106
|
-
: "
|
|
160
|
+
? ""
|
|
161
|
+
: "pb-[max(0.75rem,env(safe-area-inset-bottom))] md:pb-3",
|
|
107
162
|
)}
|
|
108
163
|
>
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
className="flex size-11 items-center justify-center rounded-md text-fg-muted"
|
|
115
|
-
>
|
|
116
|
-
<ArrowLeft className="size-5" />
|
|
117
|
-
</button>
|
|
118
|
-
<div className="min-w-0 flex-1 text-center">
|
|
119
|
-
<h1 className="truncate text-sm font-semibold">{title}</h1>
|
|
120
|
-
{subtitle && (
|
|
121
|
-
<p className="truncate text-2xs text-fg-muted">{subtitle}</p>
|
|
122
|
-
)}
|
|
123
|
-
</div>
|
|
124
|
-
<button
|
|
125
|
-
type="button"
|
|
126
|
-
onClick={onExit}
|
|
127
|
-
aria-label="Cancel"
|
|
128
|
-
className="flex size-11 items-center justify-center rounded-md text-fg-muted"
|
|
129
|
-
>
|
|
130
|
-
<X className="size-5" />
|
|
131
|
-
</button>
|
|
132
|
-
</div>
|
|
133
|
-
{steps.length > 1 && (
|
|
134
|
-
<div className="space-y-1 px-1 pt-2">
|
|
135
|
-
<FlowStepRail count={steps.length} activeStep={activeStep} />
|
|
136
|
-
<p className="text-2xs text-fg-subtle">
|
|
137
|
-
Step {activeStep + 1} of {steps.length} · {steps[activeStep]}
|
|
138
|
-
</p>
|
|
139
|
-
</div>
|
|
140
|
-
)}
|
|
141
|
-
</header>
|
|
164
|
+
{footer}
|
|
165
|
+
</footer>
|
|
166
|
+
)}
|
|
167
|
+
</div>
|
|
168
|
+
);
|
|
142
169
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
170
|
+
if (contained) {
|
|
171
|
+
return (
|
|
172
|
+
<section
|
|
173
|
+
ref={screenRef}
|
|
174
|
+
aria-label={title}
|
|
175
|
+
tabIndex={-1}
|
|
176
|
+
className={frame}
|
|
177
|
+
>
|
|
178
|
+
{body}
|
|
179
|
+
</section>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
153
182
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
)}
|
|
166
|
-
</div>
|
|
167
|
-
</div>
|
|
183
|
+
return (
|
|
184
|
+
<section
|
|
185
|
+
ref={screenRef}
|
|
186
|
+
role="dialog"
|
|
187
|
+
aria-modal="true"
|
|
188
|
+
aria-label={title}
|
|
189
|
+
tabIndex={-1}
|
|
190
|
+
className={frame}
|
|
191
|
+
>
|
|
192
|
+
{body}
|
|
193
|
+
</section>
|
|
168
194
|
);
|
|
169
195
|
}
|