Command palette

@yasinsensoy/web

Web · Components

FileUpload

File upload area: single and multiple file selection in a drag-and-drop, gallery or single-row layout.

21 exports

Live example

Preview

Code

import { Form, FileField, SubmitButton } from "@yasinsensoy/web";

const [state, formAction] = useActionState(save, null);

<Form action={formAction} state={state}>
  <FileField label="Attachments" name="attachments" multiple maxSize="5 MB" maxFiles={6} tooltip="You can also paste a screenshot with Ctrl+V."
    edit />
  <SubmitButton>Submit</SubmitButton>
</Form>

// Server action
async function save(_: unknown, data: FormData) {
  const attachments = data.getAll("attachments"); // File (new) and string (existing / uploaded)
}

Events

  1. Events appear here when you interact with the preview.

Gallery: photos, full screen preview (←/→, swipe)

Images, Video · Up to 10 MB · Up to 8 files

Profile photo: a single image, Change button

PNG, JPEG, WEBP · Up to 2 MB

Edit form: files on the server and type icons

  • sozlesme-2026.pdf

    471 KB

  • budget.xlsx

    89.1 KB

  • presentation-october.pptx

    5.2 MB

  • kaynak-code.zip

    12 MB

Inline: in compact forms

PDF · Up to 5 MB

Import

import {
  FileIcon,
  FileUpload,
  parseFileSize,
  formatFileSize,
  fileExtension,
  acceptsFile,
  fileKind,
  isPreviewable,
} from "@yasinsensoy/web";

FileUpload

componentClient component

File upload area: single and multiple file selection in a drag-and-drop, gallery or single-row layout.

  • Rules: accept (type), maxSize/minSize (size), maxFiles (count) and validate (your own rule). Files that don't fit are not added; the reason is written under the area with the file name. Drag and drop and paste are checked too. The same file is not added twice.
  • Preview: Images and videos appear as thumbnails and open full screen on click. Other files are shown with a colored icon by type and the extension (PDF, XLSX…). Name and size ("1.4 MB") are written for every file. Preview URLs are created only when needed and freed from memory when the file is removed; images load lazily.
  • Ways to add: click to choose, drag and drop, and paste while the area is focused (a screenshot with Ctrl+V). On mobile, capture opens the camera directly.
  • Upload: With upload, files upload as soon as they are selected: progress bar, queueing, cancel (when the file is removed) and "Try again" are built in.
  • Form: Without upload, files enter FormData under name via a hidden native <input type="file"> (formData.getAll(name) in server actions). Existing files from the server and uploaded files are submitted as text (value or id) under the same name, so the server also knows which old files remain. required works with browser validation, and the initial files return when the form is reset.
  • Mobile: On touch screens the remove buttons are always visible with a 44 px touch target; the "drag and drop" text is not shown. The preview closes with the back button.

For use with a label, description and error message, see FileField.

FileUpload(props: FileUploadProps): JSX.Element

Examples

<FileUpload name="documents" multiple accept=".pdf,.docx" maxSize="10 MB" maxFiles={5} />

<FileUpload name="photos" layout="gallery" multiple accept="image/*" maxSize="5 MB"
  upload={async (file, { signal }) => (await uploadFile(file, signal)).url} />

<FileUpload name="resume" layout="inline" accept=".pdf" required />

// Edit form: existing files come from the server.
<FileUpload name="attachments" multiple
  defaultValue={[{ id: "12", name: "contract.pdf", size: 48213, url: "/files/12" }]} />

FileIcon

componentServer component

Icon by file type: a separate symbol and color for documents, spreadsheets, presentations, PDF, archives, code, images, video and audio, with an extension label below. FileUpload shows it for files that cannot be previewed; you can use it in your own file lists too. It is a server component.

FileIcon({ name, type, showExtension, className }: FileIconProps): JSX.Element

Examples

<FileIcon name="report.pdf" className="size-10" />
<FileIcon name="data" type="text/csv" />

Turns a FileSize into bytes. NaN for text that cannot be read.

parseFileSize(size: FileSize): number

Examples

parseFileSize("5 MB") // 5242880
parseFileSize(1024)   // 1024

Turns a byte count into readable text: 1536 → "1,5 KB". The number format follows the given locale.

formatFileSize(bytes: number, locale?: string): string

Examples

formatFileSize(5242880)          // "5 MB"
formatFileSize(1536, "en-US")    // "1.5 KB"

The file's extension, lowercase and without the dot ("report.PDF" → "pdf").

fileExtension(name: string): string

acceptsFile

function

