Navigation

v2.1.0
Application blocks

File upload

Choosing files, then each one in a list with its progress, its failure and what to do next.

A file upload block is where files are chosen and what happens to each one afterwards: it waits, it uploads, it arrives, or it does not and says why. The place to choose is a dropzone; every file after that is a file with its own facts and its own controls.

File upload

<section class="sheet container-inline flex flex-col gap-lg w-full max-w(640) p-lg" data-example="file-upload" aria-labelledby="upload-title">  <header class="flex flex-col gap-2xs">    <h2 class="heading heading-sm" id="upload-title">Brand assets</h2>    <p class="m(0) fg-text-muted">Logos and guidelines for Atlas Labs. Everyone in the workspace can download them.</p>  </header>  <label class="dropzone">    <input type="file" class="dropzone-input" name="assets" multiple accept="image/png,image/jpeg,application/pdf">    <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M9.4989 2.6265A0.625 0.625 0 0 0 9.375 3L9.3758 12.0307A0.625 0.625 0 0 0 10.6182 12.0917L10.625 12L10.623 2.95A0.6252 0.6252 0 0 0 10.5011 2.6265Q10.2855 2.4578 10 2.4578Q9.7145 2.4578 9.4989 2.6265Z" fill="currentColor" stroke="none" fill-rule="evenodd" />      <path d="M6.5 6.5L9.8586 3.1414A0.2 0.2 0 0 1 10.1414 3.1414L13.5 6.5" />      <path d="M3 13.5L3 15.75A1.25 1.25 0 0 0 4.25 17L15.75 17A1.25 1.25 0 0 0 17 15.75L17 13.5" />    </svg>    <span class="dropzone-title">Drop files here or choose them</span>    <span class="dropzone-description">PNG, JPEG or PDF, up to 10 MB each</span>  </label>  <div data-upload-refused hidden>    <div class="alert alert-warning" role="status">      <svg class="icon alert-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M8.8742 3.8235L13.4246 12.0144A1 1 0 0 1 12.5505 13.5L3.4495 13.5A1 1 0 0 1 2.5754 12.0144L7.1258 3.8235A1 1 0 0 1 8.8742 3.8235Z" />        <path d="M8 6.5L8 9" />        <circle cx="8" cy="11.5" r="0.5" fill="currentColor" stroke="none" />      </svg>      <div class="alert-body">        <p class="alert-title" data-upload-refused-title>1 file cannot be uploaded</p>        <p class="alert-description">Atlas takes PNG, JPEG and PDF files up to 10 MB. Remove what is marked below and choose it again in one of those formats.</p>      </div>    </div>  </div>  <div data-upload-queue hidden>    <div class="flex flex-col gap-md">      <ul class="flex flex-col gap-xs m(0) p(0)" aria-label="Files" data-upload-list></ul>      <div class="flex flex-wrap items-center justify-between gap-sm">        <p class="m(0) fg-text-muted" data-upload-summary></p>        <button type="button" class="btn btn-primary btn-sm" data-upload-start disabled>Start upload</button>      </div>    </div>  </div>  <p class="sr-only" role="status" data-upload-status></p>  <template data-upload-file>    <li class="file">      <progress class="progress file-progress" max="100" value="0" aria-label="Upload"></progress>      <span class="file-page" aria-hidden="true"></span>      <div class="file-body">        <p class="file-name" tabindex="-1"></p>        <p class="file-meta"></p>      </div>      <div class="file-end">        <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Retry" data-action="retry">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M2.1767 6.5674A5.9666 5.9666 0 0 1 12.3585 3.6514L13.7126 5.0075A0.5 0.5 0 0 1 13.8253 5.2444A0.7044 0.7044 0 0 1 13.2444 5.8253A0.5008 0.5008 0 0 1 12.9797 5.6869L11.6364 4.3432Q10.6813 3.3314 9.3396 2.9629Q7.9979 2.5945 6.66 2.9768Q5.3222 3.359 4.3776 4.3807Q3.4331 5.4023 3.1567 6.766A0.5 0.5 0 0 1 2.1667 6.6649A0.4988 0.4988 0 0 1 2.1767 6.5674Z" fill="currentColor" stroke="none" fill-rule="evenodd" />            <path d="M13.3333 2.3333L13.3333 5.1333A0.2 0.2 0 0 1 13.1333 5.3333L10.3333 5.3333" />            <path d="M2.1747 10.7556A0.5008 0.5008 0 0 0 2.3131 11.0203L3.6415 12.3486A5.9659 5.9659 0 0 0 13.8276 9.4084A0.5 0.5 0 0 0 12.8631 9.1632L12.8433 9.234Q12.5669 10.5977 11.6224 11.6193Q10.6778 12.641 9.34 13.0232Q8.0021 13.4055 6.6604 13.0371Q5.3187 12.6686 4.3636 11.6568L2.9925 10.2874A0.5 0.5 0 0 0 2.7556 10.1747A0.7044 0.7044 0 0 0 2.1747 10.7556Z" fill="currentColor" stroke="none" fill-rule="evenodd" />            <path d="M2.6667 13.6667L2.6667 10.8667A0.2 0.2 0 0 1 2.8667 10.6667L5.6667 10.6667" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Cancel upload of" data-action="cancel">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M4 4L12 12" />            <path d="M4 12L12 4" />          </svg>        </button>        <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove" data-action="remove">          <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">            <path d="M4 4L12 12" />            <path d="M4 12L12 4" />          </svg>        </button>      </div>    </li>  </template></section>
Press Start upload, cancel one and retry it, remove a file, or choose files of your own. Nothing is uploaded: the progress is a timer, and a file's content is never read.

