UIComponentsReact
A crop area over an image that the user moves, resizes, zooms, rotates and flips, with a value in image pixels.
Built on the headless Image Cropper.See its API in Docs
Rendered by React 19.3.1
Installation
npx shadcn@4.23.0 add https://hamur.dev/r/react/image-cropper.jsonIt also adds utils, button, slider. The app needs its setup first: Installation.
Usage
Put an ImageCropperViewport inside ImageCropper, an ImageCropperImage inside the Viewport, and an ImageCropperArea after the Image with an ImageCropperGrid and eight ImageCropperHandles (top, right, bottom, left, top-left, top-right, bottom-left, bottom-right) inside it. Give the Image an alt, and add an ImageCropperStatus anywhere in the Root so zoom, rotation and flip changes made elsewhere are announced. The Area appears once the image is loaded and measured. Arrow keys move the crop, Alt with an arrow resizes it, + and - zoom, and a pointer drag on the Area or a Handle does the same; the value, the form value, the output helpers (toBlob, toCanvas), the labels and the announcements come from the headless Image Cropper.
The zoom and rotation are the Root's zoom and rotation, so the controls beside the Viewport are the items you already have: a Slider with min, max and step of minZoom, maxZoom and zoomStep for the zoom, a Slider from -180 to 180 for the rotation, and Buttons that turn the image by 90 degrees and flip it on the screen. Name each of them, for example with aria-label.
import {
ImageCropper,
ImageCropperArea,
ImageCropperGrid,
ImageCropperHandle,
ImageCropperImage,
ImageCropperStatus,
ImageCropperViewport,
} from '@/components/ui/image-cropper'Examples
Default
A photograph with a crop area, a Slider for the zoom, a Slider for the rotation and Buttons for the turns and the flips.
The Viewport is bg-muted with padding, so the Handles on the edge of an image that fills the Viewport are not clipped by its overflow: hidden. The Area has a 1 px light stroke and a 1 px dark stroke, so its edge is visible on any photograph (WCAG 1.4.11), and darkens everything outside it with a box-shadow spread; shape="circle" rounds it. Its keyboard focus is a 2 px outline, not a ring, because the ring would share box-shadow with the shade; the Viewport's padding leaves room for it (WCAG 2.4.11). A Handle is 10 px wide and has a 24 by 24 CSS pixel hit area from a ::before pseudo-element (WCAG 2.5.8). The Grid draws the thirds of the crop as background lines while it is dragged or resized. This item takes Base UI's component ID as its slug and one name per Base UI part.
API reference
The styled parts pass every prop to the headless part they wrap and add classes. The props, data attributes and keyboard are on Image Cropper's API reference.