Tearsheet

Issue 01 · Forms · p.13

File dropzone

Drag files in or click to browse. A real file input underneath keeps it keyboard accessible.

Why it works: State the accepted types and size limit before people try. The visually hidden input stays focusable, and the whole zone shows its focus ring.

Try it:
Receipt: axe clean · contrast 5.93:1 · 1 tab stop · RTL ✓ · long text ✓

Tab order

  1. button "Drop files here or browse PNG, JPG or PDF, up to 10 MB each"

What assistive tech is given

- button "Drop files here or browse PNG, JPG or PDF, up to 10 MB each"
- text: Drop files here or browse PNG, JPG or PDF, up to 10 MB each

Automated checks in CI (axe in Chromium, reduced motion on). They catch a lot, not everything: still try it with a screen reader.

See it in the zine npx -y tearsheet add file-dropzone

Needs the shared tokens.css once per project.

HTML + CSS

index.html
<!-- tearsheet file-dropzone vcd41a5dc · https://qusaialjbali.github.io/tearsheet/zine/parts/file-dropzone.html -->
<label class="drop">
  <input class="drop-input" type="file" multiple accept="image/*,.pdf">
  <svg class="drop-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 16V4M7 9l5-5 5 5M4 15v3a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-3" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
  <span class="drop-title">Drop files here or <u>browse</u></span>
  <span class="drop-hint">PNG, JPG or PDF, up to 10 MB each</span>
  <span class="drop-files" aria-live="polite"></span>
</label>
styles.css
/* tearsheet file-dropzone vcd41a5dc · https://qusaialjbali.github.io/tearsheet/zine/parts/file-dropzone.html */
.drop {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 6px;
  width: 100%;
  max-width: 440px;
  padding: 32px 24px;
  text-align: center;
  background: var(--ui-surface);
  border: 2px dashed var(--ui-border-strong);
  border-radius: var(--ui-radius);
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;
}
.drop:hover, .drop.is-over { border-color: var(--ui-accent); background: var(--ui-accent-soft); }
.drop:focus-within { box-shadow: var(--ui-focus); }
.drop-input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop-icon { width: 36px; height: 36px; margin-bottom: 6px; color: var(--ui-accent); }
.drop-title { font-weight: 600; color: var(--ui-text); }
.drop-title u { color: var(--ui-accent); text-underline-offset: 3px; }
.drop-hint { font-size: 13px; color: var(--ui-muted); }
.drop-files:not(:empty) {
  margin-top: 10px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ui-text);
  background: var(--ui-surface-2);
  border-radius: 999px;
}
script.js
/* tearsheet file-dropzone vcd41a5dc · https://qusaialjbali.github.io/tearsheet/zine/parts/file-dropzone.html */
function setup(root) {
  const input = root.querySelector('.drop-input');
  const list = root.querySelector('.drop-files');

  const show = () => {
    const names = [...input.files].map((f) => f.name);
    list.textContent = names.length ? names.length + ' selected: ' + names.join(', ') : '';
  };
  const over = (e) => {
    e.preventDefault();
    root.classList.add('is-over');
  };
  const leave = () => root.classList.remove('is-over');
  const drop = (e) => {
    e.preventDefault();
    leave();
    input.files = e.dataTransfer.files;
    show();
  };

  root.addEventListener('dragover', over);
  root.addEventListener('dragleave', leave);
  root.addEventListener('drop', drop);
  input.addEventListener('change', show);
  return () => {
    root.removeEventListener('dragover', over);
    root.removeEventListener('dragleave', leave);
    root.removeEventListener('drop', drop);
    input.removeEventListener('change', show);
  };
}

document.querySelectorAll(".drop").forEach(setup);

React

FileDropzone.jsx
/* tearsheet file-dropzone vcd41a5dc · https://qusaialjbali.github.io/tearsheet/zine/parts/file-dropzone.html */
import { useEffect, useRef } from 'react';
import './FileDropzone.css';

