Skip to content

Media viewer

Pro

A full-screen viewer for a set of photos: swipe between them, zoom, and read each caption and position. Not an M3 component (@windmorph/react-pro).

import { MediaViewer } from '@windmorph/react-pro';

Part of Windmorph Pro, paid components on the free core, not on sale yet. See the Pro guide for what it includes and to register interest.

Props

By name or description, across the 21 props below

MediaViewer 21 props

  • itemstype readonly MediaViewerItem[]Required

    The photos, in order.

  • indextype number

    The open photo (controlled).

  • defaultIndextype numberdefault 0

    The photo to open on when uncontrolled.

  • opentype boolean

    Whether the viewer is open (controlled).

  • defaultOpentype booleandefault false

    Whether the viewer starts open when uncontrolled.

  • origintype Element | DOMRectReadOnly

    What the viewer grows out of as it opens and shrinks back into as it closes, the M3 container transform (the core Dialog's origin): usually the thumbnail that was tapped, or a box in window coordinates. Keep it on the open photo's thumbnail as the viewer moves, so it closes into the right one. Reduced motion keeps only the fades. Left out, the viewer slides up and fades in, as a full-screen dialog does.

  • actionstype ReactNode

    Extra controls at the end of the top bar, before nothing else: usually icon buttons or a menu ("Open post", "Download"). They're shown on black in the dark colour scheme, and their icon buttons are standard unless they set a variant, as in a top app bar.

  • closeIcontype ReactElement<unknown, string | JSXElementConstructor<any>>

    Replaces the close button's icon.

  • previousIcontype ReactElement<unknown, string | JSXElementConstructor<any>>

    Replaces the Previous button's icon (mirrored in right-to-left layouts).

  • nextIcontype ReactElement<unknown, string | JSXElementConstructor<any>>

    Replaces the Next button's icon (mirrored in right-to-left layouts).

  • zoomInIcontype ReactElement<unknown, string | JSXElementConstructor<any>>

    Replaces the zoom-in button's icon.

  • zoomOutIcontype ReactElement<unknown, string | JSXElementConstructor<any>>

    Replaces the zoom-out button's icon.

  • reftype Ref<HTMLDivElement>

    The panel (the element with role="dialog"), as the core Dialog's ref.

Events

  • onIndexChangetype ((index: number) => void)

    Called with the new photo's index when the viewer moves to another photo.

  • onOpenChangetype ((open: boolean) => void)

    Called when the viewer opens or closes.

Text

  • labelstype Partial<MediaViewerLabels>

    Every word the viewer shows or announces, from the app's translations. English by default. A LabelsProvider sets them for the whole app.

  • aria-labeltype string

    Names the viewer's dialog for screen readers, e.g. "Photos from Alex's post". Required unless aria-labelledby is given.

  • aria-labelledbytype string

    The id of a visible element that names the viewer's dialog. Required unless aria-label is given.

Also accepts the root element's HTML attributes (id, data-*, event handlers).

Types

MediaViewerItem 8 fields

One photo in a MediaViewer.

  • keytype stringRequired

    Identifies the photo across renders, e.g. its id.

  • srctype stringRequired

    The photo's URL.

  • srcSettype string

    Responsive sources, as on <img>; the viewer is as wide as the window.

  • sizestype stringdefault "100vw" when srcSet is set

    The image's display sizes for srcSet, as on <img>.

  • alttype stringRequired

    Describes the photo; read by screen readers, and shown when there is no caption.

  • widthtype numberRequired

    The photo's width (its aspect ratio is what matters), so it's laid out before it loads.

  • heighttype numberRequired

    The photo's height, with width.

  • captiontype string

    Shown under the photo, cut to 2 lines until tapped. Line breaks are kept.

Spec status

Not part of Material Design 3: a Windmorph Pro component, built on M3's colour roles, type scale, shapes and motion. Its variants and configurations each link to a live example.

  • Opened from thumbnails, at the pressed photo

    Built

    Use case · Gallery

  • Growing out of the thumbnail tapped (origin, the M3 container transform)

    Built

    Configuration · Gallery

  • Captions, cut to two lines until tapped

    Built

    Configuration · Gallery

  • Actions in the top bar (actions)

    Built

    Configuration · Actions

  • Translated words (labels)

    Built

    Configuration · Actions

Theming

  • It is the core's full-screen Dialog (role dialog), so it takes the dialog's focus handling, Escape and motion: without an origin it slides up and fades in as a full-screen dialog does; with one it grows out of it. Its panel joins the core dialog's tokens (md-comp-dialog wm-media-viewer).
  • Photos read best on black, so the viewer fills the window with a black backdrop and shows its controls in the theme's dark colour scheme, in light and dark mode alike. The top bar is the full-screen header's 56px, with the position in title-large; its icon buttons are standard and keep density 0, and Previous and Next are tonal so they stay visible over a bright photo. A menu passed in actions matches.
  • With reduced motion it only fades, and photos change and zoom without moving.
  • As on the core Dialog, className and style go on the outermost, full-window layer, so a layout class never hides or clips the viewer; ref and other attributes go on the panel. classNames reaches the panel, the backdrop, the toolbar, its position and actions, the media area, previous, next and the caption.
  • closeIcon, previousIcon, nextIcon, zoomInIcon and zoomOutIcon replace the built-in icons (Previous and Next are mirrored in right-to-left layouts). Forced colours keep every control and the focus ring visible.