Skip to content

Countdown ​

Wall-clock-driven countdown: the target wall clock is recorded once and remaining time = target − now (clamped to ≥ 0), never decremented per tick — staying accurate after background switches or timer throttling. Controlled display and uncontrolled self-running modes, format tokens DD/HH/mm/ss/S, and a single onFinish on zero.

Contract: lh-ui-core/specs/countdown.json · Golden cases: cases/countdown.json (10 cases, translated to JVM unit tests + Compose UI Test)

Basic usage ​

The uncontrolled mode takes the total duration via time and self-runs after mount according to autoStart; the default format is HH:mm:ss. Digits render tabular (the countdown.digitMinWidth token + tnum) and separators are de-emphasized.

kotlin
LhCountdown(time = 60_000L)           // self-runs 60 seconds after mount
LhCountdown(time = 60_000L, format = "mm:ss")

Format tokens ​

The format string is case-sensitive: only DD (days) / HH (hours) / mm (minutes) / ss (seconds) / S (milliseconds, zero-padded to three digits) are tokens; every other character renders literally. Without DD, HH carries the total hours (may exceed 24); with DD, HH only carries the 0-23 positional value. Negative / invalid values always render as zero.

kotlin
LhCountdown(value = 3665000L)                          // 01:01:05
LhCountdown(value = 181810000L)                        // 50:30:10 (HH carries total hours)
LhCountdown(value = 86401500L, format = "DD天HH:mm:ss.S")  // 01天00:00:01.500
LhCountdown(value = 89500L, format = "mm:ss.S", millisecond = true)

Controlled and uncontrolled ​

Passing value makes the component controlled: it does not self-drive, displays the external value, and advancing plus the finish callback are the caller's responsibility. Passing time makes it uncontrolled: it self-runs after mount when autoStart and calls onChange(remainingMs) on every step. Both modes write back through onChange.

kotlin
// Controlled: the caller owns the remaining value
var remaining by remember { mutableLongStateOf(10_000L) }
LhCountdown(value = remaining, onChange = { remaining = it })

// Uncontrolled: self-running, onFinish fires once at zero
LhCountdown(time = 5_000L, onFinish = { /* finished */ })

Handle methods ​

LhCountdownHandle exposes instance methods as a ref: start() (resume from the paused point, no-op when disabled), pause() (freeze the remaining value), and reset(timeMs?) (return to the total duration and restart per autoStart). The handle has no effect in controlled mode — controlled display is driven by the caller.

kotlin
val handle = remember { LhCountdownHandle() }
LhCountdown(time = 60_000L, handle = handle)
// From interactions: handle.start() / handle.pause() / handle.reset(30_000L)

States ​

disabled = true greys the text (using color.text.disabled) and pauses the countdown: the remaining value freezes and resumes from the paused point when re-enabled. millisecond = true refreshes every frame (16ms step; recommended when the millisecond token is shown, otherwise 250ms).

kotlin
LhCountdown(value = 65000L, disabled = true)
LhCountdown(time = 8950L, format = "mm:ss.S", millisecond = true)

API ​

Props ​

PropDescriptionTypeDefault
modifierModifierModifierModifier
valueRemaining ms (controlled: the component does not self-drive)Long?null
timeUncontrolled total duration ms (self-runs per autoStart)Long0
formatFormat string; tokens DD/HH/mm/ss/SString"HH:mm:ss"
autoStartWhether the uncontrolled mode starts after mountBooleantrue
millisecondFrame-level refresh (16ms step, otherwise 250ms)Booleanfalse
disabledDisabled: greys and pauses the countdownBooleanfalse
ariaLabelAccessibility name (falls back to the formatted text)String?null
handleMethod handle (start/pause/reset); no-op in controlled modeLhCountdownHandle?null
onChangeRemaining-ms change (uncontrolled steps; controlled write-backs)((Long) -> Unit)?null
onFinishFires once when reaching zero(() -> Unit)?null
contentTrailing content slot (rendered after the digits)(@Composable () -> Unit)?null

LhCountdownHandle methods ​

MethodDescriptionParameter
start()Resume from the paused point (no-op when disabled)none
pause()Pause and freeze the remaining msnone
reset(timeMs)Return to the total duration (optional new value) and restart per autoStarttimeMs: Long?

Token mapping ​

Tokens consumed by the countdown (aligned with tokenRefs of specs/countdown.json):

TokenUsage
countdown.digitMinWidthMinimum digit width (tabular-figure stabilization)
typography.fontSize.baseFont size of digits and separators
color.text.primaryDigit text color (normal state)
color.text.secondaryDe-emphasized separator color
color.text.disabledDisabled text color (digits and separators)

Golden case mapping ​

The 10 assertions of cases/countdown.json and their local tests:

case idAssertionLocal test
renders-formatted-remainingControlled value renders 01:01:05 with the default formatLhCountdownTest.controlled_value_renders_formatted_text (Compose UI Test)
negative-clamps-to-zeroNegative value clamps to all zerosLhCountdownTest.negative_value_renders_zeros + LhCountdownDefaultsTest.negative_clamps_to_zero
hours-over-24-without-ddWithout DD, HH carries total hours 50:30:10LhCountdownTest.hours_over_24_without_dd_renders_total_hours
dd-format-splits-daysDD splits days and zero-pads milliseconds to 01天00:00:01.500LhCountdownTest.dd_format_splits_days_and_pads_millis
uncontrolled-autostart-ticksUncontrolled autoStart self-runs and emits onChangeLhCountdownDefaultsTest.uncontrolled_autostart_ticks
finish-emits-once-at-zeroReaching zero emits onFinish once and stops the tickerLhCountdownDefaultsTest.finish_emits_once_at_zero
controlled-does-not-self-driveControlled mode does not self-driveLhCountdownDefaultsTest.controlled_does_not_self_drive
autostart-false-waits-for-startautoStart=false waits for start()LhCountdownDefaultsTest.autostart_false_waits_for_start
disabled-pauses-and-greysDisabled greys (text.disabled) and does not driveLhCountdownDefaultsTest.disabled_pauses_and_greys
aria-role-timerAccessibility name falls back to the formatted text 00:01:05LhCountdownTest.aria_label_defaults_to_formatted_text
bash
./gradlew :components:countdown:test                        # JVM part
./gradlew :components:countdown:connectedAndroidTest        # Rendering part (device required)

lh-ui native bloodline · Android (Kotlin + Compose)