Skip to content

ImageCropper

Image cropping component with preview and file selection.

Also known as: avatar upload, image editor, photo crop, profile picture upload

View in Storybook

When to use

  • Uploading and cropping images (avatars, profile pictures). Provides crop preview and configurable aspect ratio.

Import

tsx
import { ImageCropper } from '@vacano/ui'

Usage

tsx
const [image, setImage] = useState<string | null>(null)

<ImageCropper
  value={image}
  onCrop={(result) => setImage(result.dataUrl)}
/>

Custom Button Label

tsx
<ImageCropper
  value={image}
  onCrop={handleCrop}
  buttonLabel="Upload Photo"
/>

Preview Size

tsx
<ImageCropper
  value={image}
  onCrop={handleCrop}
  previewSize={150}
/>

Crop Options

Configure the cropping behavior:

tsx
<ImageCropper
  value={image}
  onCrop={handleCrop}
  aspectRatio={1}        // Square crop
  minWidth={100}
  minHeight={100}
/>

Crop Result

The onCrop callback receives a result object:

tsx
type ImageCropperResult = {
  dataUrl: string    // Base64 data URL
  blob: Blob         // Image blob
  width: number      // Cropped width
  height: number     // Cropped height
}

Full Example

tsx
function AvatarUploader() {
  const [avatar, setAvatar] = useState<string | null>(null)

  const handleCrop = (result: ImageCropperResult) => {
    setAvatar(result.dataUrl)
    // Upload to server
    uploadAvatar(result.blob)
  }

  return (
    <div>
      <h3>Profile Picture</h3>
      <ImageCropper
        value={avatar}
        onCrop={handleCrop}
        buttonLabel="Choose Photo"
        previewSize={120}
        aspectRatio={1}
      />
    </div>
  )
}

Props

PropTypeDefaultDescription
valuestring | null-Current image URL
onCrop(result: ImageCropperResult) => void-Crop complete callback
buttonLabelstring-Upload button label
previewSizenumber-Preview image size
aspectRationumber-Crop aspect ratio
minWidthnumber-Minimum crop width
minHeightnumber-Minimum crop height
classNamestring-CSS class name
classnamesImageCropperClassNames-Custom class names

ClassNames

KeyDescription
buttonUpload button
previewImage preview

Released under the MIT License.