# Time Picker

> A popover with hour and minute columns that picks a wall time.

```tsx title="hero.tsx"
import {
  TimePicker,
  TimePickerClear,
  TimePickerContent,
  TimePickerLabel,
  TimePickerTrigger,
} from '@langgenius/dify-ui/time-picker'

export function TimePickerHeroDemo() {
  return (
    <TimePicker defaultValue="09:00">
      <div className="flex w-full max-w-52 flex-col gap-1">
        <TimePickerLabel>Daily digest time</TimePickerLabel>
        <div className="flex items-center gap-1">
          <TimePickerTrigger className="w-auto min-w-0 flex-1" />
          <div className="size-8 shrink-0">
            <TimePickerClear aria-label="Clear daily digest time" />
          </div>
        </div>
      </div>
      <TimePickerContent />
    </TimePicker>
  )
}
```

## Usage guidelines

- Use `TimePicker` for a wall time stored as 24-hour `HH:mm`, such as a daily schedule. Use `DatePicker` for a civil date `YYYY-MM-DD` and `DateTimePicker` for an instant (`Date`) shown in an IANA time zone. The pickers do not accept typed text.
- `timeZone` only sets Now and the initial empty draft; it never converts the stored wall time. Parse API payloads and convert business values outside the picker.
- **Name the field at the call site**: prefer one visible picker Label. It names both the trigger and the popup through `aria-labelledby`, and clicking its text focuses the trigger without opening it. A Label with `className="sr-only"` names a compact field without visible text. `FieldLabel nativeLabel={false} render={<div />}` also works. Do not point a native `label[for]` at the trigger: activating it clicks the button.
- Without a Label, name the trigger and the content with `aria-label` or `aria-labelledby`. The trigger's name appends its visible value (or placeholder) to label text; an explicit `aria-label` replaces the whole name, so include the visible value in it. Put meaningful annotations inside the Value part so they join the name.
- **Name Clear explicitly**: Clear follows the `IconButton` naming contract, so give it an `aria-label` such as "Clear start date". Place it next to the trigger, never inside the trigger button.
- Use `defaultValue`, or `value` with `onValueChange`, and keep that choice for the field's lifetime. `null` means empty, and Clear emits `null`. `details.reason` is `item-press`, `clear-press` or `reset`; `details.cancel()` rejects that change and its follow-up close. Unchanged values do not emit again.
- Edits in the popup, including Now, stay in a draft until OK. Close, `Escape` and an outside press discard the draft, and reopening starts from the committed value.
- `hourCycle` switches between 12-hour (default) and 24-hour display; the stored value stays 24-hour. `minuteStep` (1, 5, 10, 15, 20 or 30) limits the minute column, and Now rounds down to that step. A value off the step counts as unavailable.
- `isTimeUnavailable` checks the complete time. Unavailable drafts stay reachable while you move through the columns, but OK is disabled and the reason is announced.
- Content keeps its natural height and flips above or below the trigger; it does not shrink on small screens. Pass `maxHeight` only to opt into scrolling the body while the actions stay visible.
- `locale` takes an Intl locale identifier and changes formatting and digits, not the stored value. Translate action labels through `labels`, plus placeholders, `requiredLabel` and `validationMessage`. Direction comes from the nearest `DirectionProvider`; for a local RTL region, also set `dir` on the wrapper and on the content.
- With `name`, the committed value joins native `FormData`, and an empty field submits `''`. Inside a `Field`, `Field.name` takes precedence, `validate` receives the domain value, and `Form.onFormSubmit` receives the same strings as `FormData`. Required and invalid fields block submission and focus the first invalid trigger. Reset restores `defaultValue`. Disabled fields are left out; read-only fields open for browsing and still submit.

## Anatomy

```tsx
import {
  TimePicker,
  TimePickerClear,
  TimePickerContent,
  TimePickerLabel,
  TimePickerTrigger,
  TimePickerValue,
} from '@langgenius/dify-ui/time-picker'
```

```tsx
<TimePicker>
  <TimePickerLabel />
  <TimePickerTrigger>
    <TimePickerValue />
  </TimePickerTrigger>
  <TimePickerClear />
  <TimePickerContent />
</TimePicker>
```

`TimePickerTrigger` is a native button whose default children are the Value and a decorative clock icon. When you replace them, include exactly one `TimePickerValue` and keep the trailing clock icon; its render callback receives the formatted text, not the raw value. `TimePickerContent` owns the portal, initial focus, columns and actions.

## Examples

### Minute step and 24-hour display

`hourCycle={24}` drops the period column, and `minuteStep={15}` offers quarter hours. The time-zone suffix lives inside `TimePickerValue`, so it is part of the trigger's name.

