All components plainsurfacesmmdlg
Timer
Countdown and stopwatch with a large tabular readout and start, pause, resume and reset controls.
05
00
12345678910111213141516171819202122232425import { Timer } from "#components/ui/ark-ui/timer.tsx";import { indigoPalette } from "@stylexblocks/ui/themes/palettes.ts";
export function Example() { return ( // variant/size resolve once on Root; ActionTriggers hide themselves // when their action doesn't apply (no Pause while paused, ...) <Timer.Root countdown startMs={5 * 60 * 1000} variant="plain" size="md" palette={indigoPalette}> <Timer.Area> <Timer.Item type="minutes" /> <Timer.Separator>:</Timer.Separator> <Timer.Item type="seconds" /> </Timer.Area> <Timer.Control> <Timer.ActionTrigger action="start">Start</Timer.ActionTrigger> <Timer.ActionTrigger action="pause">Pause</Timer.ActionTrigger> <Timer.ActionTrigger action="resume">Resume</Timer.ActionTrigger> <Timer.ActionTrigger action="reset">Reset</Timer.ActionTrigger> </Timer.Control> </Timer.Root> );}
// stopwatch: <Timer.Root startMs={0}> (add targetMs to stop at a limit)// completion: <Timer.Root onComplete={() => ...} completeLabel="Time's up">Variants
05
00
05
00
Sizes
05
00
05
00
05
00
Stopwatch
Without countdown the timer counts up from startMs; a 10ms interval drives the milliseconds item. Restart zeroes it and keeps running.
00
00
000
Completion
The readout is role="timer", so screen readers stay quiet while it ticks; a polite live region announces once when the countdown reaches zero.
00
10
Install
1npx stylexblocks add timerSource
components/ui/ark-ui/timer.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257import * as stylex from "@stylexjs/stylex";import { useState } from "react";import { Timer as ArkTimer } from "@ark-ui/react/timer";import { createSlotRecipeContext } from "#lib/ark-ui/create-slot-recipe-context.tsx";import { defineSlotRecipe } from "#lib/recipe-props.ts";import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'import { letterSpacings } from '@stylexblocks/ui/tokens/letter-spacings.stylex.ts'import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'import { semanticShadows } from '@stylexblocks/ui/semantic-tokens/shadows.stylex.ts'import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
const timerRoot = stylex.create({ base: { display: 'inline-flex', flexDirection: 'column', alignItems: 'center', gap: spacing._4, color: semanticColors.fg, },})
const timerRootSizes = stylex.create({ sm: { '--timer-font-size': fontSizes._2xl, '--timer-item-px': spacing._1_5, '--timer-item-py': spacing._1, '--timer-trigger-height': sizes.controlXs, '--timer-trigger-px': spacing._2, '--timer-trigger-font-size': fontSizes.xs, }, md: { '--timer-font-size': fontSizes._4xl, '--timer-item-px': spacing._2, '--timer-item-py': spacing._1, '--timer-trigger-height': sizes.controlSm, '--timer-trigger-px': spacing._2_5, '--timer-trigger-font-size': fontSizes.sm, }, lg: { '--timer-font-size': fontSizes._6xl, '--timer-item-px': spacing._3, '--timer-item-py': spacing._1_5, '--timer-trigger-height': sizes.controlMd, '--timer-trigger-px': spacing._3, '--timer-trigger-font-size': fontSizes.sm, },})
/** The time readout: one big line of tabular numerals. */const timerArea = stylex.create({ base: { display: 'flex', alignItems: 'center', gap: spacing._1, fontSize: 'var(--timer-font-size)', lineHeight: 1, fontWeight: fontWeights.semibold, letterSpacing: letterSpacings.tight, fontVariantNumeric: 'tabular-nums', },})
const timerItem = stylex.create({ base: { minWidth: '2ch', textAlign: 'center', fontVariantNumeric: 'tabular-nums', },})
const timerItemVariants = stylex.create({ plain: {}, surface: { paddingInline: 'var(--timer-item-px)', paddingBlock: 'var(--timer-item-py)', borderRadius: semanticRadii.panel, backgroundColor: colorPalette.subtle, color: colorPalette.fg, boxShadow: `inset 0 0 0 1px ${colorPalette.border}`, },})
const timerSeparator = stylex.create({ base: { color: semanticColors.fgSubtle, fontWeight: fontWeights.medium, userSelect: 'none', },})
const timerControl = stylex.create({ base: { display: 'flex', alignItems: 'center', gap: spacing._2, },})
/** Start / pause / resume / reset buttons, styled like a `surface` Button. */const timerActionTrigger = stylex.create({ base: { display: { default: 'inline-flex', ':is([hidden])': 'none', }, alignItems: 'center', justifyContent: 'center', gap: spacing._1_5, appearance: 'none', flexShrink: 0, userSelect: 'none', whiteSpace: 'nowrap', height: 'var(--timer-trigger-height)', minWidth: 'var(--timer-trigger-height)', paddingInline: 'var(--timer-trigger-px)', borderRadius: semanticRadii.control, fontFamily: 'inherit', fontSize: 'var(--timer-trigger-font-size)', lineHeight: lineHeights.sm, fontWeight: fontWeights.medium, color: colorPalette.fg, backgroundColor: { default: semanticColors.bg, ':hover': colorPalette.muted, }, borderWidth: '1px', borderStyle: 'solid', borderColor: { default: `color-mix(in oklch, ${colorPalette.emphasized} 90%, transparent)`, ':hover': colorPalette.emphasized, }, boxShadow: semanticShadows.xs, cursor: { default: 'pointer', ':is(:disabled, [disabled], [data-disabled])': 'not-allowed', }, opacity: { default: null, ':is(:disabled, [disabled], [data-disabled])': 0.5, }, transitionProperty: 'background-color, border-color, color, box-shadow', transitionDuration: durations.moderate, outlineWidth: { default: null, ':focus-visible': '1px', }, outlineOffset: { default: null, ':focus-visible': '2px', }, outlineStyle: { default: 'none', ':focus-visible': 'solid', }, outlineColor: { default: null, ':focus-visible': colorPalette.focusRing, }, },})
export type TimerSize = keyof typeof timerRootSizes
export type TimerVariant = keyof typeof timerItemVariants
const timerRecipe = defineSlotRecipe({ slots: { root: { styles: timerRoot, sizes: timerRootSizes }, area: { styles: timerArea }, item: { styles: timerItem, variants: timerItemVariants }, separator: { styles: timerSeparator }, control: { styles: timerControl }, actionTrigger: { styles: timerActionTrigger }, }, defaultVariants: { size: "md", variant: "plain" },});
const chrome = stylex.create({ srOnly: { position: "absolute", width: "1px", height: "1px", padding: 0, margin: "-1px", overflow: "hidden", clip: "rect(0, 0, 0, 0)", whiteSpace: "nowrap", borderWidth: 0, },});
const { withProvider, withContext } = createSlotRecipeContext(timerRecipe);
/** * The machine's Area is `role="timer"` (implicitly `aria-live="off"`), so * ticks stay silent. Root adds a polite live region that speaks once, when * the timer reaches its target. */function RootNode({ children, onComplete, completeLabel = "Timer complete", ...props}: React.ComponentProps<typeof ArkTimer.Root> & { /** Announced to screen readers when the timer reaches its target. */ completeLabel?: string;}) { const [announcement, setAnnouncement] = useState(""); return ( <ArkTimer.Root {...props} onTick={(details) => { if (announcement) setAnnouncement(""); props.onTick?.(details); }} onComplete={() => { setAnnouncement(completeLabel); onComplete?.(); }} > {children} <span role="status" aria-live="polite" {...stylex.props(chrome.srOnly)}> {announcement} </span> </ArkTimer.Root> );}
/** * Timer — Ark UI's timer machine (countdown or stopwatch) wearing the timer * slot recipe. `<Timer.Root variant size>` resolves the recipe once; every * part inherits it through context. Area holds the big tabular-number * readout of Items (`type="hours" | "minutes" | "seconds" | ...`) and * Separators; Control holds ActionTriggers (`action="start" | "pause" | * "resume" | "reset" | "restart"`), which the machine hides when they don't * apply. */export const Timer = { Root: withProvider(RootNode, "root", "TimerRoot"), Area: withContext(ArkTimer.Area, "area", "TimerArea"), Item: withContext(ArkTimer.Item, "item", "TimerItem"), Separator: withContext(ArkTimer.Separator, "separator", "TimerSeparator"), Control: withContext(ArkTimer.Control, "control", "TimerControl"), ActionTrigger: withContext(ArkTimer.ActionTrigger, "actionTrigger", "TimerActionTrigger"), Context: ArkTimer.Context,};
export type TimerRootProps = React.ComponentProps<typeof Timer.Root>;export type TimerItemProps = React.ComponentProps<typeof Timer.Item>;export type TimerActionTriggerProps = React.ComponentProps<typeof Timer.ActionTrigger>;