Does the file match one of the accept rules? Same rules as the native <input accept>: an extension (.pdf), a full type (image/png) or a type family (image/*). Without rules every file is accepted. The component calls this itself on drag and drop and paste, since the browser does not check those.

acceptsFile(file: { name: string; type?: string; }, accept: string | readonly string[] | undefined): boolean

fileKind

function

Finds a file's kind from its MIME type, else from its extension.

fileKind(file: { name: string; type?: string; }): FileKind

Can the file be previewed as an image or video in the browser?

isPreviewable(file: { name: string; type?: string; }): boolean

FileIconProps

interface

Props of the FileIcon component.

NameTypeDefaultDescription
namestringName of the file; the type and extension label are derived from it.
type?string | undefinedMIME type (e.g. "application/pdf"); checked before the extension if given.
showExtension?booleantrueShow an extension label (PDF, XLSX) under the icon?
className?stringClasses for size and color (e.g. size-10).

Upload state of a file (only when upload is given).

  • queued: waiting in line (at most uploadConcurrency files upload at once).
  • uploading: uploading; progress gives the percentage.
  • done: uploaded; value is what the server returned.
  • error: failed; error gives the message, and the user can retry.
type FileUploadStatus = "queued" | "uploading" | "done" | "error"

A file in the FileUpload list.

NameTypeDescription
idstringUnique key. Given automatically to newly selected files.
namestringDisplayed file name.
size?number | undefinedSize (bytes).
type?string | undefinedMIME type (e.g. "image/png"); the icon and preview are chosen by it.
url?string | undefinedPreview or download URL. Given for files on the server (uploaded earlier); previews of newly selected images and videos are created automatically and not written here.
file?File | undefinedThe file the user selected. Missing for files coming from the server.
status?FileUploadStatus | undefinedUpload state; only when upload is given. See FileUploadStatus.
progress?number | undefinedUpload percentage (0 – 100).
error?string | undefinedMessage of the upload error.
value?string | undefinedText submitted in the form for this file: the value returned by upload or the id of a file from the server. If omitted, id is submitted.
original?File | undefinedOriginal file of an edited image/video (with edit). Re-editing starts from here; file is the edited version.
edits?MediaEdits | undefinedApplied edits (with edit). See MediaEdits.

Image and video editing settings: which tools are enabled for which type (MediaEditorTools), whether it opens right after selection, and output settings.

Extends: MediaEditorTools

NameTypeDefaultDescription
auto?booleantrueOpen the editor before adding when images/videos are selected (like WhatsApp). If cancelled, the files are not added. If false, it opens only via the file's "Edit" button.
output?MediaEditorOutputType, quality and size of edited files. See MediaEditorOutput.

File for value and defaultValue; if id is omitted, the url or name is used. Enough to show files on the server: { name: "contract.pdf", url: "/files/12", size: 48213 }.

type FileUploadInit = Omit<FileUploadItem, "id"> & { /** Id of the file; generated if omitted. */ id?: string | undefined; }
NameTypeDescription
namestringDisplayed file name.
size?number | undefinedSize (bytes).
type?string | undefinedMIME type (e.g. "image/png"); the icon and preview are chosen by it.
url?string | undefinedPreview or download URL. Given for files on the server (uploaded earlier); previews of newly selected images and videos are created automatically and not written here.
file?File | undefinedThe file the user selected. Missing for files coming from the server.
status?FileUploadStatus | undefinedUpload state; only when upload is given. See FileUploadStatus.
progress?number | undefinedUpload percentage (0 – 100).
error?string | undefinedMessage of the upload error.
value?string | undefinedText submitted in the form for this file: the value returned by upload or the id of a file from the server. If omitted, id is submitted.
original?File | undefinedOriginal file of an edited image/video (with edit). Re-editing starts from here; file is the edited version.
edits?MediaEdits | undefinedApplied edits (with edit). See MediaEdits.
id?string | undefinedId of the file; generated if omitted.

Why a file was not added.

type FileRejectionReason = "type" | "size" | "minSize" | "count" | "duplicate" | "custom"

FileRejection

interface

A file that was not added and the reason; reported via onReject.

NameTypeDescription
fileFileA file that was not added.
reasonFileRejectionReasonReason: type, size, count limit, duplicate file or validate.
messagestringMessage shown to the user.

Tools passed to the upload function.

NameTypeDescription
signalAbortSignalAborted when the user removes the file or the component unmounts; pass it to fetch.
onProgress(percent: number) => voidReport the upload percentage (0 – 100); the bar advances.

