# Bring images closer with React

> Lightweight image zoom for React. Keep the source. Make it yours.

Source: <https://medium-zoom.francoischalifour.com/react>

## Install

```sh
npx shadcn@latest add @medium-zoom/image-zoom
```

Read the component source in the [registry item](https://medium-zoom.francoischalifour.com/r/image-zoom.json).

- **Yours to change.** Typed React source. Install with shadcn/ui.
- **Keyboard and theme ready.** Accessible controls. Match your background.
- **High-resolution on demand.** Load sharper images only when zooming.

## Add zoom to an image.

Use it like an image. Add your source, alt text, and styles.

### Basic

Pass your source, alt text, and styles. The component handles zoom and keyboard access.

```tsx title="components/photo.tsx"
import { ImageZoom } from "@/components/ui/image-zoom"

export function Photo() {
  return (
    <ImageZoom
      src="https://medium-zoom.francoischalifour.com/assets/images/ocean-sunset.jpg"
      alt="Ocean sunset"
      width={1920}
      height={1280}
      className="rounded-lg"
    />
  )
}
```

Works with SSR and React Server Components.

### Composed

Style the trigger and image separately. Add a label inside the button.

```tsx title="components/photo.tsx"
import * as ImageZoom from "@/components/ui/image-zoom"

export function Photo() {
  return (
    <ImageZoom.Root options={{ margin: 32 }}>
      <ImageZoom.Trigger
        aria-label="View photo: Denali at sunrise"
        className="relative rounded-xl"
      >
        <ImageZoom.Image
          src="https://medium-zoom.francoischalifour.com/assets/images/denali.jpg"
          alt="Denali at sunrise"
          className="rounded-xl"
        />
        <span className="absolute bottom-3 right-3">View photo</span>
      </ImageZoom.Trigger>
    </ImageZoom.Root>
  )
}
```

Works with SSR and React Server Components.

### Controlled

Own the open state. Trigger activation and dismissal call onOpenChange.

```tsx title="components/photo.tsx"
"use client"

import * as React from "react"
import * as ImageZoom from "@/components/ui/image-zoom"

export function Photo() {
  const [open, setOpen] = React.useState(false)

  return (
    <>
      <ImageZoom.Root open={open} onOpenChange={setOpen}>
        <ImageZoom.Trigger aria-label="Enlarge Denali at sunrise">
          <ImageZoom.Image
            src="https://medium-zoom.francoischalifour.com/assets/images/denali.jpg"
            alt="Denali at sunrise"
            className="rounded-xl"
          />
        </ImageZoom.Trigger>
      </ImageZoom.Root>
      <button onClick={() => setOpen(true)}>Open image</button>
    </>
  )
}
```

Use a Client Component to manage zoom state.

### With caption

Compose with figure and figcaption to keep the caption alongside your image.

```tsx title="components/photo.tsx"
import { ImageZoom } from "@/components/ui/image-zoom"

export function Photo() {
  return (
    <figure className="space-y-3">
      <ImageZoom
        src="https://medium-zoom.francoischalifour.com/assets/images/denali.jpg"
        alt="Denali at sunrise"
        width={2400}
        height={1594}
        className="rounded-lg"
      />
      <figcaption className="text-sm text-muted-foreground">
        First light in Denali National Park.
      </figcaption>
    </figure>
  )
}
```

Works with SSR and React Server Components.

### Multiple images

Map your photos into a grid. Each image opens independently.

```tsx title="components/multiple-images.tsx"
import { ImageZoom } from "@/components/ui/image-zoom"

const photos = [
  { src: "https://medium-zoom.francoischalifour.com/assets/images/ocean-sunset.jpg", alt: "Ocean sunset", width: 1920, height: 1280 },
  { src: "https://medium-zoom.francoischalifour.com/assets/images/denali.jpg", alt: "Denali at sunrise", width: 2400, height: 1594 },
  { src: "https://medium-zoom.francoischalifour.com/assets/images/yellowstone.jpg", alt: "Yellowstone at sunrise", width: 2400, height: 1600 },
]

export function MultipleImages() {
  return (
    <div className="grid grid-cols-3 items-start gap-3">
      {photos.map(photo => (
        <ImageZoom
          key={photo.src}
          {...photo}
          className="h-auto w-full rounded-lg"
        />
      ))}
    </div>
  )
}
```

Works with SSR and React Server Components.

### High resolution

Load a small preview first. Fetch the full-resolution image when zoom opens.

```tsx title="components/photo.tsx"
import { ImageZoom } from "@/components/ui/image-zoom"

export function Photo() {
  return (
    <ImageZoom
      src="https://medium-zoom.francoischalifour.com/assets/images/denali-preview.webp"
      data-zoom-src="https://medium-zoom.francoischalifour.com/assets/images/denali.jpg"
      alt="Denali at sunrise"
      width={2400}
      height={1594}
      className="rounded-lg"
    />
  )
}
```

Works with SSR and React Server Components.

Image attributes and the forwarded ref reach the underlying `<img>`. [Explore all zoom options](https://medium-zoom.francoischalifour.com/docs/reference/javascript/options/)

## Small API. Sensible defaults.

Pass image attributes and zoom options to `ImageZoom`. Compose its parts for custom layouts.

| Prop                 | What it does                                    | Default        |
| -------------------- | ----------------------------------------------- | -------------- |
| `options.margin`     | Space around the zoomed image.                  | `24`           |
| `options.background` | Overlay color, resolved from your theme.        | `--background` |
| `data-zoom-src`      | A higher-resolution source to open.             | Original image |
| `open`               | Control whether the image is zoomed.            | Internal state |
| `onOpenChange`       | Set the next open value when controlling state. | —              |
| `disabled`           | Disable zoom; keep the image visible.           | `false`        |

## Add zoom to your project.

Install with shadcn/ui. Make the component yours. [Read the documentation](https://medium-zoom.francoischalifour.com/docs/).
