# Time Picker

Clock and keyboard time selection with Zenos inputs, Field integration, and theme tokens.

Time Picker uses [timepicker-ui's React wrapper](https://timepicker-ui.vercel.app/react) for its clock and keyboard views. Zenos supplies the input appearance, Field connections, typography, and theme colors. The engine remains an optional recipe dependency.

## Install

```bash
pnpm dlx @zentara-co/zenos-cli add time-picker
npx @zentara-co/zenos-cli add time-picker
yarn dlx @zentara-co/zenos-cli add time-picker
bunx @zentara-co/zenos-cli add time-picker
```

The CLI copies `time-picker.tsx`, `time-range-picker.tsx`, and `time-picker.css` to your chosen component directory and installs `timepicker-ui-react` and `timepicker-ui`. The files belong to your project. The base picker imports its CSS, including the engine's stylesheet; keep all three files together.

Configure the Zenos stylesheet in your global `index.css` using the [installation CSS setup](/getting-started/installation/#2-load-the-styles), then import the recipe from its installed path:

```tsx
import { TimePicker } from "./components/time-picker";

<TimePicker aria-label="Appointment time" defaultValue="09:30 AM" />;
```

This recipe is verified with `timepicker-ui-react` 1.7.0 and `timepicker-ui` 4.7.0. When installing manually, use those compatible versions. A direct core dependency is needed because the recipe imports its stylesheet, including in pnpm projects.

## Confirmed values

Use `value` with `onValueChange` for controlled state, or `defaultValue` for an uncontrolled input. `onValueChange` runs when the user confirms a time or clears the field. Clock movements remain drafts until confirmation; canceling preserves the previous value. Use the engine's `onUpdate` callback when you also need draft selections.

```tsx
import { useState } from "react";
import { Button, Field, Stack, Text } from "@zentara-co/zenos-ui";
import { TimePicker } from "./components/time-picker";

function TimePickerExample() {
  const [value, setValue] = useState("09:30 AM");
  return (
    <Stack gap="3" className="w-full max-w-sm">
      <Field>
        <Field.Label>Appointment time</Field.Label>
        <TimePicker
          name="appointment-time"
          value={value}
          onValueChange={setValue}
          options={{ ui: { clearButton: true } }}
        />
        <Field.Description>Choose a time, then confirm to save it.</Field.Description>
      </Field>
      <Text size="s" role="status">
        Selected time: {value || "None"}
      </Text>
      <Button variant="primary" onClick={() => setValue("02:15 PM")}>
        Set to 2:15 PM
      </Button>
    </Stack>
  );
}
```

## 24-hour and keyboard entry

`format="24h"` returns values such as `14:30`; the default 12-hour format returns values such as `02:30 PM`. The view switch is enabled by default. To open directly in keyboard view, set `options.ui.initialView` to `keyboard`.

The outer input opens the picker. Time editing happens inside the picker, rather than through free-form typing in the trigger input.

For controlled fields, validate required selection in application state: the upstream trigger is read-only, so browser constraint validation does not enforce `required` on it.

```tsx
import { useState } from "react";
import { Field, Stack, Text } from "@zentara-co/zenos-ui";
import { TimePicker } from "./components/time-picker";

function TimePickerKeyboardExample() {
  const [value, setValue] = useState("14:30");
  return (
    <Stack gap="3" className="w-full max-w-sm">
      <Field>
        <Field.Label>Departure time</Field.Label>
        <TimePicker
          format="24h"
          value={value}
          onValueChange={setValue}
          options={{ ui: { initialView: "keyboard" }, clock: { incrementMinutes: 5 } }}
        />
        <Field.Description>
          24-hour format with keyboard entry and five-minute steps.
        </Field.Description>
      </Field>
      <Text size="s" role="status">
        Departure: {value}
      </Text>
    </Stack>
  );
}
```

## Seconds

`options.clock.showSeconds` adds a seconds field. Confirmed values then include seconds, such as `09:15:30`. Configure the seconds increment through the engine's clock options.

```tsx
import { useState } from "react";
import { Field, Stack, Text } from "@zentara-co/zenos-ui";
import { TimePicker } from "./components/time-picker";

function TimePickerSecondsExample() {
  const [value, setValue] = useState("09:15:30");
  return (
    <Stack gap="3" className="w-full max-w-sm">
      <Field>
        <Field.Label>Start time</Field.Label>
        <TimePicker
          value={value}
          onValueChange={setValue}
          format="24h"
          options={{
            clock: { showSeconds: true, incrementSeconds: 15 },
            ui: { initialView: "keyboard" },
          }}
        />
        <Field.Description>Include seconds in the confirmed value.</Field.Description>
      </Field>
      <Text size="s" role="status">
        Start: {value}
      </Text>
    </Stack>
  );
}
```

## Time ranges

Import `TimeRangePicker` from the companion file. It registers the range plugin automatically and uses structured `{ from, to }` values. `onValueChange` reports a confirmed range, or `undefined` when cleared. Cancellation preserves the previous value. Both pickers accept the same input variants, sizes, Field props, and native input refs.

```tsx
import { useState } from "react";
import { TimeRangePicker, type TimeRangeValue } from "./components/time-range-picker";

const [range, setRange] = useState<TimeRangeValue | undefined>({
  from: "09:00",
  to: "10:30",
});

<TimeRangePicker
  value={range}
  onValueChange={setRange}
  format="24h"
  variant="filled"
  options={{ range: { minDuration: 30, maxDuration: 240 } }}
  aria-label="Meeting window"
/>;
```

Duration limits use minutes. Linked endpoints enforce ordering and duration constraints by default. Native forms receive a combined string such as `09:00 - 10:30`; application state receives the structured object. `onRangeConfirm` also exposes the upstream duration payload. Programmatic controlled updates synchronize both the input and an already-open picker.

```tsx
import { useState } from "react";
import { Button, Field, Stack, Text } from "@zentara-co/zenos-ui";
import { TimeRangePicker, type TimeRangeValue } from "./components/time-range-picker";

function TimeRangePickerExample() {
  const [value, setValue] = useState<TimeRangeValue | undefined>({ from: "09:00", to: "10:30" });
  return (
    <Stack gap="3" className="w-full max-w-sm">
      <Field>
        <Field.Label>Meeting window</Field.Label>
        <TimeRangePicker
          value={value}
          onValueChange={setValue}
          name="meeting-window"
          format="24h"
          variant="filled"
          options={{
            ui: { initialView: "keyboard", clearButton: true },
            labels: { rangeFrom: "Start", rangeTo: "End" },
            range: { minDuration: 30, maxDuration: 240 },
          }}
        />
        <Field.Description>
          Choose a start and end time, between 30 minutes and four hours apart.
        </Field.Description>
      </Field>
      <Text size="s" role="status">
        Meeting: {value ? `${value.from}–${value.to}` : "None"}
      </Text>
      <Button variant="primary" onClick={() => setValue({ from: "13:00", to: "14:00" })}>
        Set afternoon window
      </Button>
    </Stack>
  );
}
```

Use `options.range.linked: false` when endpoints may cross midnight. In that mode the engine treats the ends independently and does not enforce duration limits; validate the range together with its dates in your application.

```tsx
import { Field } from "@zentara-co/zenos-ui";
import { TimeRangePicker } from "./components/time-range-picker";

function TimeRangePickerOvernightExample() {
  return (
    <Field className="w-full max-w-sm">
      <Field.Label>Night shift</Field.Label>
      <TimeRangePicker
        defaultValue={{ from: "22:00", to: "06:00" }}
        format="24h"
        variant="none"
        options={{ range: { linked: false }, ui: { initialView: "keyboard" } }}
      />
      <Field.Description>
        Independent endpoints allow a range across midnight. Validate its dates in your application.
      </Field.Description>
    </Field>
  );
}
```

## Available times

Use `options.clock.disabledTime` for unavailable hours or time ranges. Keep `isTimeDisabled` predicates stable when using custom logic; a new function reference can make the upstream wrapper recreate its UI. These constraints govern selection; validate submitted values in your application as well.

```tsx
import { Field } from "@zentara-co/zenos-ui";
import { TimePicker } from "./components/time-picker";

function TimePickerConstraintsExample() {
  return (
    <Field className="w-full max-w-sm">
      <Field.Label>Office appointment</Field.Label>
      <TimePicker
        defaultValue="10:00"
        format="24h"
        options={{ clock: { disabledTime: { ranges: ["00:00 - 08:59", "17:00 - 23:59"] } } }}
      />
      <Field.Description>Appointments are available from 09:00 through 16:59.</Field.Description>
    </Field>
  );
}
```

## Fields and input styling

Compose with `Field.Label`, `Field.Description`, and `Field.Error`. The recipe inherits the control ID, description and error associations, required state, disabled state, and invalid state. Explicit native input props override the inherited values.

`variant` follows Input, and `size` accepts `sm`, `md`, or `lg`. With no explicit size, compact density uses `sm` and comfortable density uses `md`. `className` styles the input; `wrapperClassName` styles its outer container. The forwarded ref points to the native input for focus and form integrations. Native `name`, `required`, `aria-*`, and `data-*` props pass through.

The available variants are `default` (bordered input), `filled` (neutral filled input), and `none` (transparent input). They use the shared Input styles, including focus, disabled, and invalid treatments, on both `TimePicker` and `TimeRangePicker`.

```tsx
import { Field, Stack } from "@zentara-co/zenos-ui";
import { TimePicker } from "./components/time-picker";

function TimePickerVariantsExample() {
  return (
    <Stack gap="4" className="w-full max-w-sm">
      {(["default", "filled", "none"] as const).map((variant) => (
        <Field key={variant}>
          <Field.Label>{variant} time</Field.Label>
          <TimePicker variant={variant} defaultValue="09:30 AM" />
        </Field>
      ))}
    </Stack>
  );
}
```

```tsx
import { Field, Stack } from "@zentara-co/zenos-ui";
import { TimePicker } from "./components/time-picker";

function TimePickerStatesExample() {
  return (
    <Stack gap="4" className="w-full max-w-sm">
      <Field disabled>
        <Field.Label>Unavailable time</Field.Label>
        <TimePicker defaultValue="09:30 AM" />
      </Field>
      <Field invalid required>
        <Field.Label>Required time</Field.Label>
        <TimePicker name="required-time" placeholder="Select time" />
        <Field.Error>Select a time before continuing.</Field.Error>
      </Field>
      <Field>
        <Field.Label>Compact time</Field.Label>
        <TimePicker size="sm" variant="filled" defaultValue="11:45 AM" />
      </Field>
    </Stack>
  );
}
```

## Theme and advanced options

`time-picker.css` maps the engine's `--tp-*` variables to Zenos semantic tokens. The input and portaled picker follow the active light or dark theme without swapping engine themes. The recipe owns `options.ui.theme` and `options.ui.cssClass`; customize the popup in your editable `.zenos-time-picker` stylesheet. For example:

```css
.zenos-time-picker {
  --tp-border-radius: var(--radius-xl);
  --tp-primary: var(--background-accent-teal-bolder-default);
}
```

Other [upstream options](https://timepicker-ui.vercel.app/docs/api/options) and typed event callbacks remain available, including clock increments, labels, clear behavior, disabled times, inline containers, and view switching. `TimeRangePicker` includes range setup. Optional wheel and timezone modes require their upstream plugins; register those before rendering the picker. Timezone integrations use their engine callbacks.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| value / defaultValue | string | — | Controlled value or initial uncontrolled time. Empty string clears selection. |
| onValueChange | (value: string) => void | — | Reports confirmed or cleared values. Draft updates and cancellation do not commit. |
| format | 12h \| 24h | 12h | Overrides options.clock.type when supplied. |
| options | TimePickerOptions | — | Engine options with the theme and CSS hook supplied by the recipe. |
| variant | default \| filled \| none | default | Uses the same appearances as Input. |
| size / density | sm \| md \| lg / compact \| comfortable | — | Input sizing and inherited density. |
| className / wrapperClassName | string | — | Input and outer container class overrides. |
| ref | Ref<HTMLInputElement> | — | The native input element. |
| onConfirm / onCancel / onUpdate / onClear | upstream typed callbacks | — | Preserves engine event payloads alongside onValueChange. |

`TimeRangePicker` shares the API above except for `value` and `defaultValue` (`TimeRangeValue | undefined`), `onValueChange` (`(value: TimeRangeValue | undefined) => void`), and `options` (`TimeRangePickerOptions`). Range mode is always enabled for this component. Use `options.range` for `minDuration`, `maxDuration`, and `linked`.

## Accessibility and server rendering

Give each input a visible Field label or an accessible name. The picker retains the engine's dialog semantics, keyboard controls, and focus trap. Focus returns to the input after closing by default. Reduced-motion preferences disable the recipe's popup motion. Keyboard view provides an alternative to the clock face, and disabled controls remain unavailable.

The React wrapper renders an input on the server and initializes the engine after mounting. In Next.js App Router, use the recipe from a client component. Keep any DOM-dependent options, such as inline container elements, client-side. Native forms can submit the input's `name` and value; reset controlled values through application state.

Source: https://zenos-ui.pages.dev/recipes/time-picker/