# TimePicker

Panel to choose a time of day: a scrolling column of hours and one of minutes (every `minuteStep`), plus an AM/PM column on a 12-hour clock. The value is always a 24-hour `HH:mm` string; the clock comes from `locale` (`en` is 12-hour, `it` 24-hour) or from `hour12`. When it appears each column scrolls to the chosen option, or to the current time when there is no value. `min` and `max` disable the options outside the range, which crosses midnight when `min` is later than `max`; a chosen minute that is not on the step stays in the list. The value is set once hours and minutes (and AM or PM) are chosen. Each column is an ARIA listbox with one Tab stop: Arrow Up and Down move in the column, Arrow Left and Right go to the other columns, Home and End go to the first and last option, Page Up and Page Down jump five options, typing digits (or A and P) goes to the matching option and Enter or Space choose it and move to the next column. It is a `role="group"` named by `aria-label` (the field gives it `role="dialog"`) with the surface of a popup; `TimePickerTextField` shows it in a menu under a field. Its state is exposed as `data-hour-cycle` and `data-disabled` on the panel, `data-column` on the columns and `data-value`, `data-selected` and `data-disabled` on the options, for app CSS.

```ts
import { TimePicker } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/dates/TimePicker.svelte`.

## Examples

### Choose a time

Hours and minutes in two columns that scroll to the chosen time when they appear. The value is a 24-hour `HH:mm` string; with the default `en` locale the clock is 12-hour and a third column chooses AM or PM.

```svelte
<script lang="ts">
	import { TimePicker } from '@likable-hair/svelte';

	let value = $state<string | undefined>('14:30');
</script>

<div class="example">
	<TimePicker bind:value aria-label="Start time" />
	<p>Value: {value ?? 'none'}</p>
</div>

<style>
	.example {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

### 12-hour and 24-hour clock

`locale` chooses the clock (`it`, `en-GB` and most of Europe are 24-hour, `en` is 12-hour) and the names of AM and PM; `hour12` forces one. The value is 24-hour in both. `locale` also names the columns for screen readers ("Ore", "Minuti"); `hoursLabel`, `minutesLabel` and `periodLabel` change them.

```svelte
<script lang="ts">
	import { TimePicker } from '@likable-hair/svelte';

	let value = $state<string | undefined>('18:45');
</script>

<div class="example">
	<figure>
		<TimePicker bind:value locale="it" aria-label="Orario" />
		<figcaption>locale="it"</figcaption>
	</figure>
	<figure>
		<TimePicker bind:value locale="it" hour12 aria-label="Orario (12 ore)" />
		<figcaption>locale="it" hour12</figcaption>
	</figure>
	<p>Value: {value ?? 'none'}</p>
</div>

