Command palette

@yasinsensoy/web

Web · Components

Countdown

Countdown or count-up: a sale ending, an event starting, exam time, a "resend code" wait, call duration.

15 exports

Live example

Preview

Code

import { Countdown } from "@yasinsensoy/web";

<Countdown to="2026-12-31T23:59:59+03:00" variant="boxes" urgent={60000} onComplete={() => done()} />

Events

  1. Events appear here when you interact with the preview.

Campaign card

Until the November sale ends

Resend code (a component instead of useCountdown)

until you can resend

Import

import {
  Countdown,
  splitDuration,
  autoUnits,
  isoDuration,
  unitLabel,
  formatCountdown,
  useCountdown,
} from "@yasinsensoy/web";

Countdown

componentClient component

Countdown or count-up: a sale ending, an event starting, exam time, a "resend code" wait, call duration.

  • Three modes: to (until a moment), duration (ms, a timer starting when rendered), from (time elapsed since that moment).
  • Appearance: clock (03:04:05), text (3 hours 4 minutes) or boxes. Unit names are written per locale with Intl; no translation needed.
  • Efficient: renders only when the smallest visible unit changes; stops while the tab is hidden. Digits are fixed width, so the text doesn't jiggle.
  • SSR safe: the server clock isn't trusted; a placeholder on the first render, the real value right after hydration.
  • Accessibility: role="timer" and <time dateTime="PT…">; not announced every second, "Time's up" is read once at the end.

For your own UI see useCountdown.

Countdown({ variant: variantProp, effect, size, tone, urgent, labels, completed, placeholder, label, tooltip, className, classNames, ...options }: CountdownProps): ReactNode

Examples

<Countdown to="2026-12-31T23:59:59+03:00" variant="boxes" completed="Happy new year!" />
<Countdown duration={5 * 60_000} urgent={60_000} onComplete={() => finishExam()} />
<Countdown from={call.startedAt} variant="clock" label="Call duration" />

Splits a duration into the given units. The largest unit collects the overflow: with ["hours", "minutes"], 2 days 3 hours → 51 hours.

splitDuration(total: number, units: readonly CountdownUnit[]): CountdownParts

Examples

splitDuration(93_784_000, ["days", "hours", "minutes", "seconds"]);
// { days: 1, hours: 2, minutes: 3, seconds: 4, milliseconds: 0, total: 93784000 }

autoUnits

function

"auto" units: from the largest non-zero unit of the duration down to seconds (3 hours → hours, minutes, seconds). Minutes and seconds always show, so the format does not jump when the time drops to 59 seconds.

autoUnits(total: number): CountdownUnit[]

isoDuration

function

ISO 8601 duration string (for <time dateTime>): P1DT2H3M4S.

isoDuration(total: number): string

unitLabel

function

Only the unit name, by language and number ("gün", "days", "day"). Uses Intl; no translation needed.

unitLabel(value: number, unit: CountdownUnit, locale: string, display?: "long" | "short" | "narrow"): string

Turns a duration into readable text: "2 days 3 hours 4 minutes". Zero units are skipped (the smallest unit is written when all are zero).

formatCountdown(parts: CountdownParts, units: readonly CountdownUnit[], locale: string, display?: "long" | "short" | "narrow"): string

useCountdown

function

Countdown or count-up hook: for drawing your own UI.

  • Efficient: wakes not every second but when the smallest visible unit changes (once a minute if only minutes are shown). The timer stops while the tab is hidden and is recomputed from the real clock on return; no drift.
  • SSR: since server and client clocks differ, the first render returns ready: false; the value arrives right after hydration.
useCountdown(options: UseCountdownOptions): UseCountdownResult

Examples

const { minutes, seconds, done, reset } = useCountdown({ duration: 60_000 });
return done
  ? <Button onClick={() => { resend(); reset(); }}>Resend</Button>
  : <span>{minutes}:{String(seconds).padStart(2, "0")}</span>;

Countdown appearance.

type CountdownVariant = /** Digital clock: `2 gün 03:04:05`, `12:30`. */ "clock" /** Readable text: "2 days 3 hours 4 minutes". */ | "text" /** Each unit in its own box with its name below (campaign, event pages). */ | "boxes"

Classes that can be given to the inner parts of Countdown.

NameTypeDescription
unit?stringUnit box (boxes) or unit group.
value?stringNumber.
label?stringUnit name.
separator?stringSeparator (:).

Props of the Countdown component.

