GooseUI

Components

Image Viewer

New

Image viewer with zoom from 50% to 400%, panning, 90° rotation, reset and fit. Zoom follows the cursor: the point under the pointer stays in place. Works with the mouse wheel, trackpad and two-finger pinch, and has an inline mode and a fullscreen dialog. Zoom and panning need JavaScript: the component ships about 4 kB of its own code and no libraries. The fullscreen mode is a native <dialog>, so the browser handles focus, Esc and the inert page behind it.

Browser Support

<dialog>

Displays the modal dialog

Your browser:
@starting-style

Animates the initial appearance

Your browser:
:has()

Locks page scrolling while open

Your browser:
::backdrop

Styles the viewer backdrop

Your browser:

Preview

Scroll or pinch to zoom, drag to pan, double-click to zoom in or back. Focus the image and try +, -, 0, R and the arrow keys.

Use plus and minus to zoom, arrow keys to pan, R to rotate and 0 to fit the image.Aerial view of a city street at sunset between tall skyscrapers

Installation

npx shadcn@latest add @gooseui/image-viewer
pnpm dlx shadcn@latest add @gooseui/image-viewer
yarn dlx shadcn@latest add @gooseui/image-viewer
bunx --bun shadcn@latest add @gooseui/image-viewer

Usage

import { ImageViewer } from "@/components/ui/image-viewer"

export default function Example() {
  return (
    <ImageViewer
      src="/images/floor-plan.png"
      alt="Floor plan of the second floor"
      className="h-[480px]"
    />
  )
}

Examples

Fullscreen dialog

ImageViewerDialog opens the viewer in a native modal <dialog>. Pass a single element as children: its click opens the dialog. Escape closes it, focus moves to the image on open and returns to the trigger on close.

import { Button } from "@/components/ui/button"

import { ImageViewerDialog } from "@/components/ui/image-viewer"

<ImageViewerDialog src="/images/poster.png" alt="Concert poster">
  <Button type="button">
    <img src="/images/poster-small.png" alt="Concert poster" />
  </Button>
</ImageViewerDialog>

Zoom limits

minZoom and maxZoom are relative to the fitted size. Here the image cannot be smaller than the frame and the buttons step by 50%.

Use plus and minus to zoom, arrow keys to pan, R to rotate and 0 to fit the image.Aerial view of a city street at sunset between tall skyscrapers
<ImageViewer
  src="/images/map.png"
  alt="City map"
  minZoom={1}
  maxZoom={3}
  zoomStep={0.5}
  className="h-72"
/>

Without toolbar

showControls={false} hides the toolbar. Gestures and keyboard shortcuts keep working.

Use plus and minus to zoom, arrow keys to pan, R to rotate and 0 to fit the image.Snowy mountains under a starry night sky with the Milky Way
<ImageViewer src="/images/poster.png" alt="Poster" showControls={false} />

Translated labels

Every button label and the screen reader hint come from the labels prop.

<ImageViewer
  src="/images/plan.png"
  alt="Plan"
  labels={{
    viewer: "Visor de imágenes",
    zoomIn: "Acercar",
    zoomOut: "Alejar",
    rotate: "Girar",
    fit: "Ajustar",
    reset: "Restablecer",
  }}
/>

Props

PropTypeDefaultDescription
srcstring-Image URL
altstring-Alternative text, also part of the viewer label
minZoomnumber0.5Smallest zoom relative to the fitted size
maxZoomnumber4Largest zoom relative to the fitted size
zoomStepnumber0.25Zoom change for the buttons and + / - keys
showControlsbooleantrueShow the toolbar
toolbarEndReactNode-Extra content at the end of the toolbar
imageClassNamestring-Classes for the <img> element
labelsPartial<ImageViewerLabels>EnglishButton labels, hint and error text
classNamestring-Classes for the root. Default height is h-96

ImageViewerDialog

Accepts all ImageViewer props except className and toolbarEnd, plus:

PropTypeDefaultDescription
openboolean-Controlled open state
defaultOpenbooleanfalseInitial open state
onOpenChange(open: boolean) => void-Called when the dialog opens or closes
childrenReactNode-Trigger element. A single element gets an onClick that opens the dialog
titlestringaltDialog title, alt by default
classNamestring-Classes for the dialog content

Features

  • Zoom to the cursor: wheel, pinch and double-click keep the point under the pointer still
  • Pan limits: the image cannot be dragged out of the frame
  • Rotation: 90° steps, the image is refitted for the new orientation
  • Keyboard: + and - zoom, 0 fits, R rotates (Shift+R back), arrows pan, Shift speeds them up
  • Accessibility: the viewport is a labelled region with a hidden hint, every button has a label
  • Reduced motion: button and key transitions are turned off