Skip to the code

A modal that closes by going back

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

What the tests check

  1. shows the tea in a dialog named by its title
    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();
    
  2. Close goes back to the list
    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"]);
    
  3. Escape goes back, and other keys don't
    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"]);
    
  4. a click on the backdrop goes back, and a click inside the dialog doesn't
    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");
    
  5. moves focus to Close when it opens
    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();
    
  6. stops listening for Escape once it's gone
    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"]);
    

Learn more

app/ui/modal.tsxSaved in this browser

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).