```tsx title="step-24-hour.tsx"
import {
  TimePicker,
  TimePickerContent,
  TimePickerLabel,
  TimePickerTrigger,
  TimePickerValue,
} from '@langgenius/dify-ui/time-picker'
import * as React from 'react'

export function TimePickerStep24HourDemo() {
  return (
    <TimePicker defaultValue="02:30" hourCycle={24} minuteStep={15} timeZone="UTC">
      <div className="flex w-full max-w-48 flex-col gap-1">
        <TimePickerLabel>Knowledge sync time</TimePickerLabel>
        <TimePickerTrigger className="w-full">
          <TimePickerValue className="min-w-0 truncate">
            {(value) => (
              <React.Fragment>
                {value} <span className="text-text-tertiary">UTC</span>
              </React.Fragment>
            )}
          </TimePickerValue>
          <span aria-hidden="true" className="i-ri-time-line size-4 shrink-0 text-text-tertiary" />
        </TimePickerTrigger>
      </div>
      <TimePickerContent />
    </TimePicker>
  )
}
```

### Unavailable times

`isTimeUnavailable` blocks business hours here. You can still move through them, but OK stays disabled until the draft is available.

```tsx title="constraints.tsx"
import {
  TimePicker,
  TimePickerContent,
  TimePickerLabel,
  TimePickerTrigger,
} from '@langgenius/dify-ui/time-picker'
import * as React from 'react'

function isBusinessHours(time: string) {
  return time >= '09:00' && time < '18:00'
}

export function TimePickerConstraintsDemo() {
  const hintId = React.useId()

  return (
    <TimePicker defaultValue="20:00" minuteStep={30} isTimeUnavailable={isBusinessHours}>
      <div className="flex w-full max-w-60 flex-col gap-1">
        <TimePickerLabel>Reindex window starts at</TimePickerLabel>
        <TimePickerTrigger className="w-full" aria-describedby={hintId} />
        <p id={hintId} className="system-xs-regular text-text-tertiary">
          30-minute steps; 9:00 AM–5:59 PM is unavailable.
        </p>
      </div>
      <TimePickerContent />
    </TimePicker>
  )
}
```

### Controlled value in a form

The controlled value changes only when OK confirms the draft. Inside a `Field`, `onFormSubmit` receives the `HH:mm` string.

```tsx title="form.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Field, FieldDescription, FieldError } from '@langgenius/dify-ui/field'
import { Form } from '@langgenius/dify-ui/form'
import {
  TimePicker,
  TimePickerClear,
  TimePickerContent,
  TimePickerLabel,
  TimePickerTrigger,
} from '@langgenius/dify-ui/time-picker'
import * as React from 'react'

export function TimePickerFormDemo() {
  const [runAt, setRunAt] = React.useState<string | null>('08:30')
  const [submitted, setSubmitted] = React.useState('')

  return (
    <Form
      aria-label="Workflow schedule"
      className="flex w-full max-w-72 flex-col gap-4"
      onFormSubmit={(values) => setSubmitted(JSON.stringify(values))}
    >
      <Field name="runAt">
        <TimePicker
          required
          value={runAt}
          onValueChange={(value) => {
            setRunAt(value)
            setSubmitted('')
          }}
        >
          <TimePickerLabel>Workflow run time</TimePickerLabel>
          <div className="flex items-center gap-1">
            <TimePickerTrigger className="w-auto min-w-0 flex-1" />
            <div className="size-8 shrink-0">
              <TimePickerClear aria-label="Clear workflow run time" />
            </div>
          </div>
          <TimePickerContent />
        </TimePicker>
        <FieldDescription>The workflow runs every day at this time.</FieldDescription>
        <FieldError match="valueMissing">Choose a run time.</FieldError>
      </Field>
      <div className="flex gap-2">
        <Button type="submit" variant="primary">
          Save schedule
        </Button>
      </div>
      <p aria-live="polite" className="system-xs-regular break-all text-text-tertiary tabular-nums">
        Value: {runAt ?? 'null'}
        {submitted && ` · Submitted: ${submitted}`}
      </p>
    </Form>
  )
}
```

## Keyboard

| Key                    | Action                                                                       |
| ---------------------- | ---------------------------------------------------------------------------- |
| `Enter`, `Space`       | Opens the popup from the trigger and focuses the selected hour.              |
| `ArrowUp`, `ArrowDown` | Selects the previous or next option in the draft, stopping at the ends.      |
| `PageUp`, `PageDown`   | Moves the selection five options.                                            |
| `Home`, `End`          | Selects the first or last option.                                            |
| Digits                 | Selects the option that starts with the typed number.                        |
| `Tab`                  | Moves between the columns, Now and OK, wrapping inside the popup.            |
| `Enter`, `Space`       | On OK, commits the draft, closes the popup and returns focus to the trigger. |
| `Escape`               | Discards the draft, closes the popup and returns focus to the trigger.       |

