Examples

Square avatar

cropImage turns the crop into a Blob (here 256 px wide) for upload or preview.

Preview
Portrait placeholder
Arrow keys move the crop area; Shift + arrow keys resize it. Hold Alt for larger steps.
Example.tsx
import { ImageCropper, cropImage, type CropArea, type CropRotation } from '@reactloom/files';
import { Button, Image, Stack } from '@reactloom/ui';
import { useState } from 'react';

const photo =
  'data:image/svg+xml;utf8,' +
  encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="400" height="300" viewBox="0 0 400 300"><rect width="400" height="300" fill="#6b8cae"/><circle cx="200" cy="130" r="60" fill="#f2d4b6"/><rect x="110" y="200" width="180" height="100" rx="40" fill="#2f4858"/></svg>');

export default function Example() {
  const [crop, setCrop] = useState<CropArea>({ x: 0.25, y: 0.1, width: 0.5, height: 0.67 });
  const [rotation, setRotation] = useState<CropRotation>(0);
  const [preview, setPreview] = useState<string | null>(null);
  return (
    <Stack gap="3" style={{ width: '100%', maxWidth: 420 }}>
      <ImageCropper
        src={photo}
        alt="Portrait placeholder"
        aspect={1}
        aspectOptions={[{ label: 'Square', value: 1 }, { label: '4:3', value: 4 / 3 }, { label: 'Free', value: 'free' }]}
        crop={crop}
        onCropChange={setCrop}
        rotation={rotation}
        onRotationChange={setRotation}
      />
      <Button
        style={{ alignSelf: 'start' }}
        onClick={async () => {
          const blob = await cropImage(photo, crop, rotation, { maxWidth: 256 });
          setPreview(URL.createObjectURL(blob));
        }}
      >
        Use this crop
      </Button>
      {preview && <Image src={preview} alt="Cropped avatar" aspectRatio={1} width={96} radius="full" />}
    </Stack>
  );
}

Free crop

Without aspect presets or rotation.

Preview
Mountain banner placeholder
Arrow keys move the crop area; Shift + arrow keys resize it. Hold Alt for larger steps.
Example.tsx
import { ImageCropper } from '@reactloom/files';

const photo =
  'data:image/svg+xml;utf8,' +
  encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="600" height="300" viewBox="0 0 600 300"><rect width="600" height="300" fill="#2f4858"/><path d="M0 260 L150 140 L260 220 L400 90 L600 250 V300 H0Z" fill="#86bbd8"/></svg>');

export default function Example() {
  return (
    <div style={{ width: '100%', maxWidth: 520 }}>
      <ImageCropper src={photo} alt="Mountain banner placeholder" rotatable={false} />
    </div>
  );
}

API reference

ImageCropper props
PropTypeDefault
src · altrequired
The image and its description.
string—
aspect
Width / height of the crop.
number | 'free''free'
aspectOptions
Aspect choices above the image.
{ label, value }[]—
crop / defaultCrop / onCropChange
Fractions (0–1) of the (rotated) image.
{ x, y, width, height }—
rotatable · rotation / defaultRotation / onRotationChange
Quarter-turn rotation buttons.
boolean · 0 | 90 | 180 | 270true
minSize
Smallest crop, as a fraction of each side.
number0.05
messages
Names, instructions and the announcement.
Partial<ImageCropperMessages>—
cropImage(src, crop, rotation?, options?)
PropTypeDefault
options.type · quality
Output image type and quality.
string · number'image/png'
options.maxWidth
Scale the result down to this width.
number—
returns
The image must be same-origin or allow CORS.
Promise<Blob>—

Customization

Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.

CSS variables

VariableDefault
--pl-cropper-area-color#fff
--pl-cropper-bgvar(--pl-color-bg-muted)
--pl-cropper-radiusvar(--pl-radius-md)
--pl-cropper-shadergb(0 0 0 / 0.5)

State attributes

Target these in your CSS to style states without depending on class names.

AttributeValues
[data-corner]present when on

Theme tokens it reads

--pl-color-accent-solid --pl-color-bg-muted --pl-focus-ring-color --pl-focus-ring-width --pl-radius-md

Accessibility

  • The crop area is one tab stop with instructions; every keyboard change is announced as a position and size.
  • Corner handles are pointer-only; the keyboard does the same with Shift + arrow keys.
Keyboard interactions
KeyAction
← → ↑ ↓Move the crop area
Shift + arrowsResize it
Alt + arrowsLarger steps

Usage guidelines

Do

  • Use an aspect preset for fixed shapes such as avatars and banners.

Don’t

  • Don’t crop away information people need; keep the original available.