Add previous and next links to an intercepted tea modal, so one Back still closes it
The tea list at /teas opens each tea in a modal through an intercepting route: a click on Mint tea pushes /teas/mint, and app/@modal/(.)teas/[slug]/page.tsx renders the modal over the list. Visitors want to step from tea to tea without closing it. Write app/ui/tea-modal.tsx, a Client Component whose default export is TeaModal({ slug }); the intercepting page renders <TeaModal slug={slug} /> for each tea.
Render a <div role="dialog"> named by the tea's name, with the name in an <h2>, the tea's about in a paragraph, a next/link Link to the tea before it in teas from @/lib/teas, Previous: Green tea, and one to the tea after it, Next: Oolong tea, each to /teas/<slug>, and a Close button. Leave out Previous on the first tea and Next on the last, and render nothing for a slug the shop doesn't sell.
Stepping mustn't pile up history: a visitor who opened Mint tea from the list and stepped through three teas should get back to the list with one press of Back, as if they'd only opened one. So the links replace the current history entry instead of pushing a new one, and they leave the page's scroll position alone. Close goes back to the list with router.back().
// /teas → click Mint tea → /teas/mint (push), and the modal opens
// Next: Oolong tea → /teas/oolong (replace)
// Back, or Close → /teas, and the modal closes
app/ui/tea-modal.tsx"use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { teas } from "@/lib/teas";
export default function TeaModal({ slug }: { slug: string }) {
const router = useRouter();
const index = teas.findIndex((tea) => tea.slug === slug);
const tea = teas[index];
const previous = teas[index - 1];
const next = teas[index + 1];
return (
<div role="dialog" aria-label={tea.name}>
<h2>{tea.name}</h2>
<p>{tea.about}</p>
{previous && <Link href={`/teas/${previous.slug}`}>Previous: {previous.name}</Link>}
{next && <Link href={`/teas/${next.slug}`}>Next: {next.name}</Link>}
<button type="button" onClick={() => router.push("/teas")}>
Close
</button>
</div>
);
}
lib/teas.ts (read-only)export type Tea = { slug: string; name: string; about: string };
/** The shop's teas, in the order the modal steps through them. */
export const teas: Tea[] = [
{ slug: "green", name: "Green tea", about: "Light and fresh." },
{ slug: "mint", name: "Mint tea", about: "Cool and sweet." },
{ slug: "oolong", name: "Oolong tea", about: "Rich and smooth." },
];
- shows the tea in a dialog, with links to the teas on either side
- has no Previous on the first tea, no Next on the last, and nothing for a tea the shop doesn't sell
- steps to another tea in place of the open one, without scrolling
- Close goes back to the list
- Close returns to the list in one step after stepping through the teas
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…