File Upload
A drop area with a file picker, thumbnails, limits and error messages for one or several files. The chosen files stay on the input, so a form posts them.
Basic usage
A dashed drop area with a link-style label. The whole area opens the file picker; the real <input type="file"> is hidden but keeps the keyboard focus, so Tab, Enter and Space work. Without multiple a new file replaces the old one.
Resume
One file. Choosing another replaces it.
<div
id="upload-resume-field"
x-data='fileUpload({"maxFiles":0,"maxSize":0})'
x-on:dragover.prevent="dragging = true"
x-on:dragleave.prevent="dragging = false"
x-on:drop.prevent="drop($event)"
class="w-full group/field"
>
<p
id="upload-resume-label"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[input:disabled]/field:opacity-50 group-has-[input:required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>
Resume
</p>
<div
x-bind:data-dragging="dragging"
x-bind:aria-invalid="errors.length ? 'true' : null"
class="relative flex flex-col items-center justify-center gap-1 px-6 py-8 text-center bg-white dark:bg-dominant-950 rounded-lg border-2 border-dashed border-dominant-500 dark:border-dominant-400 transition-colors hover:border-primary-600 dark:hover:border-primary-400 hover:bg-primary-50 dark:hover:bg-dominant-900 has-focus-visible:border-primary-600 dark:has-focus-visible:border-primary-400 has-focus-visible:ring-2 has-focus-visible:ring-primary-500/30 dark:has-focus-visible:ring-primary-400/30 data-[dragging=true]:border-primary-600 dark:data-[dragging=true]:border-primary-400 data-[dragging=true]:bg-primary-50 dark:data-[dragging=true]:bg-dominant-900 aria-invalid:border-danger-600 dark:aria-invalid:border-danger-400 has-disabled:pointer-events-none has-disabled:opacity-50"
>
<i
aria-hidden="true"
class="text-2xl text-dominant-600 dark:text-dominant-300 fa-solid fa-cloud-arrow-up"
>
</i>
<label
for="upload-resume"
class="text-sm font-semibold text-primary-700 dark:text-primary-300 cursor-pointer after:absolute after:inset-0 hover:underline"
>
<span>Choose a file</span>
</label>
<span class="text-sm text-dominant-700 dark:text-dominant-200">or drag and drop</span>
<span id="upload-resume-limits" class="text-xs text-dominant-600 dark:text-dominant-300"
>PDF or Word</span
>
<input
id="upload-resume"
class="sr-only"
accept=".pdf,.doc,.docx"
type="file"
x-ref="input"
aria-labelledby="upload-resume-label"
aria-describedby="upload-resume-limits upload-resume-help upload-resume-errors"
x-on:change="add($event.target.files)"
/>
</div>
<p id="upload-resume-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
One file. Choosing another replaces it.
</p>
<div id="upload-resume-errors" role="alert" class="empty:hidden">
<template x-if="errors.length">
<ul class="space-y-1 mt-1.5 text-sm font-medium text-danger-700 dark:text-danger-300">
<template x-for="error in errors" x-bind:key="error">
<li class="flex items-start gap-1.5">
<i aria-hidden="true" class="mt-0.5 fa-solid fa-circle-exclamation"> </i>
<span x-text="error"></span>
</li>
</template>
</ul>
</template>
</div>
<p role="status" x-text="files.length ? summary : ''" class="sr-only"></p>
<template x-if="files.length">
<ul aria-label="Selected files" class="grid gap-2 mt-3 sm:grid-cols-2">
<template x-for="item in files" x-bind:key="item.id">
<li
class="flex items-center gap-3 p-2 bg-white dark:bg-dominant-950 rounded-lg border border-dominant-300 dark:border-dominant-600"
>
<img
x-show="item.url"
x-bind:src="item.url || null"
alt
class="shrink-0 object-cover size-12 rounded-md"
/>
<span
x-show="!item.url"
class="inline-flex shrink-0 items-center justify-center size-12 text-dominant-600 dark:text-dominant-300 bg-dominant-100 dark:bg-dominant-800 rounded-md"
>
<i aria-hidden="true" class="fa-solid fa-file"> </i>
</span>
<span class="flex-1 min-w-0">
<span
x-text="item.name"
class="block truncate text-sm font-medium text-dominant-900 dark:text-white"
>
</span>
<span x-text="item.size" class="block text-xs text-dominant-600 dark:text-dominant-300">
</span>
</span>
<button
type="button"
x-bind:aria-label="'Remove ' + item.name"
x-on:click="remove(item.id)"
class="inline-flex shrink-0 items-center justify-center size-8 text-dominant-600 dark:text-dominant-300 rounded-md cursor-pointer hover:bg-dominant-100 dark:hover:bg-dominant-800 hover:text-danger-700 dark:hover:text-danger-300 focus-visible:ring-2 focus-visible:ring-primary-500 dark:focus-visible:ring-primary-400 focus-visible:outline-none"
>
<i aria-hidden="true" class="fa-solid fa-xmark"> </i>
</button>
</li>
</template>
</ul>
</template>
</div>
Multiple images
Add multiple and the field keeps a list. Images get a thumbnail; other files get an icon. Each row has a button named “Remove” plus the file name, and a hidden status line tells a screen reader how many files are selected.
Product photos
The first photo is the cover.
<div
id="upload-photos-field"
x-data='fileUpload({"maxFiles":5,"maxSize":2097152})'
x-on:dragover.prevent="dragging = true"
x-on:dragleave.prevent="dragging = false"
x-on:drop.prevent="drop($event)"
class="w-full group/field"
>
<p
id="upload-photos-label"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[input:disabled]/field:opacity-50 group-has-[input:required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>
Product photos
</p>
<div
x-bind:data-dragging="dragging"
x-bind:aria-invalid="errors.length ? 'true' : null"
class="relative flex flex-col items-center justify-center gap-1 px-6 py-8 text-center bg-white dark:bg-dominant-950 rounded-lg border-2 border-dashed border-dominant-500 dark:border-dominant-400 transition-colors hover:border-primary-600 dark:hover:border-primary-400 hover:bg-primary-50 dark:hover:bg-dominant-900 has-focus-visible:border-primary-600 dark:has-focus-visible:border-primary-400 has-focus-visible:ring-2 has-focus-visible:ring-primary-500/30 dark:has-focus-visible:ring-primary-400/30 data-[dragging=true]:border-primary-600 dark:data-[dragging=true]:border-primary-400 data-[dragging=true]:bg-primary-50 dark:data-[dragging=true]:bg-dominant-900 aria-invalid:border-danger-600 dark:aria-invalid:border-danger-400 has-disabled:pointer-events-none has-disabled:opacity-50"
>
<i
aria-hidden="true"
class="text-2xl text-dominant-600 dark:text-dominant-300 fa-solid fa-cloud-arrow-up"
>
</i>
<label
for="upload-photos"
class="text-sm font-semibold text-primary-700 dark:text-primary-300 cursor-pointer after:absolute after:inset-0 hover:underline"
>
<span>Choose photos</span>
</label>
<span class="text-sm text-dominant-700 dark:text-dominant-200">or drag and drop</span>
<span id="upload-photos-limits" class="text-xs text-dominant-600 dark:text-dominant-300"
>JPG, PNG or WebP, up to 2 MB each, 5 photos</span
>
<input
id="upload-photos"
class="sr-only"
multiple
accept="image/jpeg,image/png,image/webp"
type="file"
x-ref="input"
aria-labelledby="upload-photos-label"
aria-describedby="upload-photos-limits upload-photos-help upload-photos-errors"
x-on:change="add($event.target.files)"
/>
</div>
<p id="upload-photos-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
The first photo is the cover.
</p>
<div id="upload-photos-errors" role="alert" class="empty:hidden">
<template x-if="errors.length">
<ul class="space-y-1 mt-1.5 text-sm font-medium text-danger-700 dark:text-danger-300">
<template x-for="error in errors" x-bind:key="error">
<li class="flex items-start gap-1.5">
<i aria-hidden="true" class="mt-0.5 fa-solid fa-circle-exclamation"> </i>
<span x-text="error"></span>
</li>
</template>
</ul>
</template>
</div>
<p role="status" x-text="files.length ? summary : ''" class="sr-only"></p>
<template x-if="files.length">
<ul aria-label="Selected files" class="grid gap-2 mt-3 sm:grid-cols-2">
<template x-for="item in files" x-bind:key="item.id">
<li
class="flex items-center gap-3 p-2 bg-white dark:bg-dominant-950 rounded-lg border border-dominant-300 dark:border-dominant-600"
>
<img
x-show="item.url"
x-bind:src="item.url || null"
alt
class="shrink-0 object-cover size-12 rounded-md"
/>
<span
x-show="!item.url"
class="inline-flex shrink-0 items-center justify-center size-12 text-dominant-600 dark:text-dominant-300 bg-dominant-100 dark:bg-dominant-800 rounded-md"
>
<i aria-hidden="true" class="fa-solid fa-file"> </i>
</span>
<span class="flex-1 min-w-0">
<span
x-text="item.name"
class="block truncate text-sm font-medium text-dominant-900 dark:text-white"
>
</span>
<span x-text="item.size" class="block text-xs text-dominant-600 dark:text-dominant-300">
</span>
</span>
<button
type="button"
x-bind:aria-label="'Remove ' + item.name"
x-on:click="remove(item.id)"
class="inline-flex shrink-0 items-center justify-center size-8 text-dominant-600 dark:text-dominant-300 rounded-md cursor-pointer hover:bg-dominant-100 dark:hover:bg-dominant-800 hover:text-danger-700 dark:hover:text-danger-300 focus-visible:ring-2 focus-visible:ring-primary-500 dark:focus-visible:ring-primary-400 focus-visible:outline-none"
>
<i aria-hidden="true" class="fa-solid fa-xmark"> </i>
</button>
</li>
</template>
</ul>
</template>
</div>
Limits and errors
A file with the wrong type, a file over maxSize and files past maxFiles are not added. The reasons are listed under the field (role="alert") and the drop area turns red with aria-invalid. The next change clears them.
Logo
Try a JPG, a larger image or a third file to see the messages.
<div
id="upload-limits-field"
x-data='fileUpload({"maxFiles":2,"maxSize":102400})'
x-on:dragover.prevent="dragging = true"
x-on:dragleave.prevent="dragging = false"
x-on:drop.prevent="drop($event)"
class="w-full group/field"
>
<p
id="upload-limits-label"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[input:disabled]/field:opacity-50 group-has-[input:required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>
Logo
</p>
<div
x-bind:data-dragging="dragging"
x-bind:aria-invalid="errors.length ? 'true' : null"
class="relative flex flex-col items-center justify-center gap-1 px-6 py-8 text-center bg-white dark:bg-dominant-950 rounded-lg border-2 border-dashed border-dominant-500 dark:border-dominant-400 transition-colors hover:border-primary-600 dark:hover:border-primary-400 hover:bg-primary-50 dark:hover:bg-dominant-900 has-focus-visible:border-primary-600 dark:has-focus-visible:border-primary-400 has-focus-visible:ring-2 has-focus-visible:ring-primary-500/30 dark:has-focus-visible:ring-primary-400/30 data-[dragging=true]:border-primary-600 dark:data-[dragging=true]:border-primary-400 data-[dragging=true]:bg-primary-50 dark:data-[dragging=true]:bg-dominant-900 aria-invalid:border-danger-600 dark:aria-invalid:border-danger-400 has-disabled:pointer-events-none has-disabled:opacity-50"
>
<i
aria-hidden="true"
class="text-2xl text-dominant-600 dark:text-dominant-300 fa-solid fa-cloud-arrow-up"
>
</i>
<label
for="upload-limits"
class="text-sm font-semibold text-primary-700 dark:text-primary-300 cursor-pointer after:absolute after:inset-0 hover:underline"
>
<span>Choose a file</span>
</label>
<span class="text-sm text-dominant-700 dark:text-dominant-200">or drag and drop</span>
<span id="upload-limits-limits" class="text-xs text-dominant-600 dark:text-dominant-300"
>PNG only, up to 100 KB each, 2 files</span
>
<input
id="upload-limits"
class="sr-only"
multiple
accept="image/png"
type="file"
x-ref="input"
aria-labelledby="upload-limits-label"
aria-describedby="upload-limits-limits upload-limits-help upload-limits-errors"
x-on:change="add($event.target.files)"
/>
</div>
<p id="upload-limits-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
Try a JPG, a larger image or a third file to see the messages.
</p>
<div id="upload-limits-errors" role="alert" class="empty:hidden">
<template x-if="errors.length">
<ul class="space-y-1 mt-1.5 text-sm font-medium text-danger-700 dark:text-danger-300">
<template x-for="error in errors" x-bind:key="error">
<li class="flex items-start gap-1.5">
<i aria-hidden="true" class="mt-0.5 fa-solid fa-circle-exclamation"> </i>
<span x-text="error"></span>
</li>
</template>
</ul>
</template>
</div>
<p role="status" x-text="files.length ? summary : ''" class="sr-only"></p>
<template x-if="files.length">
<ul aria-label="Selected files" class="grid gap-2 mt-3 sm:grid-cols-2">
<template x-for="item in files" x-bind:key="item.id">
<li
class="flex items-center gap-3 p-2 bg-white dark:bg-dominant-950 rounded-lg border border-dominant-300 dark:border-dominant-600"
>
<img
x-show="item.url"
x-bind:src="item.url || null"
alt
class="shrink-0 object-cover size-12 rounded-md"
/>
<span
x-show="!item.url"
class="inline-flex shrink-0 items-center justify-center size-12 text-dominant-600 dark:text-dominant-300 bg-dominant-100 dark:bg-dominant-800 rounded-md"
>
<i aria-hidden="true" class="fa-solid fa-file"> </i>
</span>
<span class="flex-1 min-w-0">
<span
x-text="item.name"
class="block truncate text-sm font-medium text-dominant-900 dark:text-white"
>
</span>
<span x-text="item.size" class="block text-xs text-dominant-600 dark:text-dominant-300">
</span>
</span>
<button
type="button"
x-bind:aria-label="'Remove ' + item.name"
x-on:click="remove(item.id)"
class="inline-flex shrink-0 items-center justify-center size-8 text-dominant-600 dark:text-dominant-300 rounded-md cursor-pointer hover:bg-dominant-100 dark:hover:bg-dominant-800 hover:text-danger-700 dark:hover:text-danger-300 focus-visible:ring-2 focus-visible:ring-primary-500 dark:focus-visible:ring-primary-400 focus-visible:outline-none"
>
<i aria-hidden="true" class="fa-solid fa-xmark"> </i>
</button>
</li>
</template>
</ul>
</template>
</div>
States
disabled turns the area off and fades it. required is native: the form will not submit until a file is chosen, because the chosen files are set on the input. error shows a message from the server and turns the area red.
Disabled
Contract
The file could not be scanned. Upload it again.
<div
id="upload-disabled-field"
x-data='fileUpload({"maxFiles":0,"maxSize":0})'
x-on:dragover.prevent="dragging = true"
x-on:dragleave.prevent="dragging = false"
x-on:drop.prevent="drop($event)"
class="w-full group/field"
>
<p
id="upload-disabled-label"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[input:disabled]/field:opacity-50 group-has-[input:required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>
Disabled
</p>
<div
x-bind:data-dragging="dragging"
x-bind:aria-invalid="errors.length ? 'true' : null"
class="relative flex flex-col items-center justify-center gap-1 px-6 py-8 text-center bg-white dark:bg-dominant-950 rounded-lg border-2 border-dashed border-dominant-500 dark:border-dominant-400 transition-colors hover:border-primary-600 dark:hover:border-primary-400 hover:bg-primary-50 dark:hover:bg-dominant-900 has-focus-visible:border-primary-600 dark:has-focus-visible:border-primary-400 has-focus-visible:ring-2 has-focus-visible:ring-primary-500/30 dark:has-focus-visible:ring-primary-400/30 data-[dragging=true]:border-primary-600 dark:data-[dragging=true]:border-primary-400 data-[dragging=true]:bg-primary-50 dark:data-[dragging=true]:bg-dominant-900 aria-invalid:border-danger-600 dark:aria-invalid:border-danger-400 has-disabled:pointer-events-none has-disabled:opacity-50"
>
<i
aria-hidden="true"
class="text-2xl text-dominant-600 dark:text-dominant-300 fa-solid fa-cloud-arrow-up"
>
</i>
<label
for="upload-disabled"
class="text-sm font-semibold text-primary-700 dark:text-primary-300 cursor-pointer after:absolute after:inset-0 hover:underline"
>
<span>Choose a file</span>
</label>
<span class="text-sm text-dominant-700 dark:text-dominant-200">or drag and drop</span>
<span id="upload-disabled-limits" class="text-xs text-dominant-600 dark:text-dominant-300"
>Not available</span
>
<input
id="upload-disabled"
class="sr-only"
disabled
type="file"
x-ref="input"
aria-labelledby="upload-disabled-label"
aria-describedby="upload-disabled-limits upload-disabled-errors"
x-on:change="add($event.target.files)"
/>
</div>
<div id="upload-disabled-errors" role="alert" class="empty:hidden">
<template x-if="errors.length">
<ul class="space-y-1 mt-1.5 text-sm font-medium text-danger-700 dark:text-danger-300">
<template x-for="error in errors" x-bind:key="error">
<li class="flex items-start gap-1.5">
<i aria-hidden="true" class="mt-0.5 fa-solid fa-circle-exclamation"> </i>
<span x-text="error"></span>
</li>
</template>
</ul>
</template>
</div>
<p role="status" x-text="files.length ? summary : ''" class="sr-only"></p>
<template x-if="files.length">
<ul aria-label="Selected files" class="grid gap-2 mt-3 sm:grid-cols-2">
<template x-for="item in files" x-bind:key="item.id">
<li
class="flex items-center gap-3 p-2 bg-white dark:bg-dominant-950 rounded-lg border border-dominant-300 dark:border-dominant-600"
>
<img
x-show="item.url"
x-bind:src="item.url || null"
alt
class="shrink-0 object-cover size-12 rounded-md"
/>
<span
x-show="!item.url"
class="inline-flex shrink-0 items-center justify-center size-12 text-dominant-600 dark:text-dominant-300 bg-dominant-100 dark:bg-dominant-800 rounded-md"
>
<i aria-hidden="true" class="fa-solid fa-file"> </i>
</span>
<span class="flex-1 min-w-0">
<span
x-text="item.name"
class="block truncate text-sm font-medium text-dominant-900 dark:text-white"
>
</span>
<span x-text="item.size" class="block text-xs text-dominant-600 dark:text-dominant-300">
</span>
</span>
<button
type="button"
x-bind:aria-label="'Remove ' + item.name"
x-on:click="remove(item.id)"
class="inline-flex shrink-0 items-center justify-center size-8 text-dominant-600 dark:text-dominant-300 rounded-md cursor-pointer hover:bg-dominant-100 dark:hover:bg-dominant-800 hover:text-danger-700 dark:hover:text-danger-300 focus-visible:ring-2 focus-visible:ring-primary-500 dark:focus-visible:ring-primary-400 focus-visible:outline-none"
>
<i aria-hidden="true" class="fa-solid fa-xmark"> </i>
</button>
</li>
</template>
</ul>
</template>
</div>
<form class="space-y-3 w-full">
<div
id="upload-required-field"
x-data='fileUpload({"maxFiles":0,"maxSize":0})'
x-on:dragover.prevent="dragging = true"
x-on:dragleave.prevent="dragging = false"
x-on:drop.prevent="drop($event)"
class="w-full group/field"
>
<p
id="upload-required-label"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[input:disabled]/field:opacity-50 group-has-[input:required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>
Invoice
</p>
<div
x-bind:data-dragging="dragging"
x-bind:aria-invalid="errors.length ? 'true' : null"
class="relative flex flex-col items-center justify-center gap-1 px-6 py-8 text-center bg-white dark:bg-dominant-950 rounded-lg border-2 border-dashed border-dominant-500 dark:border-dominant-400 transition-colors hover:border-primary-600 dark:hover:border-primary-400 hover:bg-primary-50 dark:hover:bg-dominant-900 has-focus-visible:border-primary-600 dark:has-focus-visible:border-primary-400 has-focus-visible:ring-2 has-focus-visible:ring-primary-500/30 dark:has-focus-visible:ring-primary-400/30 data-[dragging=true]:border-primary-600 dark:data-[dragging=true]:border-primary-400 data-[dragging=true]:bg-primary-50 dark:data-[dragging=true]:bg-dominant-900 aria-invalid:border-danger-600 dark:aria-invalid:border-danger-400 has-disabled:pointer-events-none has-disabled:opacity-50"
>
<i
aria-hidden="true"
class="text-2xl text-dominant-600 dark:text-dominant-300 fa-solid fa-cloud-arrow-up"
>
</i>
<label
for="upload-required"
class="text-sm font-semibold text-primary-700 dark:text-primary-300 cursor-pointer after:absolute after:inset-0 hover:underline"
>
<span>Choose a file</span>
</label>
<span class="text-sm text-dominant-700 dark:text-dominant-200">or drag and drop</span>
<span id="upload-required-limits" class="text-xs text-dominant-600 dark:text-dominant-300"
>PDF</span
>
<input
id="upload-required"
class="sr-only"
name="invoice"
accept=".pdf"
required
type="file"
x-ref="input"
aria-labelledby="upload-required-label"
aria-describedby="upload-required-limits upload-required-help upload-required-errors"
x-on:change="add($event.target.files)"
/>
</div>
<p id="upload-required-help" class="mt-1.5 text-sm text-dominant-600 dark:text-dominant-300">
Press Submit without a file to see the browser message.
</p>
<div id="upload-required-errors" role="alert" class="empty:hidden">
<template x-if="errors.length">
<ul class="space-y-1 mt-1.5 text-sm font-medium text-danger-700 dark:text-danger-300">
<template x-for="error in errors" x-bind:key="error">
<li class="flex items-start gap-1.5">
<i aria-hidden="true" class="mt-0.5 fa-solid fa-circle-exclamation"> </i>
<span x-text="error"></span>
</li>
</template>
</ul>
</template>
</div>
<p role="status" x-text="files.length ? summary : ''" class="sr-only"></p>
<template x-if="files.length">
<ul aria-label="Selected files" class="grid gap-2 mt-3 sm:grid-cols-2">
<template x-for="item in files" x-bind:key="item.id">
<li
class="flex items-center gap-3 p-2 bg-white dark:bg-dominant-950 rounded-lg border border-dominant-300 dark:border-dominant-600"
>
<img
x-show="item.url"
x-bind:src="item.url || null"
alt
class="shrink-0 object-cover size-12 rounded-md"
/>
<span
x-show="!item.url"
class="inline-flex shrink-0 items-center justify-center size-12 text-dominant-600 dark:text-dominant-300 bg-dominant-100 dark:bg-dominant-800 rounded-md"
>
<i aria-hidden="true" class="fa-solid fa-file"> </i>
</span>
<span class="flex-1 min-w-0">
<span
x-text="item.name"
class="block truncate text-sm font-medium text-dominant-900 dark:text-white"
>
</span>
<span
x-text="item.size"
class="block text-xs text-dominant-600 dark:text-dominant-300"
>
</span>
</span>
<button
type="button"
x-bind:aria-label="'Remove ' + item.name"
x-on:click="remove(item.id)"
class="inline-flex shrink-0 items-center justify-center size-8 text-dominant-600 dark:text-dominant-300 rounded-md cursor-pointer hover:bg-dominant-100 dark:hover:bg-dominant-800 hover:text-danger-700 dark:hover:text-danger-300 focus-visible:ring-2 focus-visible:ring-primary-500 dark:focus-visible:ring-primary-400 focus-visible:outline-none"
>
<i aria-hidden="true" class="fa-solid fa-xmark"> </i>
</button>
</li>
</template>
</ul>
</template>
</div>
<button
id="button-1869"
type="submit"
class="cursor-pointer transition-colors inline-flex items-center justify-center rounded-lg font-semibold group peer focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50 dark:focus-visible:ring-offset-dominant-950 bg-primary-600 text-white shadow-sm not-disabled:hover:bg-primary-700 not-disabled:active:bg-primary-800 focus-visible:ring-primary-700 dark:focus-visible:ring-primary-400 gap-2 px-4 py-2.5 text-sm !bg-primary-700 !shadow-none"
>
<span>Submit</span>
</button>
</form>
<div
id="upload-server-error-field"
x-data='fileUpload({"maxFiles":0,"maxSize":0})'
x-on:dragover.prevent="dragging = true"
x-on:dragleave.prevent="dragging = false"
x-on:drop.prevent="drop($event)"
class="w-full group/field"
>
<p
id="upload-server-error-label"
class="block mb-1.5 text-sm font-medium text-dominant-700 dark:text-dominant-200 group-has-[input:disabled]/field:opacity-50 group-has-[input:required]/field:after:content-['*'] after:ml-0.5 after:text-danger-700 dark:after:text-danger-300"
>
Contract
</p>
<div
x-bind:data-dragging="dragging"
x-bind:aria-invalid="'true'"
class="relative flex flex-col items-center justify-center gap-1 px-6 py-8 text-center bg-white dark:bg-dominant-950 rounded-lg border-2 border-dashed border-dominant-500 dark:border-dominant-400 transition-colors hover:border-primary-600 dark:hover:border-primary-400 hover:bg-primary-50 dark:hover:bg-dominant-900 has-focus-visible:border-primary-600 dark:has-focus-visible:border-primary-400 has-focus-visible:ring-2 has-focus-visible:ring-primary-500/30 dark:has-focus-visible:ring-primary-400/30 data-[dragging=true]:border-primary-600 dark:data-[dragging=true]:border-primary-400 data-[dragging=true]:bg-primary-50 dark:data-[dragging=true]:bg-dominant-900 aria-invalid:border-danger-600 dark:aria-invalid:border-danger-400 has-disabled:pointer-events-none has-disabled:opacity-50"
>
<i
aria-hidden="true"
class="text-2xl text-dominant-600 dark:text-dominant-300 fa-solid fa-cloud-arrow-up"
>
</i>
<label
for="upload-server-error"
class="text-sm font-semibold text-primary-700 dark:text-primary-300 cursor-pointer after:absolute after:inset-0 hover:underline"
>
<span>Choose a file</span>
</label>
<span class="text-sm text-dominant-700 dark:text-dominant-200">or drag and drop</span>
<span id="upload-server-error-limits" class="text-xs text-dominant-600 dark:text-dominant-300"
>PDF, up to 5 MB</span
>
<input
id="upload-server-error"
class="sr-only"
accept=".pdf"
type="file"
x-ref="input"
aria-labelledby="upload-server-error-label"
aria-describedby="upload-server-error-limits upload-server-error-error upload-server-error-errors"
aria-invalid="true"
x-on:change="add($event.target.files)"
/>
</div>
<p
id="upload-server-error-error"
class="flex items-start gap-1.5 mt-1.5 text-sm font-medium text-danger-700 dark:text-danger-300"
>
<i aria-hidden="true" class="mt-0.5 fa-solid fa-circle-exclamation"></i
><span>The file could not be scanned. Upload it again.</span>
</p>
<div id="upload-server-error-errors" role="alert" class="empty:hidden">
<template x-if="errors.length">
<ul class="space-y-1 mt-1.5 text-sm font-medium text-danger-700 dark:text-danger-300">
<template x-for="error in errors" x-bind:key="error">
<li class="flex items-start gap-1.5">
<i aria-hidden="true" class="mt-0.5 fa-solid fa-circle-exclamation"> </i>
<span x-text="error"></span>
</li>
</template>
</ul>
</template>
</div>
<p role="status" x-text="files.length ? summary : ''" class="sr-only"></p>
<template x-if="files.length">
<ul aria-label="Selected files" class="grid gap-2 mt-3 sm:grid-cols-2">
<template x-for="item in files" x-bind:key="item.id">
<li
class="flex items-center gap-3 p-2 bg-white dark:bg-dominant-950 rounded-lg border border-dominant-300 dark:border-dominant-600"
>
<img
x-show="item.url"
x-bind:src="item.url || null"
alt
class="shrink-0 object-cover size-12 rounded-md"
/>
<span
x-show="!item.url"
class="inline-flex shrink-0 items-center justify-center size-12 text-dominant-600 dark:text-dominant-300 bg-dominant-100 dark:bg-dominant-800 rounded-md"
>
<i aria-hidden="true" class="fa-solid fa-file"> </i>
</span>
<span class="flex-1 min-w-0">
<span
x-text="item.name"
class="block truncate text-sm font-medium text-dominant-900 dark:text-white"
>
</span>
<span x-text="item.size" class="block text-xs text-dominant-600 dark:text-dominant-300">
</span>
</span>
<button
type="button"
x-bind:aria-label="'Remove ' + item.name"
x-on:click="remove(item.id)"
class="inline-flex shrink-0 items-center justify-center size-8 text-dominant-600 dark:text-dominant-300 rounded-md cursor-pointer hover:bg-dominant-100 dark:hover:bg-dominant-800 hover:text-danger-700 dark:hover:text-danger-300 focus-visible:ring-2 focus-visible:ring-primary-500 dark:focus-visible:ring-primary-400 focus-visible:outline-none"
>
<i aria-hidden="true" class="fa-solid fa-xmark"> </i>
</button>
</li>
</template>
</ul>
</template>
</div>
Options reference
Two numbers go in x-data="fileUpload({ … })". The rest is plain HTML on the <input type="file">.
| Option | Type | Default | Description |
|---|---|---|---|
maxFiles
|
number |
0 (no limit)
|
Most files the field holds. Only with multiple. Files past the limit are skipped and the error is listed.
|
maxSize
|
number (bytes) |
0 (no limit)
|
Largest size of one file, for example 2 * 1024 * 1024 for 2 MB.
|
multiple
|
input attribute | one file | Several files. Without it a new file replaces the old one. |
accept
|
input attribute | any file |
The same values as the browser: .pdf, image/png, image/*. It filters the picker and the same rule checks dropped files.
|
name
|
input attribute | – | The chosen files are set on the input, so a form posts them under this name. |
required, disabled
|
input attributes | – |
Native: required blocks the form until a file is chosen, disabled turns the whole field off.
|
upload-change
|
event | – |
Fires on the root after every change, with { files }: the accepted File objects.
|
files, errors
|
properties | – |
The selected items ({ id, file, name, size, url }) and the messages of the last change, for your own markup.
|