Skip to content

Media upload grid

Pro

The photos someone is adding: upload progress and errors, remove and retry, drag to reorder (mouse, touch or keyboard), an add tile and file drop. Not an M3 component (@windmorph/react-pro).

import { MediaUploadGrid } 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.

Purpose

The photos someone is adding to a post, listing or product: a square thumbnail for each, with its upload progress or the reason it failed, a remove button, an optional menu, and an "Add photos" tile. Files dropped on the grid are added too, and photos reorder by dragging. It is not an M3 component: it comes from @windmorph/react-pro, built on React Aria's grid list drag and drop and the library's File trigger, Icon buttons, Menu and progress indicators.

A grid fills its container's width with 3 columns, 4 from 600px and 5 from 840px (the grid's own width, so it fits a dialog as well as a page); layout="row" is one scrolling row of 112px tiles. Below 280px the grid drops to 2 columns, so tiles stay 88px or more and their corner buttons' touch targets never overlap.

The app's part

  • The grid keeps nothing: the app holds the photos in items and changes them in the callbacks. onFileAccept(files) gets only files matching accept, and no more than fit under maxItems; at the limit the add tile goes away. accept reads as FileTrigger's: list the extensions beside the MIME types (['text/csv', '.csv']), since a file's type comes from the system and can differ from the one you expect; * or */* takes any file, and an entry may hold several types, comma-separated. onFileReject(rejections) gets the files a pick or drop brought that weren't taken, after onFileAccept and never with an empty list, each with its reason (type, off accept, or room, past maxItems), so you can say why a file didn't appear. A full grid still takes a drop, only to report it: its files off accept as type, those matching as room; it isn't drawn as a drop target then. A drag whose types can't match accept is refused while it's over the grid, with the no-drop cursor, so it's never reported.
  • onFilePick(pick) is called once for each pick or drop that brings files, with all of them: accepted (what onFileAccept gets) and rejected (what onFileReject gets), each in the order the files came; accepted is empty when every file was refused. It runs first, then the other two, all in the same task, so the composer above adds what fits and says once what was left out from one call. It alone is enough for the add tile and drops, and each callback gets its own copy of the lists.
  • Show a chosen file at once with URL.createObjectURL(file) as its previewUrl (and revoke it when it's removed), set status: 'uploading' and report progress from 0 to 1. Without a progress value a loading indicator shows. On failure, set status: 'error' and a short error.
  • onReorder(keys) gets every key in the new order. A photo's menu (itemActions, here Move earlier and Move later) is another way to reorder that needs no dragging; pass a function to give each photo its own entries.
  • Check every file on the server too: its name and type can lie. Pasting photos is up to the app, which owns the focused text field.
  • A failed photo's Retry sits in the start corner (its action menu steps aside), so no two 48px targets overlap. Removing a photo moves focus to its neighbour without scrolling the page, and the arrow keys stop at the grid's edge rather than scrolling it.

When not to use

For one photo, such as a profile picture, use a File trigger and an Image crop; for documents, list the files instead.

See also

  • File trigger: Makes any library button open the file picker, or the camera on phones; useFileTrigger does it from a menu item. Not an M3 component (@windmorph/react-pro).
  • Image crop: Frames a photo before upload: drag, pinch or zoom it behind a fixed frame, and get the crop in the original pixels. Not an M3 component (@windmorph/react-pro).
  • Progress indicators: Linear and circular indicators, determinate or indeterminate, flat or wavy.