Server rendering

The React component renders its button and image on the server, then attaches zoom behavior after hydration.

React Server Components

Render <ImageZoom> from a Server Component as you would render an image:

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

export default function Page() {
  return (
    <ImageZoom
      src="/photo.jpg"
      alt="A mountain at sunrise"
      width={1_200}
      height={800}
      options={{ margin: 32, background: '#15171c' }}
    />
  )
}

The installed component defines its own client boundary. The surrounding page can stay on the server: its props are data that React can send to the browser. Here, src and alt are strings, the dimensions are numbers, and options is a plain object containing numbers and strings.

Client state and handlers

An ordinary event-handler function cannot be passed from a Server Component to a client component. To open the image from a button, define the state and its callback in a client component:

tsx
'use client'

import { useState } from 'react'
import {
  ImageZoom,
  type ImageZoomProps,
} from '@/components/ui/image-zoom'

type PhotoPreviewProps = Omit<ImageZoomProps, 'open' | 'onOpenChange'>

export function PhotoPreview(props: PhotoPreviewProps) {
  const [open, setOpen] = useState(false)

  return (
    <div>
      <button type="button" onClick={() => setOpen(true)}>
        Preview photo
      </button>
      <ImageZoom {...props} open={open} onOpenChange={setOpen} />
    </div>
  )
}

Render PhotoPreview from your Server Component with the same image props as the first example. setOpen and the click handler are created in the browser instead of being passed from the server.

A DOM element also belongs to the browser. If you use the container option from a Server Component, pass a selector such as '#viewer'; create an element reference in client code. The controlled state guide shows a complete interactive inspector.

Server-side rendering

With other React SSR frameworks, render the component through your normal React tree. Keep the image props consistent between the server output and the initial client render. Width and height reserve space while the image loads.

The component owns zoom attachment and cleanup. Use its props to configure the image and zoom behavior.