Image viewer

Slant / Overlays

A lightbox that opens from a thumbnail, zooms, and steps through images.

Demo

Usage

@tuttn/slant
import { ImageViewer, type ImageViewerImage } from '@tuttn/slant'

const shots: ImageViewerImage[] = [
  { src: '/shots/tasks.png', thumbnail: '/shots/tasks-small.png', alt: 'The task list in Moon', caption: 'Moon', width: 2400, height: 1600 },
  { src: '/shots/board.png', alt: 'The board view in Paper', caption: 'Paper' },
]

// A gallery of thumbnails; each opens the viewer at its image. It keeps its
// own open state and index.
<ImageViewer label="Release shots" images={shots} />

// Opened from your own trigger: hide the gallery and control it.
const [open, setOpen] = useState(false)
const [index, setIndex] = useState(0)
<Button aria-haspopup="dialog" onClick={() => setOpen(true)}>View shots</Button>
<ImageViewer images={shots} gallery={false} open={open} onOpenChange={setOpen}
  index={index} onIndexChange={setIndex} />

// Size the thumbnails with a custom property.
<ImageViewer images={shots} style={{ '--sl-image-viewer-thumb-size': '140px' } as CSSProperties} />

Props

PropTypeDefaultDescription
imagesreadonly ImageViewerImage[]—The images, in order.
indexnumber—The index of the image the viewer shows. Pass it with onIndexChange to control it.
defaultIndexnumber0The image it starts on, when uncontrolled.
onIndexChange(index: number) => void—Called with the new index when the viewer steps, or a thumbnail opens it at another image.
openboolean—Whether the viewer is open. Pass it with onOpenChange to control it; leave it out and it keeps its own state.
defaultOpenbooleanfalseWhether the viewer starts open, when uncontrolled.
onOpenChange(open: boolean) => void—Called with the new state when a thumbnail opens the viewer, or Escape, the backdrop or the close button closes it.
gallerybooleantrueShows the grid of thumbnails that open the viewer. Turn it off to open the viewer from your own trigger through `open`.
labelstringmessages.images ('Images')The accessible name of the gallery and of the viewer.
maxZoomnumber4The largest zoom, as a multiple of the size that fits the screen.
ImageViewerImage{ src: string; alt: string; caption?: ReactNode; thumbnail?: string; width?: number; height?: number }—One image. alt names its thumbnail and is the viewer image’s alt text; caption shows under it in the viewer and names its figure; thumbnail is a smaller file for the gallery (src otherwise); width and height, its size in pixels, let the viewer lay it out before it loads.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref go on the root div, which holds the gallery and the viewer’s <dialog>.

Keyboard

  • In the gallery, Tab reaches each thumbnail; Enter or Space opens the viewer at its image.
  • In the viewer, ArrowLeft and ArrowRight step to the previous and next image while it fits the screen (swapped in right-to-left text); zoomed, the four arrows pan instead. PageUp and PageDown always step, and Home and End go to the first and last image. The ends don’t wrap.
  • + (or =) zooms in and - zooms out through fit, 1.5×, 2×, 3× and on to maxZoom (4×); 0 fits the image to the screen again. Stepping to another image starts it at fit.
  • Escape closes the viewer. Tab moves through the toolbar and the image, and stays inside.

Pointer

A double-click toggles between fit and 2× at the pointer. Ctrl+wheel (⌘+wheel on a Mac), and a trackpad pinch, which arrives as one, zoom at the pointer, and the page’s own zoom stays out of it. While zoomed, dragging pans the image (the cursor shows a hand), as does a plain wheel or a two-finger scroll; the pan stops at the picture’s edges.

Accessibility

The gallery is a list named by label (the root’s messages.images, "Images", by default). Each thumbnail is a button named by its image’s alt, with aria-haspopup="dialog". The viewer is a Dialog named by the same label: modal, with the page inert behind it. On opening, focus goes to the image’s figure, which the caption names and the keys act on; on closing it returns to the thumbnail that opened it, or to the page’s own trigger. The position (“2 of 5”) sits in a polite live region that also reads the new image’s alt text, so stepping is announced. The toolbar’s buttons (previous, next, zoom out, zoom in, fit, close) are named from the root’s messages and stay focusable when they can’t act, marked aria-disabled, so stepping to the last image never drops focus. A single image has no stepping buttons and no position.

Behaviour

The image grows out of its thumbnail to its place in the viewer, and shrinks back into the thumbnail of the image on screen when the viewer closes, if that thumbnail is in view; otherwise it fades. Under reduced motion it only fades. Only the image moves, through Web Animations, never the dialog, so a menu or tooltip opened inside still lands in place. The image is drawn whole (object-fit: contain) and never enlarged past its own size at fit. The images either side of the current one load while it is shown, so stepping finds them ready. With gallery={false} nothing renders but the viewer: open it through open and index. Uncontrolled, it keeps its own state from defaultOpen and defaultIndex.