<style>
	.example {
		display: flex;
		flex-wrap: wrap;
		align-items: flex-start;
		gap: 16px;
	}

	figure {
		display: grid;
		justify-items: start;
		gap: 8px;
		margin: 0;
	}

	figcaption {
		color: var(--global-color-text-3);
		font-family: var(--global-font-family-mono);
		font-size: 12px;
	}

	p {
		flex-basis: 100%;
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

### Limits and step

`min` and `max` disable the hours and minutes outside the range (when `min` is later than `max` the range crosses midnight), `minuteStep` sets the minutes offered. A chosen minute that is not on the step, like 08:10 here, stays in the list.

```svelte
<script lang="ts">
	import { TimePicker } from '@likable-hair/svelte';

	let value = $state<string | undefined>('08:10');
</script>

<div class="example">
	<TimePicker bind:value locale="en-GB" min="07:30" max="19:00" minuteStep={15} aria-label="Opening hours" />
	<p>From 07:30 to 19:00, every 15 minutes. Value: {value ?? 'none'}</p>
</div>

<style>
	.example {
		display: grid;
		justify-items: start;
		gap: 12px;
	}

	p {
		margin: 0;
		color: var(--global-color-text-2);
		font-size: 13px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (bindable) | `string` |  | The time, as a 24-hour `HH:mm` string (`HH:mm:ss` is read too). It stays `undefined` until the hours and the minutes (and AM or PM on a 12-hour clock) are chosen. |
| `locale` | `string` |  | BCP 47 locale: decides between the 12-hour and the 24-hour clock, gives the names of AM and PM and the language of the texts (`it` and `en` are built in, other languages fall back to English). Defaults to the one set with `setDefaultLocale`, else `en`. |
| `hour12` | `boolean` |  | Forces the 12-hour clock (`true`) or the 24-hour clock (`false`) instead of the one of `locale`. The value stays 24-hour. |
| `minuteStep` | `number` | `5` | Minutes between two options of the minutes column, from 1 to 60. |
| `min` | `string` |  | Earliest time that can be chosen, as `HH:mm`. |
| `max` | `string` |  | Latest time that can be chosen, as `HH:mm`. When it is earlier than `min` the range crosses midnight (`min="22:00"`, `max="06:00"`). |
| `disabled` | `boolean` | `false` | Shows the options without letting anyone choose them. |
| `hoursLabel` | `string` |  | Accessible name of the hours column. Defaults to "Hours", in the language of `locale`. |
| `minutesLabel` | `string` |  | Accessible name of the minutes column. Defaults to "Minutes", in the language of `locale`. |
| `periodLabel` | `string` |  | Accessible name of the AM/PM column. Defaults to "AM/PM", in the language of `locale`. |
| `class` | `{ container?: string; column?: string; option?: string }` | `{}` | Extra classes for each part. |
| `timePickerElement` (bindable) | `HTMLDivElement` |  | The panel element. |

It also accepts the native attributes and events of `<div>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `optionSnippet` | `{ value: number \| 'am' \| 'pm'; label: string; column: 'hours' \| 'minutes' \| 'period'; selected: boolean; disabled: boolean; }` | Replaces the text of an option. The option around it keeps click, keyboard and state. `value` is the hour as shown (0–23, or 1–12 on a 12-hour clock), the minute, or `am` / `pm`. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(event: { value: string; column: 'hours' \| 'minutes' \| 'period' }) => void` | Called when the user chooses an option and the time is whole: `value` is the time (`HH:mm`), `column` the column of the option. |

## Methods

Call them on the instance: `<TimePicker bind:this={ref} />`, then `ref.focus()`.

| Signature | Description |
| --- | --- |
| `focus()` | Moves the focus into the panel: to the chosen hour, else the hour of the current time. |

## CSS variables

For a single instance, pass them as style props: `<TimePicker --time-picker-background="tomato">`. The `--time-picker-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--time-picker-gap` | `--time-picker-default-gap`: `4px` |
| `--time-picker-width` | `--time-picker-default-width`: `auto` |
| `--time-picker-padding` | `--time-picker-default-padding`: `8px` |
| `--time-picker-border-width` | `--time-picker-default-border-width`: `var(--global-border-width)` |
| `--time-picker-border-color` | `--time-picker-default-border-color`: `var(--global-color-border-strong)` |
| `--time-picker-border-radius` | `--time-picker-default-border-radius`: `var(--global-radius-lg)` |
| `--time-picker-background` | `--time-picker-default-background`: `var(--global-color-surface-pop)` |
| `--time-picker-box-shadow` | `--time-picker-default-box-shadow`: `var(--global-shadow-lg)` |
| `--time-picker-color` | `--time-picker-default-color`: `var(--global-color-text)` |
| `--time-picker-column-gap` | `--time-picker-default-column-gap`: `2px` |
| `--time-picker-column-width` | `--time-picker-default-column-width`: `60px` |
| `--time-picker-column-height` | `--time-picker-default-column-height`: `224px` |
| `--time-picker-scrollbar-width` | `--time-picker-default-scrollbar-width`: `none` |
| `--time-picker-divider-color` | `--time-picker-default-divider-color`: `var(--global-color-border)` |
| `--time-picker-option-padding` | `--time-picker-default-option-padding`: `6px 0` |
| `--time-picker-option-border-radius` | `--time-picker-default-option-border-radius`: `var(--global-radius-sm)` |
| `--time-picker-option-background` | `--time-picker-default-option-background`: `transparent` |
| `--time-picker-option-color` | `--time-picker-default-option-color`: `var(--global-color-text-2)` |
| `--time-picker-option-font-family` | `--time-picker-default-option-font-family`: `var(--global-font-family-mono)` |
| `--time-picker-option-font-size` | `--time-picker-default-option-font-size`: `14px` |
| `--time-picker-option-font-weight` | `--time-picker-default-option-font-weight`: `500` |
| `--time-picker-option-hover-background` | `--time-picker-default-option-hover-background`: `var(--global-color-surface-2)` |
| `--time-picker-option-hover-color` | `--time-picker-default-option-hover-color`: `var(--global-color-text)` |
| `--time-picker-focus-ring-color` | `--time-picker-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--time-picker-option-selected-background` | `--time-picker-default-option-selected-background`: `var(--global-fill-primary)` |
| `--time-picker-option-selected-color` | `--time-picker-default-option-selected-color`: `var(--global-color-on-primary)` |
| `--time-picker-option-selected-box-shadow` | `--time-picker-default-option-selected-box-shadow`: `var(--global-shadow-fill)` |
| `--time-picker-option-disabled-opacity` | `--time-picker-default-option-disabled-opacity`: `0.35` |
| `--time-picker-disabled-opacity` | `--time-picker-default-disabled-opacity`: `0.5` |
