Skip to content

File Input Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Let users pick, drop, or paste a file in a form field, with no setup. <sv-file-input> shows the drop zone and the selected file with a remove button, and name submits the file with a form.

Layout
State
<div class="file-input-basic">
<label for="file-input-attachments">Attachments</label>
<sv-file-input id="file-input-attachments" name="attachments"></sv-file-input>
</div>
<script type="application/x-typescript" module>
import type { FileInput } from "@staticview/ui/file-input";
const fileInput = document.querySelector<FileInput>(".file-input-basic sv-file-input")!;
// Sample file for this preview
fileInput.files = [new File(["sample"], "report.pdf")];
</script>
<style>
.file-input-basic {
display: grid;
grid-template-columns: minmax(0, 28rem);
gap: 0.5em;
place-content: center;
}
</style>

Allowed Types

Allow only some file types, and tell the user which ones. accept refuses other files, and the hint slot shows the text under the title.

Images only
<div class="file-input-accept">
<label for="file-input-photo">Photo</label>
<sv-file-input id="file-input-photo" accept="image/*">
<span slot="hint">Images only</span>
</sv-file-input>
</div>
<style>
.file-input-accept {
display: grid;
grid-template-columns: minmax(0, 28rem);
gap: 0.5em;
place-content: center;
}
</style>

Limits

Limit the number and the size of the files, and tell the user why a file was refused. max-files and max-file-size set the limits, and the rejected event gives each refused file with its reason.

Up to 3 files, 2 MB each

<div class="file-input-limits">
<label for="file-input-photos">Photos</label>
<sv-file-input id="file-input-photos" max-file-size="2000000" max-files="3" multiple>
<span slot="hint">Up to 3 files, 2 MB each</span>
</sv-file-input>
<p role="status"></p>
</div>
<script type="application/x-typescript" module>
import type { FileInput } from "@staticview/ui/file-input";
const fileInput = document.querySelector<FileInput>(".file-input-limits sv-file-input")!;
const message = document.querySelector(".file-input-limits p")!;
fileInput.addEventListener("fileschange", () => {
message.textContent = "";
});
fileInput.addEventListener("rejected", event => {
message.textContent = event.detail
.map(({ file, reason }) => `${file.name}: ${reason}`)
.join(", ");
});
</script>
<style>
.file-input-limits {
display: grid;
grid-template-columns: minmax(0, 28rem);
gap: 0.5em;
place-content: center;
sv-file-input::part(rejected) {
display: none;
}
p {
margin: 0;
color: var(--sv-error);
}
}
</style>

File Status

Show the upload progress of each selected file on its row. setFileStatus() in a fileschange listener writes the state, the progress, and the message of a file.

<div class="file-input-status">
<label for="file-input-status-files">Attachments</label>
<sv-file-input id="file-input-status-files" multiple></sv-file-input>
</div>
<script type="application/x-typescript" module>
import type { FileInput } from "@staticview/ui/file-input";
const fileInput = document.querySelector<FileInput>(".file-input-status sv-file-input")!;
fileInput.addEventListener("fileschange", () => {
for (const file of fileInput.files) {
fileInput.setFileStatus(file, { state: "uploading", progress: 0.4, message: "Uploading" });
}
});
// Sample files for this preview
fileInput.files = [new File(["sample"], "report.pdf"), new File(["sample"], "notes.txt")];
</script>
<style>
.file-input-status {
display: grid;
grid-template-columns: minmax(0, 28rem);
gap: 0.5em;
place-content: center;
}
</style>

Custom Trigger

Open the file picker from your own upload card, with no drop zone. variant="button" removes the drop zone, and the trigger slot holds the card.

