# TimePickerTextField

Time field: type the time or choose it in a panel of hours and minutes. The value is `value`, a 24-hour `HH:mm` string, also on a 12-hour clock. The clock comes from `locale` (`en` is 12-hour with AM/PM, `it` 24-hour) or from `hour12`, and the format is shown as a chip after the text, which hides itself when the field is too narrow for both. Typing accepts digits only and adds the colon; on a 12-hour clock A and P set AM and PM, and a whole 24-hour time (`14:30`) turns into `02:30 PM`. A time that does not exist or is outside `min` and `max` (and an unfinished one when the field loses focus) turns the field to the error state with `invalidText`, and the form refuses to submit it. On desktop, focusing or clicking the field opens a `TimePicker` in a menu; Arrow Down moves the focus into it, Escape closes it and Tab leaves it. On screens up to 1024px wide the clock button opens it in a bottom drawer instead, and the field stays typable (`mobileDrawer`). Choosing the minutes (or AM/PM on a 12-hour clock) closes the panel (`closeOnSelect`). With `name` the value is submitted as `HH:mm`. Its state is exposed as `data-state`, `data-disabled`, `data-readonly`, `data-open` and `data-hour-cycle` for app CSS.

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

Category: composed. Source: `src/lib/components/composed/forms/TimePickerTextField.svelte`.

## Examples

### Type or choose

Type the digits (the colon comes by itself) or open the panel: focus the field, then Arrow Down moves into the hours, Arrow Right to the minutes and Escape closes it. Choosing the minutes closes it. On a phone the clock button opens a bottom drawer instead.

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

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

<div class="example">
	<TimePickerTextField label="Appointment time" locale="en-GB" bind:value />
	<p>Value: {value ?? 'none'}</p>
</div>

<style>
	.example {
		display: grid;
		gap: 12px;
		width: 340px;
	}

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

### 12-hour clock and locale

With the default `en` locale the clock is 12-hour: type `930p` (A and P set AM and PM) or a 24-hour time like `1430`, which becomes `02:30 PM`. The value stays `HH:mm`. `locale="it"` gives the 24-hour clock and the Italian texts, `hour12` forces either; text props such as `invalidText` win over the texts of `locale`.

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

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

<div class="example">
	<TimePickerTextField label="Pickup time" bind:value />
	<TimePickerTextField
		label="Orario di ritiro"
		locale="it"
		invalidText="Inserisci un orario valido"
		bind:value
	/>
	<p>Value: {value ?? 'none'}</p>
</div>

<style>
	.example {
		display: grid;
		gap: 14px;
		width: 340px;
	}

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

### Limits and step

`min` and `max` limit the panel and the typed time: a time outside them, or one that does not exist, shows `invalidText` and the form refuses it. `minuteStep` sets the minutes of the panel; any minute can still be typed.

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

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

<div class="example">
	<TimePickerTextField
		label="Delivery slot"
		locale="en-GB"
		min="08:00"
		max="20:00"
		minuteStep={15}
		hint="Between 08:00 and 20:00"
		bind:value
	/>
	<TimePickerTextField
		label="Night shift start"
		locale="en-GB"
		min="22:00"
		max="06:00"
		minuteStep={30}
		value="23:00"
		hint="From 22:00 to 06:00, across midnight"
	/>
</div>

<style>
	.example {
		display: grid;
		gap: 14px;
		width: 340px;
	}
