Documentation / Forms
ImageUploader
The image field: a button that opens the file picker, an optional direct URL field, and a preview at the chosen aspect ratio with a remove button. It checks that the file really is an image and respects maxSize (5 MB by default), and shows the error message itself.
It is controlled: value is the image URL and onChange is required. Without onUpload, the chosen file is read as a data URL (handy for a prototype, heavy in a database); with onUpload(file), the application uploads it and returns the final URL, while the button shows the upload in progress. Removing calls onChange("").
A client component ("use client"). Its texts — button, preview, error messages — are imageUploader… labels: English by default, French under <UILabelsProvider labels={frenchUILabels}>, and overridable for one instance through the labels prop.
Examples
Basic usage
Without onUpload, the chosen image is read locally and returned as a data URL.
import { useState } from "react";
import { ImageUploader } from "@synertech/ui";
export function Basic() {
const [cover, setCover] = useState("");
return <ImageUploader label="Image de couverture" value={cover} onChange={setCover} />;
}Uploading to a server
onUpload receives the File and returns the public URL; a thrown error is shown below the field.
import { useState } from "react";
import { ImageUploader } from "@synertech/ui";
export function WithUpload() {
const [photo, setPhoto] = useState("");
const upload = async (file: File) => {
const body = new FormData();
body.append("file", file);
const response = await fetch("/api/uploads", { method: "POST", body });
if (!response.ok) throw new Error("L’envoi a échoué, réessayez.");
const { url } = (await response.json()) as { url: string };
return url;
};
return (
<ImageUploader
label="Photo du produit"
value={photo}
onChange={setPhoto}
onUpload={upload}
aspectRatio="4/3"
/>
);
}Square avatar, no URL
aspectRatio="1/1" for a square preview; showUrlInput={false} removes the URL field.
import { useState } from "react";
import { ImageUploader } from "@synertech/ui";
export function ProfilePicture() {
const [avatar, setAvatar] = useState("https://i.pravatar.cc/300?img=5");
return (
<div className="w-48">
<ImageUploader
label="Photo de profil"
value={avatar}
onChange={setAvatar}
aspectRatio="1/1"
showUrlInput={false}
/>
</div>
);
}File constraints
accept restricts the picker, maxSize (in bytes) the size; helpText replaces the generated help.
import { useState } from "react";
import { ImageUploader } from "@synertech/ui";
export function Constraints() {
const [logo, setLogo] = useState("");
return (
<ImageUploader
label="Logo de l’entreprise"
value={logo}
onChange={setLogo}
accept="image/png,image/svg+xml"
maxSize={1024 * 1024}
aspectRatio="auto"
urlPlaceholder="https://exemple.fr/logo.png"
helpText="PNG ou SVG, fond transparent de préférence, 1 Mo maximum."
/>
);
}Disabled
disabled blocks file selection, the URL field and removal.
import { ImageUploader } from "@synertech/ui";
export function Disabled() {
return (
<ImageUploader
label="Bannière (verrouillée)"
value="https://picsum.photos/id/1018/800/450"
onChange={() => {}}
disabled
/>
);
}API reference
Components
import { ImageUploader } from "@synertech/ui";Props — ImageUploaderProps
| Prop | Type | Default | Description |
|---|---|---|---|
onChange• |
(url: string) => void |
— | |
accept |
string |
"image/*" |
The formats accepted, as the |
aspectRatio |
"16/9" | "4/3" | "1/1" | "auto" |
"16/9" |
|
className |
string |
"" |
|
disabled |
boolean |
false |
|
helpText |
string |
— | |
label |
string |
— | |
labels |
UILabelsOf<"imageUploader"> |
— | The strings the uploader writes itself, for this instance: any |
locale |
string |
— | BCP 47 tag the size limit is written in. Defaults to the |
maxSize |
number |
5 * 1024 * 1024 |
Largest file accepted, in bytes. Checked in the browser only: the server checks again. |
onUpload |
(file: File) => Promise<string> |
— | |
showUrlInput |
boolean |
true |
|
urlPlaceholder |
string |
"https://example.com/image.jpg" |
|
value |
string |
— |