Logo upload
Choose or drop a PNG, JPEG or WebP and get back a small data URI. The image is checked, shrunk and re-encoded in the browser to fit a size limit.
View as Markdown for coding assistants, or see llms.txt.
- New
- Group: Inputs
- Added in @azmr/ui 0.14.0
Usage
import { LogoUpload } from "@azmr/ui/logo-upload";PNG, JPEG or WebP. It is shrunk in your browser to fit.
Harbour & Pine logo: none yet
<div className="flex w-full max-w-md flex-col gap-4">
<Field
label="Company logo"
description="PNG, JPEG or WebP. It is shrunk in your browser to fit."
>
{(control) => <LogoUpload {...control} value={logo} onValueChange={setLogo} />}
</Field>
<p className="text-sm text-muted-foreground">
Harbour & Pine logo:{" "}
{logo
? `${logo.length.toLocaleString()} of ${LIMIT.toLocaleString()} characters`
: "none yet"}
</p>
</div>LogoUpload lets someone choose a file or drop one on the box, shows a preview, and lets them replace or remove it. It returns a string through onValueChange: a data: URI of a small image. It does not save anything. Storing the string, and showing it later, is your app's job.
Put it in a Field with a function child, so the label, description and error are joined to it:
<Field label="Company logo" error={errors.logo}>
{(control) => <LogoUpload {...control} value={logo} onValueChange={setLogo} />}
</Field>Use value and onValueChange for a controlled upload, or defaultValue to let the component keep the value itself. Passing value at all makes it controlled, even as undefined or null, so state that starts empty (useState<string>()) can be passed straight back. onValueChange(undefined) means the logo was removed. Add name to carry the value in a native form post through a hidden input.
What it does to the file
Most logo exports and every phone photo are far bigger than a theme, a profile or an API field can hold. The component does the shrinking, so the person never has to:
- It reads the file's first bytes to see what it is. The extension and the type the browser reports are never trusted, so a
logo.pngthat is really something else is refused. - It refuses a file over 10 MB before reading it. It reads the image's own header, and refuses one that says it is over 40 million pixels before decoding it, so a tiny file that describes a huge picture costs nothing. It then decodes the image to prove it is one, and checks the decoded size too, for a file whose header could not be read.
- It draws the image onto a canvas and re-encodes it as WebP (or PNG, in a browser that cannot encode WebP), trying a longest side of 512, then 384, 256, 192, 128, 96 and 64 pixels, and four qualities at each size, until the result fits
maxChars. Transparency is kept and a small image is never made bigger.
If nothing fits, the component says so and keeps the logo the person already had. It never fetches a URL to read or upload an image. (A value you pass in that is an https: URL is shown in the preview, which makes the browser load it.)
SVG is refused. An SVG can carry script, so it is never read, drawn or stored. Ask for a PNG, JPEG or WebP; if a logo must be SVG, your app should render it from its own files.
maxChars is the most characters the data URI may have. It defaults to 32,768, the limit the @azmr/ui theme accepts for an image. Set it to your own storage limit.
Errors
A refused file shows its message under the controls, in an alert, and calls onError with a code. The code is the stable part; change the words with errors.
| Code | Default message |
|---|---|
not-an-image | That file is not a PNG, JPEG or WebP image. |
svg | SVG logos cannot be used here. Export your logo as a PNG, JPEG or WebP. |
file-too-large | That file is larger than 10 MB. Choose a smaller image. |
too-many-pixels | That image is too large to open. Choose a smaller one. |
cannot-fit | This logo cannot be made small enough. Try a simpler image. |
unsupported-browser | This browser cannot prepare images. Try a current browser. |
The error clears, and onError(undefined) is called, when a later file works or the logo is removed.
onBusyChange(true) is called when a file starts being prepared and onBusyChange(false) when the latest choice is done (fitted, refused or failed) the logo is removed, or the control leaves the page mid-preparation. A file that finishes after disabled turned on is dropped, never applied. Use it to keep a Save button off until the logo is ready.
With react-hook-form
<FormField
name="logo"
label="Company logo"
render={({ value, onChange, control }) => (
<LogoUpload {...control} value={value} onValueChange={onChange} />
)}
/>Using the shrinking on its own
shrinkImage(file, maxChars) is exported for a custom control. It returns { dataUri, width, height } or throws a ShrinkError with the same codes. It needs a browser, so call it from an event handler, not while rendering.
When not to use it
For photos, anything that needs cropping, or a file that must be kept at full quality, upload the original to your own storage. This control is for small images that are stored inline, such as a brand logo.
Props
Prop
Type
id and the aria-* props go on the choose button, which is why a Field function child can spread its control props straight in. ref is that button too.
Accessibility
- Choosing a file is a real button. Dropping is an extra, never the only way, so the control works with a keyboard, a screen reader and a switch.
- The preview has text alternative
previewAlt. Change it to name the logo, such as "Harbour & Pine logo". - Progress and success are read politely ("Preparing your logo...", "Logo ready"). A refusal is read as an alert and is part of the button's description.
- After Remove, focus moves to the choose button, so it is not lost.
- A button cannot take
aria-required, so a required logo is said in the button's description: the words inrequiredLabel, "Required" by default. - Disabled turns off every button, the file chooser and dropping.
| Key | Action |
|---|---|
| Tab | Moves focus to the choose or replace button, then to Remove logo. |
| Enter | On the choose or replace button, opens the file chooser. |
| Space | On the choose or replace button, opens the file chooser. |
Password input
A password field with a show/hide toggle, and an optional strength meter and requirement checklist for new passwords.
Theme studio
A ready-made screen where an admin sets the app name, a brand colour for light and for dark, and logos, with a live preview. It returns a validated theme and keeps every setting it does not show.