# Controlled state

> Control React image zoom from a parent component with open and onOpenChange state props.

Source: <https://medium-zoom.francoischalifour.com/docs/guides/controlled-state/>

Let a parent component coordinate the zoom with the rest of your interface. This photo inspector opens from an asset action and provides its own toolbar while the image is enlarged.

```tsx title="photo-inspector.tsx"
'use client'

import { useRef, useState } from 'react'
import { createPortal } from 'react-dom'

import * as ImageZoom from '@/components/ui/image-zoom'

import './photo-inspector.css'

export function PhotoInspector() {
  const [open, setOpen] = useState(false)
  const inspectButton = useRef<HTMLButtonElement>(null)

  function closePreview() {
    setOpen(false)
    inspectButton.current?.focus({ preventScroll: true })
  }

  return (
    <section className="asset-inspector" aria-label="Photo inspector">
      <header className="asset-inspector-heading">
        <span>Selected asset</span>
        <span role="status">
          {open ? 'Preview open' : 'Ready to inspect'}
        </span>
      </header>
      <div className="asset-preview">
        <ImageZoom.Root open={open} onOpenChange={setOpen}>
          <ImageZoom.Trigger aria-label="Inspect ocean sunset">
            <ImageZoom.Image
              src="/assets/images/ocean-sunset.jpg"
              alt="Ocean sunset"
              width={1_920}
              height={1_280}
            />
          </ImageZoom.Trigger>
        </ImageZoom.Root>
      </div>
      <div className="asset-inspector-details">
        <div>
          <strong>ocean-sunset.jpg</strong>
          <span>1920 × 1280 · JPEG</span>
        </div>
        <button
          ref={inspectButton}
          type="button"
          onClick={() => setOpen(true)}
        >
          Inspect photo
        </button>
      </div>
      {open &&
        createPortal(
          <div
            className="inspection-toolbar"
            role="toolbar"
            aria-label="Photo inspection"
          >
            <span>Ocean sunset</span>
            <button
              type="button"
              onClick={closePreview}
              aria-keyshortcuts="Escape"
            >
              Close preview <kbd aria-hidden="true">Esc</kbd>
            </button>
          </div>,
          document.body
        )}
    </section>
  )
}
```

```css title="photo-inspector.css"
.asset-inspector {
  max-width: 360px;
  margin: 0 auto;
  text-align: left;
}

.asset-inspector-heading,
.asset-inspector-details {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.asset-inspector-heading {
  margin-bottom: 12px;
  color: var(--muted-foreground, #71717a);
  font-size: 12px;
}

.asset-inspector .asset-preview [data-slot='image-zoom-trigger'] {
  max-width: 100%;
  border-radius: 8px;
}

.asset-inspector .asset-preview img {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  border-radius: 8px;
}

.asset-inspector-details {
  flex-wrap: wrap;
  margin-top: 16px;
}

.asset-inspector-details > div {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.asset-inspector-details strong {
  font-size: 13px;
  font-weight: 550;
}

.asset-inspector-details span {
  color: var(--muted-foreground, #71717a);
  font-size: 12px;
}

.asset-inspector button:not([data-slot]),
.inspection-toolbar button {
  padding: 8px 12px;
  border: 1px solid var(--border, #e4e4e7);
  border-radius: 6px;
  background: var(--background, #fff);
  color: var(--foreground, #18181b);
  font: inherit;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}

.inspection-toolbar {
  position: fixed;
  bottom: 24px;
  left: 50%;
  z-index: 52;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 12px 10px 16px;
  border: 1px solid var(--border, #e4e4e7);
  border-radius: 10px;
  background: var(--background, #fff);
  color: var(--foreground, #18181b);
  box-shadow: 0 8px 24px #0002;
  transform: translateX(-50%);
  font-size: 13px;
}

.inspection-toolbar kbd {
  margin-left: 8px;
}
```

## Control the zoom

Pass `open` and `onOpenChange={setOpen}` to `<ImageZoom.Root>`.

The parent owns the state. “Inspect photo” calls `setOpen(true)`; “Close preview” calls `setOpen(false)`. The inspection toolbar is rendered into `document.body` with a portal so it stays above the zoom overlay.

Apply each request from `onOpenChange` to `open`. This keeps the parent in sync when the user closes with Escape, the overlay, or scrolling, as well as with your own controls.

The same state props work with `<ImageZoom>`.

## Internal state

Omit `open` to let the component manage its own state. You can still pass `onOpenChange` to observe state requests.

Keep the component controlled or uncontrolled throughout its lifetime.

## Related pages

- [Custom triggers](https://medium-zoom.francoischalifour.com/docs/guides/custom-react-triggers/): Compose a button with your own label and styles.
- [\<ImageZoom>](https://medium-zoom.francoischalifour.com/docs/reference/react/image-zoom/): Look up image, zoom, and state props.
