TimePicker

This is a time picker that you assemble from parts. It puts a time input with segments together with a popover of wheel columns. Each value is an HH:mm string or an HH:mm:ss string. The empty state is null.

hhmma. m.

Value: null

Anatomy

TimePicker.Root holds the value and the open state. TimePicker.Input makes the segments for the locale. TimePicker.Trigger opens the popover. TimePicker.Clock calculates the wheel columns from the state of the root (granularity and hourCycle). The sequence is always the same: the hour, then the minute, then the second, then the day period. The column snippet changes each wheel. If you give no column snippet, the component makes the default columns.

<script>
	import { TimePicker } from '@human-kit/ui/timepicker';
</script>

<TimePicker.Root>
	<TimePicker.Input aria-label="Time input">
		{#snippet children(segment)}
			<TimePicker.Segment {segment} />
		{/snippet}
	</TimePicker.Input>
	<TimePicker.Trigger />

	<TimePicker.Popover>
		<TimePicker.Clock />
	</TimePicker.Popover>
</TimePicker.Root>

Minimum and maximum

The minValue and maxValue props set the limits for the text that the user types and for the wheels. The component shows a wheel item that is out of the limits with data-disabled. If the user types a time that is out of the limits, the input becomes invalid. The component does not correct the time. A range that goes through midnight is not possible.

930AM

Time constrained between 09:00 and 17:00

12-hour cycle

Set hourCycle={12} to add an AM/PM segment and a day-period wheel column. In the code, the value is always a 24-hour string. The 12-hour cycle changes only the parts that the user sees.

245PM

Emitted value stays 24-hour: 14:45

Usage guidelines

  • When your own code controls the state, use value with onChange. When the component controls the state, use defaultValue. The empty state is null.
  • The granularity prop sets the units that the user can edit: 'hour', 'minute' (the default), or 'second'. With granularity="hour", the value is HH:00.
  • Use hourStep, minuteStep, and secondStep to limit the values to given increments.
  • Use open, defaultOpen, and onOpenChange to control the popover. When a wheel stops on a value, the component immediately makes that value the new value.
  • TimePicker.Popover accepts the props of Popover.Content, for example placement (the default is bottom), offset, and shouldFlip.
  • Put the picker in a LocaleProvider to localize the sequence of the segments, the day-period names, and the default hour cycle.

Accessibility

  • The accessible name of each segment comes from the active locale.
  • TimePicker.Input gets aria-invalid and data-invalid when the segments do not make a valid time.
  • Each wheel column has role="spinbutton" with aria-valuenow, aria-valuetext, aria-valuemin, and aria-valuemax.
  • In the popover, the ArrowUp key and the ArrowDown key change the column with the focus. The ArrowLeft key and the ArrowRight key move the focus between the columns. The Home key and the End key move to the first value and to the last value of the column.
  • When the popover opens, the focus goes to the first wheel column. The Escape key and a press outside the popover close it.

API reference

Root

The container with the state. It holds the time value, the open state of the popover, and the granularity and hour-cycle settings. The input and the clock share them.

Prop Type Default

* required. Native HTML attributes of the underlying element are also accepted.

Input

The time input with segments. It goes through the segments of the locale and gives each one to the children snippet.

Prop Type Default

* required. Native HTML attributes of the underlying element are also accepted.

Data attribute Description
data-disabled Present when the time picker is disabled.
data-focus-visible Present while the focus must be visible (keyboard modality).
data-focus-within Present while a segment inside the input has focus.
data-invalid Present while the text in the segments does not make a valid value.
data-open Present while the popover is open.
data-readonly Present when the time picker is readonly.

Segment

One segment of the time: the hour, the minute, the second, the day period, or a separator. The user can type in it, and the arrow keys change its value.

Prop Type Default

* required. Native HTML attributes of the underlying element are also accepted.

Data attribute Description
data-focus-visible Present while the focus must be visible (keyboard modality).
data-focused Present while the segment has focus.
data-placeholder Present while the segment shows its placeholder and not a value.
data-time-picker-segment Identifies a time picker segment element.
data-type The segment type, for example 'hour', 'minute', 'second', 'dayPeriod', or 'literal'.

Trigger

The button that opens and closes the clock popover.

Prop Type Default

* required. Native HTML attributes of the underlying element are also accepted.

Data attribute Description
data-focus-visible Present while the focus must be visible (keyboard modality).
data-focused Present while the trigger has focus.

Popover

The panel that floats and contains the clock. It accepts Popover.Content props, for example placement, offset, and shouldFlip. The component controls the open state, the reference to the trigger, and the id. The default placement is bottom, and the focus goes first to the first wheel column.

Prop Type Default

* required. Native HTML attributes of the underlying element are also accepted.

Clock

The wheels in the popover. It calculates the columns from the state of the root: granularity and hourCycle. The sequence is always the same: the hour, then the minute, then the second, then the day period.

Prop Type Default

* required. Native HTML attributes of the underlying element are also accepted.

Data attribute Description
data-clock Identifies the clock element.

WheelColumn

One wheel that scrolls, with role="spinbutton", for one segment: the hour, the minute, the second, or the day period.

Prop Type Default

* required. Native HTML attributes of the underlying element are also accepted.

Data attribute Description
data-focus-visible Present while the focus must be visible (keyboard modality).
data-focus-within Present while the column contains focus.
data-wheel-highlight Identifies the column's central highlight band.
data-wheel-spacer Identifies the spacers that let an item at an end reach the center.

WheelItem

An item in a wheel column, with no styles. It makes one option and shows its state in data attributes. You write all of the styles.

Prop Type Default

* required. Native HTML attributes of the underlying element are also accepted.

Data attribute Description
data-centered Present while the item is at the center of the wheel.
data-disabled Present when the user cannot select the item, because it is out of the range or not on a step.
data-selected Present when the item is the value of its column.
data-type The segment type of the item's column.
data-value The option value the item represents.
data-wheel-item Identifies a wheel item element.