Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion apps/web/src/components/files/PdfViewer.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { expect, it, vi } from "vite-plus/test";
import { render } from "../../../test/render";
import { userEvent } from "vite-plus/test/browser";

import { RightPanelSheet } from "~/components/RightPanelSheet";
import { act, render } from "../../../test/render";
import "~/index.css";

// Vite serves the worker module through its transform with an inline source map (6.8 MB for
Expand Down Expand Up @@ -333,3 +336,48 @@ it("follows a link to another page and exposes a web link", async () => {
URL.revokeObjectURL(url);
}
});

it("Escape in Find in PDF clears it without closing the panel sheet", async () => {
const url = URL.createObjectURL(testPdf());
const onCloseSheet = vi.fn();
try {
await using screen = await render(
<RightPanelSheet animationDurationMs={0} open onClose={onCloseSheet}>
<PdfViewer src={url} title="Planning PDF" />
</RightPanelSheet>,
);
const find = screen.getByRole("searchbox", { name: "Find in PDF" });
await find.fill("quarterly");
for (const composition of [
{ key: "Escape", isComposing: true, keyCode: 27 },
{ key: "Escape", isComposing: false, keyCode: 229 },
]) {
// Safari ends the composition just before the Escape keydown.
await act(() => {
find.element().dispatchEvent(new CompositionEvent("compositionend", { bubbles: true }));
find
.element()
.dispatchEvent(new KeyboardEvent("keydown", { ...composition, bubbles: true }));
});
await expect.element(find).toHaveValue("quarterly");
}
expect(find.element().getAttribute("type")).toBe("text");

await userEvent.keyboard("{Escape}");
await expect.element(find).toHaveValue("");
// Holding Escape repeats the keydown, which must not also close the sheet.
await act(() => {
find
.element()
.dispatchEvent(
new KeyboardEvent("keydown", { key: "Escape", repeat: true, bubbles: true }),
);
});
expect(onCloseSheet).not.toHaveBeenCalled();

await userEvent.keyboard("{Escape}");
await vi.waitFor(() => expect(onCloseSheet).toHaveBeenCalledOnce());
} finally {
URL.revokeObjectURL(url);
}
});
11 changes: 10 additions & 1 deletion apps/web/src/components/files/PdfViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,12 @@ export default function PdfViewer(props: {
<ScanIcon className="size-3.5" />
</FileSurfaceAction>
<div className="w-28">
{/* Native search inputs clear on Escape even during IME composition. */}
<Input
ref={find}
size="compact"
type="search"
type="text"
role="searchbox"
aria-label={gt("Find in PDF")}
placeholder={gt("Find in PDF")}
value={query}
Expand All @@ -534,6 +536,13 @@ export default function PdfViewer(props: {
setMatches([]);
setFindError(null);
}}
onKeyDown={(event) => {
if (event.key !== "Escape" || event.nativeEvent.isComposing || event.keyCode === 229)
return;
if (!query && !event.repeat) return;
event.stopPropagation();
setQuery("");
}}
/>
</div>
{query ? (
Expand Down
Loading