## API reference

### TimePicker

Groups all parts of a wall-clock time picker storing a 24-hour `HH:mm` value. Doesn't render its own HTML element.

Popup edits stay in a draft until confirmed with the apply button; `timeZone` affects only the "Now" shortcut and initial empty draft.

**Props**

| Prop                 | Type                                                                       | Default      | Description                                                                                                                                                                                                               |
| -------------------- | -------------------------------------------------------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `placeholder?`       | `string`                                                                   | —            | The text shown in the trigger when no value is selected.                                                                                                                                                                  |
| `disabled?`          | `boolean`                                                                  | `false`      | Whether the component should ignore user interaction.                                                                                                                                                                     |
| `readOnly?`          | `boolean`                                                                  | `false`      | Whether the user should be unable to change the value.                                                                                                                                                                    |
| `required?`          | `boolean`                                                                  | `false`      | Whether the user must choose a value before submitting a form.                                                                                                                                                            |
| `requiredLabel?`     | `string`                                                                   | `'Required'` | Visually hidden text that announces the field as required.                                                                                                                                                                |
| `invalid?`           | `boolean`                                                                  | `false`      | Whether to mark the value as invalid regardless of native validation.                                                                                                                                                     |
| `name?`              | `string`                                                                   | —            | Identifies the field when a form is submitted.                                                                                                                                                                            |
| `form?`              | `string`                                                                   | —            | Identifies the form that owns the hidden input. Useful when the picker is rendered outside the form.                                                                                                                      |
| `validationMessage?` | `string`                                                                   | —            | The error message reported when `invalid` is set or a required value is missing. Falls back to `'Choose a valid value.'` and `'Choose a value.'` respectively; an unavailable value reports `labels.unavailable` instead. |
| `onOpenChange?`      | `((open: boolean, details: PickerOpenChangeDetails) => void)`              | —            | Event handler called when the popup is opened or closed.                                                                                                                                                                  |
| `children`           | `React.ReactNode`                                                          | —            | The picker parts.                                                                                                                                                                                                         |
| `hourCycle?`         | `12 \| 24`                                                                 | `12`         | Whether hours are shown on a 12-hour clock with AM and PM, or a 24-hour clock.                                                                                                                                            |
| `minuteStep?`        | `1 \| 5 \| 10 \| 15 \| 20 \| 30`                                           | `1`          | The interval between selectable minutes.                                                                                                                                                                                  |
| `value?`             | `string \| null`                                                           | —            | The controlled value, a 24-hour `HH:mm` wall time independent of a date or time zone; `null` represents an empty field. To render an uncontrolled time picker, use the `defaultValue` prop instead.                       |
| `defaultValue?`      | `string \| null`                                                           | `null`       | The uncontrolled value when the picker is initially rendered. To render a controlled time picker, use the `value` prop instead.                                                                                           |
| `onValueChange?`     | `((value: string \| null, details: TimePickerChangeEventDetails) => void)` | —            | Called on OK, clearing or form reset; popup draft edits do not emit changes.                                                                                                                                              |
| `isTimeUnavailable?` | `((value: string) => boolean)`                                             | —            | Returns `true` for an `HH:mm` time that cannot be selected.                                                                                                                                                               |
| `locale?`            | `Intl.LocalesArgument`                                                     | `'en-US'`    | The locale used to format the displayed time.                                                                                                                                                                             |
| `timeZone?`          | `string`                                                                   | —            | IANA zone used for Now and the initial empty draft; it does not convert value.                                                                                                                                            |
| `labels?`            | `Partial<TimePickerLabels>`                                                | —            | Text for the built-in controls and messages, usually translated strings. Missing entries fall back to English.                                                                                                            |

