Skip to content

File attach

Attach files to a record from a button, checked before anything is sent, and listed after.

<div data-mount="files"></div>

Installation

// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/file-attach.css";

Usage

<div class="cap-attach">
  <div class="cap-attach-control">
    <span class="cap-attach-button">
      <input class="cap-attach-input" id="up" type="file" name="file" accept=".pdf" aria-describedby="up-hint" />
      <label for="up" class="cap-btn" data-size="sm">Upload file</label>
    </span>
    <span class="cap-attach-hint" id="up-hint">PDF, up to 4.5 MB</span>
  </div>
  <p class="cap-field-error" role="alert" hidden></p>
  <ul class="cap-attach-list">
    <li class="cap-attach-item">
      <a href="/files/f1" target="_blank" rel="noopener noreferrer">receipt.pdf<span class="cap-sr-only"> (opens in a new tab)</span></a>
      <span class="cap-attach-item-end">
        <span class="cap-attach-meta">Nov 3</span>
        <button type="submit" class="cap-btn" data-size="sm" data-variant="quiet">Remove file<span class="cap-sr-only"> receipt.pdf</span></button>
      </span>
    </li>
  </ul>
</div>

Examples

None yet, uploading

<div data-mount="pending"></div>

Read only, with the reason in place of the button, and a refusal from the server

<div data-mount="off"></div>

API reference

FileAttach

PropTypeRequiredDoes
filesAttachedFile[]YesThe files already attached, in order.
labelstringThe button's words. Default "Upload file".
pendingboolean
pendingLabelstring
acceptstringThe input's accept list, and the limits checked before anything is sent.
maxBytesnumber
maxFilesnumberThe most files the list may hold; the button goes once it is full.
namestringThe file input's name, and the id of the form it posts in (its form attribute), for a form placed elsewhere on the page (forms cannot nest).
formstring
onFiles(files: File[], input: HTMLInputElement) => voidCalled with the files that pass the checks, as soon as they are chosen: the app uploads them (a fetcher submit of input.form). Without script the Upload button posts the form.
errorReactNodeThe server's answer to the last upload or remove, when it refused.
unavailableReactNodeWhy files cannot be added here (read-only, no storage): the button gives way to these words.
renderRemove(file: AttachedFile) => ReactNodeA file's own remove control (a confirmed post). Left out, the list cannot be changed.
emptyTextReactNode
newTabTextstringThe words for a link that opens in a new tab.

Parts and data attributes

ClassIs
.cap-attachthe root
.cap-attach-buttona part
.cap-attach-controla part
.cap-attach-emptya part
.cap-attach-hinta part
.cap-attach-inputa part
.cap-attach-itema part
.cap-attach-item-enda part
.cap-attach-lista part
.cap-attach-metaa part
.cap-attach-unavailablea part
StateRead from
:focus-visiblethe platform

Accessibility

KeyDoes
TabTo the upload button, then each file's link and remove control
Enter or Space on the buttonOpens the file picker