Skip to content

Media grid

Pro

Square photo tiles, a sideways row on compact windows and a grid from medium up; a press on a tile reports it (`onAction`), for the app to open the viewer. Not an M3 component (@windmorph/react-pro).

import { MediaGrid } 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

MediaGrid 11 props

  • itemstype readonly MediaGridItem[]Required

    The photos, in order.

  • maxtype numberdefault every item

    How many tiles show.

  • columnstype numberdefault 3

    Columns from medium windows up.

  • sizestype stringdefault "auto, (min-width: 600px) calc(100vw / 3), 112px", with columns in place of 3

    How wide a tile shows, as sizes on <img>, for the items with a srcSet in widths. Left out, the grid says it from its own layout: auto, the tile's laid-out width, for the browsers that read it on a lazy image, then for the others the row's 112px tile on compact windows and an equal share of the window from medium up (the widest a tile can be). Pass your own when the tile's width or the columns are set in CSS, for the browsers that fall back.

  • fallbackIcontype ReactNodedefault a broken-picture icon

    The icon of a tile whose photo couldn't be loaded. Such a tile keeps its square, in a container colour (--wm-media-grid-fallback-color), with this icon in place of the photo; it has data-status="error", is still a button named by the photo's alt and still calls onAction. A new src or srcSet is a new try: the placeholder goes at once.

  • reftype Ref<HTMLUListElement>

    The list. Its other props (id, aria-*, data-*) go to it too.

Events

  • onActiontype (index: number, tile: Element) => voidRequired

    Called with the pressed tile's index and the tile itself, e.g. to open MediaViewer there, growing out of the tile (its origin).

Text

  • aria-labeltype stringRequired

    The list's name ("Photos").

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

Types

MediaGridItem 4 fields

  • keytype stringRequired

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

  • srctype stringRequired

    The photo's URL.

  • srcSettype string

    Responsive sources, as on <img>. With widths (photo-640.webp 640w) the browser picks the smallest file that fills the tile, from the grid's sizes.

  • alttype stringRequired

    Names the tile for screen readers.

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.

  • A row that scrolls sideways on compact windows

    Built

    Configuration · Viewer, Columns

  • Some of the photos (max), opening the viewer out of the pressed tile

    Built

    Use case · Viewer

  • Columns from medium windows up (columns)

    Built

    Configuration · Columns

  • Photos stored at several widths (srcSet, sizes)

    Built

    Configuration · Sources

  • A photo that can’t be loaded (fallbackIcon)

    Built

    Use case · Failed

Theming & tokens

Set the tokens below on a grid or any ancestor. Style the parts with classNames (root, item, tile, image, and fallback for the placeholder of a photo that can't be loaded).

Media grid tokens
VariableDefaultPaints
--wm-media-grid-gap4pxBetween tiles.
--wm-media-grid-columns3Columns from medium windows up (`columns`).
--wm-media-grid-cornervar(--md-sys-shape-corner-medium)The grid’s outer corners, from medium windows up.
--wm-media-grid-tile-cornervar(--md-sys-shape-corner-small)Each tile’s corners in the compact row.
--wm-media-grid-tile-width112pxEach tile’s width in the compact row.
--wm-media-grid-fallback-colorvar(--md-sys-color-surface-container-highest)The square of a tile whose photo couldn’t be loaded.
--wm-media-grid-fallback-icon-colorvar(--md-sys-color-on-surface-variant)The icon of a tile whose photo couldn’t be loaded.
--wm-media-grid-fallback-icon-size24pxThat icon’s size.