</style>
```

### States and forms

With `name` the time is submitted as `HH:mm` (empty without a time), `required` reaches the input and `clearable` adds a button that empties it. `state` and `hint` show a validation message; `disabled` and `readonly` lock the field.

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

	let submitted = $state('');

	function submit(event: SubmitEvent & { currentTarget: HTMLFormElement }) {
		event.preventDefault();
		submitted = new URLSearchParams(new FormData(event.currentTarget) as never).toString();
	}
</script>

<form onsubmit={submit}>
	<TimePickerTextField label="Opening" name="opening" locale="en-GB" value="08:30" required clearable />
	<TimePickerTextField label="Closing" name="closing" locale="en-GB" state="error" hint="Closing must be after opening" />
	<TimePickerTextField label="Break" locale="en-GB" value="13:00" state="success" hint="Saved" />
	<TimePickerTextField label="Check-in" locale="en-GB" value="15:00" disabled />
	<TimePickerTextField label="Check-out" locale="en-GB" value="11:00" readonly />
	<Button type="submit">Save hours</Button>
	<p>{submitted ? `Submitted: ${submitted}` : 'Nothing submitted yet'}</p>
</form>

<style>
	form {
		display: grid;
		justify-items: start;
		gap: 14px;
		width: 340px;
	}

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

### Mobile drawer

On screens up to 1024px wide (try it in a narrow window) focusing the field does not open anything: the field stays typable with the keyboard, and the clock button opens the panel in a bottom drawer titled `drawerTitle`. `mobileDrawer={false}` keeps the menu, `closeOnSelect={false}` keeps the panel open after a choice.

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

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

<div class="example">
	<TimePickerTextField label="Dinner" locale="en-GB" drawerTitle="Dinner time" bind:value />
	<TimePickerTextField label="Reminder" locale="en-GB" closeOnSelect={false} mobileDrawer={false} value="07:45" />
</div>

<style>
	.example {
		display: grid;
		gap: 14px;
		width: 340px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (bindable) | `string` |  | The time, as a 24-hour `HH:mm` string (`HH:mm:ss` is read too). `undefined` while the field is empty or its text is not a valid time. |
| `open` (bindable) | `boolean` |  | Whether the panel is open. `undefined` counts as closed. |
| `locale` | `string` |  | BCP 47 locale: decides between the 12-hour clock (`en`) and the 24-hour clock (`it`), 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. Typed minutes can be any. |
| `min` | `string` |  | Earliest time that can be chosen or typed, as `HH:mm`. |
| `max` | `string` |  | Latest time that can be chosen or typed, as `HH:mm`. When it is earlier than `min` the range crosses midnight. |
| `label` | `string` |  | Visible label above the field, linked to the input. In the mobile drawer it is the title. |
| `hint` | `string` |  | Text below the field. With `state` it becomes the error or success message. |
| `state` | `'error' \| 'success'` |  | Validation state: colors the border and the hint and shows an icon. `error` also sets `aria-invalid`. Without it the field still shows the error state for an invalid time. |
| `invalidText` | `string` |  | Message shown below the field, and given to the form, when the typed time is not valid. Defaults to "Enter a valid time", in the language of `locale`. |
| `placeholder` | `string` |  | Placeholder of the input. Without it the format chip tells what to type. |
| `disabled` | `boolean` | `false` | Native `disabled`: the field and the panel cannot be used. |
| `readonly` | `boolean` | `false` | Native `readonly`: the time is shown and can be selected, but not changed. |
| `required` | `boolean` | `false` | Native `required` on the input. |
| `id` | `string` |  | `id` of the input. Generated when missing, so the label always points to it. |
| `name` | `string` |  | Name of a hidden input that submits the time as `HH:mm` (empty when there is no time). |
| `clearable` | `boolean` | `false` | Shows a button that empties the field. |
| `clearLabel` | `string` |  | Accessible name of the clear button. Defaults to "Clear", in the language of `locale`. |
| `closeOnSelect` | `boolean` | `true` | Closes the panel when the minutes are chosen (on a 12-hour clock, the minutes or AM/PM) and the time is whole. |
| `placement` | `'bottom-start' \| 'bottom' \| 'bottom-end' \| 'top-start' \| 'top' \| 'top-end' \| 'left-start' \| 'left' \| 'left-end' \| 'right-start' \| 'right' \| 'right-end'` | `'bottom-start'` | Side of the field where the panel opens. It flips when there is not enough space. |
| `mobileDrawer` | `boolean` | `true` | On screens up to 1024px wide, opens the panel in a bottom drawer from the clock button instead of a menu, and focusing the field does not open it. |
| `drawerTitle` | `string` |  | Title of the mobile drawer. Defaults to `label`. |
| `closeLabel` | `string` |  | Accessible name of the close button of the mobile drawer. Defaults to "Close", in the language of `locale`. |
| `openLabel` | `string` |  | Accessible name of the clock button, and of the panel when there is no `label`. Defaults to "Choose time", in the language of `locale`. |
| `showFormat` | `boolean` | `true` | Shows the format as a chip after the text, also read by screen readers. The chip hides itself while the field is too narrow for the whole text and the chip. |
| `formatLabel` | `string` |  | Text of the format chip. Defaults to `hh:mm`, or `hh:mm am` on a 12-hour clock. |
| `hoursLabel` | `string` |  | Accessible name of the hours column of the panel. Defaults to "Hours", in the language of `locale`. |
| `minutesLabel` | `string` |  | Accessible name of the minutes column of the panel. Defaults to "Minutes", in the language of `locale`. |
| `periodLabel` | `string` |  | Accessible name of the AM/PM column of the panel. Defaults to "AM/PM", in the language of `locale`. |
| `input` (bindable) | `HTMLInputElement` |  | The input element. |
| `class` | `{ container?: string; label?: string; row?: string; field?: string; input?: string; hint?: string; picker?: string; }` | `{}` | Extra classes for each part. `picker` goes on the `TimePicker`. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the label content. It stays linked to the input. |
| `hintSnippet` | `{ hint: string \| undefined }` | Replaces the hint below the field. |
| `stateIconSnippet` | `{ state: 'error' \| 'success' }` | Replaces the icon shown for the validation state. Render nothing to hide it. |
| `iconSnippet` |  | Replaces the clock icon. The button around it keeps opening the panel. |
| `prependSnippet` |  | Content outside the field, on the left. |
| `appendInnerSnippet` |  | Content inside the field, after the format chip and before the clear button. |
| `appendSnippet` |  | Content outside the field, on the right. |
| `clearSnippet` |  | Replaces the icon of the clear button. |
| `formatSnippet` | `{ format: string }` | Replaces the content of the format chip. |
| `optionSnippet` | `{ value: number \| 'am' \| 'pm'; label: string; column: 'hours' \| 'minutes' \| 'period'; selected: boolean; disabled: boolean; }` | Replaces the text of an option in the panel (see `TimePicker`). |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(event: { value: string \| undefined }) => void` | Called when the user changes the time: in the panel, by typing a whole valid time (or making it invalid again) and with the clear button. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--time-picker-text-field-border-color` | `--time-picker-text-field-default-border-color`: `var(--global-input-border-color)` |
| `--time-picker-text-field-hover-border-color` | `--time-picker-text-field-default-hover-border-color`: `var(--global-input-hover-border-color)` |
| `--time-picker-text-field-focus-border-color` | `--time-picker-text-field-default-focus-border-color`: `var(--global-input-focus-border-color)` |
| `--time-picker-text-field-focus-ring-color` | `--time-picker-text-field-default-focus-ring-color`: `var(--global-input-focus-ring-color)` |
| `--time-picker-text-field-hint-color` | `--time-picker-text-field-default-hint-color`: `var(--global-input-hint-color)` |
| `--time-picker-text-field-icon-size` | `--time-picker-text-field-default-icon-size`: `18px` |
| `--time-picker-text-field-gap` | `--time-picker-text-field-default-gap`: `6px` |
| `--time-picker-text-field-width` | `--time-picker-text-field-default-width`: `100%` |
| `--time-picker-text-field-max-width` | `--time-picker-text-field-default-max-width`: `none` |
| `--time-picker-text-field-error-color` | `--time-picker-text-field-default-error-color`: `var(--global-input-error-color)` |
| `--time-picker-text-field-success-color` | `--time-picker-text-field-default-success-color`: `var(--global-input-success-color)` |
| `--time-picker-text-field-label-color` | `--time-picker-text-field-default-label-color`: `var(--global-input-label-color)` |
| `--time-picker-text-field-label-font-size` | `--time-picker-text-field-default-label-font-size`: `var(--global-input-label-font-size)` |
| `--time-picker-text-field-label-font-weight` | `--time-picker-text-field-default-label-font-weight`: `var(--global-input-label-font-weight)` |
| `--time-picker-text-field-outer-gap` | `--time-picker-text-field-default-outer-gap`: `8px` |
| `--time-picker-text-field-icon-color` | `--time-picker-text-field-default-icon-color`: `var(--global-input-icon-color)` |
| `--time-picker-text-field-inner-gap` | `--time-picker-text-field-default-inner-gap`: `var(--global-input-gap)` |
| `--time-picker-text-field-height` | `--time-picker-text-field-default-height`: `var(--global-input-height)` |
| `--time-picker-text-field-padding` | `--time-picker-text-field-default-padding`: `0 var(--global-input-padding-x)` |
| `--time-picker-text-field-background` | `--time-picker-text-field-default-background`: `var(--global-input-background)` |
| `--time-picker-text-field-border-width` | `--time-picker-text-field-default-border-width`: `var(--global-input-border-width)` |
| `--time-picker-text-field-border-radius` | `--time-picker-text-field-default-border-radius`: `var(--global-input-radius)` |
| `--time-picker-text-field-box-shadow` | `--time-picker-text-field-default-box-shadow`: `var(--global-input-shadow)` |
| `--time-picker-text-field-focus-ring-width` | `--time-picker-text-field-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--time-picker-text-field-color` | `--time-picker-text-field-default-color`: `var(--global-input-color)` |
| `--time-picker-text-field-font-family` | `--time-picker-text-field-default-font-family`: `var(--global-font-family-mono)` |
| `--time-picker-text-field-font-size` | `--time-picker-text-field-default-font-size`: `var(--global-input-font-size)` |
| `--time-picker-text-field-font-weight` | `--time-picker-text-field-default-font-weight`: `400` |
| `--time-picker-text-field-placeholder-color` | `--time-picker-text-field-default-placeholder-color`: `var(--global-input-placeholder-color)` |
| `--time-picker-text-field-format-padding` | `--time-picker-text-field-default-format-padding`: `2px 6px` |
| `--time-picker-text-field-format-border-color` | `--time-picker-text-field-default-format-border-color`: `var(--global-color-border)` |
| `--time-picker-text-field-format-border-radius` | `--time-picker-text-field-default-format-border-radius`: `var(--global-radius-xs)` |
| `--time-picker-text-field-format-background` | `--time-picker-text-field-default-format-background`: `var(--global-color-surface-2)` |
| `--time-picker-text-field-format-color` | `--time-picker-text-field-default-format-color`: `var(--global-color-text-3)` |
| `--time-picker-text-field-format-font-size` | `--time-picker-text-field-default-format-font-size`: `11px` |
| `--time-picker-text-field-hint-font-size` | `--time-picker-text-field-default-hint-font-size`: `var(--global-input-hint-font-size)` |
| `--time-picker-text-field-disabled-background` | `--time-picker-text-field-default-disabled-background`: `var(--global-input-disabled-background)` |
| `--time-picker-text-field-disabled-opacity` | `--time-picker-text-field-default-disabled-opacity`: `var(--global-input-disabled-opacity)` |
