Data input

File Upload

A native file input with drop, local file checks, removable file rows and image previews.

Class nameTypeDescription
file-uploadplf:file-uploadComponentWraps the file input and its list
file-upload-inputplf:file-upload-inputPartThe native file input
file-upload-dropzoneplf:file-upload-dropzonePartA drop area around the input
file-upload-instructionplf:file-upload-instructionPartThe drop hint
file-upload-statusplf:file-upload-statusPartThe status message the script adds
file-upload-removeplf:file-upload-removePartA row's remove button
file-upload-preview-cardplf:file-upload-preview-cardPartA row's preview layout the script adds
file-upload-previewplf:file-upload-previewPartA row's image frame the script adds
file-upload-imageplf:file-upload-imagePartThe preview image the script adds
file-upload-preview-fallbackplf:file-upload-preview-fallbackPartThe loading or unavailable text the script adds
file-upload-preview-nameplf:file-upload-preview-namePartA preview's truncated name the script adds
file-upload-gallery-gridplf:file-upload-gallery-gridPartThe editor grid
file-upload-pickerplf:file-upload-pickerPartThe editor's Add photos card
file-upload-picker-labelplf:file-upload-picker-labelPartThe Add photos card's label
file-upload-picker-iconplf:file-upload-picker-iconPartThe Add photos card's SVG icon; an icon class there doesn't need it
file-upload-countplf:file-upload-countPartThe editor's photo count
file-upload-existing-controlsplf:file-upload-existing-controlsPartWraps the saved-image checkboxes
file-upload-editor-cardplf:file-upload-editor-cardPartAn editor thumbnail card the script adds
file-upload-editor-previewplf:file-upload-editor-previewPartA thumbnail's square frame the script adds
file-upload-editor-imageplf:file-upload-editor-imagePartA thumbnail image the script adds
file-upload-editor-metadataplf:file-upload-editor-metadataPartA thumbnail's visually hidden name and size the script adds
file-upload-editor-removeplf:file-upload-editor-removePartA thumbnail's corner remove button the script adds
file-upload-editor-errorplf:file-upload-editor-errorPartA thumbnail's error the script adds
file-upload-preview-galleryplf:file-upload-preview-galleryModifierThe 4:3 gallery frame the script adds

Default

File Upload checks names, types and sizes in the browser and never uploads, so validate the files again on your server.

PDF or plain text; up to 3 files, 2 MB each. New selections replace the previous selection.

Upload with fetch

The chosen files stay in the native input, so new FormData(form) sends them; PLF.Button.loading() keeps the button busy meanwhile.

One PDF up to 5 MB.

Image preview

Previews use local blob: URLs, so a Content Security Policy must allow blob: in img-src.

PNG or JPEG; one file up to 2 MB.

Saved images are checked checkboxes, so removing a card unchecks it and the form submits only the ids you keep.

Add photos using the card, or drop them onto it. Saved and new photos share the eight-photo limit. New photos append to this editor.

Disabled

A disabled <fieldset> around the upload also disables it.

This file input is disabled.

JavaScript

File Upload needs JavaScript for drop, removal, checks and previews; without it the native file input still works. The Full script (pelatform-ui.min.js) starts every component on page load, with no code of your own; with ESM imports, call init() yourself, as in Installation. For markup you add later, see Dynamic content.

<link rel="stylesheet" href="pelatform-ui.min.css">
<link rel="stylesheet" href="icons/icons.min.css">
<script src="pelatform-ui.min.js" defer></script>
<link rel="stylesheet" href="pelatform-ui.prefixed.min.css">
<link rel="stylesheet" href="icons/icons.prefixed.min.css">
<script src="pelatform-ui.min.js" defer></script>
Attribute Purpose
data-plf-file-upload Marks the file-upload wrapper
<ul> One empty <ul>, authored hidden, for the file rows; the script adds a row per file, an Attachment card, and a status message after the list
data-plf-file-upload-dropzone Optional element around the input that accepts dropped files
data-plf-file-upload-instruction Drop hint, shown when the browser supports drop
accept On the input, as usual: the script also turns away other types, chosen or dropped, with This file type is not accepted. on the row
data-plf-max-files On the wrapper: the most files, counting saved images; no limit by default
data-plf-max-size On the wrapper: the largest size of each file, in bytes, such as 2097152 for 2 MB; no limit by default
data-plf-preview On the wrapper: image or gallery; none by default
data-plf-file-upload-name, -size, -error For a row of your own: the slots of a <template> with one id-free <li> and one type="button" remove button
data-plf-file-upload-media Optional icon slot in that template, hidden in preview rows
data-plf-gallery-editor On the wrapper: the gallery editor; needs multiple. One direct child, the grid, holds the <ul> and the Add photos card, which is the file input’s parent and holds one label beside it
data-plf-file-upload-count In the Add photos label: the photo count, which the script writes
data-plf-file-upload-clear Optional editor button that removes all photos
data-plf-label, data-plf-url On a checked checkbox per saved image, with its id as value: the image’s name and its HTTP(S) or relative URL. The checkboxes sit together in one direct child, which the script hides once it shows the images
Method Behavior
PLF.FileUpload.getOrCreateInstance(element, options) Returns the instance, creating it for valid markup; the maxFiles, maxSize, preview and galleryEditor options override the attributes
PLF.FileUpload.getInstance(element) Returns the instance or null
instance.getFiles() Returns the selected new files
instance.getExistingFiles() Returns the kept saved images as { id, name, url }
instance.getState() Returns files, existingFiles, errors, valid, disabled, writable, preview and galleryEditor
instance.setFiles(files) Replaces the new files with an array of File objects
instance.remove(index) Removes the new file at a zero-based index
instance.removeExisting(id) Unchecks a saved image in a gallery editor
instance.clear() Removes all new files and saved images
instance.refresh() Shows files or validity your code set on the input
instance.destroy() Removes the rows and listeners; the selected files stay