function setup(root) {
  const input = root.querySelector('.drop-input');
  const list = root.querySelector('.drop-files');

  const show = () => {
    const names = [...input.files].map((f) => f.name);
    list.textContent = names.length ? names.length + ' selected: ' + names.join(', ') : '';
  };
  const over = (e) => {
    e.preventDefault();
    root.classList.add('is-over');
  };
  const leave = () => root.classList.remove('is-over');
  const drop = (e) => {
    e.preventDefault();
    leave();
    input.files = e.dataTransfer.files;
    show();
  };

  root.addEventListener('dragover', over);
  root.addEventListener('dragleave', leave);
  root.addEventListener('drop', drop);
  input.addEventListener('change', show);
  return () => {
    root.removeEventListener('dragover', over);
    root.removeEventListener('dragleave', leave);
    root.removeEventListener('drop', drop);
    input.removeEventListener('change', show);
  };
}

export function FileDropzone() {
  const ref = useRef(null);
  useEffect(() => setup(ref.current), []);

  return (
    <label ref={ref} className="drop">
      <input className="drop-input" type="file" multiple accept="image/*,.pdf" />
      <svg className="drop-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 16V4M7 9l5-5 5 5M4 15v3a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-3" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>
      <span className="drop-title">Drop files here or <u>browse</u></span>
      <span className="drop-hint">PNG, JPG or PDF, up to 10 MB each</span>
      <span className="drop-files" aria-live="polite"></span>
    </label>
  );
}
FileDropzone.css
/* tearsheet file-dropzone vcd41a5dc · https://qusaialjbali.github.io/tearsheet/zine/parts/file-dropzone.html */
.drop {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 6px;
  width: 100%;
  max-width: 440px;
  padding: 32px 24px;
  text-align: center;
  background: var(--ui-surface);
  border: 2px dashed var(--ui-border-strong);
  border-radius: var(--ui-radius);
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;
}
.drop:hover, .drop.is-over { border-color: var(--ui-accent); background: var(--ui-accent-soft); }
.drop:focus-within { box-shadow: var(--ui-focus); }
.drop-input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop-icon { width: 36px; height: 36px; margin-bottom: 6px; color: var(--ui-accent); }
.drop-title { font-weight: 600; color: var(--ui-text); }
.drop-title u { color: var(--ui-accent); text-underline-offset: 3px; }
.drop-hint { font-size: 13px; color: var(--ui-muted); }
.drop-files:not(:empty) {
  margin-top: 10px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ui-text);
  background: var(--ui-surface-2);
  border-radius: 999px;
}

Vue

FileDropzone.vue
<!-- tearsheet file-dropzone vcd41a5dc · https://qusaialjbali.github.io/tearsheet/zine/parts/file-dropzone.html -->
<script setup>
import { onBeforeUnmount, onMounted, ref } from 'vue';

const el = ref(null);
let cleanup;

function setup(root) {
  const input = root.querySelector('.drop-input');
  const list = root.querySelector('.drop-files');

  const show = () => {
    const names = [...input.files].map((f) => f.name);
    list.textContent = names.length ? names.length + ' selected: ' + names.join(', ') : '';
  };
  const over = (e) => {
    e.preventDefault();
    root.classList.add('is-over');
  };
  const leave = () => root.classList.remove('is-over');
  const drop = (e) => {
    e.preventDefault();
    leave();
    input.files = e.dataTransfer.files;
    show();
  };

  root.addEventListener('dragover', over);
  root.addEventListener('dragleave', leave);
  root.addEventListener('drop', drop);
  input.addEventListener('change', show);
  return () => {
    root.removeEventListener('dragover', over);
    root.removeEventListener('dragleave', leave);
    root.removeEventListener('drop', drop);
    input.removeEventListener('change', show);
  };
}

onMounted(() => {
  cleanup = setup(el.value);
});
onBeforeUnmount(() => cleanup?.());
</script>

