Skip to content

Spinner Loading Overlay

Cover a card with a spinner while its data reloads. aria-busy="true" on the card shows the spinner and holds back the aria-live="polite" total until the new value is in.

Weekly sales

$12,480

Code

The following example assumes that you have imported the component and set up the theme.

Loading Overlay
<section class="sales-card" aria-labelledby="sales-card-title">
16 collapsed lines
<header>
<h2 id="sales-card-title">Weekly sales</h2>
<button type="button" class="btn refresh-button" aria-label="Refresh sales">
<!-- Refresh -->
<svg aria-hidden="true" fill="currentColor" viewBox="0 0 24 24">
<path
d="M17.65 6.35A7.96 7.96 0 0 0 12 4a8 8 0 1 0 7.73 10h-2.08A6 6 0 1 1 12 6c1.66 0 3.14.69 4.22 1.78L13 11h7V4l-2.35 2.35Z"
/>
</svg>
</button>
</header>
<p class="sales-total" aria-live="polite">$12,480</p>
<sv-spinner aria-label="Refreshing sales"></sv-spinner>
</section>
<script type="application/x-typescript" module>
14 collapsed lines
const salesCard = document.querySelector<HTMLElement>(".sales-card")!;
const salesTotal = salesCard.querySelector(".sales-total")!;
salesCard.querySelector(".refresh-button")!.addEventListener("click", async () => {
salesCard.ariaBusy = "true";
salesTotal.textContent = await fetchSalesTotal();
salesCard.ariaBusy = "false";
});
// Stands in for the request that loads the new total
function fetchSalesTotal() {
const total = `$${Math.round(10_000 + Math.random() * 5000).toLocaleString("en-US")}`;
return new Promise<string>(resolve => setTimeout(resolve, 1500, total));
}
</script>
<style>
60 collapsed lines
.sales-card {
position: relative;
inline-size: min(100%, 18em);
padding: 1.25em;
margin: 2em auto;
background-color: var(--sv-gray-7);
border: 1px solid var(--sv-gray-5);
border-radius: var(--sv-bdr-rad-lg);
header {
display: flex;
align-items: center;
justify-content: space-between;
}
h2 {
margin: 0;
font-size: 0.9em;
font-weight: 500;
color: var(--sv-gray-3);
}
sv-spinner {
--sv-spinner-sz: 2em;
--sv-spinner-fill-clr: var(--sv-accent);
position: absolute;
inset: 0;
align-items: center;
justify-content: center;
color: var(--sv-gray-1);
background-color: color-mix(in srgb, var(--sv-gray-7) 70%, transparent);
border-radius: inherit;
backdrop-filter: blur(4px);
transition: 200ms;
transition-property: opacity, visibility;
}
&:not([aria-busy="true"]) sv-spinner {
visibility: hidden;
opacity: 0;
}
}
.refresh-button {
display: grid;
padding: 0.3em;
svg {
inline-size: 1em;
}
}
.sales-total {
margin: 0.5em 0 0;
font-size: 2em;
font-weight: 700;
font-variant-numeric: tabular-nums;
color: var(--sv-gray-0);
}
</style>