Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

UIComponentsReact

Timer

A countdown or a stopwatch, with the time in large digits and buttons that start, pause, resume and reset it.

Built on the headless Timer.See its API in Docs

Rendered by React 19.3.1

timer.tsx

Installation

npx shadcn@4.23.0 add https://hamur.dev/r/react/timer.json

It also adds utils, button. The app needs its setup first: Installation.

Usage

Give Timer a duration in milliseconds for a countdown, or mode="stopwatch" for a stopwatch, or a target instant to count down to. Put a TimerLabel, a TimerValue and a TimerTrigger for each action inside it: toggle starts, pauses, resumes or restarts by status, and start, pause, resume, reset, restart and add do one thing each. The Value draws the time ("4:59") and gives a screen reader its long form ("4 minutes, 59 seconds"); put TimerSegments with TimerSeparators or TimerSegmentLabels inside it to draw the units yourself. A Trigger whose action does not apply in the current status stays focusable and is marked disabled. The run, overtime, interval, largestUnit, announce, labels, onValueChange and onComplete come from the headless Timer.

import {
  Timer,
  TimerLabel,
  TimerSegment,
  TimerSegmentLabel,
  TimerSeparator,
  TimerTrigger,
  TimerValue,
} from '@/components/ui/timer'

Examples

Default

A five-minute countdown with a label, the time and a start, pause and reset button.

Its preview and code are at the top of the page.

The Value is text-4xl with tabular digits, so the width does not change as the time counts. Past the end with overtime the time is drawn in the destructive colour with a minus sign before it, so it is never told by colour alone (WCAG 1.4.1). Each Trigger is the Button's outline icon, 36 by 36 CSS pixels (WCAG 2.5.8), with a focus ring (WCAG 2.4.7); it shows a play icon, a pause icon while running and a restart icon once completed, hidden from assistive technology, and is named by its action ("Start", "Pause", "Reset"). An add Trigger has no icon: give it a text such as "+1 min" and change its size. The Value is not announced as it changes; the end and the announce milestones are, through a status region inside the Root. This item takes Base UI's component ID as its slug and one name per Base UI part.

API reference

The styled parts pass every prop to the headless part they wrap and add classes. The props, data attributes and keyboard are on Timer's API reference.