# Timer
URL: https://ark-ui.com/docs/components/timer
Source: https://raw.githubusercontent.com/chakra-ui/ark/refs/heads/main/website/src/content/pages/components/timer.mdx
Used to record the time elapsed from zero or since a specified target time.
---
## Anatomy
```tsx
```
## Examples
**Example: basic**
#### React
```tsx
import { Timer } from '@ark-ui/react/timer'
import { PauseIcon, PlayIcon } from 'lucide-react'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Basic = () => (
days
:
hours
:
minutes
:
seconds
Play
Resume
Pause
)
```
#### Solid
```tsx
import { Timer } from '@ark-ui/solid/timer'
import { PauseIcon, PlayIcon } from 'lucide-solid'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Basic = () => (
days
:
hours
:
minutes
:
seconds
Play
Resume
Pause
)
```
#### Vue
```vue
days
:
hours
:
minutes
:
seconds
Play
Resume
Pause
```
#### Svelte
```svelte
days
:
hours
:
minutes
:
seconds
Play
Resume
Pause
```
### Countdown
You can create a countdown timer by setting the `countdown` prop to `true` and `startMs` to the initial time:
**Example: countdown**
#### React
```tsx
import { Timer } from '@ark-ui/react/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-react'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Countdown = () => (
minutes
:
seconds
Start
Pause
Reset
)
```
#### Solid
```tsx
import { Timer } from '@ark-ui/solid/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-solid'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Countdown = () => (
minutes
:
seconds
Start
Pause
Reset
)
```
#### Vue
```vue
minutes
:
seconds
Start
Pause
Reset
```
#### Svelte
```svelte
minutes
:
seconds
Start
Pause
Reset
```
### Interval
Use the `interval` prop to control how frequently the timer updates. This is useful for displaying milliseconds:
**Example: interval**
#### React
```tsx
import { Timer } from '@ark-ui/react/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-react'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Interval = () => (
seconds
.
ms
Start
Pause
Reset
)
```
#### Solid
```tsx
import { Timer } from '@ark-ui/solid/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-solid'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Interval = () => (
seconds
.
ms
Start
Pause
Reset
)
```
#### Vue
```vue
seconds
.
ms
Start
Pause
Reset
```
#### Svelte
```svelte
seconds
.
ms
Start
Pause
Reset
```
### Events
The Timer component provides events that you can listen to for various timer-related actions.
- The `onComplete` event is triggered when the timer reaches its target time.
- The `onTick` event is called on each timer update, providing details about the current timer state.
**Example: events**
#### React
```tsx
import { Timer } from '@ark-ui/react/timer'
import { PlayIcon, RotateCcwIcon } from 'lucide-react'
import { useState } from 'react'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Events = () => {
const [ticks, setTicks] = useState(0)
return (
console.log('Timer completed')}
onTick={() => setTicks((t) => t + 1)}
>
minutes
:
seconds
Start
Reset
)
}
```
#### Solid
```tsx
import { Timer } from '@ark-ui/solid/timer'
import { PlayIcon, RotateCcwIcon } from 'lucide-solid'
import { createSignal } from 'solid-js'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Events = () => {
const [ticks, setTicks] = createSignal(0)
return (
console.log('Timer completed')}
onTick={() => setTicks((t) => t + 1)}
>
minutes
:
seconds
Start
Reset
)
}
```
#### Vue
```vue
minutes
:
seconds
Start
Reset
```
#### Svelte
```svelte
minutes
:
seconds
Start
Reset
```
### Pomodoro
Here's an example of building a pomodoro timer that alternates between work and break sessions:
**Example: pomodoro**
#### React
```tsx
import { Timer } from '@ark-ui/react/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-react'
import { useState } from 'react'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Pomodoro = () => {
const [isWorking, setIsWorking] = useState(true)
const [cycles, setCycles] = useState(0)
const handleComplete = () => {
setIsWorking(!isWorking)
if (!isWorking) setCycles((c) => c + 1)
}
return (
{isWorking ? 'Work Session' : 'Break Session'}
minutes
:
seconds
Start
Pause
Reset
)
}
```
#### Solid
```tsx
import { Timer } from '@ark-ui/solid/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-solid'
import { createSignal } from 'solid-js'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const Pomodoro = () => {
const [isWorking, setIsWorking] = createSignal(true)
const [cycles, setCycles] = createSignal(0)
const handleComplete = () => {
setIsWorking(!isWorking())
if (!isWorking()) setCycles((c) => c + 1)
}
return (
{isWorking() ? 'Work Session' : 'Break Session'}
minutes
:
seconds
Start
Pause
Reset
)
}
```
#### Vue
```vue
{{ isWorking ? 'Work Session' : 'Break Session' }}
minutes
:
seconds
Start
Pause
Reset
```
#### Svelte
```svelte
{isWorking ? 'Work Session' : 'Break Session'}
minutes
:
seconds
Start
Pause
Reset
```
### Root Provider
An alternative way to control the timer is to use the `RootProvider` component and the `useTimer` hook. This way you can
access the state and methods from outside the component.
**Example: root-provider**
#### React
```tsx
import { Timer, useTimer } from '@ark-ui/react/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-react'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const RootProvider = () => {
const timer = useTimer({ targetMs: 60 * 60 * 1000 })
return (
hours
:
minutes
:
seconds
Start
Resume
Pause
Reset
)
}
```
#### Solid
```tsx
import { Timer, useTimer } from '@ark-ui/solid/timer'
import { PauseIcon, PlayIcon, RotateCcwIcon } from 'lucide-solid'
import button from 'styles/button.module.css'
import styles from 'styles/timer.module.css'
export const RootProvider = () => {
const timer = useTimer({ targetMs: 60 * 60 * 1000 })
return (
hours
:
minutes
:
seconds
Start
Resume
Pause
Reset
)
}
```
#### Vue
```vue
hours
:
minutes
:
seconds
Start
Resume
Pause
Reset
```
#### Svelte
```svelte
hours
:
minutes
:
seconds
Start
Resume
Pause
Reset
```
## API Reference
### Props
**Component API Reference**
#### React
**Root Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `autoStart` | `boolean` | No | Whether the timer should start automatically |
| `countdown` | `boolean` | No | Whether the timer should countdown, decrementing the timer on each tick. |
| `ids` | `Partial<{ root: string; area: string }>` | No | The ids of the timer parts |
| `interval` | `number` | No | The interval in milliseconds to update the timer count. |
| `onComplete` | `() => void` | No | Function invoked when the timer is completed |
| `onTick` | `(details: TickDetails) => void` | No | Function invoked when the timer ticks |
| `startMs` | `number` | No | The total duration of the timer in milliseconds. |
| `targetMs` | `number` | No | The minimum count of the timer in milliseconds. |
**ActionTrigger Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `action` | `TimerAction` | Yes | |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Area Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Control Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Item Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `type` | `keyof Time` | Yes | |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Item Data Attributes:**
| Attribute | Value |
|-----------|-------|
| `[data-scope]` | timer |
| `[data-part]` | item |
| `[data-type]` | The type of the item |
**Item CSS Variables:**
| Variable | Description |
|----------|-------------|
| `--value` | The current value |
**RootProvider Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `UseTimerReturn` | Yes | |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Separator Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
#### Solid
**Root Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `autoStart` | `boolean` | No | Whether the timer should start automatically |
| `countdown` | `boolean` | No | Whether the timer should countdown, decrementing the timer on each tick. |
| `ids` | `Partial<{ root: string; area: string }>` | No | The ids of the timer parts |
| `interval` | `number` | No | The interval in milliseconds to update the timer count. |
| `onComplete` | `() => void` | No | Function invoked when the timer is completed |
| `onTick` | `(details: TickDetails) => void` | No | Function invoked when the timer ticks |
| `startMs` | `number` | No | The total duration of the timer in milliseconds. |
| `targetMs` | `number` | No | The minimum count of the timer in milliseconds. |
**ActionTrigger Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `action` | `TimerAction` | Yes | |
| `asChild` | `(props: ParentProps<'button'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Area Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Control Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Item Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `type` | `keyof Time` | Yes | |
| `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Item Data Attributes:**
| Attribute | Value |
|-----------|-------|
| `[data-scope]` | timer |
| `[data-part]` | item |
| `[data-type]` | The type of the item |
**Item CSS Variables:**
| Variable | Description |
|----------|-------------|
| `--value` | The current value |
**RootProvider Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `UseTimerReturn` | Yes | |
| `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Separator Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `(props: ParentProps<'div'>) => Element` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
#### Vue
**Root Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `autoStart` | `boolean` | No | Whether the timer should start automatically |
| `countdown` | `boolean` | No | Whether the timer should countdown, decrementing the timer on each tick. |
| `id` | `string` | No | The unique identifier of the machine. |
| `ids` | `Partial<{ root: string; area: string }>` | No | The ids of the timer parts |
| `interval` | `number` | No | The interval in milliseconds to update the timer count. |
| `startMs` | `number` | No | The total duration of the timer in milliseconds. |
| `targetMs` | `number` | No | The minimum count of the timer in milliseconds. |
**ActionTrigger Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `action` | `TimerAction` | Yes | |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Area Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Control Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Item Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `type` | `keyof Time` | Yes | |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Item Data Attributes:**
| Attribute | Value |
|-----------|-------|
| `[data-scope]` | timer |
| `[data-part]` | item |
| `[data-type]` | The type of the item |
**Item CSS Variables:**
| Variable | Description |
|----------|-------------|
| `--value` | The current value |
**RootProvider Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `TimerApi` | Yes | |
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
**Separator Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `boolean` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
#### Svelte
**Root Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `autoStart` | `boolean` | No | Whether the timer should start automatically |
| `countdown` | `boolean` | No | Whether the timer should countdown, decrementing the timer on each tick. |
| `id` | `string` | No | The unique identifier of the machine. |
| `ids` | `Partial<{ root: string; area: string }>` | No | The ids of the timer parts |
| `interval` | `number` | No | The interval in milliseconds to update the timer count. |
| `onComplete` | `() => void` | No | Function invoked when the timer is completed |
| `onTick` | `(details: TickDetails) => void` | No | Function invoked when the timer ticks |
| `ref` | `Element` | No | |
| `startMs` | `number` | No | The total duration of the timer in milliseconds. |
| `targetMs` | `number` | No | The minimum count of the timer in milliseconds. |
**ActionTrigger Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `action` | `TimerAction` | Yes | |
| `asChild` | `Snippet<[PropsFn<'button'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `ref` | `Element` | No | |
**Area Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `ref` | `Element` | No | |
**Context Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `api` | `Snippet<[UseTimerContext]>` | No | |
**Control Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `ref` | `Element` | No | |
**Item Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `type` | `keyof Time` | Yes | |
| `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `ref` | `Element` | No | |
**Item Data Attributes:**
| Attribute | Value |
|-----------|-------|
| `[data-scope]` | timer |
| `[data-part]` | item |
| `[data-type]` | The type of the item |
**Item CSS Variables:**
| Variable | Description |
|----------|-------------|
| `--value` | The current value |
**RootProvider Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `value` | `UseTimerReturn` | Yes | |
| `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `ref` | `Element` | No | |
**Separator Props:**
| Prop | Type | Required | Description |
|------|------|----------|-------------|
| `asChild` | `Snippet<[PropsFn<'div'>]>` | No | Use the provided child element as the default rendered element, combining their props and behavior. |
| `ref` | `Element` | No | |
### Context
**API:**
| Property | Type | Description |
|----------|------|-------------|
| `running` | `boolean` | Whether the timer is running. |
| `paused` | `boolean` | Whether the timer is paused. |
| `time` | `Time` | The formatted timer count value. |
| `formattedTime` | `Time` | 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. |