typescriptadvanced
Parallel Routes for Modal Pattern
Implement modal dialogs using parallel routes and route interception for shareable modal URLs.
typescriptPress ⌘/Ctrl + Shift + C to copy
// File structure:
// app/
// @modal/
// (.)photo/[id]/page.tsx <- intercepted route (modal)
// default.tsx
// photo/[id]/page.tsx <- full page
// layout.tsx
// page.tsx
// app/layout.tsx
export default function Layout({
children,
modal,
}: {
children: React.ReactNode;
modal: React.ReactNode;
}) {
return (
<html>
<body>
{children}
{modal}
</body>
</html>
);
}
// app/@modal/default.tsx
export default function Default() {
return null; // No modal by default
}
// app/@modal/(.)photo/[id]/page.tsx
import { Modal } from '@/components/Modal';
export default async function PhotoModal({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const photo = await getPhoto(id);
return (
<Modal>
<img src={photo.url} alt={photo.alt} className="max-h-[80vh]" />
<p>{photo.caption}</p>
</Modal>
);
}
// app/photo/[id]/page.tsx (full page fallback)
export default async function PhotoPage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const photo = await getPhoto(id);
return (
<div className="max-w-4xl mx-auto p-8">
<img src={photo.url} alt={photo.alt} className="w-full" />
<h1 className="text-2xl mt-4">{photo.caption}</h1>
</div>
);
}
// components/Modal.tsx
'use client';
import { useRouter } from 'next/navigation';
export function Modal({ children }: { children: React.ReactNode }) {
const router = useRouter();
return (
<div className="fixed inset-0 bg-black/60 flex items-center justify-center z-50"
onClick={() => router.back()}>
<div className="bg-white rounded-xl p-6 max-w-2xl" onClick={(e) => e.stopPropagation()}>
{children}
</div>
</div>
);
}Use Cases
- photo galleries
- product quickview
- shareable modal URLs
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Next.js Intercepting Routes for Modals
Implement modal patterns with intercepting routes that work with both soft and hard navigation.
Best for: Image gallery modals with shareable URLs
#nextjs#routing
typescriptadvanced
Parallel Routes Layout
Next.js parallel routes pattern to render multiple page slots simultaneously in a shared layout.
Best for: Dashboard multi-panel layouts
#parallel-routes#layout
typescriptadvanced
Intercepting Routes Modal
Show content in a modal on soft navigation while preserving the full page on hard navigation.
Best for: Photo gallery modals
#routing#modals
typescriptadvanced
Next.js Parallel Routes Layout
Use parallel routes with named slots to render multiple pages simultaneously in a layout.
Best for: Dashboard layouts with independent loading states
#nextjs#routing