Skip to content

Commit 39d3eaa

Browse files
StiensWoutclaudejuliusmarminge
authored
fix(web): EPUB chapter and zoom buttons keep keyboard focus in the reader at their limits (#48150)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 85b3800 commit 39d3eaa

2 files changed

Lines changed: 114 additions & 29 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useState } from "react";
2+
import { expect, it } from "vite-plus/test";
3+
import { userEvent } from "vite-plus/test/browser";
4+
5+
import { render } from "../../../test/render";
6+
import EpubViewer from "./EpubViewer";
7+
import type { OfficeWorkerResult } from "./officePreviewProtocol";
8+
import "~/index.css";
9+
10+
const book = {
11+
type: "epub",
12+
first: 0,
13+
rtl: false,
14+
chapters: [
15+
{ title: "First", linear: true },
16+
{ title: "Last", linear: true },
17+
],
18+
contents: [],
19+
} as const;
20+
21+
function Reader(props: { readonly first: number }) {
22+
const [response, setResponse] = useState<Extract<
23+
OfficeWorkerResult,
24+
{ type: "epub-page" }
25+
> | null>(null);
26+
const loadPage = (index: number) =>
27+
setResponse({ type: "epub-page", index, result: { html: `<p>Chapter ${index + 1}</p>` } });
28+
return (
29+
<div className="flex h-[300px] w-[320px]">
30+
<EpubViewer
31+
name="book.epub"
32+
book={{ ...book, first: props.first }}
33+
response={response}
34+
sizeBytes={undefined}
35+
loadPage={loadPage}
36+
/>
37+
</div>
38+
);
39+
}
40+
41+
it.each([
42+
{ button: "Next chapter", from: 0, presses: 1 },
43+
{ button: "Previous chapter", from: 1, presses: 1 },
44+
{ button: "Decrease reading size", from: 0, presses: 2 },
45+
])(
46+
"moves keyboard focus to the reader when $button reaches its limit",
47+
async ({ button, from, presses }) => {
48+
await using screen = await render(<Reader first={from} />);
49+
const action = screen.getByRole("button", { name: button, exact: true });
50+
(action.element() as HTMLElement).focus();
51+
for (let press = 1; press < presses; press++) {
52+
await userEvent.keyboard("{Enter}");
53+
await expect.element(action).toHaveFocus();
54+
}
55+
await userEvent.keyboard("{Enter}");
56+
await expect.element(action).toBeDisabled();
57+
await expect.element(screen.getByRole("region", { name: "book.epub" })).toHaveFocus();
58+
},
59+
);

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

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@ export default function EpubViewer(props: {
202202
);
203203
const previous = props.book.chapters.findLastIndex((chapter, i) => i < index && chapter.linear);
204204
const next = props.book.chapters.findIndex((chapter, i) => i > index && chapter.linear);
205+
const reader = useRef<HTMLDivElement>(null);
206+
// A button that disables itself when pressed would drop keyboard focus out of the reader.
207+
const keepFocusIf = (disables: boolean) => {
208+
if (disables) reader.current?.focus({ preventScroll: true });
209+
};
210+
const stepChapter = (target: number) => {
211+
const forward = target > index;
212+
keepFocusIf(
213+
!props.book.chapters.some(
214+
(chapter, i) => chapter.linear && (forward ? i > target : i < target),
215+
),
216+
);
217+
setLocation({ index: target });
218+
};
219+
const changeZoom = (value: number, limit: number) => {
220+
keepFocusIf(value === limit);
221+
setZoom(value);
222+
};
205223
return (
206224
<div className="flex min-h-0 flex-1 flex-col">
207225
{props.book.chapters.length > 1 || props.book.contents.length > 1 ? (
@@ -212,7 +230,7 @@ export default function EpubViewer(props: {
212230
<FileSurfaceAction
213231
label={gt`Previous chapter`}
214232
disabled={previous < 0}
215-
onPress={() => setLocation({ index: previous })}
233+
onPress={() => stepChapter(previous)}
216234
>
217235
{props.book.rtl ? (
218236
<ChevronRightIcon className="size-3.5" />
@@ -245,7 +263,7 @@ export default function EpubViewer(props: {
245263
<FileSurfaceAction
246264
label={gt`Next chapter`}
247265
disabled={next < 0}
248-
onPress={() => setLocation({ index: next })}
266+
onPress={() => stepChapter(next)}
249267
>
250268
{props.book.rtl ? (
251269
<ChevronLeftIcon className="size-3.5" />
@@ -263,50 +281,58 @@ export default function EpubViewer(props: {
263281
<FileSurfaceAction
264282
label={gt`Decrease reading size`}
265283
disabled={zoom <= 0.5}
266-
onPress={() => setZoom(zoom - 0.25)}
284+
onPress={() => changeZoom(zoom - 0.25, 0.5)}
267285
>
268286
<MinusIcon className="size-3.5" />
269287
</FileSurfaceAction>
270288
<span className="min-w-10 text-center tabular-nums">{Math.round(zoom * 100)}%</span>
271289
<FileSurfaceAction
272290
label={gt`Increase reading size`}
273291
disabled={zoom >= 2}
274-
onPress={() => setZoom(zoom + 0.25)}
292+
onPress={() => changeZoom(zoom + 0.25, 2)}
275293
>
276294
<PlusIcon className="size-3.5" />
277295
</FileSurfaceAction>
278296
<FileSurfaceAction
279297
label={gt`Reset reading size`}
280298
disabled={zoom === 1}
281-
onPress={() => setZoom(1)}
299+
onPress={() => changeZoom(1, 1)}
282300
>
283301
<RotateCcwIcon className="size-3.5" />
284302
</FileSurfaceAction>
285303
</div>
286-
{page ? (
287-
<ReadingFrame
288-
key={index}
289-
page={page}
290-
name={`${props.name}: ${chapterLabel(index)}`}
291-
location={location}
292-
zoom={zoom}
293-
navigate={setLocation}
294-
/>
295-
) : result && "message" in result ? (
296-
<div
297-
role="alert"
298-
className="flex flex-1 flex-col items-center justify-center gap-1 px-6 text-center"
299-
>
300-
<p className="text-sm font-medium">
301-
<T>No preview for this file</T>
302-
</p>
303-
<p className="max-w-sm text-xs text-muted-foreground">
304-
{previewFailureText(result, messages)}
305-
</p>
306-
</div>
307-
) : (
308-
<FileSurfaceLoading />
309-
)}
304+
<div
305+
ref={reader}
306+
role="region"
307+
aria-label={props.name}
308+
tabIndex={-1}
309+
className="flex min-h-0 flex-1 flex-col outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
310+
>
311+
{page ? (
312+
<ReadingFrame
313+
key={index}
314+
page={page}
315+
name={`${props.name}: ${chapterLabel(index)}`}
316+
location={location}
317+
zoom={zoom}
318+
navigate={setLocation}
319+
/>
320+
) : result && "message" in result ? (
321+
<div
322+
role="alert"
323+
className="flex flex-1 flex-col items-center justify-center gap-1 px-6 text-center"
324+
>
325+
<p className="text-sm font-medium">
326+
<T>No preview for this file</T>
327+
</p>
328+
<p className="max-w-sm text-xs text-muted-foreground">
329+
{previewFailureText(result, messages)}
330+
</p>
331+
</div>
332+
) : (
333+
<FileSurfaceLoading />
334+
)}
335+
</div>
310336
</div>
311337
);
312338
}

0 commit comments

Comments
 (0)