Build a multi-layout system in Next.js using parallel routes and intercepting routes to create a modal that opens on top of the current page when clicked but acts as a standalone page if the URL is opened directly.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This is a great question about advanced Next.js features - parallel routes and intercepting routes. I need to build a comprehensive example showing how to create a modal system that:
Opens as a modal on top of the current page when clicked
Acts as a standalone page if the URL is opened directly
Uses parallel routes to handle the layout
Uses intercepting routes to intercept navigation
Let me structure this properly for Next.js 13+ with the App Router.
Key concepts:
Parallel routes use the @ symbol to create named slots
Intercepting routes use the (..) convention to intercept navigation at different levels
Modals can be opened within a layout slot and dismissed properly
This allows graceful fallback when URL is opened directly
// app/components/PhotoGallery.tsx
'use client';
import Link from 'next/link';
import PhotoCard from './PhotoCard';
import { photos } from '@/app/lib/photos';
export default function PhotoGallery() {
return (
<div className="space-y-4 mb-8">
<h1 className="text-3xl font-bold text-gray-900">Photo Gallery</h1>
<p className="text-gray-600">
Click any photo to view in a modal. Open directly via URL to see full page view.
</p>
</div>
);
}
// app/lib/photos.ts
export interface Photo {
id: string;
title: string;
description: string;
image: string;
category: string;
photographer: string;
}
export const photos: Photo[] = [
{
id: '1',
title: 'Mountain Vista',
description:
'A breathtaking view of snow-capped mountains stretching across the horizon. Captured during golden hour to showcase the alpine beauty.',
image: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=600&h=600&fit=crop',
category: 'Landscape',
photographer: 'John Smith',
},
{
id: '2',
title: 'Ocean Waves',
description:
'Powerful waves crashing against the rocky coastline. This dynamic seascape captures the raw energy of nature at its finest.',
image: 'https://images.unsplash.com/photo-1505142468610-359e7d316be0?w=600&h=600&fit=crop',
category: 'Seascape',
photographer: 'Sarah Johnson',
},
{
id: '3',
title: 'Urban Architecture',
description:
'Modern skyscrapers create a striking geometric pattern against the evening sky. This architectural study highlights contemporary design.',
image: 'https://images.unsplash.com/photo-1480714378408-67cf0d13bc1b?w=600&h=600&fit=crop',
category: 'Architecture',
photographer: 'Mike Chen',
},
{
id: '4',
title: 'Forest Path',
description:
'A winding path through an ancient forest, dappled with sunlight filtering through the canopy. Nature at its most serene.',
image: 'https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=600&h=600&fit=crop',
category: 'Nature',
photographer: 'Emma Wilson',
},
{
id: '5',
title: 'Sunset Reflection',
description:
'The golden sun reflects perfectly on still water, creating a mirror image of the brilliant sky. A moment of absolute tranquility.',
image: 'https://images.unsplash.com/photo-1495616811223-4d98c6e9c869?w=600&h=600&fit=crop',
category: 'Landscape',
photographer: 'David Brown',
},
{
id: '6',
title: 'City Lights',
description:
'Urban landscape illuminated by countless lights creating a vibrant tapestry. This nocturnal view showcases the energy of city life.',
image: 'https://images.unsplash.com/photo-1480706111222-742910eb3773?w=600&h=600&fit=crop',
category: 'Urban',
photographer: 'Lisa Anderson',
},
];
export function getPhoto(id: string): Photo | undefined {
return photos.find((photo) => photo.id === id);
}