Skip to content

Countdown

High-performance countdown component supporting target time/duration modes, pause/resume, warning state, flip animation, server time calibration, and more. Uses requestAnimationFrame for precise timing.

Basic Usage

Set value to a target timestamp or Date object. The component automatically calculates remaining time and counts down.

01:59:59
Basic Usage

Duration Mode

When passing milliseconds (less than 978307200000, i.e., 2001-01-01), it is treated as duration mode.

01:00
05 min 00 sec
Duration Mode

Custom Format & Text

Customize the display format via the format prop. Supports DD, HH, mm, ss, SSS placeholders. Also supports title and suffix.

01 days 01:01:01
01:01.500
Remaining:
01:00:00
(estimated)
Custom Format

Flip Animation

Set flip-animation to enable flip-card style. Combine with labels to add labels for each time unit.

01
h
:
01
m
:
01
s
Flip Animation

Control Countdown

Get the component instance via ref to manually control the countdown lifecycle.

00:01:00
Control Countdown

Warning State

Set a warning threshold via warning-threshold. When the countdown enters this range, is-warning class is added for style customization.

00:00:15
Warning State

Fully Custom

Use the default slot to fully take over rendering. The slot scope exposes the complete current format object and state flags.

01:00:00
Fully Custom

Scenario: Flash Sale

Combines flip animation, labels, and warning state to create an urgent e-commerce countdown.

Flash Sale
00
h
:
59
m
:
59
s
Flash Sale

Server Time Calibration

Handles client clock deviations caused by manual adjustments or system delays.

01:00:00
Server Time Calibration

Real-World: Dialog Sync

In complex SPA applications, the same timer task may appear in multiple places (e.g., product detail page and purchase popup). The component ensures through internal time alignment that the time display stays absolutely consistent when dialogs are frequently opened/closed.

Page countdown:
00:09:59
Dialog Sync

Real-World: Flash Sale List

High-performance requestAnimationFrame scheduling keeps each countdown independent while maintaining smooth UI updates, even in waterfall lists and high-frequency refresh scenarios.

Smart Watch Pro$199
Ends in
00:59:59
ANC Headphones Max$289
Ends in
01:59:59
Mechanical Keyboard G9$89
Ends in
00:29:59
Flash Sale List

Use in Nuxt

The Countdown component fully supports SSR rendering in Nuxt 3/4. Components are auto-imported in Nuxt projects.

01:59:59
Use in Nuxt

SSR Notes:

  • The initial countdown structure and formatted text can be rendered during SSR.
  • Remaining time is calculated from the current server render time before hydration.
  • The internal animation frame loop starts only on the client after mount.
  • Use server-time-offset when you need stricter server-client time alignment across devices.

SSR Performance

Countdown uses requestAnimationFrame internally for timing, which only executes on the client and consumes zero server resources. During initial render, remaining values are pre-calculated based on the current server time, ensuring consistent full-pipeline experience.

API

Props

PropDescriptionTypeDefault
valueTarget time or duration.YhCountdownValueRequired
formatFormat template or custom formatter.YhCountdownFormat'HH:mm:ss'
auto-startWhether the countdown starts immediately.booleantrue
intervalRefresh interval in milliseconds.number1000
precisionTiming precision in milliseconds.1000 | 100 | 101000
titlePrefix text.string''
suffixSuffix text.string''
use-monospace-fontWhether a monospace font is used to avoid digit jitter.booleantrue
flip-animationWhether flip-card animation is enabled.booleanfalse
value-styleInline style applied to the value area.CSSProperties | stringundefined
separatorSeparator between time units.string':'
show-daysWhether to show days. 'auto' shows days only when needed.boolean | 'auto''auto'
show-hoursWhether to show hours.booleantrue
show-minutesWhether to show minutes.booleantrue
show-secondsWhether to show seconds.booleantrue
show-millisecondsWhether to show milliseconds.booleanfalse
labelsLabel mapping for time units.Partial<Record<keyof YhCountdownTimeUnits, string>>undefined
keep-alive-on-finishWhether the zero state stays visible after finish.booleantrue
warning-thresholdRemaining time threshold that triggers warning mode, in milliseconds.numberundefined
timezone-offsetTimezone offset used for time alignment, in minutes.numberundefined
server-time-offsetDifference between server time and local time, in milliseconds.number0
theme-overridesComponent-level theme overrides.ComponentThemeVarsundefined

Events

Event NameDescriptionParameters
changeTriggered on countdown update(ctx: YhCountdownFormatContext) => void
finishTriggered when countdown ends() => void
startTriggered when countdown starts() => void
pauseTriggered on pause() => void
resumeTriggered on resume() => void
resetTriggered on reset() => void
warningTriggered when entering warning range(ctx: YhCountdownFormatContext) => void
status-changeTriggered on status change(status: YhCountdownStatus) => void

Slots

Slot NameDescriptionParameters
defaultFully custom rendering{ current, remaining, formatted, status, isWarning, isFinished }
prefixPrefix content-
suffixSuffix content-
valueCustom digit display{ text: string }
separatorCustom unit separator-
[key]-cellCustom specific cell (e.g., seconds-cell){ value: string }

Expose

MethodDescriptionParametersReturn
startStart timing-void
pausePause timing-void
resumeResume timing-void
resetReset timing-void
getRemainGet current remaining milliseconds-number
getStatusGet current countdown status-YhCountdownStatus

Type Definitions

typescript
type YhCountdownStatus = 'pending' | 'running' | 'paused' | 'finished'

interface YhCountdownFormatContext {
  total: number // Total remaining ms
  days: number
  hours: number
  minutes: number
  seconds: number
  milliseconds: number
  DD: string // Two-digit days
  HH: string
  mm: string
  ss: string
  SSS: string
  SS: string // First two digits of ms
  S: string // First digit of ms
}

Theme Variables

The Countdown component supports customizing styles by overriding the following CSS variables. All color variables integrate with the global theme system, automatically supporting dark mode:

VariableDescriptionDefault
--yh-countdown-font-sizeDigit font size24px
--yh-countdown-value-colorDigit colorvar(--yh-text-color-primary)
--yh-countdown-label-colorLabel colorvar(--yh-text-color-secondary)
--yh-countdown-separator-colorSeparator colorvar(--yh-text-color-placeholder)
--yh-countdown-warning-colorWarning state colorvar(--yh-color-danger)
--yh-countdown-finished-colorFinished state colorvar(--yh-color-success)
--yh-countdown-bgComponent root backgroundtransparent
--yh-countdown-block-bgFlip block backgroundvar(--yh-fill-color-light)
--yh-countdown-block-shadowFlip block shadowvar(--yh-shadow-sm)
--yh-countdown-block-radiusFlip block border radiusvar(--yh-radius-md)
--yh-countdown-block-paddingFlip block padding12px 16px
--yh-countdown-gapInternal element gap8px
--yh-countdown-font-familyDefault font familyvar(--yh-font-family)
--yh-countdown-monospace-fontMonospace font (anti-jitter)JetBrains Mono, SF Mono...

Type Exports

NameDescription
YhCountdownPropsProps type for YhCountdown
YhCountdownEmitsEmits type for YhCountdown
YhCountdownExposeExpose type for YhCountdown
YhCountdownTimeUnitsTime units structure type
YhCountdownFormatContextFormatting context type
YhCountdownValueCountdown target value type
YhCountdownFormatFormat config type
YhCountdownStatusCountdown status union
YhCountdownInstancePublic instance type for YhCountdown

Released under the MIT License.