# 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** ```ripple import { Timer } from 'ark-ripple/timer'; import { Pause, Play } from 'lucide-ripple'; import button from 'styles/button.module.css'; import styles from 'styles/timer.module.css'; export component Basic() {
{'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** ```ripple import { Timer } from 'ark-ripple/timer'; import { Pause, Play, RotateCcw } from 'lucide-ripple'; import button from 'styles/button.module.css'; import styles from 'styles/timer.module.css'; export component Countdown() {
{'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** ```ripple import { Timer } from 'ark-ripple/timer'; import { Pause, Play, RotateCcw } from 'lucide-ripple'; import button from 'styles/button.module.css'; import styles from 'styles/timer.module.css'; export component Interval() {
{'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** ```ripple import { Timer } from 'ark-ripple/timer'; import { Play, RotateCcw } from 'lucide-ripple'; import { track } from 'ripple'; import button from 'styles/button.module.css'; import styles from 'styles/timer.module.css'; export component Events() { let ticks = track(0); { console.log('Timer completed'); }} onTick={() => { @ticks = @ticks + 1; }} >
{'minutes'}
{':'}
{'seconds'}
{'Start'} {'Reset'} {'Ticks: '} {@ticks}
} ``` ### Pomodoro Here's an example of building a pomodoro timer that alternates between work and break sessions: **Example: pomodoro** ```ripple import { Timer } from 'ark-ripple/timer'; import { Pause, Play, RotateCcw } from 'lucide-ripple'; import { track } from 'ripple'; import button from 'styles/button.module.css'; import styles from 'styles/timer.module.css'; export component Pomodoro() { let isWorking = track(true); let cycles = track(0); const handleComplete = () => { @isWorking = !@isWorking; if (!@isWorking) { @cycles = @cycles + 1; } };

{@isWorking ? 'Work Session' : 'Break Session'}

{'minutes'}
{':'}
{'seconds'}
{'Start'} {'Pause'} {'Reset'} {'Completed cycles: '} {@cycles}
} ``` ### 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** ```ripple import { Timer, useTimer } from 'ark-ripple/timer'; import { Pause, Play, RotateCcw } from 'lucide-ripple'; import button from 'styles/button.module.css'; import styles from 'styles/timer.module.css'; export component RootProvider() { const timer = useTimer({ targetMs: 60 * 60 * 1000 });
{'timer: '} {JSON.stringify(@timer.time)}
{'hours'}
{':'}
{'minutes'}
{':'}
{'seconds'}
{'Start'} {'Resume'} {'Pause'} {'Reset'}
} ``` ## API Reference ### Props **Component API Reference** **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. | ### 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. |