Skip to content

File Picker

A drop zone that is also a button. The user clicks it, presses Enter or Space, or drops files on it. Files that do not match accept or maxSize are refused with a message, and the rest are listed with a Remove button.

Decision

When Use
Choosing files to upload, such as a logo or a cover File Picker
Showing files already stored Asset List
One image with a preview Image Picker
A path on the server Folder Picker

Usage

import { createFilePicker } from './configurationpage?name=myplugin_stolon.js';

var picker = createFilePicker({ accept: 'image/*', maxSize: 2097152, multiple: true, onChange: function (files) { upload(files); } });
container.appendChild(picker.element);

Classes: stolon-file-picker, stolon-file-zone, stolon-file-list, stolon-file-item. The handle is { element, input, getValue, setValue, clear, destroy }.

Parts: stolon-file-name, stolon-file-size.

States and Modifiers

  • getValue() returns an array of File objects. In single mode a new file replaces the old one.
  • accept is an accept list such as 'image/*,.png'. maxSize is the most bytes one file may have, and the size limit follows the size message in parentheses, such as (2 MB).
  • The zone has role="button" and tabindex="0", and is built as stolon-icon-text, so its icon lines up with the first line of the text. The stolon-dragover modifier shows the accent.
  • dropText is "Drop files here or click to select." by default. typeText, sizeLimitText, and singleText replace the refusal messages, and {value} is the file name.
  • The refusal messages show together under the zone as an error Field Message.
  • onChange(files) runs after each change. onError(messages) runs with the refusal messages.
  • stolon-file-list is a List of stolon-item-row rows, with no empty sentence, and is hidden while empty. Remove moves focus to the next Remove button, the one before it, or the zone.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createFilePicker

function createFilePicker(options?: FilePickerOptions): FilePickerHandle

A drop zone that is also a button. The user clicks it, presses Enter or Space, or drops files on it. Files that do not match accept or maxSize are refused with a message, and the rest are listed with a Remove button.

Option Type What it does
accept string accept list such as 'image/*,.png' [optional]
maxSize number most bytes one file may have [optional]
multiple boolean true allows several files (default false)
dropText string text in the zone (default 'Drop files here or click to select.')
removeLabel string aria-label of a remove button, where {value} is the file name (default 'Remove {value}')
typeText string message for a file of the wrong type, where {value} is its name (default '{value} is not an accepted type.')
sizeLimitText string message for a file over maxSize, where {value} is its name (default '{value} is larger than the size limit.')
singleText string message when several files were dropped and multiple is off (default 'Select one file.')
onChange (files: File[]): void function(files) called with the File objects after each change [optional]
onError (messages: string[]): void function(messages) called with the refusal messages [optional]
Handle member Type
input HTMLInputElement
getValue (): File[]
setValue (next?: File[] | null): void
clear (): void
destroy (): void
Class What it looks like
stolon-file-picker Drop zone that is also a button, with a list of the chosen files under it
stolon-file-list One chosen file with its name, size, and remove button

Example

Example Code

<!-- title: File Picker -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
const status = document.createElement('div');
status.className = 'stolon-status-text';
mount.appendChild(base.createFilePicker({ accept: 'image/*,.json', maxSize: 1048576, multiple: true, onChange: (files) => { status.textContent = files.length + ' chosen'; } }).element);
mount.appendChild(status);
</script>