---
title: "Timer · Ark UI — StyleX Blocks"
description: "Countdown and stopwatch with a large tabular readout and start, pause, resume and reset controls."
url: "https://stylexblocks.com/components/ark-ui/timer/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Timer

Countdown and stopwatch with a large tabular readout and start, pause, resume and reset controls.

- Behavior engine: Ark UI
- Also available for: [Base UI](https://stylexblocks.com/components/base-ui/timer.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/timer.md)
- Category: Display
- Variants: plain, surface
- Sizes: sm, md, lg
- HTML page: https://stylexblocks.com/components/ark-ui/timer/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init
```

Then add the item as source you own:

```sh
npx stylexblocks add timer
```

Equivalent: `npx shadcn add @stylexblocks/timer`. Registry item JSON: https://stylexblocks.com/r/ark-ui/timer.json

## Usage

```tsx
import { 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">
```

## Source

`components/ui/ark-ui/timer.tsx`

```tsx
import * 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>;
```
