@remit/ui 0.0.178 → 0.0.179
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/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>
|
|
@@ -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
|
+
}
|