Skip to content

Download Button Examples

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

Basic

Add a button that saves text as a file. value holds the file content, and filename names the file.

--downloaded--failed
Content
Text in the default slot shows before the icon and names the button.
Fills the download-icon and success-icon slots.
State
<div class="download-button-basic">
<sv-download-button filename="hello.txt" value="Hello, world!"></sv-download-button>
</div>
<style>
.download-button-basic {
display: grid;
place-content: center;
svg {
display: block;
inline-size: var(--sv-download-button-icon-sz, 1.25em);
block-size: var(--sv-download-button-icon-sz, 1.25em);
fill: none;
stroke: currentcolor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
}
</style>

Save a field

Save the text of a field as a file. from="id" reads the textarea with that id at each click, and type sets the file type.

Save
<div class="download-button-from">
<header>
<label for="download-button-notes">Notes</label>
<sv-download-button type="text/markdown" filename="notes.md" from="download-button-notes">
Save
</sv-download-button>
</header>
<textarea id="download-button-notes" rows="5">
# Meeting notes
- Ship the beta on Friday.
- Ask for feedback in the forum.</textarea>
</div>
<style>
.download-button-from {
display: grid;
gap: 0.5em;
inline-size: min(100%, 24rem);
margin-inline: auto;
sv-download-button {
padding-inline-start: 0.2em;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
}
label {
font-weight: 600;
}
textarea {
padding: 0.4em 0.75em;
font: inherit;
color: inherit;
resize: vertical;
background-color: var(--sv-gray-7);
border: solid var(--sv-bdr-sz-sm) var(--sv-gray-5);
border-radius: var(--sv-bdr-rad-md);
}
}
</style>

Export a table

Export a table as a CSV file. A script sets value to a Blob, and the button saves it with the type of the Blob.

Visitors by city Export CSV
City Visitors
Lisbon 1240
Berlin 980
Tokyo 1515
<div class="download-button-blob">
<header>
<strong>Visitors by city</strong>
<sv-download-button filename="visitors.csv">Export CSV</sv-download-button>
</header>
<table>
<thead>
<tr>
<th>City</th>
<th>Visitors</th>
</tr>
</thead>
<tbody>
<tr>
<td>Lisbon</td>
<td>1240</td>
</tr>
<tr>
<td>Berlin</td>
<td>980</td>
</tr>
<tr>
<td>Tokyo</td>
<td>1515</td>
</tr>
</tbody>
</table>
</div>
<script type="module">
const container = document.querySelector(".download-button-blob");
const rows = [...container.querySelectorAll("tr")].map(row =>
[...row.cells].map(cell => cell.textContent).join(",")
);
container.querySelector("sv-download-button").value = new Blob([rows.join("\n")], {
type: "text/csv",
});
</script>
<style>
.download-button-blob {
display: grid;
gap: 0.75em;
inline-size: min(100%, 22rem);
margin-inline: auto;
sv-download-button {
padding-inline-start: 0.2em;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
}
table {
border-collapse: collapse;
}
th,
td {
padding: 0.4em 0.75em;
text-align: start;
border-block-end: solid var(--sv-bdr-sz-sm) var(--sv-gray-5);
}
td:last-child,
th:last-child {
text-align: end;
}
}
</style>

Download an image

Add a download button to an image. href takes the URL of the image, and filename names the saved file.

<figure class="download-button-link">
<img alt="A planet with a ring" src="@assets/avatars/orbit.svg" />
<figcaption>
orbit.svg
<sv-download-button download-label="Download image" filename="orbit.svg"></sv-download-button>
</figcaption>
</figure>
<script type="module">
const figure = document.querySelector(".download-button-link");
figure.querySelector("sv-download-button").href = figure.querySelector("img").src;
</script>
<style>
.download-button-link {
display: grid;
gap: 0.5em;
inline-size: fit-content;
margin: 0 auto;
img {
inline-size: 10rem;
border-radius: var(--sv-bdr-rad-md);
}
figcaption {
display: flex;
gap: 0.75em;
align-items: center;
justify-content: space-between;
}
}
</style>