Skip to content

Commit 58fdc24

Browse files
StiensWoutclaudejuliusmarminge
authored
fix(web): SQLite paging keeps keyboard focus while the next page loads (#48142)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 39d3eaa commit 58fdc24

4 files changed

Lines changed: 100 additions & 3 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
import { userEvent } from "vite-plus/test/browser";
3+
import { render } from "../../../test/render";
4+
import type { BinaryWorkerRequest } from "./binaryPreviewProtocol";
5+
import BinaryViewer from "./BinaryViewer";
6+
7+
async function sqliteUrl(rowCount: number) {
8+
const [{ default: initSqlJs }, { default: wasmUrl }] = await Promise.all([
9+
import("sql.js"),
10+
import("sql.js/dist/sql-wasm.wasm?url"),
11+
]);
12+
const SQL = await initSqlJs({ locateFile: () => wasmUrl });
13+
const database = new SQL.Database();
14+
database.run(`
15+
CREATE TABLE events (id INTEGER PRIMARY KEY, label TEXT);
16+
WITH RECURSIVE numbers(n) AS (VALUES (1) UNION ALL SELECT n + 1 FROM numbers WHERE n < ${rowCount})
17+
INSERT INTO events SELECT n, 'Event ' || n FROM numbers;
18+
`);
19+
const url = URL.createObjectURL(new Blob([new Uint8Array(database.export())]));
20+
database.close();
21+
return { url, [Symbol.dispose]: () => URL.revokeObjectURL(url) };
22+
}
23+
24+
/** Holds each armed row request until released, so the viewer stays loading. */
25+
function holdRowRequests() {
26+
const postMessage = Worker.prototype.postMessage;
27+
let armed: { held: PromiseWithResolvers<void>; release: PromiseWithResolvers<void> } | null =
28+
null;
29+
const spy = vi.spyOn(Worker.prototype, "postMessage").mockImplementation(function (
30+
this: Worker,
31+
message: BinaryWorkerRequest,
32+
options,
33+
) {
34+
const hold = armed;
35+
if (hold && message.type === "rows") {
36+
armed = null;
37+
hold.held.resolve();
38+
void hold.release.promise.then(() => postMessage.call(this, message, options));
39+
} else postMessage.call(this, message, options);
40+
});
41+
return {
42+
next: () => {
43+
const hold = { held: Promise.withResolvers<void>(), release: Promise.withResolvers<void>() };
44+
armed = hold;
45+
return { held: hold.held.promise, release: () => hold.release.resolve() };
46+
},
47+
[Symbol.dispose]: () => spy.mockRestore(),
48+
};
49+
}
50+
51+
describe("BinaryViewer SQLite paging", () => {
52+
it("keeps keyboard focus on Next rows while a page loads and at the last page", async () => {
53+
using src = await sqliteUrl(120);
54+
using rowRequests = holdRowRequests();
55+
await using screen = await render(
56+
<div style={{ width: 500, height: 400, display: "flex", flexDirection: "column" }}>
57+
<BinaryViewer src={src.url} name="events.sqlite" kind="sqlite" />
58+
</div>,
59+
);
60+
await expect.element(screen.getByRole("cell", { name: "Event 1", exact: true })).toBeVisible();
61+
const next = screen.getByRole("button", { name: "Next rows", exact: true });
62+
const loading = screen.getByRole("status", { name: "Loading file" });
63+
const press = async (range: string) => {
64+
const hold = rowRequests.next();
65+
await userEvent.keyboard("{Enter}");
66+
await hold.held;
67+
await expect.element(loading).toBeVisible();
68+
const focusedWhileLoading = document.activeElement;
69+
const disabledWhileLoading = next.element().getAttribute("aria-disabled");
70+
// A press while the page loads is ignored instead of skipping a page.
71+
await userEvent.keyboard("{Enter}");
72+
hold.release();
73+
await expect.element(screen.getByText(range, { exact: true })).toBeVisible();
74+
await expect.element(loading).not.toBeInTheDocument();
75+
expect(document.activeElement).toBe(next.element());
76+
expect(focusedWhileLoading).toBe(next.element());
77+
expect(disabledWhileLoading).toBe("true");
78+
};
79+
80+
next.element().focus();
81+
await press("51–100 of 120");
82+
await expect.element(screen.getByRole("cell", { name: "Event 51", exact: true })).toBeVisible();
83+
await expect.element(next).not.toHaveAttribute("aria-disabled");
84+
await press("101–120 of 120");
85+
await expect
86+
.element(screen.getByRole("cell", { name: "Event 120", exact: true }))
87+
.toBeVisible();
88+
await expect.element(next).toHaveAttribute("aria-disabled", "true");
89+
});
90+
});

‎apps/web/src/components/files/BinaryViewer.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export default function BinaryViewer(props: {
330330
<FileSurfaceAction
331331
label={gt`Previous rows`}
332332
disabled={busy || page === 0}
333+
focusableWhenDisabled
333334
onPress={() => selectPage(page - 1)}
334335
>
335336
<ChevronLeftIcon className="size-3.5" />
@@ -342,6 +343,7 @@ export default function BinaryViewer(props: {
342343
<FileSurfaceAction
343344
label={gt`Next rows`}
344345
disabled={busy || (page + 1) * SQLITE_PAGE_SIZE >= table.count}
346+
focusableWhenDisabled
345347
onPress={() => selectPage(page + 1)}
346348
>
347349
<ChevronRightIcon className="size-3.5" />

‎apps/web/src/components/files/FilePreviewPanel.browser.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ describe("table preview select all", () => {
13491349
.getByRole("table", { name: "catalog.sqlite: products" })
13501350
.element().parentElement;
13511351
await act(() =>
1352-
screen.container.querySelector<HTMLButtonElement>('[aria-label="Table"]')!.focus(),
1352+
screen.container.querySelector<HTMLButtonElement>('[aria-label="Next rows"]')!.focus(),
13531353
);
13541354
await userEvent.tab();
13551355
expect(document.activeElement).toBe(scroller);

‎apps/web/src/components/files/fileSurfaceChrome.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,24 @@ export function FileSurfaceAction(props: {
6363
readonly label: string;
6464
readonly pressed?: boolean;
6565
readonly disabled?: boolean;
66+
/** Keeps a disabled button focusable, so pressing it does not drop keyboard focus. */
67+
readonly focusableWhenDisabled?: boolean;
6668
readonly onPress: () => void;
6769
readonly children: ReactNode;
6870
}) {
6971
const pressed = props.pressed;
72+
const disabled = props.disabled ?? false;
73+
const focusable = disabled && props.focusableWhenDisabled === true;
7074
return (
7175
<SharedTooltipTrigger
7276
render={
7377
pressed === undefined ? (
7478
<Button
7579
type="button"
7680
className="shrink-0"
77-
disabled={props.disabled ?? false}
78-
onClick={props.onPress}
81+
disabled={disabled && !focusable}
82+
aria-disabled={focusable || undefined}
83+
onClick={focusable ? undefined : props.onPress}
7984
aria-label={props.label}
8085
variant="ghost"
8186
size="icon-sm"

0 commit comments

Comments
 (0)