Guides
Multiple images
Make several images zoomable without changing the page’s layout. Each image opens independently.
Press Enter or Space to zoom. Click, press Escape, or scroll to close.
multiple-images.tsx
import { ImageZoom } from '@/components/ui/image-zoom'
import './multiple-images.css'
const photos = [
{
src: '/assets/images/ocean-sunset.jpg',
alt: 'Ocean sunset',
width: 1_920,
height: 1_280,
},
{
src: '/assets/images/denali.jpg',
alt: 'Denali at sunrise',
width: 2_400,
height: 1_594,
},
{
src: '/assets/images/yellowstone.jpg',
alt: 'Yellowstone at sunrise',
width: 2_400,
height: 1_600,
},
{
src: '/assets/images/aurora.jpg',
alt: 'Aurora over Yellowstone',
width: 2_400,
height: 1_600,
},
]
export function MultipleImages() {
return (
<div className="photo-gallery">
{photos.map(photo => (
<ImageZoom key={photo.src} {...photo} />
))}
</div>
)
}multiple-images.css
.photo-gallery {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
align-items: start;
gap: 12px;
}
.photo-gallery img {
width: 100%;
height: auto;
}JavaScript
Attach several images with one instance:
html
<div class="photo-gallery">
<img src="/ocean.jpg" alt="Ocean sunset" data-zoomable />
<img src="/mountain.jpg" alt="Denali at sunrise" data-zoomable />
<img
src="/yellowstone.jpg"
alt="Yellowstone at sunrise"
data-zoomable
/>
<img src="/aurora.jpg" alt="Aurora over Yellowstone" data-zoomable />
</div>js
import mediumZoom from 'medium-zoom'
const zoom = mediumZoom('.photo-gallery [data-zoomable]')After adding images to the page, attach them to the instance:
js
zoom.attach('.photo-gallery [data-zoomable]')An image is attached once, even when it matches the selector again. See attach() and detach() for image lifecycle handling.
React
Render one <ImageZoom> per image. With composition, use a separate <ImageZoom.Root> for each image.
tsx
import { ImageZoom } from '@/components/ui/image-zoom'
const photos = [
{
src: '/ocean.jpg',
alt: 'Ocean sunset',
width: 1_920,
height: 1_280,
},
{
src: '/mountain.jpg',
alt: 'Denali at sunrise',
width: 2_400,
height: 1_594,
},
]
export function MultipleImages() {
return (
<div className="photo-gallery">
{photos.map(photo => (
<ImageZoom key={photo.src} {...photo} />
))}
</div>
)
}Set the grid layout in CSS. Close the current zoom before opening another image.