Timer

Used to record the time elapsed from zero or since a specified target time.

Loading...

Anatomy

02Hours15Minutes30Seconds::StartPauseResetrootareaitemcontrolaction trigger
<Timer.Root>
  <Timer.Area>
    <Timer.Item />
    <Timer.Separator />
  </Timer.Area>
  <Timer.Control>
    <Timer.ActionTrigger />
  </Timer.Control>
</Timer.Root>

Examples

Countdown

Set countdown to true and startMs to the initial time.

Interval

interval sets how often the timer updates. Lower it to display milliseconds.

Events

  • onComplete fires when the timer reaches its target time.
  • onTick fires on each update with the current timer state.

Pomodoro

A timer that alternates between work and break sessions.

Root Provider

Use RootProvider with the useTimer hook to access the state and methods from outside the component.

API Reference

Props

Root

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
autoStart
boolean

Whether the timer should start automatically

countdown
boolean

Whether the timer should countdown, decrementing the timer on each tick.

ids
ElementIds

The ids of the timer parts

interval1000
number

The interval in milliseconds to update the timer count.

onComplete
() => void

Function invoked when the timer is completed

onTick
(details: TickDetails) => void

Function invoked when the timer ticks

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

startMs
number

The total duration of the timer in milliseconds.

targetMs
number

The minimum count of the timer in milliseconds.

translations
IntlTranslations

Specifies the localized strings that identifies the accessibility elements and their states

ActionTrigger

Defines the <button> element.

AttributeDefaultType
action
TimerAction

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<TimerActionTriggerState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Area

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Control

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Item

Defines the <div> element.

AttributeDefaultType
type
keyof Time<number>

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-timer-item]<uid>
[data-type]The type of the item
CSS VariableDescription
--valueThe current value

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UseTimerReturn

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Separator

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Context

API

PropertyType
running
boolean

Whether the timer is running.

paused
boolean

Whether the timer is paused.

time
Time

The formatted timer count value.

formattedTime
Time<string>

The formatted time parts of the timer count.

start
VoidFunction

Function to start the timer.

pause
VoidFunction

Function to pause the timer.

resume
VoidFunction

Function to resume the timer.

reset
VoidFunction

Function to reset the timer.

restart
VoidFunction

Function to restart the timer.

progressPercent
number

The progress percentage of the timer.

getActionTriggerState
(props: ActionTriggerProps) => ActionTriggerState

Returns the state of an action trigger