Image Cropper
Chooses the part of an image to keep, by resizing a selection or by panning and zooming under a frame. Built on ui-sdk’s Image Cropper.
Usage
import {
ImageCropper,
ImageCropperHandles,
ImageCropperImage,
ImageCropperSelection,
ImageCropperViewport,
} from 'nyctoui/image-cropper'
<ImageCropper aria-label="Cover image" src={src} aspectRatio={16 / 9}>
<ImageCropperViewport>
<ImageCropperImage alt="Your cover image" />
<ImageCropperSelection />
<ImageCropperHandles />
</ImageCropperViewport>
</ImageCropper>The crop is a box in the image’s own pixels, with its rotation and flip. Read it from onCropCommit, or control it with crop and onCropChange, and pass it to cropImage, exported here too, for a file. The selection and handles move with the arrow keys (Shift for ten times as far), + and − zoom, R rotates, and Escape puts a drag back. Thirds show over the selection while it is dragged.
Good for
- Cropping a profile photo to a circle before upload.
- Framing a cover image for a blog post or a store banner.
- Trimming a product photo to a square in a marketplace listing.
- Choosing the part of a scanned document to keep in an expense app.
Anatomy
<ImageCropper>
<ImageCropperViewport>
<ImageCropperImage />
<ImageCropperSelection />
<ImageCropperHandles />
</ImageCropperViewport>
<ImageCropperControls>
<ImageCropperZoom />
<ImageCropperRotate />
<ImageCropperFlip />
</ImageCropperControls>
</ImageCropper>Examples
Avatar
mode="frame" keeps the selection still and pans the image under it; drag it, scroll or pinch to zoom. shape="circle" rounds the frame. The crop is still a box, so pass the shape to cropImage as well.
Best practices
- Set
aspectRatiowhen the result has a fixed shape, like a square avatar or a 16:9 cover. - Use the circle
shapefor avatars, so people see how the photo will look. - Save from
onCropCommit, which fires once a drag ends, rather than on every move. - Give the image real
alttext, and keep the zoom and rotate controls for people who can’t drag.
- Crop in the browser and throw the original away. Keep it, so people can crop again.
- Use a cropper when any shape is fine. Let people upload the image as it is.
API reference
ImageCropper
Holds the crop and the image’s src. Renders a <div role=group>.
| Prop | Type | Default |
|---|---|---|
srcThe image URL, such as an object URL for a picked file. | string | — |
modeResize a selection, or pan and zoom the image under a fixed frame. | 'box' | 'frame' | 'box' |
aspectRatioKeeps the crop’s width over its height. Free without one. | number | — |
shapeThe selection’s shape. | 'rectangle' | 'circle' | 'rectangle' |
defaultCrop / cropThe crop, uncontrolled or controlled. | ImageCrop | — |
onCropChangeCalled on every step of a drag, and once the image loads. | (crop, details) => void | — |
onCropCommitCalled once an interaction ends. For saving. | (crop, details) => void | — |
minWidth / minHeightThe smallest crop, in image pixels. | number | — |
maxZoomHow far the crop magnifies. | number | 3 |
disabledStops every change. | boolean | false |
controllerFrom createImageCropperController(), to set the crop from elsewhere. | ImageCropperController | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ImageCropperViewport
The well the image shows in. Fills its container’s width. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ImageCropperImage
The image. Renders an <img>.
| Prop | Type | Default |
|---|---|---|
altDescribes the image. | string | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ImageCropperSelection
The crop over the image, with the outside dimmed. Renders a <div role=slider>.
| Prop | Type | Default |
|---|---|---|
gridDraws thirds while dragging. | boolean | true |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ImageCropperHandles
All the resize handles. Draws nothing in frame mode.
| Prop | Type | Default |
|---|---|---|
handlesEight handles, or the four corners. | 'all' | 'corners' | 'all' |
ImageCropperHandle
One resize handle, for a custom set.
| Prop | Type | Default |
|---|---|---|
positionThe edge or corner it resizes. | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw' | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ImageCropperControls
A row under the viewport. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ImageCropperZoom
A Slider from 1 to maxZoom, with a zoom out and zoom in button at its ends.
| Prop | Type | Default |
|---|---|---|
buttonsShows the buttons. | boolean | true |
aria-labelNames the slider. | string | 'Zoom' |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
ImageCropperRotate / ImageCropperFlip
Ghost icon Buttons. Rotate turns a quarter clockwise; Flip mirrors the image.
| Prop | Type | Default |
|---|---|---|
directionOn Flip: which way it mirrors. | 'horizontal' | 'vertical' | 'horizontal' |
sizeButton size. | 'sm' | 'md' | 'lg' | 'sm' |
childrenA text label in place of the icon. | ReactNode | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |