Drag & Drop Upload
Dropzone with per-file progress and a drag counter that fixes the flicker every naive dragleave handler produces.
Framing a profile photo with keyboard-operable zoom and position, showing what gets trimmed rather than hiding it behind the mask.
components/avatar-crop-upload.tsxnpx hoverlab add avatar-crop-upload
Or over MCP, from your editor's agent — no account needed.
Free to read, copy and install, for personal and non-commercial projects. Shipping it in client work or a paid product needs Pro ($79 once). The source lands in your repo and stops being ours — no attribution, nothing to upgrade.
Start a page with this section — add more, order them, and leave with the page source.
Drag the sliders until your face fills the circle. Everything outside it is trimmed.
Preview · 512 × 512 PNG
Rendered live in your current theme — this is the same component whose source is below, not a screenshot of it.
'use client'
/**
* <AvatarCropUpload> — pick a photo, frame it, save it.
*
* The upload blocks in this catalog handle files as files: a dropzone, a
* queue, a progress bar. An avatar is a different job that happens to start
* the same way. Nobody's photo is square, the interesting part of it is
* rarely in the middle, and a product that centre-crops whatever it is
* given produces a wall of members photographed from the chin down.
*
* So the framing is the block, and the upload is one line of it.
*
* WHY THE MASK IS A RING AND NOT A CLIPPED IMAGE
*
* The image is drawn in full and a ring is laid over it, dimming everything
* outside the circle rather than hiding it. Cropping tools that show only
* the kept region make people frame blind — you cannot tell whether the top
* of someone's head is just outside the circle or was never in the file.
* Seeing the discarded part is what makes one drag enough.
*
* WHY ZOOM IS A RANGE INPUT
*
* A range is the only zoom control that is keyboard-operable without extra
* work: arrows step it, Home and End reach the bounds, and it announces its
* value. Pinch and scroll-to-zoom are additions to this, never replacements
* for it, and a crop tool that can only be driven by a mouse locks people
* out of setting their own face.
*
* WHAT IS SIMPLIFIED, SAID PLAINLY
*
* Position is two sliders rather than a drag, and the output is the
* transform values rather than a canvas-rendered file. Dragging needs
* pointer capture and bounds maths against the scaled image, and encoding
* needs a canvas and a blob — both are real work with real edge cases, and
* both would bury the part worth copying. The three numbers this produces
* are exactly what a `<canvas>` `drawImage` call or a server-side resize
* needs, so the remaining step is arithmetic rather than design.
*
* The sample photograph is an inline SVG data URI. Every artifact in this
* catalog has to render with no network and no asset pipeline, and an
* off-centre subject is the whole point of the demo — a centred one would
* make the controls look unnecessary.
*/
import * as React from 'react'
import { Camera, RotateCcw, Upload } from 'lucide-react'
export interface AvatarCropUploadProps {
heading?: string
description?: string
/** Any image URL. Defaults to a self-contained SVG portrait. */
imageSrc?: string
/** Rendered size of the circular frame, in pixels. */
frameSize?: number
onSave?: (crop: { zoom: number; x: number; y: number }) => void
className?: string
}
/*
A deliberately off-centre subject: the head sits high and left, so a
centre crop clips it and the controls have something to fix.
*/
const SAMPLE_PORTRAIT =
'data:image/svg+xml;utf8,' +
encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 320">
<defs>
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="#bae6fd"/><stop offset="100%" stop-color="#e0f2fe"/>
</linearGradient>
<linearGradient id="coat" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="#3f4d63"/><stop offset="100%" stop-color="#1e293b"/>
</linearGradient>
</defs>
<rect width="320" height="320" fill="url(#bg)"/>
<circle cx="252" cy="66" r="30" fill="#fde68a" opacity="0.75"/>
<path d="M126 246c62 0 112 34 112 74H14c0-40 50-74 112-74Z" fill="url(#coat)"/>
<path d="M104 196h44v56h-44z" fill="#eab894"/>
<circle cx="126" cy="132" r="74" fill="#f5d0b0"/>
<path d="M52 130a74 74 0 0 1 148-8c4-44-30-72-74-72s-78 32-74 80Z" fill="#6b4226"/>
<circle cx="102" cy="138" r="6" fill="#3f2a1c"/>
<circle cx="152" cy="138" r="6" fill="#3f2a1c"/>
<path d="M108 170c10 9 28 9 38 0" stroke="#b9795a" stroke-width="6" fill="none" stroke-linecap="round"/>
</svg>`,
)
const MIN_ZOOM = 1
const MAX_ZOOM = 2.5
/*
Where the sliders start, and deliberately not a perfect crop.
The subject sits up and left of centre, so these values pull the face
most of the way into the circle and leave it slightly high — a photo that
arrives already framed makes the controls look like decoration. Note the
translate percentages are in the image's own pre-scale space: a shift of
n% moves it by `n% x width x zoom` on screen, which is why these numbers
are smaller than the visible correction suggests.
*/
const DEFAULT_CROP: { zoom: number; x: number; y: number } = { zoom: 1.3, x: 7, y: 6 }
export function AvatarCropUpload({
heading = 'Profile photo',
description = 'Drag the sliders until your face fills the circle. Everything outside it is trimmed.',
imageSrc = SAMPLE_PORTRAIT,
frameSize = 176,
onSave,
className = '',
}: AvatarCropUploadProps) {
/*
Per-instance prefix for every id this block emits.
The literals these replaced were a latent duplicate the moment the
block appeared twice on one document, and `aria-labelledby` on a
duplicated id resolves to the first match -- so the second copy was
labelled by the first copy's heading. Client component, so `useId` is
the right tool.
*/
const uid = React.useId()
const [zoom, setZoom] = React.useState(DEFAULT_CROP.zoom)
const [x, setX] = React.useState(DEFAULT_CROP.x)
const [y, setY] = React.useState(DEFAULT_CROP.y)
const dirty =
zoom !== DEFAULT_CROP.zoom || x !== DEFAULT_CROP.x || y !== DEFAULT_CROP.y
function reset() {
setZoom(DEFAULT_CROP.zoom)
setX(DEFAULT_CROP.x)
setY(DEFAULT_CROP.y)
}
return (
<section
aria-labelledby={`${uid}-avatar-crop-heading`}
className={`mx-auto w-full max-w-3xl px-4 py-16 sm:px-6 lg:px-8 ${className}`}
>
<div className="rounded-2xl border border-border bg-card p-6 sm:p-8">
<h2 id={`${uid}-avatar-crop-heading`} className="text-lg font-semibold text-foreground">
{heading}
</h2>
<p className="mt-1 max-w-prose text-sm text-muted-foreground">{description}</p>
<div className="mt-6 flex flex-col gap-8 md:flex-row md:items-start">
<div className="flex flex-col items-center gap-3">
{/*
The frame. `overflow-hidden` on the outer square clips the
image to the working area; the ring inside marks the circle
that will actually be kept, with the discarded corners dimmed
rather than removed.
*/}
<div
className="relative shrink-0 overflow-hidden rounded-xl bg-muted"
style={{ width: frameSize, height: frameSize }}
>
<img
src={imageSrc}
alt="Your profile photo, positioned inside the crop frame"
className="h-full w-full object-cover"
style={{
transform: `scale(${zoom}) translate(${x}%, ${y}%)`,
transformOrigin: 'center',
}}
/>
{/* The mask: a very large spread shadow paints everything
outside the circle, which is one element instead of four
corner overlays that never quite line up.
`color-mix` rather than a slash-alpha on the token. These
tokens are complete `oklch()` colours, so `var(--background)/0.72`
is not a colour and the mask silently disappears. */}
<div
aria-hidden
className="pointer-events-none absolute inset-0 rounded-full shadow-[0_0_0_9999px_color-mix(in_oklab,var(--background)_72%,transparent)]"
/>
<div
aria-hidden
className="pointer-events-none absolute inset-0 rounded-full ring-2 ring-inset ring-primary/70"
/>
</div>
<p className="text-xs text-muted-foreground">Preview · 512 × 512 PNG</p>
</div>
<div className="min-w-0 flex-1 space-y-5">
<label className="block">
<span className="flex items-baseline justify-between text-sm font-medium text-foreground">
Zoom
<span className="font-mono text-xs text-muted-foreground">
{zoom.toFixed(2)}×
</span>
</span>
<input
type="range"
min={MIN_ZOOM}
max={MAX_ZOOM}
step={0.05}
value={zoom}
onChange={(event) => setZoom(Number(event.target.value))}
className="mt-2 h-2 w-full cursor-pointer appearance-none rounded-full bg-muted accent-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
/>
</label>
<label className="block">
<span className="flex items-baseline justify-between text-sm font-medium text-foreground">
Horizontal
<span className="font-mono text-xs text-muted-foreground">{x}%</span>
</span>
<input
type="range"
min={-40}
max={40}
value={x}
onChange={(event) => setX(Number(event.target.value))}
className="mt-2 h-2 w-full cursor-pointer appearance-none rounded-full bg-muted accent-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
/>
</label>
<label className="block">
<span className="flex items-baseline justify-between text-sm font-medium text-foreground">
Vertical
<span className="font-mono text-xs text-muted-foreground">{y}%</span>
</span>
<input
type="range"
min={-40}
max={40}
value={y}
onChange={(event) => setY(Number(event.target.value))}
className="mt-2 h-2 w-full cursor-pointer appearance-none rounded-full bg-muted accent-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
/>
</label>
<div className="flex flex-wrap items-center gap-2 pt-1">
<button
type="button"
className="inline-flex h-9 items-center gap-1.5 rounded-lg border border-border bg-background px-3 text-sm font-medium text-foreground transition hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
>
<Upload aria-hidden className="h-4 w-4" />
Replace photo
</button>
<button
type="button"
onClick={reset}
disabled={!dirty}
className="inline-flex h-9 items-center gap-1.5 rounded-lg px-3 text-sm font-medium text-muted-foreground transition hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-40"
>
<RotateCcw aria-hidden className="h-4 w-4" />
Reset
</button>
<button
type="button"
onClick={() => onSave?.({ zoom, x, y })}
className="ms-auto inline-flex h-9 items-center gap-1.5 rounded-lg bg-primary px-4 text-sm font-semibold text-primary-foreground transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
>
<Camera aria-hidden className="h-4 w-4" />
Save photo
</button>
</div>
</div>
</div>
</div>
</section>
)
}
bg-card, text-muted-foreground) — it inherits your theme instead of overriding it.Drop it at components/avatar-crop-upload.tsx and import it where you need the section:
import { AvatarCropUpload } from '@/components/avatar-crop-upload'2 of this block’s props are simple enough to drive from here. Change them and the block below re-renders — it is the same component whose source is above, not a mock of it. Everything else it accepts is in the table underneath.
Read out of the component’s own type and signature, so this cannot drift from the source below. Every prop has a default — the component renders standalone before you pass it anything.
| Prop | Type | Default |
|---|---|---|
heading | string | 'Profile photo' |
description | string | — |
imageSrcAny image URL. Defaults to a self-contained SVG portrait. | string | SAMPLE_PORTRAIT |
frameSizeRendered size of the circular frame, in pixels. | number | 176 |
onSave | (crop: { zoom: number; x: number; y: number }) => void | — |
className | string | '' |
The same block rendered once to markup, wrapped as a file your framework compiles. Tailwind classes are framework-agnostic, so the design transfers intact — the behaviour does not.
This block is interactive. The markup below is its initial state with the event handlers stripped — you will need to re-wire the behaviour in your framework.
<!--
Avatar Crop & Upload — markup from the Hoverlab catalog.
This is the block rendered once to HTML and wrapped as a component
file. It is not a port of the React source: the Tailwind classes carry
the design, which is the part that took the work, and they are the same
in every framework.
This block is interactive in React and the handlers are NOT here.
Buttons, toggles and menus render in their initial state and do
nothing until you wire them up.
-->
<section aria-labelledby="_R_0_-avatar-crop-heading" class="mx-auto w-full max-w-3xl px-4 py-16 sm:px-6 lg:px-8 ">
<div class="rounded-2xl border border-border bg-card p-6 sm:p-8">
<h2 id="_R_0_-avatar-crop-heading" class="text-lg font-semibold text-foreground">Profile photo</h2>
<p class="mt-1 max-w-prose text-sm text-muted-foreground">Drag the sliders until your face fills the circle. Everything outside it is trimmed.</p>
<div class="mt-6 flex flex-col gap-8 md:flex-row md:items-start">
<div class="flex flex-col items-center gap-3">
<div class="relative shrink-0 overflow-hidden rounded-xl bg-muted" style="width:176px;height:176px">
<img src="data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20320%20320%22%3E%0A%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%20%20%20%20%3ClinearGradient%20id%3D%22bg%22%20x1%3D%220%22%20y1%3D%220%22%20x2%3D%221%22%20y2%3D%221%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cstop%20offset%3D%220%25%22%20stop-color%3D%22%23bae6fd%22%2F%3E%3Cstop%20offset%3D%22100%25%22%20stop-color%3D%22%23e0f2fe%22%2F%3E%0A%20%20%20%20%20%20%20%20%3C%2FlinearGradient%3E%0A%20%20%20%20%20%20%20%20%3ClinearGradient%20id%3D%22coat%22%20x1%3D%220%22%20y1%3D%220%22%20x2%3D%221%22%20y2%3D%221%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cstop%20offset%3D%220%25%22%20stop-color%3D%22%233f4d63%22%2F%3E%3Cstop%20offset%3D%22100%25%22%20stop-color%3D%22%231e293b%22%2F%3E%0A%20%20%20%20%20%20%20%20%3C%2FlinearGradient%3E%0A%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%20%20%3Crect%20width%3D%22320%22%20height%3D%22320%22%20fill%3D%22url(%23bg)%22%2F%3E%0A%20%20%20%20%20%20%3Ccircle%20cx%3D%22252%22%20cy%3D%2266%22%20r%3D%2230%22%20fill%3D%22%23fde68a%22%20opacity%3D%220.75%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M126%20246c62%200%20112%2034%20112%2074H14c0-40%2050-74%20112-74Z%22%20fill%3D%22url(%23coat)%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M104%20196h44v56h-44z%22%20fill%3D%22%23eab894%22%2F%3E%0A%20%20%20%20%20%20%3Ccircle%20cx%3D%22126%22%20cy%3D%22132%22%20r%3D%2274%22%20fill%3D%22%23f5d0b0%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M52%20130a74%2074%200%200%201%20148-8c4-44-30-72-74-72s-78%2032-74%2080Z%22%20fill%3D%22%236b4226%22%2F%3E%0A%20%20%20%20%20%20%3Ccircle%20cx%3D%22102%22%20cy%3D%22138%22%20r%3D%226%22%20fill%3D%22%233f2a1c%22%2F%3E%0A%20%20%20%20%20%20%3Ccircle%20cx%3D%22152%22%20cy%3D%22138%22%20r%3D%226%22%20fill%3D%22%233f2a1c%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M108%20170c10%209%2028%209%2038%200%22%20stroke%3D%22%23b9795a%22%20stroke-width%3D%226%22%20fill%3D%22none%22%20stroke-linecap%3D%22round%22%2F%3E%0A%20%20%20%20%3C%2Fsvg%3E" alt="Your profile photo, positioned inside the crop frame" class="h-full w-full object-cover" style="transform:scale(1.3) translate(7%, 6%);transform-origin:center" />
<div aria-hidden="true" class="pointer-events-none absolute inset-0 rounded-full shadow-[0_0_0_9999px_color-mix(in_oklab,var(--background)_72%,transparent)]"></div>
<div aria-hidden="true" class="pointer-events-none absolute inset-0 rounded-full ring-2 ring-inset ring-primary/70"></div>
</div>
<p class="text-xs text-muted-foreground">Preview · 512 × 512 PNG</p>
</div>
<div class="min-w-0 flex-1 space-y-5">
<label class="block">
<span class="flex items-baseline justify-between text-sm font-medium text-foreground">Zoom<span class="font-mono text-xs text-muted-foreground">1.30×</span></span>
<input type="range" min="1" max="2.5" step="0.05" class="mt-2 h-2 w-full cursor-pointer appearance-none rounded-full bg-muted accent-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background" value="1.3" />
</label>
<label class="block">
<span class="flex items-baseline justify-between text-sm font-medium text-foreground">Horizontal<span class="font-mono text-xs text-muted-foreground">7%</span></span>
<input type="range" min="-40" max="40" class="mt-2 h-2 w-full cursor-pointer appearance-none rounded-full bg-muted accent-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background" value="7" />
</label>
<label class="block">
<span class="flex items-baseline justify-between text-sm font-medium text-foreground">Vertical<span class="font-mono text-xs text-muted-foreground">6%</span></span>
<input type="range" min="-40" max="40" class="mt-2 h-2 w-full cursor-pointer appearance-none rounded-full bg-muted accent-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background" value="6" />
</label>
<div class="flex flex-wrap items-center gap-2 pt-1">
<button type="button" class="inline-flex h-9 items-center gap-1.5 rounded-lg border border-border bg-background px-3 text-sm font-medium text-foreground transition hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-upload h-4 w-4" aria-hidden="true"><path d="M12 3v12"></path><path d="m17 8-5-5-5 5"></path><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path></svg>Replace photo</button>
<button type="button" disabled="" class="inline-flex h-9 items-center gap-1.5 rounded-lg px-3 text-sm font-medium text-muted-foreground transition hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-40"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-rotate-ccw h-4 w-4" aria-hidden="true"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"></path><path d="M3 3v5h5"></path></svg>Reset</button>
<button type="button" class="ms-auto inline-flex h-9 items-center gap-1.5 rounded-lg bg-primary px-4 text-sm font-semibold text-primary-foreground transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-camera h-4 w-4" aria-hidden="true"><path d="M14.5 4h-5L7 7H4a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-3l-2.5-3z"></path><circle cx="12" cy="13" r="3"></circle></svg>Save photo</button>
</div>
</div>
</div>
</div>
</section>
What each framework gets across the whole catalog — effects convert properly; this rung is markup.
The component, its props, the design tokens it expects and the command that installs it — as one prompt. Paste it into Claude, Cursor, v0 or ChatGPT and what they build around it will match the rest of the catalog instead of inventing its own system.
Want the whole screen instead of this one section? Open a page and copy it entire.
All of them free to read, copy and install — no account, no locked tiles, no watermarked preview. The whole catalog is open, and so are the API and the CLI.
Browse File UploadCopying the code is free. Putting it in client work or a paid product is what Pro is for — the licence, not the access.
Dropzone with per-file progress and a drag counter that fixes the flicker every naive dragleave handler produces.
The list that follows the dropzone: complete, uploading, queued and failed rows with cancel and retry — because hiding the failure is the classic upload bug.
One big file on a connection that will drop: a bar allowed to go backwards when a chunk is retried, a time estimate that is a range or nothing, and a promise about closing the tab made before it is needed.
The screen between "file uploaded" and "data imported": source columns mapped to schema fields, with a sample value under each and required fields blocking the button.
An upload form that states the constraints before the file is chosen, rather than rejecting it after a two-minute transfer.
The import path for a file that is already on the internet, which does not need to travel through the browser at all.