Composition

The composition API exports <ImageZoom.Root>, <ImageZoom.Trigger>, and <ImageZoom.Image>. Each root manages one trigger and one image.

tsx
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

Required

The trigger, image, and surrounding layout. Use one trigger and one image for each root.

Type
ReactNode

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.

Type
ZoomOptions

disabled

Disables opening and the trigger.

Type
boolean
Default
false

open

Controls the open state from the parent. When omitted, state is internal.

Type
boolean

onOpenChange

Receives the requested next open state. The controlled state guide describes how the parent applies each request.

Type
(open: boolean) => void

<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.

Type
string

style

Inline styles for the button.

Type
CSSProperties

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.

Type
string

disabled

Disables this button. A disabled root also disables the trigger.

Type
boolean

onClick

Runs before the zoom toggle. Call event.preventDefault() in the handler to prevent the toggle.

Type
MouseEventHandler<HTMLButtonElement>

<ImageZoom.Image>

<ImageZoom.Image> renders the native image managed by the root. The forwarded ref points to the HTMLImageElement.

alt

Required

Describes the image and labels the surrounding trigger. The button provides keyboard interaction.

Type
string

className

Styles the image separately from the trigger.

Type
string

data-zoom-src

The HD image URL loaded when the zoom opens.

Type
string

It also accepts native image props, including src, srcSet, sizes, width, height, loading, and style. The <ImageZoom> reference describes these props.