Time Picker
A time typed part by part, or picked from wheels that open under it.
Installation
pnpm dlx @human-kit/design add time-pickernpx @human-kit/design add time-pickeryarn dlx @human-kit/design add time-pickerbunx @human-kit/design add time-pickerThe command copies the files to src/lib/components/ui/time-picker. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as TimePicker from '$lib/components/ui/time-picker';
let time = $state<string | null>(null);
</script>
<TimePicker.Root aria-label="Appointment time" bind:value={time} /> Examples
In a field, with limits
Spread what a Field.Root hands over to name the picker and tie its hint and error to it. minValue and maxValue turn off the times outside the range on the wheels, and minuteStep steps the minutes.
Between 7:00 and 19:00.
Guidelines
- Use it for a time of day: an appointment, the start of a shift. For a length of time, such as minutes of a procedure, use a number field.
- The value is a string,
HH:mmorHH:mm:ss, always in 24 hours.hourCyclechanges only how it reads on the screen. - Set
granularityto what the answer needs:hourfor a slot on the hour,secondonly when seconds matter. - Give
minuteStepwhen the agenda works in slots, 15 or 30: the wheels then offer only those minutes. - Use
defaultin a form,ghostin a dense row,underlinewhere the surface is already a box. Match the size of the fields beside it.
Accessibility
- The hours and the minutes are
role="spinbutton"parts, each named and with its value read aloud.TaborArrowLeftandArrowRightmove between them, and digits type a value. ArrowUpandArrowDownstep the focused part,PageUpandPageDownby five on the minutes and the seconds,HomeandEndgo to its first and last value.Backspaceclears it.- The clock button opens the wheels with
EnterorSpace; each wheel is a spinbutton the arrow keys turn, andEscapecloses. - Name it with
aria-label, or put it in aField.Root: the field label names the parts througharia-labelledby.triggerLabelandpanelLabelname the button and the panel. - With an
idor aname, a hidden input carries the value, so a<label for>reaches the picker and a form submits it.
API reference
| Prop | Type | Default |
|---|---|---|
value | string | null The time, | — |
defaultValue | string | null The time at the start, when you give no | — |
onChange | (value: string | null) => void Called when the time changes. | — |
variant | 'default' | 'filled' | 'ghost' | 'underline' A white box, no box until used, or one line under it. | 'default' |
size | 'sm' | 'default' | 'lg' The height, the same as an input of that size. | 'default' |
granularity | 'hour' | 'minute' | 'second' The smallest part to pick. | 'minute' |
hourCycle | 12 | 24 How the hours read. By default, as the locale writes them. | — |
minValue | string The earliest time allowed, | — |
maxValue | string The latest time allowed. A range over midnight is not supported. | — |
hourStep | number The step of the hours, on the wheel and with the arrows. | 1 |
minuteStep | number The step of the minutes. | 1 |
secondStep | number The step of the seconds. | 1 |
invalid | boolean Marks the time as wrong: a red ring and | false |
readonly | boolean Shows the time in a grey box. It can be focused, not changed. | false |
disabled | boolean Turns the picker off. | false |
required | boolean Says that a time is needed. | false |
open | boolean Whether the wheels are open. Bindable. | — |
onOpenChange | (open: boolean, details) => void Called when the wheels open or close. | — |
id | string The id of the hidden input, for a | — |
name | string The name of the hidden input, for a form. | — |
triggerLabel | string The name of the clock button, read aloud. | 'Choose a time' |
panelLabel | string The name of the panel of wheels, read aloud. | 'Time' |
class | string Classes for the box. Give it a width: it fills its container. | — |
The picker builds its input, button and panel itself, so it takes no other attributes.