Blocks are markup to copy. They take the page's own text color and size; the previews set fg-text-body text-sm on the body. Each block is a size container, so it reflows by its own width: in a wide page, a sidebar's worth of room or a dialog.

#What is in it

  • One sheet at a reading width, max-w(640): a title that says what the files are for, the dropzone, then the list. A file's name and its controls stay within one glance of each other.
  • The dropzone says the limits before a file is chosen. "PNG, JPEG or PDF, up to 10 MB each" is its dropzone-description, and the input's accept narrows the picker to the same types. A limit that is only reported afterwards has already cost a try.
  • Every file is a file on a list item: the small page with its extension, the name, and one line of facts in file-meta. That line is the size and the state in words, "184 KB · Ready", and both keep one line, so each state is said briefly.
  • An upload under way is the sheet being tinted. A native <progress class="progress file-progress"> lies under the words and fills from the start edge; the percentage is in the facts as well, since the tint is a drawing of it.
  • A failure is file-failed: the contour and the facts take the danger ink, and the facts say what went wrong. A failure that can be tried again offers Retry; a file that breaks the limits offers only Remove, because trying again cannot help.
  • The controls are the file's own, in file-end: a quiet square button for each, named with the file, "Remove atlas-logo.png". They change with the state: Remove while it waits, Cancel while it uploads, Retry and Remove once it has stopped.
  • What cannot be uploaded is said once, above the list, in an alert that counts the files and says what to do; each of them says its own reason in its row.
  • The count and the one primary button close the block: "1 of 3 files uploaded" at the start of the row, "Start upload" at its end. The button is disabled while nothing waits.

#What needs a script

The dropzone needs none: a click opens the picker and a file dropped on the zone lands in its input. main.js is everything after that.

  • It lists what was chosen. Each file of the input's files becomes a row, cloned from the <template> in the markup, with its name set as text. Then the input is emptied, so choosing the same file again is still a change. Two files of one name are two rows, each with its own state.
  • It checks type and size, by the file's MIME type, or by its extension where the browser reports no type. A file that fails is kept in the list, marked, with its reason. This is feedback for the reader; the server checks again.
  • It keeps one state per file and draws the row from it: the facts, the bar and the buttons. The count, the alert and whether "Start upload" can be pressed are all derived from the same list.
  • It moves focus when a control leaves. After Remove, focus goes to the file that takes its place, then to the one before, then to the dropzone. When Cancel turns into Retry, focus stays in the row.
  • It says what happened in one role="status" line that is not drawn: "atlas-logo.png uploaded.", "2 files added, 1 cannot be uploaded."

The upload itself is a timer in the example: every file takes the same two seconds, and none fails by chance, so the block reads the same each time. In an application, replace upload() and tick() with the request, write its progress into item.progress, and set failed with the server's reason. The function mount returns stops the timer and empties the list.

#Accessibility

  • The file input is the control: it takes focus, Enter and Space open the picker, and the zone draws its ring. Its title and description are its label.
  • The list is a <ul> named "Files". Each button's name ends with its file's name, so "Remove" is never one of five.
  • Cancel and Remove share a cross, since both take the file out of the way; their names say which one it is.
  • A progress bar has a name, "Upload of launch-hero.jpg", and its value; the same number is in the text.
  • A failure is not said by color alone: the facts name it, and the alert counts what needs attention.
  • The status line is polite. It reports that a file arrived, not every percent on the way.
  • Under reduced motion the bar still fills: it is the amount, not a decoration.