Inherits `defaultOpen`, `open` from [Base UI Popover.Root](https://base-ui.com/react/components/popover#root).

### TimePickerClear

A button component that can be used to trigger actions. Renders a `<button>` element.

**Props**

| Prop               | Type                                                                                                     | Default     | Description                                                                                          |
| ------------------ | -------------------------------------------------------------------------------------------------------- | ----------- | ---------------------------------------------------------------------------------------------------- |
| `aria-label?`      | `string`                                                                                                 | —           | Names the element for assistive technology. Pass either this or `aria-labelledby`.                   |
| `aria-labelledby?` | `string`                                                                                                 | —           | Identifies the element that names this one. Pass either this or `aria-label`.                        |
| `variant?`         | `'default' \| 'ghost' \| 'ghost-accent' \| 'primary' \| 'secondary' \| 'secondary-accent' \| 'tertiary'` | `'default'` | The visual emphasis of the button. The accent variants cannot be combined with `tone="destructive"`. |
| `tone?`            | `'default' \| 'destructive'`                                                                             | `'default'` | Whether the button represents a destructive action.                                                  |
| `ref?`             | `React.Ref<HTMLButtonElement>`                                                                           | —           | —                                                                                                    |

Valid prop combinations:

- `aria-label: string`, `aria-labelledby?: undefined`, `variant?: 'default' | 'ghost' | 'primary' | 'secondary' | 'tertiary'`, `tone?: 'default' | 'destructive'`
- `aria-label: string`, `aria-labelledby?: undefined`, `variant: 'ghost-accent' | 'secondary-accent'`, `tone?: 'default'`
- `aria-label?: undefined`, `aria-labelledby: string`, `variant?: 'default' | 'ghost' | 'primary' | 'secondary' | 'tertiary'`, `tone?: 'default' | 'destructive'`
- `aria-label?: undefined`, `aria-labelledby: string`, `variant: 'ghost-accent' | 'secondary-accent'`, `tone?: 'default'`

Inherits `className`, `style`, `focusableWhenDisabled` from [Base UI Button](https://base-ui.com/react/components/button).

**Data attributes**

| Attribute       | Description                          |
| --------------- | ------------------------------------ |
| `data-disabled` | Present when the button is disabled. |

### TimePickerContent

A container for the popover contents. Renders a `<div>` element.

**Props**

| Prop          | Type                           | Default   | Description                                                                                             |
| ------------- | ------------------------------ | --------- | ------------------------------------------------------------------------------------------------------- |
| `sideOffset?` | `number \| OffsetFunction`     | `8`       | Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance. |
| `align?`      | `'center' \| 'end' \| 'start'` | `'start'` | How to align the popup relative to the specified side.                                                  |

Inherits `className`, `style`, `side`, `alignOffset`, `finalFocus` from [Base UI Popover.Popup](https://base-ui.com/react/components/popover#popup).

**Data attributes**

| Attribute             | Description                                                           |
| --------------------- | --------------------------------------------------------------------- |
| `data-open`           | Present when the popup is open.                                       |
| `data-closed`         | Present when the popup is closed.                                     |
| `data-starting-style` | Present when the popup begins animating in.                           |
| `data-ending-style`   | Present when the popup is animating out.                              |
| `data-side`           | Indicates which side the popup is positioned relative to the trigger. |
| `data-align`          | Indicates how the popup is aligned relative to specified side.        |
| `data-instant`        | Present if animations should be instant.                              |

**CSS variables**

| Variable         | Type | Description              |
| ---------------- | ---- | ------------------------ |
| `--popup-width`  | —    | The width of the popup.  |
| `--popup-height` | —    | The height of the popup. |

### TimePickerLabel

Inherits `className`, `style` from [Base UI null](https://base-ui.com/react/handbook/composition).

**Data attributes**

| Attribute       | Description                                                      |
| --------------- | ---------------------------------------------------------------- |
| `data-disabled` | Present when the component should ignore user interaction.       |
| `data-touched`  | Present when the field has been touched.                         |
| `data-dirty`    | Present when the field value has changed from its initial value. |
| `data-filled`   | Present when the field has a value.                              |
| `data-focused`  | Present when the field is focused.                               |

### TimePickerTrigger

A button that opens the popover. Renders a `<button>` element.

**Props**

| Prop   | Type                           | Default | Description |
| ------ | ------------------------------ | ------- | ----------- |
| `ref?` | `React.Ref<HTMLButtonElement>` | —       | —           |

Inherits `id`, `className`, `style` from [Base UI Popover.Trigger](https://base-ui.com/react/components/popover#trigger).

**Data attributes**

| Attribute         | Description                                     |
| ----------------- | ----------------------------------------------- |
| `data-readonly`   | Present when the picker is read-only.           |
| `data-required`   | Present when the picker requires a value.       |
| `data-popup-open` | Present when the corresponding popover is open. |
| `data-pressed`    | Present when the trigger is pressed.            |
| `data-disabled`   | Present when the trigger is disabled.           |

### TimePickerValue

**Props**

| Prop        | Type                                                             | Default | Description                                                            |
| ----------- | ---------------------------------------------------------------- | ------- | ---------------------------------------------------------------------- |
| `children?` | `((displayValue: string) => React.ReactNode) \| React.ReactNode` | —       | Receives the formatted committed value, or the placeholder when empty. |
