# Time Picker

> A Time Picker widget to allow user to pick a time from a drop-down list of slots or type it via keyboard

Time Picker widgets are [Input Fields](https://golemui.com/dx/widgets-reference/input-fields/overview.md) that let the user pick a time from a drop-down list of slots. The slots are generated from `minTime` to `maxTime` (inclusive) stepping `minuteStep` minutes, with their labels localized to the user's 12-hour or 24-hour convention. By default the field is read-only and values come only from the list; set `allowCustomTime` to also let the user type a time. The list closes with the Esc key.

The widget emits an ISO time string (`HH:mm:ss`) on change — pair it with the `{ format: 'time' }` validator.


```ts title="timepicker.ts"

export default [
  gui.inputs.timePicker('meetingTime', {
    label: 'Meeting Time',
  }),
];
```

_Live demo on the docs page. Form definition: [timepicker.ts](https://golemui.com/template/widgets-dx/timepicker/timepicker.ts)._

## Props

Use these `props` to customize your Time Picker widget.

| prop                      | type                  | description                                                                                                                            |
| ------------------------- | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `allowCustomTime`         | boolean               | Allows typing a time in the input; when `false` (default) values come only from the list                                               |
| `dayPeriodAriaLabel`      | string                | ARIA label for the AM/PM segment input. Defaults to `AM/PM`                                                                            |
| `disabledRangeMessage`    | string \| Localizable | Error message shown when a typed time falls inside a disabled range                                                                    |
| `disabledRanges`          | array                 | `{ start, end }` ISO time ranges (both ends inclusive) whose slots render disabled                                                     |
| `height`                  | number                | Total height of the drop-down list viewport. Defaults to `300`                                                                         |
| `hint`                    | string                | A description to display below the input                                                                                               |
| `hourAriaLabel`           | string                | ARIA label for the hour segment input. Defaults to `Hour`                                                                              |
| `hourFormat`              | `'12' \| '24'`        | Forces 12-hour or 24-hour slot labels and typing mode; when omitted the locale decides                                                 |
| `icon`                    | string                | A css class to display an icon inside the input                                                                                        |
| `incompleteMessage`       | string \| Localizable | Error message shown when focus leaves the widget with a partially filled time behind; the value goes to `null` so validators report it |
| `itemHeight`              | number                | Fixed height of each slot row. Defaults to `40`                                                                                        |
| `maxTime`                 | string                | Last selectable slot (ISO time, inclusive). Defaults to `23:59:59`                                                                     |
| `maxTimeMessage`          | string \| Localizable | Error message shown when a typed time is after `maxTime`                                                                               |
| `minTime`                 | string                | First selectable slot (ISO time, inclusive). Defaults to `00:00:00`                                                                    |
| `minTimeMessage`          | string \| Localizable | Error message shown when a typed time is before `minTime`                                                                              |
| `minuteAriaLabel`         | string                | ARIA label for the minute segment input. Defaults to `Minute`                                                                          |
| `minuteStep`              | number                | Minutes between generated slots, also the arrow-key minute increment when typing. Defaults to `30`                                     |
| `noAvailableTimesMessage` | string \| Localizable | Shown in the list when the bounds yield no slots. Defaults to `No available times`                                                     |
| `toggleAriaLabel`         | string                | ARIA label for the popup toggle button. Defaults to `Show time list`                                                                   |

### Hint

<Aside>A `label` is required for the `hint` to be displayed.</Aside>

Use the property `hint` to add a description or validation instructions.


```ts title="timepicker.ts"

export default [
  gui.inputs.timePicker('meetingTime', {
    hint: 'Please pick the meeting start time',
    label: 'Meeting Time',
  }),
];
```

_Live demo on the docs page. Form definition: [timepicker-hint.ts](https://golemui.com/template/widgets-dx/timepicker/timepicker-hint.ts)._

### Icon

Use the property `icon` to add an icon, which appears next to the toggle chevron. The value of `icon` represents a set of CSS classes separated by spaces.


```ts title="timepicker.ts"

export default [
  gui.inputs.timePicker('meetingTime', {
    icon: 'schedule',
    label: 'Meeting Time',
  }),
];
```

_Live demo on the docs page. Form definition: [timepicker-icon.ts](https://golemui.com/template/widgets-dx/timepicker/timepicker-icon.ts)._

### Hour Format

By default the 12-hour/24-hour convention comes from the locale's hour cycle and drives both the slot labels (e.g. `9:30 AM` vs `09:30`) and the typing mode. Use the property `hourFormat` (`'12'` or `'24'`) to force one of them.


```ts title="timepicker.ts"

export default [
  gui.inputs.timePicker('meetingTime12', {
    hourFormat: '12',
    label: 'Meeting Time (12h)',
  }),
  gui.inputs.timePicker('meetingTime24', {
    hourFormat: '24',
    label: 'Meeting Time (24h)',
  }),
];
```

_Live demo on the docs page. Form definition: [timepicker-hourformat.ts](https://golemui.com/template/widgets-dx/timepicker/timepicker-hourformat.ts)._

### Time Slots

Use `minTime`, `maxTime` and `minuteStep` to control which slots are generated, and `disabledRanges` to grey out slots inside `{ start, end }` ranges (both ends inclusive). Disabled slots are skipped by keyboard navigation and cannot be selected.


```ts title="timepicker.ts"

export default [
  gui.inputs.timePicker('meetingTime', {
    hint: '30m slots from 9:00 to 18:00, lunch break disabled',
    minTime: '09:00:00',
    maxTime: '18:00:00',
    minuteStep: 30,
    disabledRanges: [
      {
        start: '13:00:00',
        end: '14:00:00',
      },
    ],
    label: 'Pick a time slot',
  }),
];
```

_Live demo on the docs page. Form definition: [timepicker-slots.ts](https://golemui.com/template/widgets-dx/timepicker/timepicker-slots.ts)._

### List Size

Use `height` to set the drop-down list viewport height (default `300`) and `itemHeight` to set the height of each slot row (default `40`). Both are in pixels; the list scrolls when the slots exceed the viewport.


```ts title="timepicker.ts"

export default [
  gui.inputs.timePicker('meetingTime', {
    hint: 'A shorter 200px viewport with taller 56px rows',
    minTime: '09:00:00',
    maxTime: '18:00:00',
    minuteStep: 30,
    height: 200,
    itemHeight: 56,
    label: 'Meeting Time',
  }),
];
```

_Live demo on the docs page. Form definition: [timepicker-size.ts](https://golemui.com/template/widgets-dx/timepicker/timepicker-size.ts)._

### Custom Time

Use the property `allowCustomTime` to let the user type any time directly in the input, in addition to picking a slot. Typed times are still validated against `minTime`, `maxTime` and `disabledRanges`: a violation raises an input error whose message you can customize (and localize) with `minTimeMessage`, `maxTimeMessage` and `disabledRangeMessage`.


```ts title="timepicker.ts"

export default [
  gui.inputs.timePicker('meetingTime', {
    hint: 'Type any time between 9:00 and 18:00 or pick a slot',
    allowCustomTime: true,
    minTime: '09:00:00',
    maxTime: '18:00:00',
    disabledRanges: [
      {
        start: '13:00:00',
        end: '14:00:00',
      },
    ],
    minTimeMessage: 'Office not open until 9:00',
    maxTimeMessage: 'Office closed from 18:00',
    disabledRangeMessage: 'Lunch break is not available',
    label: 'Meeting Time',
  }),
];
```

_Live demo on the docs page. Form definition: [timepicker-customtime.ts](https://golemui.com/template/widgets-dx/timepicker/timepicker-customtime.ts)._

### No Available Times

When `minTime` and `maxTime` generate no slots, the drop-down list shows an empty-state message. Use `noAvailableTimesMessage` to customize (and localize) it (it defaults to `No available times`).


```ts title="timepicker.ts"

export default [
  gui.inputs.timePicker('meetingTime', {
    hint: 'These bounds generate no slots, so the list shows the empty-state message',
    minTime: '18:00:00',
    maxTime: '09:00:00',
    noAvailableTimesMessage: 'No slots available',
    label: 'Meeting Time',
  }),
];
```

_Live demo on the docs page. Form definition: [timepicker-notimes.ts](https://golemui.com/template/widgets-dx/timepicker/timepicker-notimes.ts)._

## Styling

Time Pickers can be styled as explained in the [Styling Guide](https://golemui.com/dx/styling/overview.md). The typed field and the slot list each have their own styling variables.

### CSS Variables

Following you will find a list with the CSS Variables and a quick description of what you will style.

| CSS Variable                 | Description                                                     |
| ---------------------------- | --------------------------------------------------------------- |
| `--gui-text-default`         | Input and slot text color, active piece (hour/minute) underline |
| `--gui-intent-error`         | Input border color when invalid                                 |
| `--gui-border-focus`         | Input border and focused slot outline color                     |
| `--gui-bg-default`           | Background color for the drop-down list container               |
| `--gui-border-default`       | Border color for the drop-down list container                   |
| `--gui-radius-md`            | Border radius for the drop-down list container                  |
| `--gui-intent-primary`       | Background color for the selected slot                          |
| `--gui-intent-primary-hover` | Background color for slots on hover                             |
| `--gui-intent-primary-text`  | Text color for the selected and hovered slots                   |
| `--gui-bg-disabled`          | Background color for disabled slots                             |
| `--gui-text-muted`           | Text color of the "no available times" message                  |

### Anatomy

This is the anatomy of the Time Picker Widget in case you want to use your CSS styles.


```html title="timepicker-anatomy.html"
<gui-time-picker-input class="gui-time-picker gui-field">
  <gui-time-picker class="gui-field">
    <span class="gui-label" id="fieldUid_label">
      <span class="gui-label__text">Meeting Time</span>
    </span>
    <div class="gui-widget">
      <gui-time id="fieldUid_time" class="gui-field" readonly>
        <div class="gui-widget">
          <div
            role="group"
            id="fieldUid"
            class="gui-widget-input gui-parts gui-parts-ring gui-time-input"
            aria-labelledby="fieldUid_label"
          >
            <div dir="ltr" class="gui-parts__time-cluster gui-time-input__time-cluster">
              <div class="gui-parts__touch-target gui-time-input__touch-target">
                <input
                  type="text"
                  inputmode="numeric"
                  role="spinbutton"
                  autocomplete="off"
                  class="gui-parts__part gui-time-input__part"
                  data-type="hour"
                  maxlength="2"
                  placeholder="hh"
                  tabindex="0"
                  aria-label="Hour"
                  aria-valuemin="1"
                  aria-valuemax="12"
                  readonly
                  aria-readonly="true"
                />
                <div class="gui-parts__visual-underline gui-time-input__visual-underline"></div>
              </div>
              <span class="gui-time-input__separator">:</span>
              <div class="gui-parts__touch-target gui-time-input__touch-target">
                <input
                  type="text"
                  inputmode="numeric"
                  role="spinbutton"
                  autocomplete="off"
                  class="gui-parts__part gui-time-input__part"
                  data-type="minute"
                  maxlength="2"
                  placeholder="mm"
                  tabindex="-1"
                  aria-label="Minute"
                  aria-valuemin="0"
                  aria-valuemax="59"
                  readonly
                  aria-readonly="true"
                />
                <div class="gui-parts__visual-underline gui-time-input__visual-underline"></div>
              </div>
            </div>
            <span class="gui-time-input__separator"></span>
            <div class="gui-parts__touch-target gui-time-input__touch-target">
              <button
                type="button"
                class="gui-parts__part gui-parts__dayperiod gui-time-input__part gui-time-input__dayperiod"
                data-type="dayPeriod"
                tabindex="-1"
                aria-label="AM/PM"
              >
                AM
              </button>
              <div class="gui-parts__visual-underline gui-time-input__visual-underline"></div>
            </div>
          </div>
        </div>
      </gui-time>
      <button
        type="button"
        class="gui-time-picker__arrow"
        aria-haspopup="dialog"
        aria-label="Show time list"
        aria-expanded="true"
        aria-controls="fieldUid_popup"
      >
        <span class="gui-caret" aria-hidden="true"></span>
      </button>
      <div class="gui-picker__panel" popover="manual">
        <gui-time-list role="dialog" id="fieldUid_popup" aria-labelledby="fieldUid_label">
          <div
            class="gui-time-list__viewport"
            role="listbox"
            aria-label="Meeting Time"
            style="
              --gui-time-list-height: 300px;
              --gui-time-list-item-height: 40px;
              --gui-time-list-columns: 1;
            "
          >
            <button
              type="button"
              role="option"
              class="gui-time-list__option"
              tabindex="0"
              data-value="00:00:00"
              aria-selected="false"
              aria-disabled="false"
            >
              12:00 AM
            </button>
            <button
              type="button"
              role="option"
              class="gui-time-list__option"
              tabindex="-1"
              data-value="00:30:00"
              aria-selected="false"
              aria-disabled="false"
            >
              12:30 AM
            </button>
            <!-- More options, one per time slot -->
          </div>
        </gui-time-list>
      </div>
    </div>
    <div class="gui-validator gui-validator--empty" id="fieldUid_errors" role="alert"></div>
  </gui-time-picker>
</gui-time-picker-input>
```
