Composition
The composition API exports <ImageZoom.Root>, <ImageZoom.Trigger>, and <ImageZoom.Image>. Each root manages one trigger and one image.
import * as ImageZoom from '@/components/ui/image-zoom'
export function Photo() {
return (
<ImageZoom.Root>
<ImageZoom.Trigger aria-label="View mountain photo">
<ImageZoom.Image src="/photo.jpg" alt="A mountain at sunrise" />
</ImageZoom.Trigger>
</ImageZoom.Root>
)
}Custom triggers covers button styling and visible labels.
<ImageZoom.Root>
<ImageZoom.Root> owns the zoom's options and state. It renders its children through context.
children
RequiredThe trigger, image, and surrounding layout. Use one trigger and one image for each root.
options
Configures margin, background, scrollOffset, and container. The margin defaults to 24 pixels, and the scroll offset defaults to 40 pixels. The background follows the image's inherited --background CSS property, falling back to white.
container accepts the values described in the JavaScript reference.
disabled
Disables opening and the trigger.
open
Controls the open state from the parent. When omitted, state is internal.
onOpenChange
Receives the requested next open state. The controlled state guide describes how the parent applies each request.
<ImageZoom.Trigger>
<ImageZoom.Trigger> renders a native button with type="button". Activating it toggles the zoom. The forwarded ref points to the HTMLButtonElement.
className
Styles the button, including its focus state and surrounding label.
style
Inline styles for the button.
aria-label
Sets the button's accessible name. By default, the name is Zoom image: followed by the image's alt text. Include any visible button label in the accessible name.
disabled
Disables this button. A disabled root also disables the trigger.
onClick
Runs before the zoom toggle. Call event.preventDefault() in the handler to prevent the toggle.
<ImageZoom.Image>
<ImageZoom.Image> renders the native image managed by the root. The forwarded ref points to the HTMLImageElement.
alt
RequiredDescribes the image and labels the surrounding trigger. The button provides keyboard interaction.
className
Styles the image separately from the trigger.
data-zoom-src
The HD image URL loaded when the zoom opens.
It also accepts native image props, including src, srcSet, sizes, width, height, loading, and style. The <ImageZoom> reference describes these props.