Custom triggers
Compose a zoom button with your own label, styles, and markup using <ImageZoom.Root>, <ImageZoom.Trigger>, and <ImageZoom.Image>.
Press Enter or Space to zoom. Click, press Escape, or scroll to close.
custom-trigger-photo.tsx
import * as ImageZoom from '@/components/ui/image-zoom'
import './custom-trigger-photo.css'
export function CustomTriggerPhoto() {
return (
<ImageZoom.Root options={{ margin: 32 }}>
<figure className="custom-trigger-example">
<ImageZoom.Image
src="/assets/images/ocean-sunset.jpg"
alt="Ocean sunset"
width={1_920}
height={1_280}
/>
<figcaption>
<span>Ocean sunset</span>
<ImageZoom.Trigger
aria-label="View photo: ocean sunset"
className="photo-action"
>
View photo
</ImageZoom.Trigger>
</figcaption>
</figure>
</ImageZoom.Root>
)
}custom-trigger-photo.css
.custom-trigger-example {
max-width: 320px;
margin: 0 auto;
text-align: left;
}
.custom-trigger-example img {
display: block;
width: 100%;
height: auto;
border-radius: 6px;
}
.custom-trigger-example figcaption {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: 12px;
margin-top: 16px;
font-size: 13px;
}
.custom-trigger-example figcaption .photo-action {
display: inline-flex;
max-width: none;
margin: 0;
padding: 8px 12px;
border: 1px solid var(--border, #e4e4e7);
border-radius: 6px;
background: var(--background, #fff);
color: var(--foreground, #18181b);
font: inherit;
white-space: nowrap;
}Compose the button
Put the image and button inside the same <ImageZoom.Root>. They can be siblings, so the button can sit beside or below the image:
tsx
import * as ImageZoom from '@/components/ui/image-zoom'
export function Photo() {
return (
<ImageZoom.Root>
<ImageZoom.Image src="/photo.jpg" alt="Ocean sunset" />
<ImageZoom.Trigger aria-label="View photo: ocean sunset">
View photo
</ImageZoom.Trigger>
</ImageZoom.Root>
)
}The button's accessible name includes its visible “View photo” label and identifies the image. Keep other buttons and links outside the trigger.
Style the trigger
The example places the button in a caption row below the image. Use className on <ImageZoom.Trigger> to style the button independently. The Code view includes the complete stylesheet.
