- LaralCN-UI: shadcn-style Blade components and blocks you own · part 17 of 26
- Laravel
- Blade
- Javascript
- LaralCN-UI
LaralCN-UI file upload: a dropzone that keeps the native input
The LaralCN-UI file upload is a drag-and-drop zone with a file list, image previews and type and size checks, built on top of a normal file input that still submits with the form. Most of the work went into making drops and repeat picks add to the input instead of replacing it.
4 min read
file-upload is a dashed dropzone you can click or drop files onto, with a list underneath that shows each file's name and size, plus a thumbnail for images. It checks accept and max-size in the browser and lists rejected files with an error instead of adding them. Under all of that sits a plain <input type="file">, and that input is still the field your form submits.
Why the native input stays
Most dropzone libraries hide the file input and keep their own array of files, then upload them over XHR. That breaks the two ways Laravel developers send files: a normal form post with enctype="multipart/form-data", and Livewire's WithFileUploads, which watches the input through wire:model. So the input stays in the DOM, inside the dropzone's <label>, hidden with sr-only rather than display: none so it can still take focus:
<label data-ui-file-upload-zone class="flex cursor-pointer flex-col ... has-focus-visible:ring-[3px] ...">
<input type="file" data-ui-file-upload-input class="sr-only" ... {{ $attributes->except('class') }} />
Clicking anywhere in the zone opens the picker because it is a label. Keyboard users tab to the input and the whole zone gets the focus ring through has-focus-visible:. Every attribute you pass, including name, id, required and wire:model, goes onto the input.
Writing files into an input
A file input's files property is read-only in spirit but assignable in practice: you can hand it a FileList from a DataTransfer object. The component keeps its own list of items (the file, plus an error or a preview URL where needed), and after every change rebuilds the input from the accepted ones:
function sync(root) {
var transfer = new DataTransfer();
items(root).forEach(function(item) {
if (!item.error) transfer.items.add(item.file);
});
input(root).files = transfer.files;
paint(root);
}
Rejected files show up in the list with a red border and the message, but never reach the input, so the server never sees them. In single mode a new file replaces the old one. In multiple mode new files merge in, and a file that matches one already in the list on name, size and lastModified is skipped.
The picker kept wiping the list
Drops were easy because the browser does nothing with them by default. The picker was the harder case. When you click the zone, choose two files, then click again and choose a third, the browser replaces the input's files with just the third. If the component merged the list back in the normal bubbling phase, any change listener the consumer had on the input would run first and see one file, then the list would quietly grow back to three behind its back.
So the component listens in the capture phase on document, which runs before any listener on the input or its form:
// Capture phase: runs before any listener on the input or its form,
// so by the time a consumer sees the picker's change event the input
// already holds the merged, validated file list.
document.addEventListener('change', function(event) {
if (!event.isTrusted || !event.target.matches('[data-ui-file-upload-input]')) return;
add(event.target.closest('[data-ui-file-upload]'), event.target.files);
}, true);
The isTrusted check matters. After a drop or a removal the component fires its own change event so consumers hear about it, and without the check that synthetic event would feed the merged list back into add.
Checks, previews and focus
accept understands the same forms as the native attribute: extensions like .pdf, wildcards like image/*, and exact MIME types such as application/pdf. max-size takes bytes or a string like 500KB or 10MB, counted in powers of 1024. If you pass neither hint nor custom copy, the component builds a hint like "PNG, JPG, PDF up to 10MB" from those two props.
Image thumbnails use URL.createObjectURL, and the URL is revoked when the file is removed or replaced so large photos do not sit in memory. When you remove a file with the keyboard, focus moves to the next remove button, or back to the input if the list is empty, instead of dropping to the top of the page.
It does not upload
That is deliberate. The component prepares a file input; sending it is your app's job. A plain form post works with no extra code. For an AJAX upload, the usage example points at fetch('/upload', { method: 'POST', body: new FormData(field.form) }), and for Livewire you add WithFileUploads and put wire:model="photos" on the component. Progress bars and chunked uploads belong to whichever of those you choose.
Where it is
composer require --dev safi/laralcn-ui
php artisan ui:init
php artisan ui:add file-upload
Docs and demos: https://laralcn-ui.abdulkadersafi.com/components/file-upload (Arabic at /ar/components/file-upload). Source: https://github.com/Abdulkader-Safi/LaralCN-UI. MIT licensed.
Next: the stepper, the one new component with no JavaScript at all.
Last updated 1 Dec 2026 · filed under Laravel, blade, javascript, laralcn-ui