Skip to the content
human-kit design
28 — Pickers

Time Picker

A time typed part by part, or picked from wheels that open under it.

930AM

Installation

pnpm dlx @human-kit/design add time-picker
npx @human-kit/design add time-picker
yarn dlx @human-kit/design add time-picker
bunx @human-kit/design add time-picker

The 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.

730AM

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:mm or HH:mm:ss, always in 24 hours. hourCycle changes only how it reads on the screen.
  • Set granularity to what the answer needs: hour for a slot on the hour, second only when seconds matter.
  • Give minuteStep when the agenda works in slots, 15 or 30: the wheels then offer only those minutes.
  • Use default in a form, ghost in a dense row, underline where 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. Tab or ArrowLeft and ArrowRight move between them, and digits type a value.
  • ArrowUp and ArrowDown step the focused part, PageUp and PageDown by five on the minutes and the seconds, Home and End go to its first and last value. Backspace clears it.
  • The clock button opens the wheels with Enter or Space; each wheel is a spinbutton the arrow keys turn, and Escape closes.
  • Name it with aria-label, or put it in a Field.Root: the field label names the parts through aria-labelledby. triggerLabel and panelLabel name the button and the panel.
  • With an id or a name, a hidden input carries the value, so a <label for> reaches the picker and a form submits it.

API reference

PropTypeDefault
valuestring | null

The time, HH:mm or HH:mm:ss, or null when empty. Bindable.

—
defaultValuestring | null

The time at the start, when you give no value.

—
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. hour gives values on the hour, HH:00.

'minute'
hourCycle12 | 24

How the hours read. By default, as the locale writes them.

—
minValuestring

The earliest time allowed, HH:mm.

—
maxValuestring

The latest time allowed. A range over midnight is not supported.

—
hourStepnumber

The step of the hours, on the wheel and with the arrows.

1
minuteStepnumber

The step of the minutes.

1
secondStepnumber

The step of the seconds.

1
invalidboolean

Marks the time as wrong: a red ring and aria-invalid.

false
readonlyboolean

Shows the time in a grey box. It can be focused, not changed.

false
disabledboolean

Turns the picker off.

false
requiredboolean

Says that a time is needed.

false
openboolean

Whether the wheels are open. Bindable.

—
onOpenChange(open: boolean, details) => void

Called when the wheels open or close. details.cancel() refuses it.

—
idstring

The id of the hidden input, for a <label for>.

—
namestring

The name of the hidden input, for a form.

—
triggerLabelstring

The name of the clock button, read aloud.

'Choose a time'
panelLabelstring

The name of the panel of wheels, read aloud.

'Time'
classstring

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.