# TimePicker

A field-looking trigger that opens a list of hours and minutes, for picking a time of day in a form. The value is a 24-hour HH:mm string. A date stays on DatePicker, and a date with a time is a DatePicker beside a TimePicker.

**Dependencies:** npm: 1 direct, 3 total; registry: 4 direct, 4 total.

Native: iOS stable, Android stable.

## Usage

### When to use

- Picking a time of day in a form, with the same field look as DatePicker.
- Limiting the minutes to a step, such as every 15 minutes.

### When not to use

- Picking a date or a range of dates. Use DatePicker.
- Typing a time the user already knows. Use Input.

### Native

The trigger has the field look, and the time wheel opens in a bottom Sheet with a Done button.

**Check-in time**

```text
WerewolfTimePicker(sheetTitle: "Check-in time", value: time, onValueChange: { time = $0 })
```

```text
WerewolfTimePicker(sheetTitle = "Check-in time", value = time, onValueChange = { time = it })
```

**Quarter hours**

```text
WerewolfTimePicker(sheetTitle: "Check-in time", value: time, onValueChange: { time = $0 }, minuteStep: .fifteen)
```

```text
WerewolfTimePicker(sheetTitle = "Check-in time", value = time, onValueChange = { time = it }, minuteStep = WerewolfTimePickerMinuteStep.Fifteen)
```

## Variants

### minuteStep

Minutes between choices. 5 is every five minutes. 1 lists every minute.

Values: `1`, `5`, `15`, `30`. Default: `5`.

### size

Trigger height from size-element-*, the same steps as Input and Select: sm 28px, md 32px (the default), lg 36px, xl 44px with a mouse; on touch md is 36px, lg 44px and xl 48px. Match the Input or Button next to it.

Values: `sm`, `md`, `lg`, `xl`. Default: `md`.

## Examples

### Check-in time

The trigger has the field look, and the hours and minutes open in a popover.

```tsx
<Field className="w-full max-w-sm">
  <FieldLabel>Check-in time</FieldLabel>
  <FieldControl>
    <TimePicker />
  </FieldControl>
  <FieldDescription>Local time at the property.</FieldDescription>
</Field>
```

### Quarter hours

```tsx
<TimePicker minuteStep={15} defaultValue="09:30" />
```

## Do and don't

### Do

- Use TimePicker for a time of day in a form, next to other fields.
- Name it with a Label pointing at its id.
- Put a date and a time in two Fields beside each other: DatePicker, then TimePicker.

### Don't

- Don't use TimePicker for a date or a range of dates. Use DatePicker.
- Don't use a native time input when the rest of the form uses DatePicker. The trigger should match.
- Don't put the hour and the minute in two Fields. One TimePicker is the time.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `string` |  |  | The picked time as HH:mm when you control it. An explicit undefined clears it. Pair it with onValueChange. |
| `defaultValue` | `string` |  |  | The time on first render when you do not control it, as HH:mm. Not generated for iOS and Android. |
| `onValueChange` | `(value: string) => void` |  |  | Called with HH:mm once both an hour and a minute are picked. |
| `minuteStep` | `enum` | `1`, `5`, `15`, `30` | `5` | Minutes between choices. 5 is every five minutes. 1 lists every minute. |
| `placeholder` | `string` |  | `Pick a time` | Text shown in the trigger while no time is picked. It also names the dialog when there is no aria-label. |
| `disabled` | `boolean` |  | `false` | Disables the trigger. |
| `invalid` | `boolean` |  | `false` | Marks the value as wrong: sets aria-invalid, which draws the danger border. |
| `size` | `enum` | `sm`, `md`, `lg`, `xl` | `md` | Trigger height from size-element-*, the same steps as Input and Select: sm 28px, md 32px (the default), lg 36px, xl 44px with a mouse; on touch md is 36px, lg 44px and xl 48px. Match the Input or Button next to it. |
| `modal` | `boolean` |  | `false` | Whether the open popover blocks interaction with the rest of the page. Not generated for iOS and Android. |
| `hourLabel` | `string` |  | `Hours` | Name of the hour list. |
| `minuteLabel` | `string` |  | `Minutes` | Name of the minute list. |
| `name` | `string` |  |  | Form name. The submitted value is HH:mm, on a hidden input. Only on web. Not generated for iOS and Android. |
| `sheetTitle` (required) | `string` |  |  | The name of the Sheet that holds the time wheel. Shown as its title and read by assistive technology. Native only. Only on iOS, Android. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The trigger button: the same field look as DatePicker and Input, in the size you set. |
| `icon` | A decorative Tabler clock icon at the start, 16px (20px in xl), secondary. |
| `label` | The picked time as HH:mm, or the placeholder in secondary text. |
| `hitArea` | Transparent ::before centered on the trigger, never smaller than size-target-min (24px on mouse, 44px on touch), so sm, md and lg stay easy to tap. |
| `content` | The Popover: a column of hours and a column of minutes. |

## Accessibility

Role: `button`

| Keys | Action |
| --- | --- |
| `Space, Enter` | On the trigger, opens the popover. On an hour or a minute, picks it. |
| `ArrowUp / ArrowDown` | Move through the hours or the minutes. Home and End jump to the first and last. |
| `Escape` | Closes the popover and returns focus to the trigger. A time is sent only when both an hour and a minute are picked. |

- The trigger opens a dialog named by aria-label or the placeholder. Hours and minutes are two listboxes.
- Picking a minute, once an hour is picked, sends the time and closes. Picking an hour first leaves the popover open.
- The trigger accepts id, aria-label and aria-describedby, so a Label and a hint can name and describe it like any field.
- name submits HH:mm on a hidden input. The trigger is a button, so it does not submit the draft.

## Install

```bash
npx werewolf-ui add time-picker
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.

```bash
npx werewolf-ui add time-picker --platform ios
npx werewolf-ui add time-picker --platform android
```

For iOS and Android, run `npx werewolf-ui init --platform ios` or `npx werewolf-ui init --platform android` once, then add with `--platform`. See [Installation](https://www.werewolf-ui.dev/docs/installation.md).
