Skip to content

Image Picker

Picks an image file, shows it in a frame, and hands the caller the file data. It never uploads, so the caller sends the data where it belongs.

Decision

When Use
One image the user chooses from their computer, such as a logo, a poster, or a backdrop Image Picker
Files that are not images, or several files File Picker
Showing a user's picture Avatar

Usage

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

var logo = createImagePicker({
    label: 'Logo',
    value: config.LogoUrl,
    maxBytes: 1048576,
    onChange: function (value) { pendingLogo = value; tracker.check(); }
});
container.appendChild(logo.element);

Classes: stolon-image-picker, stolon-image-picker-body, stolon-image-picker-frame, stolon-image-picker-actions. The handle is { element, input, getValue, setValue, fallbackDataUrl, destroy }.

Parts: stolon-image-picker-label, stolon-image-picker-image, stolon-image-picker-placeholder, stolon-image-picker-empty, stolon-image-picker-fallback, stolon-image-picker-choose, stolon-image-picker-remove, stolon-image-picker-description.

States and Modifiers

  • shape is square (default), portrait, or wide, and sits on the root as a modifier, such as stolon-square. stolon-filled is on the root while an image shows.
  • onChange(value) runs with { name, type, size, dataUrl, base64 } after a pick, and with null after Remove. base64 is the data after the comma of dataUrl.
  • getValue() returns the picked file's data, the URL string while it shows a URL from value or setValue, or null while empty. setValue(url) does not call onChange.
  • accept is "image/*" by default. A file that does not match it, or is larger than maxBytes, is refused with a message under the field, and the old image stays.
  • The defaults are "Select image" (chooseText), "Replace" (replaceText), "Remove" (removeText), "No image." (emptyText), "The image is larger than {value}." (tooLargeText, where {value} is the limit such as 2 MB), "This file is not an image." (wrongTypeText), and "Could not read this file." (readErrorText).
  • Remove moves focus to the choose button.
  • crop: true crops a picked image from its center to the frame's shape, and a number crops it to that width over height. maxSize scales the longer side down to that many pixels, never up. Either one draws the image on a canvas, keeps a PNG, JPEG, or WebP type and makes anything else a PNG, and adds width and height to the value.
  • removable: false never shows Remove, for an image that must always be set.
  • fallback may also be the URL of an image, such as a generated logo, shown in the empty frame without being the value, and fallbackDataUrl() returns it as it is. setFallback(fallback) swaps the fallback.
  • fallback: { text, icon, color } fills the empty frame with the initials of text, such as "AL" for Ada Lovelace, or else icon, on a status color or hex code (default blue). fallbackDataUrl(size) draws it as a square PNG data URL, so a page can save it as the image, and returns '' without a fallback. initials(text) is exported on its own.

Reference

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

createImagePicker

function createImagePicker(options?: ImagePickerOptions): ImagePickerHandle

Picks an image file, shows it in a frame, and hands the caller the file data. It never uploads. The frame shows a placeholder icon while empty, and a refused or unreadable file shows a message under the field.

Option Type What it does
value string | null a URL or data URL to show at first [optional]
label string visible label above the frame [optional]
description string sentence under the field [optional]
accept string accept list of the file input (default 'image/*')
maxBytes number most bytes the file may have [optional]
chooseText string text of the choose button while empty (default 'Select image')
replaceText string text of the choose button while an image shows (default 'Replace')
removeText string text of the remove button (default 'Remove')
emptyText string text in the empty frame (default 'No image.')
tooLargeText string message for a file over maxBytes, where {value} is the limit (default 'The image is larger than {value}.')
wrongTypeText string message for a file that does not match accept (default 'This file is not an image.')
readErrorText string message when the file cannot be read (default 'Could not read this file.')
shape 'square' | 'portrait' | 'wide' frame shape, 'square', 'portrait', or 'wide' (default 'square')
crop boolean | number true crops a picked image from its center to the frame's shape, or a number crops it to that width over height (default false)
maxSize number the longest side in pixels a picked image is scaled down to [optional]
fallback ImagePickerFallback | string | null { text, icon, color } drawn in the empty frame, initials from text or else an icon, on a status color or hex code (default blue), or the URL of an image shown in the empty frame, such as a generated logo [optional]
removable boolean false never shows Remove, for an image that must always be set (default true)
onChange (value: ImagePickerValue | null): void function(value) called with { name, type, size, dataUrl, base64 } after a pick, and null after Remove [optional]
Handle member Type
input HTMLInputElement
getValue (): ImagePickerValue | string | null
setValue (url: string | null): void
setFallback (fallback: ImagePickerFallback | string | null): void
fallbackDataUrl (size?: number): string
destroy (): void
Type What it is
ImagePickerValue A picked image file. dataUrl is the whole data URL, and base64 is only its data after the comma.
ImagePickerFallback The picture an empty image picker shows: initials from text, or an icon, on a color.
Class What it looks like
stolon-image-picker An image frame in a square, portrait, or wide shape beside its Select Image and Remove buttons
stolon-image-picker-frame The stolon-thumb frame that holds the preview, or the placeholder icon and text while empty
stolon-image-picker-fallback The picture an empty picker shows in place of its placeholder: initials or an icon on a status color over the surface, filling the frame

Example

Example Code

<!-- title: Image Picker -->
<div id="mount" style="display: flex; flex-direction: column; gap: 1.5em;"></div>
<div id="imageStatus" class="stolon-status-text"></div>
<script type="module">
const base = await window.__baseReady;
const mount = document.getElementById('mount');
const status = document.getElementById('imageStatus');
const report = (value) => { status.textContent = value ? value.name + ', ' + value.size + ' bytes.' : 'Removed.'; };
mount.appendChild(base.createImagePicker({ label: 'Logo', maxBytes: 1048576, onChange: report }).element);
mount.appendChild(base.createImagePicker({ label: 'Poster', shape: 'portrait', value: window.__gallery.rect(120, 180, '#9b59b6', 'P'), onChange: report }).element);
mount.appendChild(base.createImagePicker({ label: 'Backdrop', shape: 'wide', description: 'A wide image behind the title.', onChange: report }).element);
mount.appendChild(base.createImagePicker({ label: 'Avatar', description: 'Cropped square and scaled to 256 pixels. Empty, it shows the initials.', crop: true, maxSize: 256, fallback: { text: 'Ada Lovelace', color: 'purple' }, onChange: report }).element);
mount.appendChild(base.createImagePicker({ label: 'Server Icon', fallback: { icon: 'dns', color: 'teal' }, onChange: report }).element);
</script>