Image Cropper
Crop and transform images with zoom, rotation, and aspect ratio controls.
Anatomy
<ImageCropper.Root>
<ImageCropper.Viewport>
<ImageCropper.Image />
<ImageCropper.Selection>
<ImageCropper.Handle />
<ImageCropper.Grid />
</ImageCropper.Selection>
</ImageCropper.Viewport>
</ImageCropper.Root>
Examples
Basic
Drag the handles to resize the selection. Drag inside it to pan the image.
Aspect Ratio
Use aspectRatio to lock the crop area, such as 16/9 for widescreen or 1 for square.
Circle Crop
Set cropShape="circle" for a circular selection, as used for avatars.
Initial Crop
Use initialCrop to set the starting crop area as x, y, width, and height in pixels.
Controlled Zoom
Use zoom and onZoomChange to control zoom, for example from external buttons.
Zoom Limits
Set minZoom and maxZoom to limit how far users can zoom in or out.
Rotation
Use rotation and onRotationChange to rotate the image. Values are in degrees.
Flip
Use flip with horizontal and vertical booleans to flip the image.
Min and Max Size
Use minWidth, minHeight, maxWidth, and maxHeight to bound the crop area size.
Fixed Crop Area
Set fixedCropArea to keep the crop area still while the image moves underneath.
Crop Preview
Call getCroppedImage() from the context to get the result. Pass { output: 'dataUrl' } for a base64 string to use as
an img src.
Reset
Call reset() from the context to restore the initial state, for example from a "start over" button.
Events
Use onCropChange and onZoomChange to track the crop and zoom, for example to sync external state or show a live
preview.
Context
Use ImageCropper.Context to reach the API anywhere inside the root, including zoomBy, rotateBy, and setZoom.
Root Provider
Use RootProvider with useImageCropper to control the cropper from outside the component tree, such as from a custom
toolbar.
API Reference
Props
Root
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
aspectRatio | numberThe aspect ratio to maintain for the crop area (width / height). For example, an aspect ratio of 16 / 9 will maintain a width to height ratio of 16:9. If not provided, the crop area can be freely resized. | |
cropShape | 'rectangle' | 'circle' | 'rectangle'The shape of the crop area. |
defaultFlip | { horizontal: false, vertical: false } | FlipStateThe initial flip state to apply to the image. |
defaultRotation | 0 | numberThe initial rotation to apply to the image in degrees. |
defaultZoom | 1 | numberThe initial zoom factor to apply to the image. |
fixedCropArea | false | booleanWhether the crop area is fixed in size and position. |
flip | FlipStateThe controlled flip state of the image. | |
ids | ElementIdsThe ids of the image cropper elements | |
initialCrop | RectThe initial rectangle of the crop area. If not provided, a smart default will be computed based on viewport size and aspect ratio. | |
maxHeight | Infinity | numberThe maximum height of the crop area |
maxWidth | Infinity | numberThe maximum width of the crop area |
maxZoom | 5 | numberThe maximum zoom factor allowed. |
minHeight | 40 | numberThe minimum height of the crop area |
minWidth | 40 | numberThe minimum width of the crop area |
minZoom | 1 | numberThe minimum zoom factor allowed. |
nudgeStep | 1 | numberThe base nudge step for keyboard arrow keys (in pixels). |
nudgeStepCtrl | 50 | numberThe nudge step when Ctrl/Cmd key is held (in pixels). |
nudgeStepShift | 10 | numberThe nudge step when Shift key is held (in pixels). |
onCropChange | (details: CropChangeDetails) => voidCallback fired when the crop area changes. | |
onFlipChange | (details: FlipChangeDetails) => voidCallback fired when the flip state changes. | |
onRotationChange | (details: RotationChangeDetails) => voidCallback fired when the rotation changes. | |
onZoomChange | (details: ZoomChangeDetails) => voidCallback fired when the zoom level changes. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<ImageCropperRootState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
rotation | numberThe controlled rotation of the image in degrees (0 - 360). | |
translations | IntlTranslationsSpecifies the localized strings that identify accessibility elements and their states. | |
zoom | numberThe controlled zoom level of the image. | |
zoomSensitivity | 2 | numberControls how responsive pinch-to-zoom is. |
zoomStep | 0.1 | numberThe amount of zoom applied per wheel step. |
| Attribute | Description |
|---|---|
[data-image-cropper-root] | <uid> |
[data-fixed] | |
[data-shape] | |
[data-pinch] | |
[data-dragging] | Present when in the dragging state |
[data-panning] |
| CSS Variable | Description |
|---|---|
--crop-width | The width of the Root |
--crop-height | The height of the Root |
--crop-x | The crop x value for the Root |
--crop-y | The crop y value for the Root |
--image-zoom | The image zoom value for the Root |
--image-rotation | The image rotation value for the Root |
--image-offset-x | The offset position for image |
--image-offset-y | The offset position for image |
Grid
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
axis | 'horizontal' | 'vertical'The axis of the grid lines to display | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-image-cropper-grid] | <uid> |
[data-axis] | The axis to resize |
[data-dragging] | Present when in the dragging state |
[data-panning] |
Handle
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
placement | HandlePositionThe placement of the handle | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-image-cropper-handle] | <uid> |
[data-placement] | The placement of the handle |
[data-disabled] | Present when disabled |
Image
Defines the <img> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<ImageCropperImageState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-image-cropper-image] | <uid> |
[data-ready] | |
[data-flip-horizontal] | |
[data-flip-vertical] |
RootProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseImageCropperReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | type ONLY_FOR_FORMAT =
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<ImageCropperRootProviderState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Selection
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<ImageCropperSelectionState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-image-cropper-selection] | <uid> |
[data-disabled] | Present when disabled |
[data-shape] | |
[data-measured] | |
[data-dragging] | Present when in the dragging state |
[data-panning] |
Viewport
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-image-cropper-viewport] | <uid> |
[data-disabled] | Present when disabled |
Context
API
| Property | Type |
|---|---|
zoom | numberThe current zoom level of the image. |
rotation | numberThe current rotation of the image in degrees. |
flip | FlipStateThe current flip state of the image. |
crop | RectThe current crop area rectangle in viewport coordinates. |
offset | PointThe current offset (pan position) of the image. |
naturalSize | SizeThe natural (original) size of the image. |
viewportRect | BoundingRectThe viewport rectangle dimensions and position. |
dragging | booleanWhether the crop area is currently being dragged. |
panning | booleanWhether the image is currently being panned. |
setZoom | (zoom: number) => voidFunction to set the zoom level of the image. |
zoomBy | (delta: number) => voidFunction to zoom the image by a relative amount. |
setRotation | (rotation: number) => voidFunction to set the rotation of the image. |
rotateBy | (degrees: number) => voidFunction to rotate the image by a relative amount in degrees. |
setFlip | (flip: Partial<FlipState>) => voidFunction to set the flip state of the image. |
flipHorizontally | (value?: boolean) => voidFunction to flip the image horizontally. Pass a boolean to set explicitly or omit to toggle. |
flipVertically | (value?: boolean) => voidFunction to flip the image vertically. Pass a boolean to set explicitly or omit to toggle. |
resize | (handlePosition: HandlePosition, delta: number) => voidFunction to resize the crop area from a handle programmatically. |
setCrop | (crop: Rect) => voidFunction to set the crop area in viewport coordinates, constrained like `initialCrop` (min/max size, aspect ratio and viewport bounds). Zoom, rotation, flip and pan are kept. Does nothing when `fixedCropArea` is set or before the viewport is measured. |
reset | () => voidFunction to reset the cropper to its initial state. |
getCroppedImage | (options?: GetCroppedImageOptions) => Promise<Blob | string | null>Function to get the cropped image with all transformations applied. Returns a Promise that resolves to either a Blob or data URL. |
getCropData | () => CropDataFunction to get the crop geometry in natural image pixels. The rect is axis-aligned; `corners` preserves the exact source quad. |
getRootState | () => RootStateReturns the state of the root |
getImageState | () => ImageStateReturns the state of the image |
getSelectionState | () => SelectionStateReturns the state of the selection |