Skip to content
Haze UI

Avatar upload

Select a real profile image, preview and crop it locally, then submit the prepared PNG through a native file input.

pnpm dlx shadcn@latest add @haze/avatar-upload
Open preview in a new tab
Livesmall script

When to use

  • A profile form needs a local image preview and a square crop before submission.
  • Zoom and crop position should have native keyboard accessible controls.

When not to use

  • Use file-upload when multiple files or nonimage attachments are accepted.
  • Use avatar when the photo is display only.

Installation

Add the Haze registry to components.json once, then install Avatar upload with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.

pnpm dlx shadcn@latest add @haze/avatar-upload
First time? Registry and theme setup

1. Add to components.json:

{
  "registries": {
    "@haze": "http://localhost:4329/r/{name}.json"
  }
}

2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";

API reference

PropTypeDefaultDescription
id*string–Unique caller supplied prefix for the control and its associated content.
namestringidName submitted by the native form control.
labelstringProfile photoLegend for the photo field and crop controls.
initialsstringMLFallback initials shown without an image.
srcstring–Optional previously saved photo to show before selecting a new file.
maxBytesnumber5242880Maximum accepted local file size.
disabledbooleanfalsePrevents user changes through the native disabled state.
requiredbooleanfalseRequires a new file through native form validation.
classstring–Additional classes on this component's outer wrapper.
[key: string]unknown–Additional native file input attributes.

Keyboard interactions

EnterSpace on the file input
Open the operating system file chooser.
Arrow keys on crop controls
Adjust zoom or crop position.
Tab, EnterSpace
Apply, cancel or remove the photo.

Accessibility

  • A labelled native file input remains visible and accepts PNG, JPEG and WebP; the preview image is decorative.
  • Zoom plus horizontal and vertical crop positions are native range inputs, so cropping has a complete keyboard path.
  • Type and size errors are reported in text and through native validity; no upload or progress is simulated.
  • Applying the crop prepares a 256 pixel PNG file in the file input and emits haze:avatar-change; submission and persistence belong to the caller's form.
  • Replacement, cancellation, reset and Astro page swaps revoke temporary object URLs. If the browser cannot replace the selected file, the original remains selected and the limitation is stated.

Notes for AI

Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.

  • Install and use

    Install the Haze UI component "Avatar upload" with `npx shadcn@latest add @haze/avatar-upload`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import AvatarUpload from "@/components/haze/ui/avatar-upload.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/avatar-upload.md

  • Customize with real content

    Adapt the Haze avatar-upload ui for our product. Replace the demo values of these props with our real content: id, name, label, initials, src, maxBytes, disabled, required, class, [key: string]. Keep the layout, the heading level rules and these accessibility details: A labelled native file input remains visible and accepts PNG, JPEG and WebP; the preview image is decorative. Zoom plus horizontal and vertical crop positions are native range inputs, so cropping has a complete keyboard path. Type and size errors are reported in text and through native validity; no upload or progress is simulated. Applying the crop prepares a 256 pixel PNG file in the file input and emits haze:avatar-change; submission and persistence belong to the caller's form. Replacement, cancellation, reset and Astro page swaps revoke temporary object URLs. If the browser cannot replace the selected file, the original remains selected and the limitation is stated. Do not add raw color classes; use the Haze tokens.

  • Choose the right block

    Before using avatar-upload, check it fits: use it when a profile form needs a local image preview and a square crop before submission.; zoom and crop position should have native keyboard accessible controls.. Avoid it when use file-upload when multiple files or nonimage attachments are accepted.; use avatar when the photo is display only.. Alternatives: @haze/file-upload, @haze/avatar, @haze/slider.

Dependencies

  • @haze/slider
  • @haze/utils
  • @haze/theme (one-time setup)

Source · Original Haze UI work.

Catalog

↑↓ moveEnter openEsc close

Keyboard shortcuts

CtrlK
Command palette
/
Focus search
←↑→↓
Move between cards
jk
Next / previous card
ci
Copy code / install command (detail page)
[]
Previous / next entry
?
This help
Esc
Close dialog