@remit/ui 0.0.178 → 0.0.180
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.render.test.ts +61 -0
- package/src/components/bottom-sheet.stories.tsx +28 -1
- package/src/components/bottom-sheet.tsx +20 -2
- package/src/components/category-presentation.stories.tsx +298 -0
- package/src/components/dialog.tsx +3 -11
- package/src/components/rescue-from-spam-flow.tsx +6 -1
- package/src/components/rich-text-correction-menu.tsx +1 -0
- package/src/index.ts +1 -0
- package/src/lib/use-initial-focus.test.ts +78 -0
- package/src/lib/use-initial-focus.ts +36 -0
package/package.json
CHANGED
|
@@ -12,6 +12,7 @@ describe("BottomSheet", () => {
|
|
|
12
12
|
createElement(BottomSheet, {
|
|
13
13
|
open: true,
|
|
14
14
|
onClose: noop,
|
|
15
|
+
label: "Sheet",
|
|
15
16
|
// biome-ignore lint/correctness/noChildrenProp: React 19 types require children in props object when using createElement
|
|
16
17
|
children: "Sheet body",
|
|
17
18
|
}),
|
|
@@ -21,11 +22,70 @@ describe("BottomSheet", () => {
|
|
|
21
22
|
assert.match(html, /Drag down to dismiss/);
|
|
22
23
|
});
|
|
23
24
|
|
|
25
|
+
it("takes the grabber out of the tab order, since it has no keyboard operation", () => {
|
|
26
|
+
const html = renderToString(
|
|
27
|
+
createElement(BottomSheet, {
|
|
28
|
+
open: true,
|
|
29
|
+
onClose: noop,
|
|
30
|
+
label: "Sheet",
|
|
31
|
+
// biome-ignore lint/correctness/noChildrenProp: React 19 types require children in props object when using createElement
|
|
32
|
+
children: "Sheet body",
|
|
33
|
+
}),
|
|
34
|
+
);
|
|
35
|
+
const grabber = html.match(/<div role="slider"[^>]*>/)?.[0] ?? "";
|
|
36
|
+
assert.match(grabber, /tabindex="-1"/);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("carries modal dialog semantics when open", () => {
|
|
40
|
+
const html = renderToString(
|
|
41
|
+
createElement(BottomSheet, {
|
|
42
|
+
open: true,
|
|
43
|
+
onClose: noop,
|
|
44
|
+
label: "Sheet",
|
|
45
|
+
// biome-ignore lint/correctness/noChildrenProp: React 19 types require children in props object when using createElement
|
|
46
|
+
children: "Sheet body",
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
assert.match(html, /role="dialog"/);
|
|
50
|
+
assert.match(html, /aria-modal="true"/);
|
|
51
|
+
assert.doesNotMatch(html, /aria-hidden="true"/);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("names itself for assistive tech from the label prop", () => {
|
|
55
|
+
const html = renderToString(
|
|
56
|
+
createElement(BottomSheet, {
|
|
57
|
+
open: true,
|
|
58
|
+
onClose: noop,
|
|
59
|
+
label: "Rescue from spam",
|
|
60
|
+
// biome-ignore lint/correctness/noChildrenProp: React 19 types require children in props object when using createElement
|
|
61
|
+
children: "Sheet body",
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
assert.match(html, /aria-labelledby="bottom-sheet-title"/);
|
|
65
|
+
assert.match(html, /id="bottom-sheet-title"[^>]*>Rescue from spam</);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("goes inert and hides from assistive tech when closed", () => {
|
|
69
|
+
const html = renderToString(
|
|
70
|
+
createElement(BottomSheet, {
|
|
71
|
+
open: false,
|
|
72
|
+
onClose: noop,
|
|
73
|
+
label: "Sheet",
|
|
74
|
+
// biome-ignore lint/correctness/noChildrenProp: React 19 types require children in props object when using createElement
|
|
75
|
+
children: "Sheet body",
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
assert.match(html, /role="dialog"/);
|
|
79
|
+
assert.match(html, /aria-hidden="true"/);
|
|
80
|
+
assert.match(html, /inert=""/);
|
|
81
|
+
});
|
|
82
|
+
|
|
24
83
|
it("uses the provided dismiss label on the scrim", () => {
|
|
25
84
|
const html = renderToString(
|
|
26
85
|
createElement(BottomSheet, {
|
|
27
86
|
open: true,
|
|
28
87
|
onClose: noop,
|
|
88
|
+
label: "Sheet",
|
|
29
89
|
dismissLabel: "Close rescue",
|
|
30
90
|
// biome-ignore lint/correctness/noChildrenProp: React 19 types require children in props object when using createElement
|
|
31
91
|
children: "x",
|
|
@@ -39,6 +99,7 @@ describe("BottomSheet", () => {
|
|
|
39
99
|
createElement(BottomSheet, {
|
|
40
100
|
open: false,
|
|
41
101
|
onClose: noop,
|
|
102
|
+
label: "Sheet",
|
|
42
103
|
// biome-ignore lint/correctness/noChildrenProp: React 19 types require children in props object when using createElement
|
|
43
104
|
children: "x",
|
|
44
105
|
}),
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
2
|
import { useState } from "react";
|
|
3
|
+
import { expect, userEvent, within } from "storybook/test";
|
|
3
4
|
import { BottomSheet } from "./bottom-sheet.js";
|
|
4
5
|
import { Button } from "./button.js";
|
|
5
6
|
|
|
@@ -47,7 +48,11 @@ function Demo() {
|
|
|
47
48
|
Open sheet
|
|
48
49
|
</Button>
|
|
49
50
|
)}
|
|
50
|
-
<BottomSheet
|
|
51
|
+
<BottomSheet
|
|
52
|
+
open={open}
|
|
53
|
+
onClose={() => setOpen(false)}
|
|
54
|
+
label="Action sheet"
|
|
55
|
+
>
|
|
51
56
|
<div className="px-row-inset py-6">
|
|
52
57
|
<h2 className="text-sm font-semibold text-fg">Action sheet</h2>
|
|
53
58
|
<p className="mt-1 text-xs text-fg-subtle">
|
|
@@ -69,3 +74,25 @@ function Demo() {
|
|
|
69
74
|
export const Default: Story = {
|
|
70
75
|
render: () => <Demo />,
|
|
71
76
|
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Same as `Default`, plus the dialog semantics the sheet now carries: a
|
|
80
|
+
* `role="dialog"` node opens focused on its first control, and Escape
|
|
81
|
+
* dismisses it the same way tapping the scrim does.
|
|
82
|
+
*/
|
|
83
|
+
export const MirrorsDialogSemantics: Story = {
|
|
84
|
+
render: () => <Demo />,
|
|
85
|
+
play: async ({ canvasElement }) => {
|
|
86
|
+
const canvas = within(canvasElement);
|
|
87
|
+
const sheet = await canvas.findByRole("dialog");
|
|
88
|
+
await expect(sheet).toHaveAttribute("aria-modal", "true");
|
|
89
|
+
await expect(sheet).toHaveAccessibleName("Action sheet");
|
|
90
|
+
await expect(canvas.getByRole("button", { name: "Got it" })).toHaveFocus();
|
|
91
|
+
|
|
92
|
+
await userEvent.keyboard("{Escape}");
|
|
93
|
+
await expect(
|
|
94
|
+
canvas.getByRole("button", { name: "Open sheet" }),
|
|
95
|
+
).toBeVisible();
|
|
96
|
+
await expect(canvas.queryByRole("dialog")).toBeNull();
|
|
97
|
+
},
|
|
98
|
+
};
|
|
@@ -2,6 +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 { useInitialFocus } from "../lib/use-initial-focus.js";
|
|
5
6
|
|
|
6
7
|
const SNAP_MS = 320;
|
|
7
8
|
const SNAP_EASE = "cubic-bezier(0.32, 0.9, 0.3, 1)";
|
|
@@ -17,6 +18,8 @@ export interface BottomSheetProps {
|
|
|
17
18
|
open: boolean;
|
|
18
19
|
onClose: () => void;
|
|
19
20
|
children: ReactNode;
|
|
21
|
+
/** Accessible name for the sheet itself, read before its content. */
|
|
22
|
+
label: string;
|
|
20
23
|
/** Accessible label for the drag-to-dismiss scrim and grabber. */
|
|
21
24
|
dismissLabel?: string;
|
|
22
25
|
}
|
|
@@ -31,14 +34,18 @@ export function BottomSheet({
|
|
|
31
34
|
open,
|
|
32
35
|
onClose,
|
|
33
36
|
children,
|
|
37
|
+
label,
|
|
34
38
|
dismissLabel = "Dismiss",
|
|
35
39
|
}: BottomSheetProps) {
|
|
36
40
|
useOverlayScope({ id: "bottom-sheet", open, answers: { back: onClose } });
|
|
37
41
|
|
|
38
42
|
const sheetRef = useRef<HTMLDivElement>(null);
|
|
43
|
+
const contentRef = useRef<HTMLDivElement>(null);
|
|
39
44
|
const [height, setHeight] = useState(HEIGHT_FALLBACK);
|
|
40
45
|
const [drag, setDrag] = useState<number | null>(null);
|
|
41
46
|
|
|
47
|
+
useInitialFocus(contentRef, open);
|
|
48
|
+
|
|
42
49
|
useLayoutEffect(() => {
|
|
43
50
|
const el = sheetRef.current;
|
|
44
51
|
if (!el) return;
|
|
@@ -122,16 +129,24 @@ export function BottomSheet({
|
|
|
122
129
|
/>
|
|
123
130
|
<div
|
|
124
131
|
ref={sheetRef}
|
|
132
|
+
role="dialog"
|
|
133
|
+
aria-modal="true"
|
|
134
|
+
aria-labelledby="bottom-sheet-title"
|
|
135
|
+
aria-hidden={!open}
|
|
136
|
+
inert={!open}
|
|
125
137
|
className="absolute inset-x-0 bottom-0 flex max-h-[92%] flex-col rounded-t-3xl border-t border-line bg-surface pb-[env(safe-area-inset-bottom,0px)] shadow-2xl shadow-black/30"
|
|
126
138
|
style={{ transform: `translateY(${offset}px)`, transition }}
|
|
127
139
|
>
|
|
140
|
+
<h2 id="bottom-sheet-title" className="sr-only">
|
|
141
|
+
{label}
|
|
142
|
+
</h2>
|
|
128
143
|
<div
|
|
129
144
|
role="slider"
|
|
130
145
|
aria-label="Drag down to dismiss"
|
|
131
146
|
aria-valuemin={0}
|
|
132
147
|
aria-valuemax={100}
|
|
133
148
|
aria-valuenow={Math.round(openness * 100)}
|
|
134
|
-
tabIndex={
|
|
149
|
+
tabIndex={-1}
|
|
135
150
|
onPointerDown={onPointerDown}
|
|
136
151
|
onPointerMove={onPointerMove}
|
|
137
152
|
onPointerUp={finishDrag}
|
|
@@ -140,7 +155,10 @@ export function BottomSheet({
|
|
|
140
155
|
>
|
|
141
156
|
<div className="h-1 w-10 rounded-full bg-fg-subtle/40" />
|
|
142
157
|
</div>
|
|
143
|
-
<div
|
|
158
|
+
<div
|
|
159
|
+
ref={contentRef}
|
|
160
|
+
className="flex min-h-0 flex-1 flex-col overflow-hidden"
|
|
161
|
+
>
|
|
144
162
|
{children}
|
|
145
163
|
</div>
|
|
146
164
|
</div>
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react";
|
|
2
|
+
import { expect, within } from "storybook/test";
|
|
3
|
+
import { briefFilterConfig } from "../filter-presets.js";
|
|
4
|
+
import {
|
|
5
|
+
categoryTone,
|
|
6
|
+
type ThreadCategory,
|
|
7
|
+
type ThreadRowData,
|
|
8
|
+
type ThreadSection,
|
|
9
|
+
} from "./app-shell-types.js";
|
|
10
|
+
import { Badge } from "./badge.js";
|
|
11
|
+
import { BriefSection } from "./brief-section.js";
|
|
12
|
+
import {
|
|
13
|
+
type CategoryTone,
|
|
14
|
+
FilterSheet,
|
|
15
|
+
type FilterSheetCategory,
|
|
16
|
+
} from "./filter-sheet.js";
|
|
17
|
+
import { ComfortableRow } from "./message-row.js";
|
|
18
|
+
|
|
19
|
+
interface CategoryPresentation {
|
|
20
|
+
category: ThreadCategory;
|
|
21
|
+
/** The one word every surface calls this category: chip, badge, section. */
|
|
22
|
+
label: string;
|
|
23
|
+
tone: CategoryTone;
|
|
24
|
+
/** Place in the brief's section order, 1-based. */
|
|
25
|
+
section: number;
|
|
26
|
+
/** Mail of this category on the reference mailbox, for the section header. */
|
|
27
|
+
total: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The canonical presentation of every message category, in chip-row order.
|
|
32
|
+
*
|
|
33
|
+
* The labels are the ones the filter chips, the search tokens and the brief
|
|
34
|
+
* sections already share, so a category reads the same word wherever it is
|
|
35
|
+
* named — including on the row badge, which drops the separate "receipt" and
|
|
36
|
+
* "notification" wording: a reader who picks the Transactional chip has to be
|
|
37
|
+
* able to see that it was that chip which produced the badge.
|
|
38
|
+
*
|
|
39
|
+
* The tones are the row badge's, the surface the reader meets thousands of
|
|
40
|
+
* times: Personal accents, Transactional reads positive, Social warns, and
|
|
41
|
+
* everything else is neutral. Marketing is not a hazard and does not warn.
|
|
42
|
+
*
|
|
43
|
+
* The chip row leads with All and puts Unclassified straight after Personal —
|
|
44
|
+
* pending classification is a named state, offered where the reader looks for
|
|
45
|
+
* their own mail. The section order ends with it instead: a section for mail
|
|
46
|
+
* the classifier has not reached belongs under the mail it has.
|
|
47
|
+
*/
|
|
48
|
+
const CANONICAL: readonly CategoryPresentation[] = [
|
|
49
|
+
{
|
|
50
|
+
category: "personal",
|
|
51
|
+
label: "Personal",
|
|
52
|
+
tone: "accent",
|
|
53
|
+
section: 1,
|
|
54
|
+
total: 4753,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
category: "uncategorized",
|
|
58
|
+
label: "Unclassified",
|
|
59
|
+
tone: "neutral",
|
|
60
|
+
section: 7,
|
|
61
|
+
total: 12,
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
category: "transactional",
|
|
65
|
+
label: "Transactional",
|
|
66
|
+
tone: "positive",
|
|
67
|
+
section: 2,
|
|
68
|
+
total: 429,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
category: "newsletter",
|
|
72
|
+
label: "Newsletter",
|
|
73
|
+
tone: "neutral",
|
|
74
|
+
section: 3,
|
|
75
|
+
total: 2295,
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
category: "marketing",
|
|
79
|
+
label: "Marketing",
|
|
80
|
+
tone: "neutral",
|
|
81
|
+
section: 4,
|
|
82
|
+
total: 3942,
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
category: "social",
|
|
86
|
+
label: "Social",
|
|
87
|
+
tone: "warning",
|
|
88
|
+
section: 5,
|
|
89
|
+
total: 88,
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
category: "automated",
|
|
93
|
+
label: "Automated",
|
|
94
|
+
tone: "neutral",
|
|
95
|
+
section: 6,
|
|
96
|
+
total: 2680,
|
|
97
|
+
},
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
const SECTION_ORDER: readonly CategoryPresentation[] = [...CANONICAL].sort(
|
|
101
|
+
(a, b) => a.section - b.section,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const emptySectionCopy = (label: string): string =>
|
|
105
|
+
`No ${label} mail in this brief.`;
|
|
106
|
+
|
|
107
|
+
const chipCategories: FilterSheetCategory[] = [
|
|
108
|
+
{ id: "all", label: "All", tone: "neutral" },
|
|
109
|
+
...CANONICAL.map(({ category, label, tone }) => ({
|
|
110
|
+
id: category,
|
|
111
|
+
label,
|
|
112
|
+
tone,
|
|
113
|
+
})),
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
const sampleRow = (entry: CategoryPresentation): ThreadRowData => ({
|
|
117
|
+
id: `row-${entry.category}`,
|
|
118
|
+
accountId: "a1",
|
|
119
|
+
fromName: `${entry.label} sender`,
|
|
120
|
+
fromEmail: `${entry.category}@example.com`,
|
|
121
|
+
subject: `A ${entry.label.toLowerCase()} message`,
|
|
122
|
+
snippet: "A short preview of the message body.",
|
|
123
|
+
timeLabel: "9:42",
|
|
124
|
+
isRead: true,
|
|
125
|
+
category: entry.category,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const orderedSections: ThreadSection[] = SECTION_ORDER.map((entry) => ({
|
|
129
|
+
id: entry.category,
|
|
130
|
+
label: entry.label,
|
|
131
|
+
threads: [sampleRow(entry)],
|
|
132
|
+
}));
|
|
133
|
+
|
|
134
|
+
const emptiedSections: ThreadSection[] = SECTION_ORDER.map((entry) => ({
|
|
135
|
+
id: entry.category,
|
|
136
|
+
label: entry.label,
|
|
137
|
+
threads: [],
|
|
138
|
+
total: { kind: "exact", value: entry.total },
|
|
139
|
+
}));
|
|
140
|
+
|
|
141
|
+
const meta: Meta = {
|
|
142
|
+
title: "Mail/Category presentation",
|
|
143
|
+
parameters: { layout: "padded" },
|
|
144
|
+
};
|
|
145
|
+
export default meta;
|
|
146
|
+
|
|
147
|
+
type Story = StoryObj;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The decision itself, in one place: per category, the label every surface
|
|
151
|
+
* shows, the tone it carries, where it sits in the chip row and where it sits
|
|
152
|
+
* in the brief's section order, and what the section says once a chip has left
|
|
153
|
+
* it with no rows.
|
|
154
|
+
*/
|
|
155
|
+
export const CanonicalTable: Story = {
|
|
156
|
+
render: () => (
|
|
157
|
+
<table className="w-full text-left text-xs text-fg-muted">
|
|
158
|
+
<thead>
|
|
159
|
+
<tr className="text-2xs uppercase tracking-wider text-fg-subtle">
|
|
160
|
+
<th className="py-1 pr-4 font-semibold">Category</th>
|
|
161
|
+
<th className="py-1 pr-4 font-semibold">Label</th>
|
|
162
|
+
<th className="py-1 pr-4 font-semibold">Tone</th>
|
|
163
|
+
<th className="py-1 pr-4 font-semibold">Chip</th>
|
|
164
|
+
<th className="py-1 pr-4 font-semibold">Section</th>
|
|
165
|
+
<th className="py-1 font-semibold">Emptied by a chip</th>
|
|
166
|
+
</tr>
|
|
167
|
+
</thead>
|
|
168
|
+
<tbody className="divide-y divide-line">
|
|
169
|
+
{CANONICAL.map((entry, index) => (
|
|
170
|
+
<tr
|
|
171
|
+
key={entry.category}
|
|
172
|
+
data-category={entry.category}
|
|
173
|
+
data-tone={entry.tone}
|
|
174
|
+
>
|
|
175
|
+
<td className="py-1.5 pr-4 font-mono text-fg-subtle">
|
|
176
|
+
{entry.category}
|
|
177
|
+
</td>
|
|
178
|
+
<td className="py-1.5 pr-4">
|
|
179
|
+
<Badge tone={entry.tone}>{entry.label}</Badge>
|
|
180
|
+
</td>
|
|
181
|
+
<td className="py-1.5 pr-4">{entry.tone}</td>
|
|
182
|
+
<td className="py-1.5 pr-4 tabular-nums">{index + 2}</td>
|
|
183
|
+
<td className="py-1.5 pr-4 tabular-nums">{entry.section}</td>
|
|
184
|
+
<td className="py-1.5">{emptySectionCopy(entry.label)}</td>
|
|
185
|
+
</tr>
|
|
186
|
+
))}
|
|
187
|
+
</tbody>
|
|
188
|
+
</table>
|
|
189
|
+
),
|
|
190
|
+
play: async ({ canvasElement }) => {
|
|
191
|
+
const rows = Array.from(
|
|
192
|
+
canvasElement.querySelectorAll<HTMLTableRowElement>("tbody tr"),
|
|
193
|
+
);
|
|
194
|
+
await expect(rows).toHaveLength(CANONICAL.length);
|
|
195
|
+
for (const [index, entry] of CANONICAL.entries()) {
|
|
196
|
+
const row = rows[index];
|
|
197
|
+
await expect(row).toHaveAttribute("data-category", entry.category);
|
|
198
|
+
await expect(row).toHaveAttribute("data-tone", entry.tone);
|
|
199
|
+
await expect(within(row).getByText(entry.label)).toBeVisible();
|
|
200
|
+
await expect(within(row).getByText(entry.tone)).toBeVisible();
|
|
201
|
+
await expect(categoryTone[entry.category]).toBe(entry.tone);
|
|
202
|
+
}
|
|
203
|
+
const labels = CANONICAL.map((entry) => entry.label);
|
|
204
|
+
await expect(new Set(labels).size).toBe(labels.length);
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The chip row: All, then the categories in the order the reader meets them.
|
|
210
|
+
* Every chip carries its category's tone, so the chip and the badge the chip
|
|
211
|
+
* filters for read alike.
|
|
212
|
+
*/
|
|
213
|
+
export const ChipRow: Story = {
|
|
214
|
+
render: () => (
|
|
215
|
+
<div className="h-96 w-96 border border-line">
|
|
216
|
+
<FilterSheet
|
|
217
|
+
categories={chipCategories}
|
|
218
|
+
filters={briefFilterConfig().filters}
|
|
219
|
+
selectedCategory="all"
|
|
220
|
+
activeFilters={new Set<string>()}
|
|
221
|
+
expanded
|
|
222
|
+
onSelectCategory={() => undefined}
|
|
223
|
+
onToggleFilter={() => undefined}
|
|
224
|
+
onExpandedChange={() => undefined}
|
|
225
|
+
onClear={() => undefined}
|
|
226
|
+
/>
|
|
227
|
+
</div>
|
|
228
|
+
),
|
|
229
|
+
play: async ({ canvasElement }) => {
|
|
230
|
+
const canvas = within(canvasElement);
|
|
231
|
+
const chips = within(canvas.getByRole("group", { name: "Categories" }));
|
|
232
|
+
await expect(
|
|
233
|
+
chips.getAllByRole("button").map((chip) => chip.textContent),
|
|
234
|
+
).toEqual(["All", ...CANONICAL.map((entry) => entry.label)]);
|
|
235
|
+
await expect(
|
|
236
|
+
briefFilterConfig().categories.map((category) => category.id),
|
|
237
|
+
).toEqual(["all", ...CANONICAL.map((entry) => entry.category)]);
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* The brief's section order, which is not the chip order: Unclassified goes
|
|
243
|
+
* last, under the mail the classifier did reach.
|
|
244
|
+
*/
|
|
245
|
+
export const SectionOrder: Story = {
|
|
246
|
+
parameters: { layout: "fullscreen" },
|
|
247
|
+
render: () => (
|
|
248
|
+
<div className="flex h-screen w-96 flex-col overflow-y-auto border-r border-line">
|
|
249
|
+
{orderedSections.map((section) => (
|
|
250
|
+
<BriefSection
|
|
251
|
+
key={section.id}
|
|
252
|
+
section={section}
|
|
253
|
+
Row={ComfortableRow}
|
|
254
|
+
onSelectThread={() => undefined}
|
|
255
|
+
/>
|
|
256
|
+
))}
|
|
257
|
+
</div>
|
|
258
|
+
),
|
|
259
|
+
play: async ({ canvasElement }) => {
|
|
260
|
+
const canvas = within(canvasElement);
|
|
261
|
+
await expect(
|
|
262
|
+
canvas
|
|
263
|
+
.getAllByRole("button", { expanded: true })
|
|
264
|
+
.map((header) => header.textContent),
|
|
265
|
+
).toEqual(SECTION_ORDER.map((entry) => entry.label));
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Every section the chips emptied. The category holds mail — the totals here
|
|
271
|
+
* are a real mailbox's — so the section stays rather than vanishing, and says
|
|
272
|
+
* in its own words that the filter, not the mailbox, is why it is bare. The
|
|
273
|
+
* number goes: a count above no rows reads as a broken list.
|
|
274
|
+
*/
|
|
275
|
+
export const EmptiedByAChip: Story = {
|
|
276
|
+
parameters: { layout: "fullscreen" },
|
|
277
|
+
render: () => (
|
|
278
|
+
<div className="flex h-screen w-96 flex-col overflow-y-auto border-r border-line">
|
|
279
|
+
{emptiedSections.map((section) => (
|
|
280
|
+
<BriefSection
|
|
281
|
+
key={section.id}
|
|
282
|
+
section={section}
|
|
283
|
+
Row={ComfortableRow}
|
|
284
|
+
onSelectThread={() => undefined}
|
|
285
|
+
/>
|
|
286
|
+
))}
|
|
287
|
+
</div>
|
|
288
|
+
),
|
|
289
|
+
play: async ({ canvasElement }) => {
|
|
290
|
+
const canvas = within(canvasElement);
|
|
291
|
+
for (const entry of SECTION_ORDER) {
|
|
292
|
+
await expect(
|
|
293
|
+
canvas.getByText(emptySectionCopy(entry.label)),
|
|
294
|
+
).toBeVisible();
|
|
295
|
+
await expect(canvas.queryByText(entry.total.toLocaleString())).toBeNull();
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { type ReactNode,
|
|
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 { useInitialFocus } from "../lib/use-initial-focus.js";
|
|
4
5
|
import { DialogBackdrop } from "./dialog-backdrop.js";
|
|
5
6
|
|
|
6
7
|
export interface DialogProps {
|
|
@@ -29,16 +30,7 @@ export function Dialog({
|
|
|
29
30
|
const dialogRef = useRef<HTMLDivElement>(null);
|
|
30
31
|
|
|
31
32
|
useOverlayScope({ id: "dialog", open, answers: { back: onClose } });
|
|
32
|
-
|
|
33
|
-
useEffect(() => {
|
|
34
|
-
if (!open) return;
|
|
35
|
-
const dialog = dialogRef.current;
|
|
36
|
-
if (!dialog) return;
|
|
37
|
-
const focusable = dialog.querySelectorAll<HTMLElement>(
|
|
38
|
-
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])',
|
|
39
|
-
);
|
|
40
|
-
focusable[0]?.focus();
|
|
41
|
-
}, [open]);
|
|
33
|
+
useInitialFocus(dialogRef, open);
|
|
42
34
|
|
|
43
35
|
if (!open) return null;
|
|
44
36
|
|
|
@@ -331,7 +331,12 @@ export function RescueFromSpamFlow({
|
|
|
331
331
|
};
|
|
332
332
|
|
|
333
333
|
return (
|
|
334
|
-
<BottomSheet
|
|
334
|
+
<BottomSheet
|
|
335
|
+
open={open}
|
|
336
|
+
onClose={onCancel}
|
|
337
|
+
label="Rescue from spam"
|
|
338
|
+
dismissLabel="Close rescue"
|
|
339
|
+
>
|
|
335
340
|
{step === "review" && (
|
|
336
341
|
<ReviewStep
|
|
337
342
|
candidates={candidates}
|
package/src/index.ts
CHANGED
|
@@ -1128,6 +1128,7 @@ export {
|
|
|
1128
1128
|
type SuggestKeyState,
|
|
1129
1129
|
suggestKeyAction,
|
|
1130
1130
|
} from "./lib/suggest-keys.js";
|
|
1131
|
+
export { useInitialFocus } from "./lib/use-initial-focus.js";
|
|
1131
1132
|
export { type ListCursor, useListCursor } from "./lib/use-list-cursor.js";
|
|
1132
1133
|
export {
|
|
1133
1134
|
type ListKeyboard,
|
|
@@ -0,0 +1,78 @@
|
|
|
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 { useInitialFocus } from "./use-initial-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
|
+
useInitialFocus(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
|
+
describe("useInitialFocus", () => {
|
|
33
|
+
it("focuses the first focusable descendant once open", () => {
|
|
34
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
35
|
+
assert.equal(document.activeElement?.textContent, "First action");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("leaves focus on the body while closed", () => {
|
|
39
|
+
act(() => root.render(createElement(Panel, { open: false })));
|
|
40
|
+
assert.equal(document.activeElement, document.body);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("moves focus in when an already-mounted panel opens", () => {
|
|
44
|
+
act(() => root.render(createElement(Panel, { open: false })));
|
|
45
|
+
assert.equal(document.activeElement, document.body);
|
|
46
|
+
|
|
47
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
48
|
+
assert.equal(document.activeElement?.textContent, "First action");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("returns focus to the trigger once the panel closes", () => {
|
|
52
|
+
const trigger = document.createElement("button");
|
|
53
|
+
trigger.textContent = "Open panel";
|
|
54
|
+
document.body.appendChild(trigger);
|
|
55
|
+
trigger.focus();
|
|
56
|
+
assert.equal(document.activeElement, trigger);
|
|
57
|
+
|
|
58
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
59
|
+
assert.equal(document.activeElement?.textContent, "First action");
|
|
60
|
+
|
|
61
|
+
act(() => root.render(createElement(Panel, { open: false })));
|
|
62
|
+
assert.equal(document.activeElement, trigger);
|
|
63
|
+
|
|
64
|
+
trigger.remove();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("does not throw restoring focus to a trigger that left the DOM", () => {
|
|
68
|
+
const trigger = document.createElement("button");
|
|
69
|
+
document.body.appendChild(trigger);
|
|
70
|
+
trigger.focus();
|
|
71
|
+
|
|
72
|
+
act(() => root.render(createElement(Panel, { open: true })));
|
|
73
|
+
trigger.remove();
|
|
74
|
+
|
|
75
|
+
act(() => root.render(createElement(Panel, { open: false })));
|
|
76
|
+
assert.notEqual(document.activeElement, trigger);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type RefObject, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
const FOCUSABLE_SELECTOR =
|
|
4
|
+
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Moves focus to the first focusable descendant of `ref` when `open` turns
|
|
8
|
+
* true, so a dialog or sheet never leaves a keyboard user parked on whatever
|
|
9
|
+
* was focused behind it, and gives it back to whatever held it before once
|
|
10
|
+
* `open` turns false again — guarded, since the trigger can have left the DOM
|
|
11
|
+
* while the dialog was up.
|
|
12
|
+
*/
|
|
13
|
+
export function useInitialFocus(
|
|
14
|
+
ref: RefObject<HTMLElement | null>,
|
|
15
|
+
open: boolean,
|
|
16
|
+
): void {
|
|
17
|
+
const restoreTo = useRef<HTMLElement | null>(null);
|
|
18
|
+
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!open) {
|
|
21
|
+
const previous = restoreTo.current;
|
|
22
|
+
restoreTo.current = null;
|
|
23
|
+
if (previous && document.contains(previous)) previous.focus();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
restoreTo.current =
|
|
27
|
+
document.activeElement instanceof HTMLElement
|
|
28
|
+
? document.activeElement
|
|
29
|
+
: null;
|
|
30
|
+
const container = ref.current;
|
|
31
|
+
if (!container) return;
|
|
32
|
+
const focusable =
|
|
33
|
+
container.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR);
|
|
34
|
+
focusable[0]?.focus();
|
|
35
|
+
}, [open, ref]);
|
|
36
|
+
}
|