typescriptadvanced
Next.js Intercepting Routes for Modals
Implement modal patterns with intercepting routes that work with both soft and hard navigation.
typescriptPress ⌘/Ctrl + Shift + C to copy
// app/@modal/(.)photo/[id]/page.tsx
// The (.) prefix intercepts the route at the same level
import { Modal } from '@/components/Modal';
export default function PhotoModal({
params,
}: {
params: { id: string };
}) {
return (
<Modal>
<img
src={`/photos/${params.id}.jpg`}
alt="Photo"
className="max-h-[80vh] rounded-lg"
/>
</Modal>
);
}
// app/@modal/default.tsx
export default function ModalDefault() {
return null;
}
// app/layout.tsx
export default function RootLayout({
children,
modal,
}: {
children: React.ReactNode;
modal: React.ReactNode;
}) {
return (
<html>
<body>
{children}
{modal}
</body>
</html>
);
}
// app/photo/[id]/page.tsx (full page fallback for hard navigation)
export default function PhotoPage({
params,
}: {
params: { id: string };
}) {
return (
<div className="flex justify-center p-8">
<img src={`/photos/${params.id}.jpg`} alt="Photo" />
</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 z-50 flex items-center justify-center bg-black/60"
onClick={() => router.back()}>
<div onClick={(e) => e.stopPropagation()}>{children}</div>
</div>
);
}Use Cases
- Image gallery modals with shareable URLs
- Product quick-view in e-commerce listings
- Preview overlays that work as full pages on refresh
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Parallel Routes for Modal Pattern
Implement modal dialogs using parallel routes and route interception for shareable modal URLs.
Best for: photo galleries
#nextjs#parallel-routes
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