Intercepting Routes Modal
Show content in a modal on soft navigation while preserving the full page on hard navigation.
// app/@modal/(.)photo/[id]/page.tsx
// The (.) prefix intercepts /photo/[id] 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 ${params.id}`}
className="max-h-[80vh] rounded-xl"
/>
</Modal>
);
}
// app/@modal/default.tsx
export default function Default() {
return null; // No modal by default
}
// app/layout.tsx
export default function Layout({
children,
modal,
}: {
children: React.ReactNode;
modal: React.ReactNode;
}) {
return (
<>
{children}
{modal}
</>
);
}Use Cases
- Photo gallery modals
- Login interceptors
- Quick preview overlays
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Parallel Routes Layout
Next.js parallel routes pattern to render multiple page slots simultaneously in a shared layout.
Type-Safe API Route Handler
Next.js App Router route handler with input validation, typed responses, and proper error handling.
Authentication Middleware Guard
Next.js middleware that checks auth tokens on protected routes and redirects unauthenticated users to login.
Server Action with Form Validation
Next.js Server Action handling form submissions with validation, error messages, and redirect on success.