setFiles, remove, removeExisting and clear return false when nothing changes, the event is canceled or the upload is disabled.

Events

Event When
plf:file-upload:before-change Before the selection changes; cancelable when the browser lets File Upload set the file list
plf:file-upload:change After the selection changes

Each event’s detail:

Property Events Value
instance All The File Upload instance
files All The new files after the change
previousFiles All The new files before the change
existingFiles All The kept saved images after the change, as { id, name, url }
previousExistingFiles All The kept saved images before the change
reason All api, native, drop or trigger
trigger All The element used, such as the file input, the drop zone or a remove button; null from a method
errors, valid, disabled, writable, preview, galleryEditor change The other getState() fields

Changes from methods, drop and remove buttons also fire native input and change on the input.

React

FileUpload from @pelatform/react renders the file input with the script’s checks, rows, previews and status, and keeps the chosen files in the input, so its form submits them; inside a Field the input takes the field’s label, help and error.

Prop Values Default
files An array of File objects, which the application changes in onFilesChange Its own state
onFilesChange (files), called when a choice, a drop, a remove button or FileUploadClear changes the files None
maxSize The largest size of each file, in bytes, such as 2097152 for 2 MB No limit
maxFiles The most files, counting saved images No limit
preview none, image, gallery none; gallery in the gallery editor
dropzone The drop hint, in a drop area around the input that takes dropped files None
galleryEditor true for the gallery editor, whose Add photos card takes dropped files and whose new photos append false
pickerLabel The Add photos card’s label, before its photo count None
existing The gallery editor’s saved images as { id, name, url }, which the application changes in onExistingChange None
existingName The name of the saved images’ checked checkboxes, which submit the ids kept None
onExistingChange (existing), called with the saved images left after a remove button or FileUploadClear None
removeLabel (name) => string, a remove button’s name Remove {name}
Part Element
FileUploadClear A Button, type="button", that removes every file and saved image

The other props are the native file input’s, className and ref included; accept and multiple also set the checks, and the children follow the status. A form reset empties the files; the saved images stay as existing holds them.

CSS variables

File rows also take the Attachment variables.

Variable Default
--plf-file-upload-gap --plf-space-3, 0.75rem (12px)
--plf-file-upload-color --plf-color-text
--plf-file-upload-height The input’s minimum height: --plf-control-height-md, 2.25rem (36px)
--plf-file-upload-border --plf-color-input
--plf-file-upload-radius --plf-radius-lg, 0.5rem (8px)
--plf-file-upload-bg --plf-color-field
--plf-file-upload-drop-padding --plf-space-4, 1rem (16px)
--plf-file-upload-drop-border --plf-color-input, dashed; also the Add photos card’s border
--plf-file-upload-drop-bg --plf-color-field
--plf-file-upload-image-max-width 18rem (288px), the image preview card
--plf-file-upload-gallery-min-width 10rem (160px), the smallest gallery and editor column
--plf-file-upload-gallery-gap --plf-space-3, 0.75rem (12px)
--plf-file-upload-preview-ratio 1 for image and editor previews, 4 / 3 for the gallery
--plf-file-upload-preview-bg --plf-color-surface-muted
--plf-file-upload-gallery-fit cover for editor thumbnails; set contain to show the whole image

Built with Pelatform UIv0.1.0

Components

Getting started

Foundations

Components

Actions

Data display

Navigation

Feedback

Data input

Layout

Advanced

Integrations

Blocks

Application

Authentication

Icons