<template>
  <label ref="el" class="drop">
    <input class="drop-input" type="file" multiple accept="image/*,.pdf">
    <svg class="drop-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 16V4M7 9l5-5 5 5M4 15v3a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-3" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
    <span class="drop-title">Drop files here or <u>browse</u></span>
    <span class="drop-hint">PNG, JPG or PDF, up to 10 MB each</span>
    <span class="drop-files" aria-live="polite"></span>
  </label>
</template>

<!-- Not scoped: setup() creates elements at runtime that scoped styles would miss. -->
<style>
.drop {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 6px;
  width: 100%;
  max-width: 440px;
  padding: 32px 24px;
  text-align: center;
  background: var(--ui-surface);
  border: 2px dashed var(--ui-border-strong);
  border-radius: var(--ui-radius);
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;
}
.drop:hover, .drop.is-over { border-color: var(--ui-accent); background: var(--ui-accent-soft); }
.drop:focus-within { box-shadow: var(--ui-focus); }
.drop-input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop-icon { width: 36px; height: 36px; margin-bottom: 6px; color: var(--ui-accent); }
.drop-title { font-weight: 600; color: var(--ui-text); }
.drop-title u { color: var(--ui-accent); text-underline-offset: 3px; }
.drop-hint { font-size: 13px; color: var(--ui-muted); }
.drop-files:not(:empty) {
  margin-top: 10px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ui-text);
  background: var(--ui-surface-2);
  border-radius: 999px;
}
</style>

Svelte

FileDropzone.svelte
<!-- tearsheet file-dropzone vcd41a5dc · https://qusaialjbali.github.io/tearsheet/zine/parts/file-dropzone.html -->
<script>
  import { onMount } from 'svelte';
  import './FileDropzone.css';

  let node;

  function setup(root) {
    const input = root.querySelector('.drop-input');
    const list = root.querySelector('.drop-files');

    const show = () => {
      const names = [...input.files].map((f) => f.name);
      list.textContent = names.length ? names.length + ' selected: ' + names.join(', ') : '';
    };
    const over = (e) => {
      e.preventDefault();
      root.classList.add('is-over');
    };
    const leave = () => root.classList.remove('is-over');
    const drop = (e) => {
      e.preventDefault();
      leave();
      input.files = e.dataTransfer.files;
      show();
    };

    root.addEventListener('dragover', over);
    root.addEventListener('dragleave', leave);
    root.addEventListener('drop', drop);
    input.addEventListener('change', show);
    return () => {
      root.removeEventListener('dragover', over);
      root.removeEventListener('dragleave', leave);
      root.removeEventListener('drop', drop);
      input.removeEventListener('change', show);
    };
  }

  onMount(() => setup(node));
</script>

<label bind:this={node} class="drop">
  <input class="drop-input" type="file" multiple accept="image/*,.pdf">
  <svg class="drop-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 16V4M7 9l5-5 5 5M4 15v3a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-3" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
  <span class="drop-title">Drop files here or <u>browse</u></span>
  <span class="drop-hint">PNG, JPG or PDF, up to 10 MB each</span>
  <span class="drop-files" aria-live="polite"></span>
</label>
FileDropzone.css
/* tearsheet file-dropzone vcd41a5dc · https://qusaialjbali.github.io/tearsheet/zine/parts/file-dropzone.html */
.drop {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 6px;
  width: 100%;
  max-width: 440px;
  padding: 32px 24px;
  text-align: center;
  background: var(--ui-surface);
  border: 2px dashed var(--ui-border-strong);
  border-radius: var(--ui-radius);
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;
}
.drop:hover, .drop.is-over { border-color: var(--ui-accent); background: var(--ui-accent-soft); }
.drop:focus-within { box-shadow: var(--ui-focus); }
.drop-input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop-icon { width: 36px; height: 36px; margin-bottom: 6px; color: var(--ui-accent); }
.drop-title { font-weight: 600; color: var(--ui-text); }
.drop-title u { color: var(--ui-accent); text-underline-offset: 3px; }
.drop-hint { font-size: 13px; color: var(--ui-muted); }
.drop-files:not(:empty) {
  margin-top: 10px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ui-text);
  background: var(--ui-surface-2);
  border-radius: 999px;
}