function download(value, name = "untitled", label = "Save") {
const a = html`<a><button></button></a>`;
const b = a.firstChild;
b.textContent = label;
a.download = name;
async function reset() {
await new Promise(requestAnimationFrame);
URL.revokeObjectURL(a.href);
a.removeAttribute("href");
b.textContent = label;
b.disabled = false;
}
a.onclick = async event => {
b.disabled = true;
if (a.href) return reset();
b.textContent = "Saving…";
try {
const object = await value();
b.textContent = "Download";
a.href = URL.createObjectURL(object);
} catch (ignore) {
b.textContent = label;
}
if (event.eventPhase) return reset();
b.disabled = false;
};
a.addEventListener("reset", reset);
return a;
}