Use it for
Use BbDropzone when the user hands you files: receipts, attachments, an avatar, an import.
Use something else when
useBbDropzone— a button opens the file picker and no field is needed, like a row's ReplaceBbProgress— the task is showing an upload already in progress
Also see: Validated forms.
BbDropzone is the library's file field. It takes a label, hint and errors like every field, and its v-model is always a File[]. It captures and validates files; it does not upload them.
Passthrough
Hover or tap a part to outline it. The toggles turn on loading, errors and warnings where the component has them; only parts get a row.
Validate receipt attachments
Set accept, max-size and max-files, and turn every rejected file into a message the user can act on.
Drag receipts here
PNG, JPG or PDF · at most 3 files · 512 KB each
No receipts attached yet.
<template>
<div class="flex max-w-md flex-col gap-3">
<BbDropzone
id="receipts"
v-model="receipts"
:accept="[
'image/png',
'.png',
'image/jpeg',
'.jpg',
'application/pdf',
'.pdf',
]"
:errors="messages"
label="Receipts"
:max-files="3"
:max-size="512 * 1024"
@error="(errors) => (messages = errors.map(describe))"
@update:model-value="messages = []"
>
<template #default="{ open, dragging, focused }">
<div
class="zone"
:class="{
'zone--dragging': dragging,
'zone--error': messages.length,
'bb-ring': focused,
}"
>
<BbIcon class="opacity-60" icon="lucide:cloud-upload" size="lg" />
<p class="text-sm font-medium">Drag receipts here</p>
<p class="text-xs opacity-60">
PNG, JPG or PDF · at most 3 files · 512 KB each
</p>
<BbButton size="sm" variant="outline" @click="open">
Browse files
</BbButton>
</div>
</template>
</BbDropzone>
<!-- Only accepted files reach the model, so this list never shows a rejection. -->
<ul v-if="receipts.length" class="list">
<li v-for="file in receipts" :key="file.name + file.size" class="item">
<BbIcon icon="lucide:file-text" size="sm" />
<span class="min-w-0 flex-1 truncate">{{ file.name }}</span>
<span class="shrink-0 opacity-60">{{ formatFileSize(file.size) }}</span>
<BbButton
:aria-label="`Remove ${file.name}`"
icon-only
prepend:icon="lucide:x"
size="xs"
variant="ghost"
@click="remove(file)"
/>
</li>
</ul>
<p v-else class="m-0 text-xs opacity-70">No receipts attached yet.</p>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BbButton, BbDropzone, BbIcon } from 'bitboss-ui';
import { formatFileSize } from '~/demo-data';
import type { BbFileRejection } from 'bitboss-ui';
const receipts = ref<File[]>([]);
// Seeded with one rejection so the error state is visible before any drop.
const messages = ref<string[]>([
'team-dinner.heic is not a receipt (PNG, JPG or PDF only).',
]);
const remove = (target: File) => {
receipts.value = receipts.value.filter((file) => file !== target);
};
// The union is closed and discriminated on `type`, so this switch is
// exhaustive: a new variant in a later version fails the type-check here.
// The wording is yours: the component never fills `errors` for you.
const describe = (error: BbFileRejection): string => {
switch (error.type) {
case 'accept':
return `${error.filename} is not a receipt (PNG, JPG or PDF only).`;
case 'maxFiles':
return `${error.filename} was not added: ${error.maxFiles} receipts at most.`;
case 'maxSize':
return `${error.filename} is ${error.formattedSize}, over the ${error.formattedMaxSize} limit.`;
case 'uniqueness':
return `${error.filename} is already attached.`;
}
};
// A drop that accepted files updates the model first, then emits one `error`
// with every rejection of that drop. So the model update clears the last
// drop's messages, and the error, when there is one, replaces them. A drop
// that accepted nothing emits only the `error`.
</script>
<style scoped>
.zone {
align-items: center;
background: var(--bb-panel);
border: var(--bb-border-w) dashed var(--bb-border);
border-radius: var(--bb-radius);
display: flex;
flex-direction: column;
gap: 0.375rem;
padding: 1.25rem 1rem;
text-align: center;
}
.zone--dragging {
border-color: var(--bb-primary);
}
.zone--error {
border-color: var(--bb-danger);
}
.list {
display: flex;
flex-direction: column;
gap: 0.25rem;
list-style: none;
margin: 0;
padding: 0;
}
.item {
align-items: center;
border: var(--bb-border-w) solid var(--bb-border);
border-radius: var(--bb-radius);
display: flex;
font-size: 0.75rem;
gap: 0.5rem;
padding: 0.25rem 0.5rem;
}
</style>
Rejected files never enter the model. @error hands you every rejection of a drop or pick in one BbFileRejection[], after the model update (a drop that accepted nothing updates nothing). The field never fills errors itself: clear your messages on update:modelValue and set them on error (a size rejection's formattedSize is already in the app's locale).
One file or many
Bind a ref<File[]>([]). A pick or drop appends to it, and :max-files="1" makes a one-file field: read files[0].
<template>
<div class="flex max-w-md flex-col gap-5">
<!-- One file: still a `File[]`, read `resume[0]`. A second file is
rejected, never swapped in, so Replace empties the field first.
The bare tokens are shorthand: 'pdf' covers both `.pdf` and
`application/pdf`. The other demos write the pairs out. -->
<BbDropzone
id="apply-resume"
v-model="resume"
:accept="['pdf', 'docx']"
:errors="errors.resume"
label="Résumé"
:max-files="1"
@error="(rejected) => reject('resume', rejected)"
@update:model-value="clear('resume')"
>
<template #default="{ open, dragging, focused }">
<div class="zone" :class="zoneClass(dragging, focused, errors.resume)">
<BbIcon class="opacity-60" icon="lucide:file-text" size="md" />
<span class="min-w-0 flex-1 truncate text-sm">
{{ resume[0]?.name ?? 'PDF or DOCX, one file' }}
</span>
<BbButton size="xs" variant="outline" @click="replace(open)">
{{ resume.length ? 'Replace' : 'Choose' }}
</BbButton>
</div>
</template>
</BbDropzone>
<!-- Many files: picks and drops are appended, up to `max-files`.
Explicit entries: `.pdf` still matches a drop with an empty `type`. -->
<BbDropzone
id="apply-samples"
v-model="samples"
:accept="['image/*', 'application/pdf', '.pdf']"
:errors="errors.samples"
label="Work samples"
:max-files="5"
:max-size="5 * 1024 * 1024"
@error="(rejected) => reject('samples', rejected)"
@update:model-value="clear('samples')"
>
<template #default="{ open, dragging, focused }">
<div class="zone" :class="zoneClass(dragging, focused, errors.samples)">
<BbIcon class="opacity-60" icon="lucide:images" size="md" />
<span class="min-w-0 flex-1 text-sm">
Images or PDF, up to 5 files of 5 MB
</span>
<BbButton size="xs" variant="outline" @click="open">Add</BbButton>
</div>
</template>
</BbDropzone>
<!-- Removal is a filter over the model: there is no files API to call. -->
<ul v-if="samples.length" class="list">
<li v-for="file in samples" :key="fileKey(file)" class="item">
<span class="min-w-0 flex-1 truncate">{{ file.name }}</span>
<span class="shrink-0 tabular-nums opacity-60">
{{ formatFileSize(file.size) }}
</span>
<BbButton
:aria-label="`Remove ${file.name}`"
icon-only
prepend:icon="lucide:x"
size="xs"
variant="ghost"
@click="samples = samples.filter((f) => f !== file)"
/>
</li>
</ul>
</div>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { BbButton, BbDropzone, BbIcon } from 'bitboss-ui';
import { formatFileSize } from '~/demo-data';
import type { BbDropzonePick, BbFileRejection } from 'bitboss-ui';
const resume = ref<File[]>([]);
const samples = ref<File[]>([]);
const errors = reactive({ resume: [] as string[], samples: [] as string[] });
type Zone = keyof typeof errors;
const fileKey = (file: File) =>
`${file.name}-${file.size}-${file.lastModified}`;
const zoneClass = (dragging: boolean, focused: boolean, errors: string[]) => ({
'zone--dragging': dragging,
'bb-ring': focused,
'zone--error': errors.length > 0,
});
const describe = (error: BbFileRejection, zone: Zone): string => {
switch (error.type) {
case 'accept':
return `${error.filename} is not ${zone === 'resume' ? 'a PDF or DOCX' : 'an image or PDF'}.`;
case 'maxFiles':
return zone === 'resume'
? `${error.filename} was not added: replace the current file instead.`
: `Only ${error.maxFiles} samples: ${error.filename} was not added.`;
case 'maxSize':
return `${error.filename} is ${error.formattedSize}, over ${error.formattedMaxSize}.`;
case 'uniqueness':
return `${error.filename} is already in the list.`;
}
};
// A pick updates the model, then emits one `error` with all its rejections,
// so the update clears the zone and the error refills it.
const reject = (zone: Zone, rejected: BbFileRejection[]) => {
errors[zone] = rejected.map((error) => describe(error, zone));
};
const clear = (zone: Zone) => {
errors[zone] = [];
};
// Past `max-files` a new file is rejected: empty the field, then pick.
const replace = (open: () => Promise<BbDropzonePick>) => {
resume.value = [];
clear('resume');
void open();
};
</script>
<style scoped>
.zone {
align-items: center;
background: var(--bb-panel);
border: var(--bb-border-w) dashed var(--bb-border);
border-radius: var(--bb-radius);
display: flex;
gap: 0.75rem;
padding: 0.75rem 1rem;
}
.zone--dragging {
border-color: var(--bb-primary);
}
.zone--error {
border-color: var(--bb-danger);
}
.list {
display: flex;
flex-direction: column;
gap: 0.25rem;
list-style: none;
margin: -0.75rem 0 0;
padding: 0;
}
.item {
align-items: center;
border: var(--bb-border-w) solid var(--bb-border);
border-radius: var(--bb-radius);
display: flex;
font-size: 0.75rem;
gap: 0.5rem;
padding: 0.25rem 0.5rem;
}
</style>
Past max-files a new file is rejected, never swapped in. To replace the one file, empty the value first and then call the slot's open(). accept takes MIME types (application/pdf), wildcards (image/*), extensions (.pdf) or bare tokens (pdf, which covers both). Pair an explicit MIME type with its extension: a dropped file can arrive with an empty file.type.
Coming from v2multiple → max-files
multiple is gone and the value is always File[]. Replace ref<File | null>(null) with ref<File[]>([]) and read files[0]; a one-file field is :max-files="1". max-files="0" now takes nothing (leave it unset for no limit). DropZoneError is now BbFileRejection, and its maxFiles variant has no totalFiles.
Design the drop area
The default slot is the area, with no design of its own. A click anywhere in it opens the picker, except on a real button or link, so a Browse button calls open() itself.
Drag your invoices here
PDF or PNG · up to 10 MB
<template>
<div class="flex max-w-md flex-col gap-3">
<!-- The label still names the field when hidden. `labelId` on the
headline only: the name is "Invoices Drag your invoices here Select
files". On the wrapper it would also read the hint and the button. -->
<BbDropzone
id="naming-invoices"
v-model="invoices"
:accept="['application/pdf', '.pdf', 'image/png', '.png']"
hide-label
label="Invoices"
:max-size="10 * 1024 * 1024"
>
<!-- A click anywhere in the area opens the picker. The Browse button
is a real button, so it calls `open()` itself. -->
<template #default="{ labelId, open, dragging, focused }">
<div
class="zone"
:class="{ 'zone--dragging': dragging, 'bb-ring': focused }"
>
<BbIcon class="opacity-60" icon="lucide:receipt" size="lg" />
<p :id="labelId" class="m-0 text-sm font-medium">
Drag your invoices here
</p>
<p class="m-0 text-xs text-[color:var(--bb-text-muted)]">
PDF or PNG · up to 10 MB
</p>
<BbButton size="xs" variant="outline" @click="open">
Browse files
</BbButton>
</div>
</template>
</BbDropzone>
<ul v-if="invoices.length" class="m-0 flex flex-col gap-1 p-0 text-xs">
<li v-for="file in invoices" :key="file.name" class="list-none truncate">
{{ file.name }}
</li>
</ul>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BbButton, BbDropzone, BbIcon } from 'bitboss-ui';
const invoices = ref<File[]>([]);
</script>
<style scoped>
.zone {
align-items: center;
background: var(--bb-panel);
border: var(--bb-border-w) dashed var(--bb-border);
border-radius: var(--bb-radius);
display: flex;
flex-direction: column;
gap: 0.375rem;
padding: 1.25rem 1rem;
text-align: center;
}
.zone--dragging {
border-color: var(--bb-primary);
}
</style>
Paint the drop highlight from dragging and the library focus ring with :class="{ 'bb-ring': focused }". hide-label hides the label and keeps it as the name. Put labelId on one headline only (on a wrapper, hints and buttons join the name). For a field that is just a button, render <BbButton tag="span" :id="labelId">: it stays a single tab stop. Do not nest a <label> in the slot.
Coming from v2slot id removed
The slot's id (for your own <label for>) is gone: set the field's label. The area is a <div> and tag is gone; wrap the field for any other element.
Upload and submit
Inside a BbForm, give the field a name and build FormData from the files @submit hands you.
<template>
<BbForm
:model="submission"
class="flex max-w-md flex-col gap-4"
@submit="onSubmit"
>
<BbTextInput id="submission-title" label="Submission title" name="title" />
<div class="flex flex-col gap-1">
<p class="m-0 text-sm font-medium">Sent with the Q2 pack</p>
<ul class="list">
<li v-for="file in attached" :key="file.id" class="item">
<BbIcon class="opacity-60" icon="lucide:file-text" size="sm" />
<span class="min-w-0 flex-1 truncate">{{ file.name }}</span>
<span class="shrink-0 tabular-nums opacity-60">
{{ formatFileSize(file.size) }}
</span>
</li>
</ul>
</div>
<!-- A field like the others: `name` is its key in the form and in the
multipart body, and the label names it in the rule's message. -->
<BbDropzone
id="submission-documents"
:accept="['application/pdf', '.pdf']"
label="Q3 documents"
name="documents"
rules="required"
>
<template #default="{ open, dragging, focused }">
<div
class="zone"
:class="{ 'zone--dragging': dragging, 'bb-ring': focused }"
>
<p class="m-0 text-sm font-medium">Drag the Q3 PDFs here</p>
<p class="m-0 text-xs text-[color:var(--bb-text-muted)]">
PDF, listed below once added
</p>
<BbButton size="xs" variant="outline" @click="open">
Browse files
</BbButton>
</div>
</template>
</BbDropzone>
<!-- The form holds the picked files: read them from the bound object. -->
<ul v-if="submission.documents.length" class="list -mt-2">
<li v-for="file in submission.documents" :key="file.name" class="item">
<BbIcon class="opacity-60" icon="lucide:file-text" size="sm" />
<span class="min-w-0 flex-1 truncate">{{ file.name }}</span>
<span class="shrink-0 tabular-nums opacity-60">
{{ formatFileSize(file.size) }}
</span>
<BbButton
:aria-label="`Remove ${file.name}`"
icon-only
prepend:icon="lucide:x"
size="xs"
variant="ghost"
@click="remove(file)"
/>
</li>
</ul>
<BbButton
class="self-start"
type="submit"
variant="primary"
>
Send for review
</BbButton>
<p class="m-0 text-xs text-[color:var(--bb-text-muted)]" role="status">
{{ status }}
</p>
</BbForm>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { BbButton, BbDropzone, BbForm, BbIcon, BbTextInput } from 'bitboss-ui';
import { delay, files, formatFileSize } from '~/demo-data';
const submission = reactive({
title: 'Q3 compliance pack' as string | null,
documents: [] as File[],
});
// Metadata the server already holds: a `File` cannot exist during prerender.
const attached = files.filter((file) => file.id === 'f-2201');
// Removal is a filter over the bound value; the dropzone follows it.
const remove = (target: File) => {
submission.documents = submission.documents.filter((f) => f !== target);
};
const status = ref('');
/** Runs only when every field passes; builds the multipart body from the values. */
const onSubmit = async (values: Record<string, unknown>) => {
const body = new FormData();
body.append('title', String(values.title ?? ''));
for (const file of values.documents as File[])
body.append('documents[]', file);
await delay(null, 500); // stands in for fetch('/submissions', { method: 'POST', body })
const count = body.getAll('documents[]').length;
status.value = `Sent for review with ${count} new ${count === 1 ? 'PDF' : 'PDFs'}.`;
};
</script>
<style scoped>
.zone {
align-items: center;
background: var(--bb-panel);
border: var(--bb-border-w) dashed var(--bb-border);
border-radius: var(--bb-radius);
display: flex;
flex-direction: column;
gap: 0.375rem;
padding: 1rem;
text-align: center;
}
.zone--dragging {
border-color: var(--bb-primary);
}
.list {
display: flex;
flex-direction: column;
gap: 0.25rem;
list-style: none;
margin: 0;
padding: 0;
}
.item {
align-items: center;
border: var(--bb-border-w) solid var(--bb-border);
border-radius: var(--bb-radius);
display: flex;
font-size: 0.75rem;
gap: 0.5rem;
padding: 0.375rem 0.5rem;
}
</style>
Send with no new PDF and the form stops at the field, with the label in the required message. Outside a form, name renders the hidden input a native multipart post needs. Progress, cancellation, retry and server errors stay in your own state.
Coming from v2hidden input only with name
v2 always rendered the hidden submittable input; now it exists only when name is set.
Lock the field during an upload
Disable the field while an upload runs, so the selection cannot change mid-request.
<template>
<div class="flex max-w-md flex-col gap-3">
<!-- `disabled` while the transfer is in flight, so the selection cannot
change mid-upload. There is no `loading` prop: the bar is yours. -->
<BbDropzone
id="states-zone"
v-model="file"
:accept="['.bin']"
:disabled="uploading"
label="Firmware image"
:max-files="1"
@update:model-value="uploaded = ''"
>
<template #default="{ focused }">
<div
class="zone"
:class="{ 'bb-ring': focused, 'zone--disabled': uploading }"
>
<BbIcon class="opacity-60" icon="lucide:cpu" size="md" />
<span class="min-w-0 flex-1 truncate text-sm">
{{
file[0]
? `${file[0].name} · ${formatFileSize(file[0].size)}`
: 'Drag a .bin file here or click to choose'
}}
</span>
</div>
</template>
</BbDropzone>
<div v-if="uploading" class="flex flex-col gap-1.5">
<div class="flex justify-between text-xs">
<span>Uploading, the image is locked</span>
<span class="tabular-nums text-[color:var(--bb-text-muted)]">
{{ progress }}%
</span>
</div>
<BbProgress
:label="`Uploading ${file[0]?.name}`"
:model-value="progress"
/>
</div>
<p v-else-if="uploaded" class="m-0 text-xs" role="status">
{{ uploaded }} uploaded. Drop the next image to send it.
</p>
<BbButton
class="self-start"
:disabled="!file.length || uploading"
size="sm"
variant="primary"
@click="upload"
>
Upload
</BbButton>
</div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { BbButton, BbDropzone, BbIcon, BbProgress } from 'bitboss-ui';
import { delay, formatFileSize } from '~/demo-data';
const file = ref<File[]>([]);
const uploading = ref(false);
const uploaded = ref('');
const progress = ref(0);
// A `File` cannot exist during prerender, so the demo picks one on mount.
onMounted(() => {
file.value = [
new File([new Uint8Array(1_843_200)], 'router-fw-2.4.1.bin', {
lastModified: 0,
}),
];
});
// A fake transfer, started by the click and advanced by awaited delays, never
// by a page-load timer.
async function upload() {
uploading.value = true;
progress.value = 0;
while (progress.value < 100) {
await delay(null, 150);
progress.value = Math.min(100, progress.value + 10);
}
// Sent: empty the one-file field so the next drop is taken, not rejected.
uploaded.value = file.value[0]?.name ?? '';
file.value = [];
uploading.value = false;
}
</script>
<style scoped>
.zone {
align-items: center;
background: var(--bb-panel);
border: var(--bb-border-w) dashed var(--bb-border);
border-radius: var(--bb-radius);
display: flex;
gap: 0.75rem;
padding: 0.75rem 1rem;
}
.zone--disabled {
opacity: 0.6;
}
</style>
To show files nobody can change, such as a request sent for review, set readonly. A named read-only field still submits its files (disabled would not). Hide your remove and Browse buttons with the slot's readonly flag. There is no loading state: pair the transfer with BbProgress.
Open the picker from any button
useBbDropzone({ accept, maxSize, maxFiles }) opens the picker with a dropzone's rules and no field, and open() resolves with that pick.
- Q2 revenue review.pdf2.4 MB
- Supplier contract — Aeris.pdf795.8 KB
<template>
<div class="flex max-w-md flex-col gap-2">
<ul class="list">
<li v-for="doc in docs" :key="doc.id" class="item">
<BbIcon class="opacity-60" icon="lucide:file-text" size="sm" />
<span class="min-w-0 flex-1 truncate">{{ doc.name }}</span>
<span class="shrink-0 tabular-nums opacity-60">
{{ formatFileSize(doc.size) }}
</span>
<BbButton
icon-only
prepend:icon="lucide:refresh-cw"
size="xs"
:text="`Replace ${doc.name}`"
variant="ghost"
@click="replace(doc)"
/>
</li>
</ul>
<p class="m-0 text-xs text-[color:var(--bb-danger)]" role="status">
{{ problem }}
</p>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BbButton, BbIcon, useBbDropzone } from 'bitboss-ui';
import { files, formatFileSize } from '~/demo-data';
// Metadata the server already holds: a `File` cannot exist during prerender.
const docs = ref(
files
.filter((file) => file.mimeType === 'application/pdf')
.map(({ id, name, size }) => ({ id, name, size }))
);
const problem = ref('');
// A dropzone's rules with no field on the page: no area, no label.
const picker = useBbDropzone({
accept: ['application/pdf', '.pdf'],
maxSize: 5 * 1024 * 1024,
maxFiles: 1,
});
const replace = async (doc: (typeof docs.value)[number]) => {
// The handle keeps what it picked: empty it so this pick is not
// rejected as a duplicate or past `maxFiles`. Then open before any await.
picker.files.value = [];
const { files: picked, rejections } = await picker.open();
problem.value = rejections.length ? 'Pick a PDF of 5 MB at most.' : '';
const [file] = picked;
if (file) Object.assign(doc, { name: file.name, size: file.size });
};
</script>
<style scoped>
.list {
display: flex;
flex-direction: column;
gap: 0.25rem;
list-style: none;
margin: 0;
padding: 0;
}
.item {
align-items: center;
border: var(--bb-border-w) solid var(--bb-border);
border-radius: var(--bb-radius);
display: flex;
font-size: 0.75rem;
gap: 0.5rem;
padding: 0.25rem 0.5rem;
}
</style>
Call open() before any await (browsers open a picker only from the click), and empty files first: the handle keeps what it picked. To open a mounted field from elsewhere, give it an id and call useBbDropzone('that-id').open(). A value that joins a form or shows errors belongs in a BbDropzone.