Timer
Used to record the time elapsed from zero or since a specified target time.
Anatomy
<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
onCompletefires when the timer reaches its target time.onTickfires 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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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 | booleanWhether the timer should start automatically | |
countdown | booleanWhether the timer should countdown, decrementing the timer on each tick. | |
ids | ElementIdsThe ids of the timer parts | |
interval | 1000 | numberThe interval in milliseconds to update the timer count. |
onComplete | () => voidFunction invoked when the timer is completed | |
onTick | (details: TickDetails) => voidFunction 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 | numberThe total duration of the timer in milliseconds. | |
targetMs | numberThe minimum count of the timer in milliseconds. | |
translations | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states |
ActionTrigger
Defines the <button> element.
| Attribute | Default | Type |
|---|---|---|
action | TimerAction | |
asChild | booleanUse 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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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.
| Attribute | Default | Type |
|---|---|---|
type | keyof Time<number> | |
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[data-timer-item] | <uid> |
[data-type] | The type of the item |
| CSS Variable | Description |
|---|---|
--value | The current value |
RootProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseTimerReturn | |
asChild | booleanUse 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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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
| Property | Type |
|---|---|
running | booleanWhether the timer is running. |
paused | booleanWhether the timer is paused. |
time | TimeThe formatted timer count value. |
formattedTime | Time<string>The formatted time parts of the timer count. |
start | VoidFunctionFunction to start the timer. |
pause | VoidFunctionFunction to pause the timer. |
resume | VoidFunctionFunction to resume the timer. |
reset | VoidFunctionFunction to reset the timer. |
restart | VoidFunctionFunction to restart the timer. |
progressPercent | numberThe progress percentage of the timer. |
getActionTriggerState | (props: ActionTriggerProps) => ActionTriggerStateReturns the state of an action trigger |