Layout
State
<div class="file-input-custom-trigger">
<sv-file-input aria-label="Documents" multiple variant="button">
<button type="button" class="upload-card" slot="trigger">
<svg aria-hidden="true" viewBox="0 -960 960 960" xmlns="http://www.w3.org/2000/svg">
<path d="M440-440H200v-80h240v-240h80v240h240v80H520v240h-80v-240Z"></path>
</svg>
<span>
<strong>Upload documents</strong>
<small>PDF or Word, up to 10 MB</small>
</span>
</button>
</sv-file-input>
</div>
<script type="application/x-typescript" module>
import type { FileInput } from "@staticview/ui/file-input";
const fileInput = document.querySelector<FileInput>(".file-input-custom-trigger sv-file-input")!;
// Sample file for this preview
fileInput.files = [new File(["sample"], "contract.pdf")];
</script>
<style>
.file-input-custom-trigger {
display: grid;
grid-template-columns: minmax(0, 28rem);
place-content: center;
sv-file-input[variant="button"] {
justify-self: center;
}
.upload-card {
display: flex;
gap: 0.75em;
align-items: center;
padding: 0.75em 1.25em 0.75em 0.75em;
font: inherit;
color: inherit;
text-align: start;
cursor: pointer;
background-color: var(--sv-gray-6);
border: solid var(--sv-bdr-sz-sm) var(--sv-gray-5);
border-radius: var(--sv-bdr-rad-md);
&:hover {
border-color: var(--sv-accent);
}
}
svg {
inline-size: 2.5em;
padding: 0.5em;
background-color: var(--sv-accent);
border-radius: 50%;
fill: var(--sv-accent-content);
}
span {
display: grid;
}
small {
color: var(--sv-gray-3);
}
}
</style>

Custom Drop Zone

Pick a profile photo from a round avatar that also takes dropped images, with no script. The dropzone slot holds the avatar button, part="trigger" opens the file picker, a <template slot="file"> with part="file-thumbnail" shows the photo, and no-list hides the empty list.

<div class="file-input-custom-dropzone">
<label for="file-input-avatar">Profile photo</label>
<sv-file-input id="file-input-avatar" accept="image/*" name="avatar" no-list>
<button type="button" class="avatar-button" part="trigger" slot="dropzone">
<template slot="file">
<img alt="" part="file-thumbnail" />
</template>
Add photo
</button>
</sv-file-input>
</div>
<style>
.file-input-custom-dropzone {
display: grid;
gap: 0.75em;
place-content: center;
place-items: center;
sv-file-input {
--sv-file-input-bdr-rad: 50%;
}
.avatar-button {
position: relative;
inline-size: 8em;
block-size: 8em;
padding: 0;
font: inherit;
color: var(--sv-gray-3);
cursor: pointer;
background-color: var(--sv-gray-6);
border: dashed var(--sv-bdr-sz-md) var(--sv-gray-4);
border-radius: 50%;
}
sv-file-input:state(--dragging) .avatar-button {
border-color: var(--sv-accent);
}
img {
position: absolute;
inset: 0;
inline-size: 100%;
block-size: 100%;
object-fit: cover;
border-radius: 50%;
}
}
</style>

Custom File Row

Show the selected images as a gallery of photo tiles. A <template slot="file"> holds the tile, part="file-thumbnail", part="file-name", and part="remove-button" mark its parts, and ::part(file-list) lays the tiles out in a grid.

Layout
State
<div class="file-input-custom-row">
<label for="file-input-gallery">Gallery</label>
<sv-file-input id="file-input-gallery" accept="image/*" multiple>
<template slot="file">
<div class="photo-tile" role="listitem">
<img alt="" part="file-thumbnail" />
<span part="file-name"></span>
<button type="button" class="photo-remove" aria-label="Remove" part="remove-button">
×
</button>
</div>
</template>
</sv-file-input>
</div>
<script type="application/x-typescript" module>
import type { FileInput } from "@staticview/ui/file-input";
const fileInput = document.querySelector<FileInput>(".file-input-custom-row sv-file-input")!;
const photoResponse = await fetch(new URL("@assets/avatars/orbit.svg", import.meta.url).href);
const photo = await photoResponse.blob();
// Sample files for this preview
fileInput.files = ["beach.svg", "forest.svg", "sunset.svg"].map(
name => new File([photo], name, { type: photo.type })
);
</script>
<style>
.file-input-custom-row {
display: grid;
grid-template-columns: minmax(0, 28rem);
gap: 0.5em;
place-content: center;
sv-file-input:not(:state(--empty))::part(file-list) {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.5em;
margin-block-start: 0.5em;
}
.photo-tile {
position: relative;
display: grid;
gap: 0.25em;
font-size: 0.85em;
}
img {
inline-size: 100%;
aspect-ratio: 1;
object-fit: cover;
border-radius: var(--sv-bdr-rad-md);
}
[part="file-name"] {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.photo-remove {
position: absolute;
inset-block-start: 0.25em;
inset-inline-end: 0.25em;
inline-size: 1.5em;
block-size: 1.5em;
padding: 0;
cursor: pointer;
border: none;
border-radius: 50%;
}
}
</style>