Image viewer
Turns an inline image into a full-viewport viewer with zoom, pan, rotation and optional annotations.
Select the image to open the viewer. Use the toolbar to zoom and rotate, drag to move, and press Escape to close.
When to use
- To let the user inspect a scanned document, a receipt or a photo that is too small to read inline.
- To show evidence with an annotation, such as a highlighted amount, on top of the image.
- Wherever a preview of an attachment appears in a card, a table row or a task.
When not to use
- PDF files. Use the PDF viewer, which has pages, search and text selection.
- Spreadsheets and CSV files. Use the Tabular viewer.
- Decorative images or logos. Use a plain
img. - A gallery with next and previous. The viewer shows one image; build the list around it.
Anatomy
The component is a wrapper. You pass the inline preview as children, and the viewer takes over when the user selects it.
| Part | Role |
|---|---|
children |
The inline preview. A transparent button covers it and opens the viewer. |
| Dialog | Full-viewport modal with a solid background, named by alt and described by labels.instructions. |
| Toolbar | Zoom out, zoom level, zoom in, fit, rotate left and rotate right. Shown once the image loads. |
| Close button | Top right corner, labelled with labels.close. |
overlay |
Optional annotations, drawn above the image and rotating with it. |
Examples
Annotation over the image
overlay is positioned in the image’s own coordinates, so use percentages for top, left, width and height. It is not interactive. The same shape appears in the viewer after the user opens it.
Inside a card
Keep other controls outside the preview slot, and put the caption next to it.
Enviado pelo cliente em 14/03/2025
Content
altnames the document: “Comprovante de pagamento do processo 0001234-56.2024.8.26.0100”. It becomes the dialog title.labels.openis an action: “Ampliar imagem”.labels.instructionsexplains the interaction for screen reader users: “Use os botões para ampliar, reduzir e girar. Arraste para mover. Pressione Escape para fechar.”- Every key in
labelsis required. Pass them from the app translations.
Accessibility
- Built on Radix Dialog: focus moves into the viewer on open, stays inside it, and returns to the trigger when it closes with
Escapeor the close button. - The dialog is named by
altand described bylabels.instructions, both visually hidden. - The trigger is a native button with
aria-label={labels.open}that covers the preview, with a visiblefocusoutline. - The image area is a labelled region that takes focus once the image loads. With it focused:
+and-zoom,0fits, and the arrow keys pan. Zoom runs from 100% to 800%. - Zoom and rotate also have buttons with tooltips and accessible names.
- Toolbar buttons are 44px, above the touch target minimum. Zoom out and zoom in are disabled at the limits.
- Loading and error messages use
role="status". After an error the Retry button reloads the image. - Annotations in
overlayare hidden from assistive technology (aria-hidden). Do not use them as the only place for important information.
Code
import { ImageViewer } from "@repo/ui/image-viewer";
<ImageViewer
alt="Comprovante de pagamento"
labels={labels}
src={attachment.url}
>
<img alt="Comprovante de pagamento" src={attachment.thumbnailUrl} />
</ImageViewer>;
In apps/app, wrap it once so every call passes translated labels, as ImageZoom does.
Props
ImageViewer| Prop | Type | Default | Description |
|---|---|---|---|
alt | string | — | Names the image and the dialog. Describe the document, not the action |
children | ReactNode | — | Inline preview shown on the page. Keep interactive controls out of it |
className | string | — | Extra classes for the wrapper around the preview |
labels | ImageViewerLabels | — | Every visible and accessible string, translated by the app. All keys are required |
onOpenChange | (open: boolean) => void | — | Called when the viewer opens or closes |
open | boolean | — | Controlled open state |
overlay | ReactNode | — | Non-interactive annotations positioned in percentages of the image |
src | string | — | URL of the image shown in the viewer |
ImageViewerLabels (all required)| Prop | Type | Default | Description |
|---|---|---|---|
open | string | — | Label of the button over the preview. |
close | string | — | Label of the close button. |
zoomIn | string | — | Label of the zoom in button. |
zoomOut | string | — | Label of the zoom out button. |
fit | string | — | Label of the fit to viewport button. |
rotateLeft | string | — | Label of the rotate counterclockwise button. |
rotateRight | string | — | Label of the rotate clockwise button. |
loading | string | — | Message while the image loads. |
error | string | — | Message when the image fails to load. |
retry | string | — | Label of the retry button. |
instructions | string | — | Hidden description of the dialog for assistive technology. |
The zoom range, the geometry that fits the image in the viewport and the zoom steps live in image-viewer-zoom.ts, image-viewer-geometry.ts and image-viewer-canvas.tsx. They are internal and are documented here only.
Known issues
| Issue | Effect |
|---|---|
The trigger button covers the whole preview (absolute inset-0) and takes its only name from labels.open. |
Screen reader users hear “Ampliar imagem” but not which image. Put the document name in labels.open. |
ImageViewerCanvas is keyed by src, and the viewer has no previous or next control. |
Reviewing several attachments means closing and reopening the viewer; zoom and rotation reset on each. |
The keyboard shortcuts (+, -, 0, arrows) work only while the image region itself has focus, not the toolbar buttons. |
They are not announced anywhere except in the text you pass as labels.instructions. |