DocsGuidesDownload button
A download button that shows progress
The arrow fills as the bytes arrive. No spinner, no extra markup.
async function download(button, url) {
const icon = button.querySelector('.fidg');
const res = await fetch(url);
const total = Number(res.headers.get('content-length')) || 0;
const reader = res.body.getReader();
let got = 0;
const parts = [];
for (;;) {
const { done, value } = await reader.read();
if (done) break;
parts.push(value);
got += value.length;
if (total) icon.style.setProperty('--v', got / total);
}
icon.style.setProperty('--v', 1);
return new Blob(parts);
}Something missing or unclear?Write to usRequest an icon