Synertech Web/ui v0.57.5 Site Storybook

Documentation / Forms

ImageUploader

Client@synertech/uiOpen in Storybook

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.

Open in a tab
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.

Open in a tab
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.

Open in a tab
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.

Open in a tab
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.

Open in a tab
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

ImageUploader

ComponentClient
import { ImageUploader } from "@synertech/ui";

Props — ImageUploaderProps

PropTypeDefaultDescription
onChange• (url: string) => void —
accept string "image/*"

The formats accepted, as the accept attribute writes them. Applied to the file chosen as well as to the dialog, where "All files" went around it. An SVG is refused unless named here (image/svg+xml or .svg): it can carry a script, which runs when the image is served from the application's own domain. Checked in the browser only — a name and a type are what the reader's machine says they are, and the server that stores the file checks again.

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 imageUploader… key of UILabels, its error messages included. Wins over UILabelsProvider; label and helpText win over both.

locale string —

BCP 47 tag the size limit is written in. Defaults to the locale of UILabelsProvider, then to the language of the runtime.

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 —