NameTypeDefaultDescription
variant?CountdownVariant | CustomVariant<"countdown">"clock"Appearance: built-in or a custom style added with defineAppearance (its classes are added to the root; in the clock layout when extends is omitted). Defaults to UIProvider appearance.defaults.countdown.variant. See CountdownVariant.
effect?EffectPropEffect or effects (e.g. "shine", ["lift", "beam"]). Applied to each unit box with the boxes appearance, otherwise to the counter itself. Defaults to UIProvider appearance.defaults.countdown.effect. See BuiltinEffect.
size?"sm" | "md" | "lg""md"Size.
tone?Tone"neutral"Color of the boxes with the boxes appearance.
urgent?numberWhen the remaining time drops below this (ms) it looks "urgent": red with a light pulse (no pulse with the reduced motion preference). Works for countdowns.
labels?"long" | "short" | "narrow" | false"long"Unit names: full ("long": minutes), short ("short": min) or narrowest.
completed?ReactNodeContent shown when finished (e.g. "The sale has started!"). Zeros are shown when omitted.
placeholder?ReactNode"–"` (in each digit)Placeholder until the numbers are first read (server render, hydration).
label?stringScreen reader name (e.g. "Time left until the sale ends"). The counter isn't announced every second; it is announced once at the end.
tooltip?TooltipPropTooltip on hover. See TooltipProp.
className?stringClasses added to the container.
classNames?CountdownClassNamesClasses added to the inner parts.
to?CountdownDateMoment the countdown ends (a sale ending, an event starting). Server and client clocks look at the same moment; the time zone doesn't matter.
duration?numberDuration (ms): a timer starting when the component first renders (or with reset). For exams, "resend code", payment time, etc. Ignored when to is given.
from?CountdownDateCount-up start: time elapsed since this moment is counted (call duration, live stream duration). Used when to and duration are omitted.
units?readonly CountdownUnit[] | "auto""auto"Units shown; the value updates only when the smallest one changes. "auto" picks by duration (days if any, otherwise hours…). See CountdownUnit.
precise?booleanfalseUpdate with millisecond precision (stopwatch, competition)? Renders every frame; turn it on only when needed.
paused?booleanfalsePaused? The remaining time is kept while paused.
onComplete?() => voidCalled once when the countdown ends.
onTick?(parts: CountdownParts) => voidCalled whenever the visible value changes.

Date: Date, an ISO string ("2026-12-31T23:59:59+03:00") or milliseconds.

type CountdownDate = Date | string | number

Units that can be shown, largest to smallest.

type CountdownUnit = "days" | "hours" | "minutes" | "seconds"

The remaining (or elapsed) time split into units.

NameTypeDescription
totalnumberTotal time (ms); 0 when a countdown ends.
daysnumberDays. Added to hours when days are not shown.
hoursnumberHours.
minutesnumberMinutes.
secondsnumberSeconds.
millisecondsnumberMilliseconds (0–999).

Options of the useCountdown hook.

NameTypeDefaultDescription
to?CountdownDateMoment the countdown ends (a sale ending, an event starting). Server and client clocks look at the same moment; the time zone doesn't matter.
duration?numberDuration (ms): a timer starting when the component first renders (or with reset). For exams, "resend code", payment time, etc. Ignored when to is given.
from?CountdownDateCount-up start: time elapsed since this moment is counted (call duration, live stream duration). Used when to and duration are omitted.
units?readonly CountdownUnit[] | "auto""auto"Units shown; the value updates only when the smallest one changes. "auto" picks by duration (days if any, otherwise hours…). See CountdownUnit.
precise?booleanfalseUpdate with millisecond precision (stopwatch, competition)? Renders every frame; turn it on only when needed.
paused?booleanfalsePaused? The remaining time is kept while paused.
onComplete?() => voidCalled once when the countdown ends.
onTick?(parts: CountdownParts) => voidCalled whenever the visible value changes.

Value returned by the useCountdown hook.

Extends: CountdownParts

NameTypeDescription
donebooleanHas the countdown ended? Always false for a count-up.
runningbooleanCounting (not paused and not finished)?
readybooleanHas the clock been read on the client? false on the server and the first hydration; showing a placeholder instead of the value meanwhile prevents a hydration mismatch.
unitsreadonly CountdownUnit[]Units shown ("auto" resolved).
direction"down" | "up"Counting direction: to/duration → "down", from → "up".
pause() => voidPauses. Stable.
resume() => voidResumes where it left off. Stable.
reset(duration?: number) => voidStarts over: in duration mode the duration restarts (a new duration can be given); for a count-up the elapsed time resets. Stable.