Skip to content

ImagePicker ​

A multi-image picker: click or drag to select → thumbnail grid → reorder / remove, with a count limit. The component does not upload; files is handed to the host's upload chain as File[].

Basic usage ​

max=6

No images selected yet

Click “Add image” to pick files, or drop images onto the component. Each thumbnail has move-forward / remove / move-backward actions (keyboard accessible).

Count limit ​

Files beyond max are dropped and oas-exceed-limit is dispatched; once full, the add tile is hidden and the counter shows selected / max.

max=2 (pick 3 to see the feedback)

Limit: 2 images

Readonly and disabled ​

disabled

readonly (preview only)

  • disabled: the add tile and all reorder/remove buttons are disabled
  • readonly: the add entry is hidden and action buttons are disabled (for post-submit display)

Type filtering and capture ​

accept passes through the native type filter (extensions or MIME types, comma separated); filtered-out files dispatch oas-reject. capture makes mobile browsers open the camera directly.

accept only .jpg / .jpeg + capture

Try picking a PNG: it is rejected with feedback

Auto compression and EXIF orientation (compress) ​

Automatic compression of picked images (default off; when enabled it only applies to newly picked files; controlled files writes are never compressed — what the host provides is final):

  • compress="": enable with defaults (quality 0.8 / max-dimension 4096)
  • compress='{"quality":0.8,"max-dimension":1920}': JSON overrides (quality 0–1, longest-edge pixel cap with proportional downscale)
  • JPEG / WebP are re-encoded at the configured quality; PNG keeps transparency (kept as-is when no resize is needed); GIF / SVG and other types pass through untouched
  • EXIF orientation: modern browsers already decode <img> upright per EXIF (sideways phone shots display correctly), so compression only scales proportionally and never re-applies a rotation; re-encoded output is upright (JPEG/WebP/PNG re-encoding carries no orientation marker)
  • The source file's EXIF Orientation is reported as meta info: compressed[].orientation (1 = no orientation marker), so hosts can tell whether the original carried one
  • Decode failure / canvas unavailable: graceful degradation keeps the original file (meta entry stays null)
  • While enabled, oas-change detail carries a compressed array (aligned with files): each entry is { originalWidth, originalHeight, width, height, orientation, quality } (original → compressed size) or null (untouched)
  • Invalid configuration warns once in dev and behaves as off (detail returns to the current shape without compressed)

compress auto compression (pick an image to see the compression meta)

Pick an image to see the compression meta here

Working with the upload chain ​

files is a JS property (not reflected to attributes); host assignment applies silently and dispatches no event, so echo-back in a form is straightforward.

js
const picker = document.querySelector('oas-image-picker')
picker.addEventListener('oas-change', async (e) => {
  const form = new FormData()
  for (const file of e.detail.files) form.append('images', file)
  await fetch('/api/upload', { method: 'POST', body: form })
})

// Echo existing images (e.g. an edit form)
picker.files = existingFiles

Events ​

EventWhendetail
oas-changeAfter the user adds / removes / reorders{ files, count }
oas-exceed-limitThe selection exceeds the limit (extra files dropped; dispatched after oas-change){ count, max }
oas-rejectA file is filtered out by accept{ files }

API ​

oas-image-picker ​

Attributes ​

AttributeDescriptionTypeDefault
acceptFile type filter (native accept syntax, default image/*)stringimage/*
capturePasses through native capture: opens the camera directly on mobileboolean—
compressAuto compression for picked images (default off = byte-identical to current behavior): empty string = defaults (quality 0.8 / max-dimension 4096); JSON {"quality":0.8,"max-dimension":1920} overrides; invalid warns once and behaves as off. Only applies to newly picked files (controlled files writes are never compressed); EXIF orientation is handled by the browser's upright decoding — output is always upright, with the source orientation marker reported as orientation meta; while active oas-change detail carries the compressed meta arraystring—
disabledDisabled: adding, reordering and removing are all unavailableboolean—
maxMax image count (default 9); invalid/non-positive values fall back with a warningstring—
readonlyPreview only: add entry hidden and action buttons disabledboolean—

Property (JS property; see each row for attribute reflection) ​

PropertyDescriptionTypeDefault
filesCurrent image list (File[], controlled): assignment refreshes silently; only user interaction dispatches oas-changeFile[][]

Methods ​

MethodSignatureDescription
removeAtremoveAt(index: number): voidRemove the image at index (out-of-range ignored, dispatches oas-change)
movemove(index: number, delta: number): voidMove an image by delta (±1, out-of-range ignored, dispatches oas-change)
openopen(): voidOpen the system file picker (requires a user gesture)
clearclear(): voidClear the list (dispatches oas-change)

Events ​

EventDescription
oas-changeAfter the user adds / removes / reorders, detail: { files, count }
oas-exceed-limitThe selection exceeds the limit, detail: { count, max } (extra files dropped; dispatched after oas-change)
oas-rejectFiles filtered out by accept, detail: { files }

Released under the MIT OR Apache-2.0 License.