Countdown
A timer counting down to a moment.
Demo
Usage
import { Countdown } from '@tuttn/slant'
// A Date, a timestamp or an ISO string; the groups follow the time left.
<Countdown to="2026-12-31T23:59:59Z" label="Offer ends in" />
// Fixed groups, a callback at zero, and no announcement.
<Countdown to={deadline} label="Session locks in" units="hms" size="sm"
onComplete={lock} announce={false} />
// Hold the display while a panel is hidden.
<Countdown to={deadline} label="Build starts in" paused={!visible} />
// Count up from a moment instead: an elapsed-time clock.
<Countdown from={build.startedAt} label="Build running for" />
// A stopwatch: pause holds the display, and resuming moves from on
// by the time spent paused.
<Countdown from={from} label="Practice time" paused={pausedAt !== null} />
const resume = () => { setFrom(at => at + Date.now() - pausedAt); setPausedAt(null) }
// In tests, drive it with your own clock.
let time = 0
<Countdown to={65_000} label="Build starts in" now={() => time} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
to | Date | number | string | — | The moment it counts down to: a Date, a timestamp in milliseconds, or a string `new Date()` reads, such as an ISO date. Required unless `from` is given, and it wins when both are. With neither, or an invalid date, it shows the placeholder and never completes. |
from | Date | number | string | — | The moment it counts up from, as an elapsed-time clock (a build running, a call in progress), in the same forms as `to`. Used only when there is no `to`. Before that moment it holds at zero. Counting up never completes, so `onComplete` and `announce` don't apply. |
label | string | — | The timer's accessible name: what the time is until, e.g. "Sale ends in", or, counting up, what it measures, e.g. "Build running for". |
onComplete | () => void | — | Called once when the time left reaches zero, including on mount when `to` has already passed. It is called again only after `to` moves into the future and that time runs out too. Counting down only. |
paused | boolean | false | Stops the ticking and holds the time shown. `to` and `from` are fixed moments, so on resume the display catches up with the clock; to pause a duration instead, move the moment on by the time spent paused when you resume: a later `to`, or a later `from`. |
units | 'auto' | 'dhms' | 'hms' | 'ms' | 'auto' | The groups shown. `auto` shows days only while the time is a day or more and hours only while it is an hour or more. The others always show their groups, the largest carrying the rest: `ms` reads 90:00 for an hour and a half. |
size | Size | 'md' | The digits: sm sits beside a line of text, md matches a StatCard's value, lg leads a panel. |
announce | boolean | true | Says "Time is up" (`messages.timeUp`) through the root's polite live region when the countdown reaches zero while it is on screen. A countdown that mounts already at zero stays quiet. Counting down only. |
now | () => number | Date.now | The clock, in milliseconds since the epoch. Pass your own to drive it in tests or to sync with a server's time. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the timer element. |
Keyboard
A countdown is read, not operated, so it adds no focus stop, counting either way. Put the controls that act on it (restart, extend, pause) beside it.
Accessibility
The root is a timer named by label. The digits are hidden from screen readers; a hidden line says the time left in words ("2 hours, 14 minutes, 9 seconds left"), and "Time is up" at zero. A timer is not live, so the ticking is never read out; at zero the countdown says messages.timeUp once through the root's polite live region, unless announce is off or it mounted already at zero. If the countdown is a time limit on something the reader is doing, give them a way to extend it, by passing a later to (WCAG 2.2.1).
Counting up, the hidden line says the time elapsed (messages.timeElapsed: "4 minutes, 12 seconds"), so name the timer for what it measures: "Build running for", "Practice time".
Counting up
Given from and no to, the timer counts up from that moment, with the same units and sizes. Each second shows once it has passed, so it reads 00:00 for the first second; before from it holds at zero. It never reaches an end, so it never sets data-done, calls onComplete or announces anything. When both are given, to wins; with neither, it shows the placeholder.
paused holds the display either way. to and from are fixed moments, so resuming catches up with the clock; to pause the duration itself, as a stopwatch does, move from on by the time spent paused when you resume, as the demo does.
Server rendering
The time depends on the clock, which differs between the server and the browser, so a countdown renders a placeholder (--:--) until it mounts and reads the clock in a layout effect: the first painted frame already shows the time, and hydration never mismatches. It then ticks just after each second boundary with a timeout, rather than on an interval that drifts, and a countdown stops at zero. onComplete runs once at zero, also on mount when to has passed.
Direction
The timer sits at the inline start like any inline content, but the digits keep their left-to-right order under dir="rtl", as times do in Arabic and Hebrew text.