Skip to content

Image Cropper

React image cropper: drag to pan, wheel and pinch zoom, aspect presets, a round avatar mask and 90 degree rotation, returning the crop in source pixels.

src/registry/ui/image-cropper.tsx/r/image-cropper.json

Installation

bash
npx shadcn@latest add @minidev/image-cropper

MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/image-cropper.json

Usage

tsx
import { ImageCropper } from "@/components/ui/image-cropper"

export function Example() {
  return (
    <ImageCropper />
  )
}

Exports ImageCropper getCroppedBlob

API reference

ImageCropper props
PropTypeDefault
srcImage URL or data URI. Remote images need CORS headers for getCroppedBlob.stringSAMPLE_SRC
altstring"Lumen sample landscape"
aspectsAspect presets offered, as "w:h".Aspect[]["1:1", "4:3", "16:9"]
defaultAspectAspect"4:3"
defaultCircleStart with the circular avatar mask on.booleanfalse
allowCircleOffer the circular mask toggle.booleantrue
maxZoomLargest zoom, as a multiple of the size that just covers the frame.number4
showPreviewShow the live preview and crop details beside the frame.booleantrue
onCropCalled with the crop in source pixels once an interaction settles.(rect: CropRect, info: { aspect: Aspect; circle: boolean }) => void-
fileNameFile name used by the export button.string"lumen-cover.png"
classNamestring-

When to use

Use it before uploading a cover, avatar or product photo. onCrop reports the crop in source pixels; getCroppedBlob(src, rect, opts) renders it to a Blob for upload. For picking a file without framing use ImageUpload, and for a round avatar picker use AvatarUpload. Keyboard: arrows pan (Shift for bigger steps), + and - zoom, R rotates, 0 resets.

Keyboard interactions

Keyboard interactions
KeyAction
ArrowLeftorArrowRightorArrowUporArrowDownOn the crop area, moves the image 12px. Hold Shift for 48px.
Plusor=On the crop area, zooms in.
-or_On the crop area, zooms out.
ROn the crop area, rotates 90 degrees clockwise.
0On the crop area, resets position, zoom and rotation.
ArrowLeftorArrowRightorHomeorEndIn the aspect ratio control, moves the selection between presets.

Accessibility

  • The crop area is a focusable role="group" with aria-roledescription="image cropper" and aria-label="Crop {alt}". Its aria-describedby points at hidden instructions listing the drag, arrow, zoom, R and 0 controls.
  • The aspect presets are a role="radiogroup" with one Tab stop. The circle toggle has aria-pressed, and the rotate and zoom buttons have labels.
  • Load and export failures render in role="alert" so they are announced. The preview image is named "Cropped preview of {alt}".
  • Under reduced motion every spring has duration: 0, so pans, zooms and the circle mask change instantly.

Frequently asked questions

How do I upload the cropped image?

Keep the rectangle from onCrop, which fires about 160ms after an interaction settles. Then render it: const blob = await getCroppedBlob(src, rect, { type: "image/webp", width: 800 }) and append the blob to FormData. Pass circle: true for a round avatar with transparent corners.

Why does export fail for images on another domain?

Drawing a cross-origin image taints the canvas unless the server sends CORS headers. The cropper sets crossOrigin="anonymous" for remote URLs, so the server must answer with Access-Control-Allow-Origin. For a file the user picked, pass URL.createObjectURL(file), which always works.

How do I lock it to a square avatar crop?

Pass aspects={["1:1"]}, defaultAspect="1:1" and defaultCircle. Turning the circle on forces 1:1 and disables the other aspects. Set allowCircle={false} to hide the toggle.

Are the crop numbers in screen pixels?

No. x, y, width and height are in source image pixels, measured after rotation (0, 90, 180 or 270), so they stay correct whatever size the frame is shown at.

Dependencies

npm

lucide-reactmotion

Examples

Avatar crop

One square preset with the circle mask locked on, the usual setup for a profile photo picker.

Reading the crop

onCrop fires once an interaction settles with the rectangle in source pixels, ready to send to your server or to getCroppedBlob.

All media and upload components