Timer
Used to record the time elapsed from zero or since a specified target time.
Examples
Learn how to use the Timer component in your project. Let's take a look at the most basic example:
Example not foundExample not foundExample not foundExample not foundCountdown Timer
You can create a countdown timer by setting the targetMs prop to a future timestamp:
Example not foundExample not foundExample not foundExample not foundTimer Events
The Timer component provides events that you can listen to for various timer-related actions.
- The
onCompleteevent is triggered when the timer reaches its target time. - The
onTickevent is called on each timer update, providing details about the current timer state.
Example not foundExample not foundExample not foundExample not foundUsing the Root Provider
The RootProvider component provides a context for the timer. It accepts the value of the useTimer hook. You can
leverage it to access the component state and methods from outside the timer.
Example not foundExample not foundExample not foundExample not foundIf you're using the
RootProvidercomponent, you don't need to use theRootcomponent.
API Reference
Props
Root
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. 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 | Partial<{ root: string; area: string }>The 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 | |
startMs | numberThe total duration of the timer in milliseconds. | |
targetMs | numberThe minimum count of the timer in milliseconds. |
ActionTrigger
| Prop | Default | Type |
|---|---|---|
action | TimerAction | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Area
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Control
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Item
| Prop | Default | Type |
|---|---|---|
type | keyof Time<number> | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| CSS Variable | Description |
|---|---|
--value | The current value |
| Data Attribute | Value |
|---|---|
[data-scope] | timer |
[data-part] | item |
[data-type] | The type of the item |
RootProvider
| Prop | Default | Type |
|---|---|---|
value | UseTimerReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Separator
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Context
These are the properties available when using Timer.Context, useTimerContext hook or useTimer hook.
API
| Property | Type |
|---|---|
running | booleanWhether the timer is running. |
paused | booleanWhether the timer is paused. |
time | Time<number>The 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. |