Skip to Content
Cristal

Forms

File upload

A drop zone that is also a file picker, with the list of chosen files.

<cr-file-upload>CrFileUpload

Example

The zone accepts files dragged from the desktop; a click, Enter or Space opens the system file picker. Each chosen file is added to the list with its size and a Remove button.

While files are dragged over it, the zone gently breathes and takes on colour; each added file slides into place in the list.

0 files chosen

'use client';

import { useState } from 'react';
import { CrBox, CrFileUpload } from '@cristal-ui/react';

export default function FileUploadBasic() {
  const [count, setCount] = useState(0);

  return (
    <CrBox display="flex" direction="column" gap="lg" style={{ width: '100%', maxWidth: 420 }}>
      <CrFileUpload
        label="Receipts"
        hint="PDF or image, several files allowed."
        name="receipts"
        accept=".pdf,image/*"
        multiple
        onCrChange={(event) => setCount(event.detail.length)}
      />
      <CrFileUpload label="Profile picture (locked)" disabled />
      <p>{count === 1 ? '1 file chosen' : `${count} files chosen`}</p>
    </CrBox>
  );
}

File types and count

accept mirrors the native attribute: extensions (.pdf), types (application/pdf) or families (image/*), separated by commas. It also applies to dropped files: files of another type are rejected with a message. State the accepted formats in hint.

With multiple, files add up; without it, a new file replaces the previous one.

In a form

The field submits each file under its name, like a native input type="file" with multiple. onCrChange gives the updated list of files (File[]) after every addition or removal.

If the user asked their system to reduce motion, the zone only changes colour while files are dragged over it, and files appear in the list without sliding.

error-animation animates the error when a file is rejected (zigzag, badge, echo…): see Animated error.

Accessibility

  • Under the zone, a real native input type="file", named by label and described by hint: dropping is never the only way to add a file, and the keyboard reaches it with Tab.
  • Each Remove button is named after its file ("Remove invoice.pdf"); after a removal, focus moves to the next file, or to the picker when the list is empty.
  • Additions, removals and rejections are announced to screen readers ("invoice.pdf added. 2 files.").
  • The zone border stands out from the background with a contrast of at least 3:1; keyboard focus draws an outline around it.

API

Properties

PropertyTypeDefaultDescription
acceptstring—Types acceptés, comme l'attribut accept natif (« .pdf,image/* ») ; appliqué aussi aux fichiers déposés.
disabledbooleanfalseDésactive le champ.
errorAnimation"drop" | "echo" | "jitter" | "none" | "pop" | "wobble" | "zigzag"'none'Animation de l'erreur quand elle apparaît et à chaque envoi refusé : none (par défaut), zigzag, pop, drop, echo, jitter ou wobble.
hintstring—Texte d'aide (formats, taille), lu par les lecteurs d'écran.
label (obligatoire)string—Libellé affiché au-dessus de la zone (obligatoire).
multiplebooleanfalsePlusieurs fichiers ; sinon un nouveau fichier remplace le précédent.
namestring—Nom envoyé avec le formulaire, une fois par fichier.

Events

Reactevent.detailDescription
onCrChangeFile[]Émis quand la liste des fichiers change ; detail : les fichiers.

CSS parts

To style it from outside: cr-file-upload::part(name) { … }

PartDescription
errorLe message des fichiers refusés.
fileUn fichier de la liste.
filesLa liste des fichiers.
hintLe texte d'aide.
labelLe libellé.
zoneLa zone de dépôt.

On this page