Skip to the page
Supply UI

Image cropper

In code: ImageCropper

Choose the part of a picture to keep: drag the frame, pull its corners, zoom, turn, mirror

Preview

Use

The code of the preview above
import { ImageCropper, Skeleton } from '@/components/ui';

function ImageCropperDemo() {
  // In a project, `src` is the address of the uploaded photo; here the kit paints one.
  const photo = usePainted('Juniper');
  return (
    <div className="ui-grow">
      {photo ? (
        <ImageCropper src={photo} alt="Staff photo of Mira Kovac" aspectRatio={1} shape="circle" aria-label="Fit the photo to the badge" />
      ) : (
        <Skeleton className="ui-ratio" style={{ ['--ratio' as string]: '4 / 3' }} />
      )}
    </div>
  );
}

This page as text, for an AI

Options

OptionTypeWhat it does
alt NeededstringWhat the picture shows, for screen readers.
src NeededstringThe picture: a URL, or an object URL of a file the person chose.
aria-label stringIts name for screen readers, when no visible label names it.
aspectRatio numberWidth over height to keep: 1 for a square, 16 / 9. Left out, any shape.
className stringMore classes on the outer element, for layout around it (margins, grid placement), not to restyle it.
onCropCommit (crop: { x: number; y: number; width: number; height: number; }) => voidRuns when the person lets go, with the crop in the picture's own pixels.
shape "circle" | "rectangle"circle for a profile picture.

Without React

The same look on any HTML, with the classes (React only).

With the CSS in parts (/ui/css/), it needs these, besides the ones every page loads:

components/button.csscomponents/checkbox-radio.csscomponents/switch.csscomponents/range-slider.csscomponents/card.csscomponents/table.csscomponents/tree.csscomponents/colour-picker.csscomponents/image-cropper.csscomponents/resizable-panels.csscomponents/icons.csscomponents/input-group.csscomponents/choice-cards.csscomponents/button-group-toggle.csscomponents/prose.csscomponents/carousel-arrows.cssparts/range-slider.css

In the examples

Small screens made with it and the rest of the kit. They work: click, type and choose in them.

Mira KovacFront of house
Fit the badge Drag and zoom the photo into the round frame

Actions and forms: states, do and don’t

Choose the part of a picture to keep. onCropCommit gets the crop in the picture's own pixels: do the cutting on the server.

States. Every control has rest, hover, pressed, focus (a ring, from the keyboard only), disabled and, where it applies, chosen (data-checked, aria-pressed) and invalid (aria-invalid, with the message under the field). Each change of look fades with --ui-dur-fast.

Do

  • Give every field a visible label (Field label=…); a placeholder is an example, never the label.
  • Keep one primary button per view; deleting is danger and asks first with AlertDialog.
  • Say what went wrong under the field, in words a person can act on ("Enter a full email address").
  • Use NumberInput, DatePicker, Select and the rest rather than a bare <input>: they bring the keyboard, the screen-reader names and the phone layout.

Don't

  • Don't disable a button to say a form is incomplete without saying why next to it.
  • Don't put two primary buttons side by side, or a primary button inside a menu.
  • Don't use a Switch for something that only happens when a form is sent: that is a Checkbox.