Crop frame you drag and zoom over a picture; on Save it rounds into a ball and flies into the avatar slot, squashing on landing.
DragDrag
Profile
How teammates see you across the workspace.
MV
120%
avatar-cropper · , lively
avatar-cropper-demo.tsx
When to use
Use when a person picks a picture and frames it by dragging and zooming before it becomes an avatar. It hands you the cropped Blob; the upload stays yours. Presets: inline, dialog, sheet.
When not to use
For a free-form image editor with filters use a dedicated editor. To attach files without framing them use Dropzone.
Use it wherever a person frames a picture that becomes an avatar: the profile photo in account settings, a
workspace icon, a team logo. It covers pick, frame and save; the upload stays yours (onSave hands you the
cropped Blob, a data URL and the crop area). For a free-form image editor with aspect presets and filters,
use a dedicated editor; for attaching files without framing them, use the dropzone.
The crop frame is a tile over the picture. Drag the picture (1:1 with the pointer, pinch on touch, wheel over
the stage); at the pan limit the picture stops and the frame squashes along the drag axis, as a wall would.
Press Save and the frame grips: its rim thickens and the tile tightens (charge). Release: the frame
rounds into a ball, a snapshot of the framed pixels detaches, shrinks toward the slot's size while it is
still over the stage and flies into the avatar slot without crossing the text around it (action), then
covers the old avatar and lands with a squash against the slot's rim, and "Photo updated" rises below the
slot (settle).
import { AvatarCropper } from "@/components/ui/avatar-cropper"export function ProfilePhoto({ file }: { file: File }) { return ( <AvatarCropper src={file} fallback="MV" onSave={async ({ blob, area }) => { await upload(blob) // or send `area` and crop on the server }} > {/* your fields, shown next to the slot */} </AvatarCropper> )}
src takes a URL (same origin or served with CORS, so the canvas can export it) or a File. The crop is
{ x, y, zoom, rotation }: x/y are the picture's offset from the frame's centre as a fraction of the
frame, zoom 1 just covers the frame. Use value / onChange to control it, defaultValue otherwise.
shape sets the landing shape in the slot (circle, rounded 12 px, square 4 px), aspect the frame's
ratio, minZoom / maxZoom the zoom range and frameSize the frame's width (it shrinks on narrow stages).
Three layout presets share one look and one motion:
<AvatarCropper preset="inline" /> {/* slot and fields beside the cropper, in a settings card */}<AvatarCropper preset="dialog" /> {/* the slot is a button that grows into the cropper panel */}<AvatarCropper preset="sheet" /> {/* a bottom sheet with the slot in its header, for phones */}
For your own layout, compose the parts inside AvatarCropperRoot, or pass slotRef to land the flight on
an avatar element of yours. actionsRef exposes pan, zoomTo, rotate and save for guided tours.
The dialog preset with shape="rounded": the icon is the trigger. Pressing it sinks it and grows it into
the cropper panel (the Morphing Dialog); Save lands the picture in the icon and the panel contracts back
into it.
The stage is a focusable role="img" with aria-roledescription="crop area" and the label "Photo, drag
to reposition"; it is moved with the keyboard as with the pointer. Nothing is written over the picture.
The zoom is a role="slider" (aria-valuetext "150%"), the thumb 20 px inside a 24 px tall hit area.
Rotate, Remove, Cancel and Save are real buttons (36 px).
"Photo updated" is a polite live region under the slot. The slot is a labelled image (labels.slot); in
the dialog preset it is the dialog's trigger button, with focus trap, Escape and focus return from the
Morphing Dialog.
Focus rings are ring-[3px] ring-ring/50 on the stage, the thumb and every button. The scrim is 40%, so
the cut-away part of the picture stays recognisable; the frame's rim is white with a dark hairline so it
reads on any picture in both themes.
Touch: one finger pans, two fingers pinch; the slider is the fallback.
With reduced motion (the OS setting, or MotionKitProvider reducedMotion="always") there is no grip, squash
or flight. The frame rounds in a short fade and rounds back, the slot crossfades from the old avatar to the
new picture and the status fades in. Pan and zoom still follow the pointer; keyboard moves fade-glide.
The flying copy is a pre-rendered snapshot (the export canvas, outputSize px wide, so it stays sharp at
the frame's size) in one fixed element in a portal: never the live picture re-clipped per frame, never
clipped by a scrolling parent. Its position, scale and radius are animated; its rim is counter-scaled.
The picture moves by transform only (translate, scale, rotate). Pan limits are computed once per
change of zoom, rotation or frame size. Wheel zoom listens only on the stage (non-passive, so the page does
not scroll under it).
Nothing runs at rest: no loops, no timers. Timers and object URLs (for File sources) are cleaned up.
Size: about 15.7 kB gzip with every registry part it pulls. The cropper itself (with ball, beats,
morph, motion-kit and the canvas export) is about 8.2 kB; the other 7.5 kB are the morphing-dialog
used by the dialog preset. It is imported statically, so every preset pays it, inline and sheet
included. motion and Radix are npm peers and not counted.
Technical sheet
Motion
calm
260 ms · bounce 0
lively
400 ms · bounce 0.3
playful
540 ms · bounce 0.45
Main spring of each temperament. Set it with the temperament prop or MotionKitProvider; the default is lively.
Size
Not measured yet · budget 17.0 kB gzip
Component and MotionKit helpers; excludes react, react-dom, motion.
Dependencies
@radix-ui/react-dialog
@radix-ui/react-focus-scope
aria-hidden
motion · peer
Triggers
drag, press, click, a value change
Reduced motion
No grip, squash or flight: the frame rounds in a short fade, the slot crossfades to the new picture and the status fades in.
Keyboard
Arrow keys (on the picture)
Pans the picture 8 px (Shift: 32 px)
+ / -
Zooms in or out one step
R
Rotates the picture 90 degrees
ArrowLeft / ArrowRight (on the zoom slider)
Zooms one step (PageUp / PageDown: five steps)
Home / End
Minimum or maximum zoom
Enter / Space (on Save)
Grips the frame, then saves on release
Tab
Moves from the picture to the slider and the actions
Browsers
Last 2 versions of Chrome, Edge, Firefox and Safari (iOS 16.4+)