Components

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

Do
  • Set aspectRatio when the result has a fixed shape, like a square avatar or a 16:9 cover.
  • Use the circle shape for 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 alt text, and keep the zoom and rotate controls for people who can’t drag.
Don’t
  • 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>.

PropTypeDefault
src
The image URL, such as an object URL for a picked file.
string—
mode
Resize a selection, or pan and zoom the image under a fixed frame.
'box' | 'frame''box'
aspectRatio
Keeps the crop’s width over its height. Free without one.
number—
shape
The selection’s shape.
'rectangle' | 'circle''rectangle'
defaultCrop / crop
The crop, uncontrolled or controlled.
ImageCrop—
onCropChange
Called on every step of a drag, and once the image loads.
(crop, details) => void—
onCropCommit
Called once an interaction ends. For saving.
(crop, details) => void—
minWidth / minHeight
The smallest crop, in image pixels.
number—
maxZoom
How far the crop magnifies.
number3
disabled
Stops every change.
booleanfalse
controller
From createImageCropperController(), to set the crop from elsewhere.
ImageCropperController—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ImageCropperViewport

The well the image shows in. Fills its container’s width. Renders a <div>.

PropTypeDefault
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ImageCropperImage

The image. Renders an <img>.

PropTypeDefault
alt
Describes the image.
string—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ImageCropperSelection

The crop over the image, with the outside dimmed. Renders a <div role=slider>.

PropTypeDefault
grid
Draws thirds while dragging.
booleantrue
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ImageCropperHandles

All the resize handles. Draws nothing in frame mode.

PropTypeDefault
handles
Eight handles, or the four corners.
'all' | 'corners''all'

ImageCropperHandle

One resize handle, for a custom set.

PropTypeDefault
position
The edge or corner it resizes.
'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ImageCropperControls

A row under the viewport. Renders a <div>.

PropTypeDefault
sx
StyleX 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.

PropTypeDefault
buttons
Shows the buttons.
booleantrue
aria-label
Names the slider.
string'Zoom'
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

ImageCropperRotate / ImageCropperFlip

Ghost icon Buttons. Rotate turns a quarter clockwise; Flip mirrors the image.

PropTypeDefault
direction
On Flip: which way it mirrors.
'horizontal' | 'vertical''horizontal'
size
Button size.
'sm' | 'md' | 'lg''sm'
children
A text label in place of the icon.
ReactNode—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—