How files are shown.

  • list: a drag-and-drop area with files row by row below it (small preview, name, size, progress). For documents.
  • gallery: square tiles; images and videos appear as covers, and the "Add" tile is at the end of the gallery. For photos.
  • inline: a single row at text box height ("Choose file" and the selected file's name). For compact forms.
type FileUploadLayout = "list" | "gallery" | "inline"

Classes that can be passed to the parts of FileUpload.

NameTypeDescription
dropzone?stringDrag-and-drop area (or the inline box).
list?stringFile list or gallery grid (e.g. max-h-80 overflow-auto).
item?stringEach file row or tile.

Props of the FileUpload component.

NameTypeDefaultDescription
layout?FileUploadLayout"list"Layout. See FileUploadLayout.
multiple?booleanfalseMultiple files can be selected. If false, a newly selected file replaces the old one.
accept?string | readonly string[]Accepted types: an extension (.pdf), a MIME type (image/png) or a type family (image/*). A comma-separated string or an array. The file dialog shows only these; drag and drop and paste are checked too.
maxSize?FileSizeMaximum size per file: bytes or "5 MB". See FileSize.
minSize?FileSizeMinimum size per file: bytes or "1 KB".
maxFiles?numberMaximum number of files (with multiple). The add area is hidden when full.
validate?(file: File) => string | null | undefined | falseYour own rule: return the message to show the user if the file is not acceptable, return nothing if it is.
value?readonly FileUploadInit[]Files when controlled.
defaultValue?readonly FileUploadInit[]Initial files when uncontrolled; e.g. existing files on the server in an edit form. They return when the form is reset.
onValueChange?(items: FileUploadItem[]) => voidCalled when files change (add, remove, upload state).
onReject?(rejections: FileRejection[]) => voidCalled when files that break the rules are not added. The messages are already shown under the field; this is for logging or your own notice.
upload?(file: File, context: FileUploadContext) => Promise<unknown>Uploads files as soon as they are selected. Called for each file; if it returns a string (e.g. the file's URL or id), that string is submitted in the form under name and becomes the item's value. While uploading a progress bar appears, and "Try again" on error; the form is not submitted until uploads finish. If omitted, the files themselves are submitted with the form.
uploadConcurrency?number3Maximum number of files uploaded at once.
placeholder?ReactNode"Dosya seç"Main text of the drag-and-drop area.
hint?ReactNodeShort info under the area. If omitted, generated from accept, maxSize and maxFiles ("PNG, JPG · Up to 5 MB · Up to 10 files"); hidden with false.
icon?IconPropIcon of the drag-and-drop area.
preview?booleantrueOpen a full-screen preview when images and videos are clicked? In the preview, move between files with the arrow keys and swiping.
edit?boolean | FileUploadEditOptionsImage and video editing: crop, rotate, draw, text, blur, color; for video also range trimming and muting. true enables all tools; an object selects by type and tool (see FileUploadEditOptions). When a single file is selected the single editor opens, and with several the multi editor with a thumbnail strip at the bottom. Each added image/video also gets an "Edit" button; editing is non-destructive (the original file and edits are stored on the item, and reopening continues where you left off). The size limit (maxSize) applies to the edited file, so a long video can be trimmed to fit the limit.
capture?"user" | "environment"On mobile, opens the camera directly instead of files: front (user) or back (environment).
name?stringForm field name. See "Form" in the component description.
form?stringId of the form the field belongs to (if outside the form).
required?booleanThe form cannot be submitted without at least one file.
disabled?booleanDisabled.
readOnly?booleanFiles are shown but cannot be added or removed.
id?stringId of the select button; <Label htmlFor> binds to it.
tooltip?TooltipPropHint shown when hovering the drag-and-drop area. See TooltipProp.
className?stringClasses added to the container.
classNames?FileUploadClassNamesClasses added to the inner parts.
effect?EffectPropEffect or effects of the drop area (also plays while a file is dragged) (e.g. "beam", ["lift", "glow"]). If omitted, UIProvider appearance.defaults.fileUpload.effect. See BuiltinEffect.
aria-label?stringScreen reader name when there is no visible label.
aria-labelledby?stringId of the element naming the field.
aria-describedby?stringId of the element describing the field. FileField sets it itself.
aria-invalid?boolean | "true" | "false"Show the field as invalid. FileField sets it itself.

File size: a number of bytes or text with a unit ("5 MB", "500KB", "1.5 GB"). Units are base 1024 (1 KB = 1024 bytes).

type FileSize = number | `${number}${"" | " "}${"B" | "KB" | "MB" | "GB"}`

Kind of a file by how it looks; icons and previews use it.

type FileKind = "image" | "video" | "audio" | "pdf" | "document" | "spreadsheet" | "presentation" | "archive" | "code" | "other"