Leaf and Kettle's tea list opens a tea in a modal: clicking Green tea on /teas changes the URL to /teas/green and shows the tea over the list, through an intercepting route, app/@modal/(.)teas/[slug]/page.tsx. Reloading /teas/green shows the tea's full page instead. Build the modal that route wraps the tea in: app/ui/modal.tsx, whose default export is Modal({ title, children }), a Client Component.
Render a backdrop <div> with the dialog directly inside it: a <div role="dialog" aria-modal="true"> holding an <h2> with title, which names the dialog through aria-labelledby, then children, then a Close button.
Closing the modal means going back to the list with router.back(), so the list's URL comes back as it was. Close it when the visitor clicks Close, presses Escape anywhere on the page, or clicks the backdrop outside the dialog. A click inside the dialog doesn't close it.
When the modal opens, move focus to the Close button, and stop listening for Escape once the modal is gone.
/teas → click Green tea → /teas/green, the modal shows
Escape → router.back() → /teas
const openGreenTea = () => {
const view = renderClient(
<Modal title="Green tea">
<p>Light and fresh.</p>
</Modal>,
{ url: "/teas" },
);
act(() => router.push("/teas/green"));
return view;
};
openGreenTea();
const dialog = screen.getByRole("dialog", { name: "Green tea" });
expect(dialog).toHaveAttribute("aria-modal", "true");
expect(within(dialog).getByText("Light and fresh.")).toBeInTheDocument();
expect(within(dialog).getByRole("button", { name: "Close" })).toBeInTheDocument();
const openGreenTea = () => {
const view = renderClient(
<Modal title="Green tea">
<p>Light and fresh.</p>
</Modal>,
{ url: "/teas" },
);
act(() => router.push("/teas/green"));
return view;
};
openGreenTea();
await user.click(screen.getByRole("button", { name: "Close" }));
expect(router.url).toBe("/teas");
expect(router.calls.map((call) => call.method)).toEqual(["push", "back"]);
const openGreenTea = () => {
const view = renderClient(
<Modal title="Green tea">
<p>Light and fresh.</p>
</Modal>,
{ url: "/teas" },
);
act(() => router.push("/teas/green"));
return view;
};
openGreenTea();
await user.keyboard("a");
expect(router.url).toBe("/teas/green");
await user.keyboard("{Escape}");
expect(router.url).toBe("/teas");
expect(router.calls.map((call) => call.method)).toEqual(["push", "back"]);
const openGreenTea = () => {
const view = renderClient(
<Modal title="Green tea">
<p>Light and fresh.</p>
</Modal>,
{ url: "/teas" },
);
act(() => router.push("/teas/green"));
return view;
};
openGreenTea();
const dialog = screen.getByRole("dialog");
await user.click(screen.getByText("Light and fresh."));
await user.click(dialog);
expect(router.url).toBe("/teas/green");
await user.click(dialog.parentElement);
expect(router.url).toBe("/teas");
const openGreenTea = () => {
const view = renderClient(
<Modal title="Green tea">
<p>Light and fresh.</p>
</Modal>,
{ url: "/teas" },
);
act(() => router.push("/teas/green"));
return view;
};
openGreenTea();
expect(screen.getByRole("button", { name: "Close" })).toHaveFocus();
const openGreenTea = () => {
const view = renderClient(
<Modal title="Green tea">
<p>Light and fresh.</p>
</Modal>,
{ url: "/teas" },
);
act(() => router.push("/teas/green"));
return view;
};
const { unmount } = openGreenTea();
unmount();
await user.keyboard("{Escape}");
expect(router.url).toBe("/teas/green");
expect(router.calls.map((call) => call.method)).toEqual(["push"]);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with A modal that closes by going back. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.