<ImageZoom>

<ImageZoom> renders an image inside an accessible zoom button. Images are zoomable by default. The component manages attachment, state, and cleanup for one image.

tsx
import { ImageZoom } from '@/components/ui/image-zoom'

export function Photo() {
  return (
    <ImageZoom
      src="/photo.jpg"
      alt="A mountain at sunrise"
      width={1_200}
      height={800}
    />
  )
}

The React quickstart covers installation and a working example.

For wrappers, compose the exported ImageZoomProps type instead of redeclaring image or zoom props. The generated SVG guide shows an example.

Image props

These props apply to the image. The forwarded ref points to the HTMLImageElement.

alt

Required

Describes the image and supplies the zoom button's accessible name.

Type
string

src

The image URL displayed in the page.

Type
string

srcSet

Image candidates for the browser to select based on resolution or display size.

Type
string

sizes

The image's display size at each breakpoint, used with srcSet.

Type
string

width

The image's intrinsic width. Set it with height to reserve space while loading.

Type
number | string

height

The image's intrinsic height.

Type
number | string

loading

Controls when the browser loads the image. Use lazy for images below the initial viewport.

Type
'eager' | 'lazy'

className

Styles the image. Use composition to style the button separately.

Type
string

style

Inline styles for the image.

Type
CSSProperties

data-zoom-src

An HD image URL loaded when the zoom opens.

Type
string

Zoom options

options

Configures margin, background, scrollOffset, and container:

  • margin defaults to 24 pixels.
  • scrollOffset defaults to 40 pixels.
  • background follows the image's inherited --background CSS property, falling back to white.
  • container sets a custom viewport, using the values described in the JavaScript option.
Type
ZoomOptions

disabled

Set to true to prevent opening and disable the trigger.

Type
boolean
Default
false

State props

open

Controls the open state from the parent. When omitted, the component manages its own state.

Type
boolean

onOpenChange

Receives the requested next state. With controlled state, apply each request to open. The controlled state guide shows onOpenChange={setOpen}.

Type
(open: boolean) => void

Trigger customization

The composition API exposes the button and image as separate components. Custom triggers gives a complete example.