Files
ImageCropper
Choose part of an image before uploading it, such as a square avatar. Move and resize with the pointer or the keyboard, pick an aspect ratio and rotate in quarter turns.
import '@reactloom/files/styles.css';
import { ImageCropper, cropImage } from '@reactloom/files';Examples
Square avatar
cropImage turns the crop into a Blob (here 256 px wide) for upload or preview.
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.
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
| Prop | Type | Default |
|---|---|---|
src · altrequiredThe image and its description. | string | — |
aspectWidth / height of the crop. | number | 'free' | 'free' |
aspectOptionsAspect choices above the image. | { label, value }[] | — |
crop / defaultCrop / onCropChangeFractions (0–1) of the (rotated) image. | { x, y, width, height } | — |
rotatable · rotation / defaultRotation / onRotationChangeQuarter-turn rotation buttons. | boolean · 0 | 90 | 180 | 270 | true |
minSizeSmallest crop, as a fraction of each side. | number | 0.05 |
messagesNames, instructions and the announcement. | Partial<ImageCropperMessages> | — |
| Prop | Type | Default |
|---|---|---|
options.type · qualityOutput image type and quality. | string · number | 'image/png' |
options.maxWidthScale the result down to this width. | number | — |
returnsThe 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
| Variable | Default |
|---|---|
--pl-cropper-area-color | #fff |
--pl-cropper-bg | var(--pl-color-bg-muted) |
--pl-cropper-radius | var(--pl-radius-md) |
--pl-cropper-shade | rgb(0 0 0 / 0.5) |
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[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.
| Key | Action |
|---|---|
| ← → ↑ ↓ | Move the crop area |
| Shift + arrows | Resize it |
| Alt + arrows | Larger 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.