Skip to content

Progress Ring Countdown

Build a countdown timer that drains smoothly each second. On each textchange of an sv-timer, the script sets value to the seconds left, --sv-progress-ring-dur and --sv-progress-ring-ease make the fill glide for the full second, --sv-progress-ring-clockwise reverses its direction, and data-sv-progress-ring-value-text has screen readers read the timer text.

Break time

Code

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

Countdown
<div class="countdown">
9 collapsed lines
<sv-progress-ring aria-label="Time remaining" max="60" value="60">
<p class="countdown-time" data-sv-progress-ring-value-text>
<sv-timer duration="1m" running>
<sv-text-flow slot="time"></sv-text-flow>
</sv-timer>
</p>
</sv-progress-ring>
<p class="countdown-title">Break time</p>
</div>
<script type="application/x-typescript" module>
11 collapsed lines
const ring = document.querySelector<ProgressRing>(".countdown sv-progress-ring")!;
const timer = document.querySelector<Timer>(".countdown sv-timer")!;
timer.addEventListener("textchange", () => {
const secondsLeft = Math.ceil(timer.value / 1000);
ring.value = secondsLeft;
ring.classList.toggle("urgent", secondsLeft <= 10);
});
timer.addEventListener("finished", () => setTimeout(timer.start, 2000));
</script>
<style>
41 collapsed lines
.countdown {
display: grid;
gap: 1rem;
justify-items: center;
sv-progress-ring {
--sv-progress-ring-clockwise: -1;
--sv-progress-ring-dur: 1s;
--sv-progress-ring-ease: linear;
--sv-progress-ring-track-thickness: 3px;
--sv-progress-ring-fill-thickness: 6px;
inline-size: 8rem;
transition: color 300ms ease;
&.urgent {
--sv-progress-ring-fill-clr: #ef4444;
color: #ef4444;
}
@media (prefers-reduced-motion: reduce) {
--sv-progress-ring-dur: 0s;
}
}
.countdown-time {
margin: 0;
sv-text-flow::part(text) {
font-family: monospace;
font-size: 1.6rem;
}
}
.countdown-title {
margin: 0;
font-size: 0.85rem;
color: var(--sv-gray-3);
}
}
</style>