← Components

Selection and input · Material 3: Time pickers (input)

Time picker

Hour and minute segments you type into or step with arrow keys, with an AM/PM toggle in 12-hour mode.

Examples and variants

24-hour and 12-hour

Focus a segment and press ↑ or ↓.

Example
function TimePickerExample() {
  const [time, setTime] = useState("09:30")
  const [time12, setTime12] = useState("14:15")
  return (
    <div className="flex flex-col items-center gap-3">
      <TimePicker value={time} onChange={setTime} label="Meeting time" />
      <TimePicker value={time12} onChange={setTime12} hour12 label="Reminder" />
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/time-picker.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/time-picker.tsxtime-picker
      components/ui/time-picker.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      
      /*
        Time picker (Material 3 input mode): hour and minute segments you can type
        into or step with the arrow keys, plus an AM/PM toggle in 12-hour mode.
        Value is "HH:MM" in 24-hour time. Changed digits roll.
      */
      const pad = (n: number) => String(n).padStart(2, "0")
      
      function TimePicker({
        value = "09:00",
        onChange,
        hour12 = false,
        step = 5,
        label = "Time",
        className,
      }: {
        value?: string
        onChange?: (value: string) => void
        hour12?: boolean
        /** Minute step for the arrow keys. */
        step?: number
        label?: string
        className?: string
      }) {
        const [h, m] = value.split(":").map(Number)
        const pm = h >= 12
        const shownHour = hour12 ? ((h + 11) % 12) + 1 : h
      
        function set(hours: number, minutes: number) {
          onChange?.(`${pad((hours + 24) % 24)}:${pad((minutes + 60) % 60)}`)
        }
      
        function segment(kind: "hour" | "minute") {
          const current = kind === "hour" ? shownHour : m
          const max = kind === "hour" ? (hour12 ? 12 : 23) : 59
          const min = kind === "hour" && hour12 ? 1 : 0
          return (
            <input
              aria-label={kind === "hour" ? "Hours" : "Minutes"}
              inputMode="numeric"
              role="spinbutton"
              aria-valuenow={current}
              aria-valuemin={min}
              aria-valuemax={max}
              value={pad(current)}
              onFocus={(event) => event.currentTarget.select()}
              onChange={(event) => {
                const digits = event.target.value.replace(/\D/g, "").slice(-2)
                let n = Math.min(max, Math.max(min, Number(digits) || min))
                if (kind === "hour") {
                  if (hour12) n = (n % 12) + (pm ? 12 : 0)
                  set(n, m)
                } else set(h, n)
              }}
              onKeyDown={(event) => {
                if (event.key !== "ArrowUp" && event.key !== "ArrowDown") return
                event.preventDefault()
                const delta = event.key === "ArrowUp" ? 1 : -1
                if (kind === "hour") set(h + delta, m)
                else set(h, m + delta * step)
              }}
              className="hit-target time-segment w-[2.5ch] rounded-(--shape-sm) bg-transparent text-center tabular-nums outline-none focus:bg-(--color-bg-selected)"
            />
          )
        }
      
        return (
          <div
            role="group"
            aria-label={label}
            data-slot="time-picker"
            className={cn(
              "inline-flex h-(--field-height-md) items-center gap-1 rounded-(--field-corner) bg-(--field-container) px-(--field-padding-x) text-sm text-(--field-label) shadow-(--field-elevation) transition-[background-color,box-shadow] duration-(--duration-fast) focus-within:bg-(--field-container-focus) focus-within:shadow-(--field-focus-shadow)",
              className
            )}
          >
            {segment("hour")}
            <span aria-hidden className="text-(--field-placeholder)">:</span>
            {segment("minute")}
            {hour12 ? (
              <button
                type="button"
                aria-label={pm ? "PM, switch to AM" : "AM, switch to PM"}
                onClick={() => set(h + (pm ? -12 : 12), m)}
                className="hit-target liquid-press ms-1 rounded-(--shape-full) bg-(--surface-container-high) px-2 py-0.5 text-caption font-medium outline-none focus-visible:shadow-(--focus-ring)"
              >
                {pm ? "PM" : "AM"}
              </button>
            ) : null}
          </div>
        )
      }
      
      export { TimePicker }
      

Imports

What the examples on this page import:

Imports
import { CopyIcon, FileIcon, MoonIcon, PlusIcon, SettingsIcon, TrashIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Calendar, type CalendarRange } from "@/components/ui/calendar"
import { Carousel } from "@/components/ui/carousel"
import { CommandDialog, type CommandItem } from "@/components/ui/command"
import { ContextMenu, ContextMenuContent, ContextMenuTrigger } from "@/components/ui/context-menu"
import { DatePicker } from "@/components/ui/date-picker"
import { DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuShortcut } from "@/components/ui/dropdown-menu"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
import { Menubar, MenubarMenu, MenubarTrigger } from "@/components/ui/menubar"
import { Search } from "@/components/ui/search"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { TimePicker } from "@/components/ui/time-picker"

Anatomy

  1. 1Field · Text field tokens.
  2. 2Segments · Hours and minutes (spinbuttons).
  3. 3Period · AM/PM toggle (12-hour).

Props

PropTypeDefaultDescription
value / onChange"HH:MM""09:00"24-hour time.
hour12booleanfalse12-hour display with AM/PM.
stepnumber5Minutes per arrow press.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--field-height-smvar(--size-control-sm)32pxsame
--field-height-mdvar(--size-control-md)40pxsame
--field-height-lgvar(--size-control-lg)48pxsame
--field-padding-xvar(--space-inset-control-x)12pxsame
--field-padding-x-textvar(--space-inset-control-x-lg)16pxsame
--field-padding-yvar(--space-inset-control-y)8pxsame
--field-cornervar(--shape-full)999pxsame
--field-corner-multilinevar(--shape-md)14pxsame
--field-containervar(--surface-container)oklch(0.965 0.005 95)oklch(0.12 0 0)
--field-container-focusvar(--surface-container-lowest)oklch(1 0 0)oklch(0.14 0 0)
--field-labelvar(--on-surface)oklch(0.22 0.01 260)oklch(1 0 0)
--field-container-hovercolor-mix(in oklch, var(--field-container), var(--field-label) var(--state-hover-mix))color-mix(in oklch, var(--field-container), var(--field-label) var(--state-hover-mix))same
--field-container-disabledvar(--color-bg-disabled)color-mix(in oklch, var(--color-fg-default) var(--state-disabled-container-mix), transparent)same
--field-label-disabledvar(--color-fg-disabled)oklch(0.62 0.01 95)oklch(0.53 0 0)
--field-placeholdervar(--on-surface-variant)oklch(0.45 0.01 95)oklch(0.68 0 0)
--field-stroke-widthvar(--stroke-width-control)0pxsame
--field-stroke-colorvar(--stroke-color-default)oklch(0.22 0.01 260 / 18%)oklch(1 0 0 / 12%)
--field-elevationvar(--elevation-1)0 1px 1px -0.5px oklch(0.22 0.01 260 / 7%), 0 2px 3px -1px oklch(0.22 0.01 260 / 10%), 0 4px 8px -4px oklch(0.22 0.01 260 / 6%)0 1px 2px oklch(0 0 0 / 35%), 0 2px 10px oklch(0 0 0 / 25%)
--field-focus-halo-widthvar(--state-field-stroke)3pxsame
--field-focus-shadowvar(--focus-field)0 0 0 var(--state-field-stroke) color-mix(in oklch, var(--color-fg-default) var(--state-field-stroke-mix), transparent), var(--elevation-3)same
--field-error-shadowvar(--focus-ring-invalid)0 0 0 var(--state-focus-ring-offset) var(--focus-ring-gap, var(--color-bg-canvas)), 0 0 0 calc(var(--state-focus-ring-offset) + var(--state-focus-ring-width)) var(--color-danger)same
--field-label-gapvar(--space-1)4pxsame
--field-action-labelvar(--field-placeholder)oklch(0.45 0.01 95)oklch(0.68 0 0)
--field-action-label-hovervar(--field-label)oklch(0.22 0.01 260)oklch(1 0 0)
--field-action-container-hovervar(--color-bg-hover)oklch(0.965 0.005 95)oklch(0.22 0 0)
--field-action-container-pressedvar(--color-bg-pressed)oklch(0.94 0.007 95)oklch(0.27 0 0)
--field-choice-container-hovercolor-mix(in oklch, var(--field-container), var(--field-label) var(--state-hover-mix))color-mix(in oklch, var(--field-container), var(--field-label) var(--state-hover-mix))same

Motion

TriggerWhat happensTokens
FocusField lifts like other text fields.--duration-fast

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Each segment is a spinbutton with min, max and value.
  • Group labelled by label.

Usage

Do

  • Use for exact times.

Don’t

  • Don't use a slider for times.

Code

components/ui/time-picker.tsx
"use client"

import * as React from "react"
import { cn } from "cn"

/*
  Time picker (Material 3 input mode): hour and minute segments you can type
  into or step with the arrow keys, plus an AM/PM toggle in 12-hour mode.
  Value is "HH:MM" in 24-hour time. Changed digits roll.
*/
const pad = (n: number) => String(n).padStart(2, "0")

function TimePicker({
  value = "09:00",
  onChange,
  hour12 = false,
  step = 5,
  label = "Time",
  className,
}: {
  value?: string
  onChange?: (value: string) => void
  hour12?: boolean
  /** Minute step for the arrow keys. */
  step?: number
  label?: string
  className?: string
}) {
  const [h, m] = value.split(":").map(Number)
  const pm = h >= 12
  const shownHour = hour12 ? ((h + 11) % 12) + 1 : h

  function set(hours: number, minutes: number) {
    onChange?.(`${pad((hours + 24) % 24)}:${pad((minutes + 60) % 60)}`)
  }

  function segment(kind: "hour" | "minute") {
    const current = kind === "hour" ? shownHour : m
    const max = kind === "hour" ? (hour12 ? 12 : 23) : 59
    const min = kind === "hour" && hour12 ? 1 : 0
    return (
      <input
        aria-label={kind === "hour" ? "Hours" : "Minutes"}
        inputMode="numeric"
        role="spinbutton"
        aria-valuenow={current}
        aria-valuemin={min}
        aria-valuemax={max}
        value={pad(current)}
        onFocus={(event) => event.currentTarget.select()}
        onChange={(event) => {
          const digits = event.target.value.replace(/\D/g, "").slice(-2)
          let n = Math.min(max, Math.max(min, Number(digits) || min))
          if (kind === "hour") {
            if (hour12) n = (n % 12) + (pm ? 12 : 0)
            set(n, m)
          } else set(h, n)
        }}
        onKeyDown={(event) => {
          if (event.key !== "ArrowUp" && event.key !== "ArrowDown") return
          event.preventDefault()
          const delta = event.key === "ArrowUp" ? 1 : -1
          if (kind === "hour") set(h + delta, m)
          else set(h, m + delta * step)
        }}
        className="hit-target time-segment w-[2.5ch] rounded-(--shape-sm) bg-transparent text-center tabular-nums outline-none focus:bg-(--color-bg-selected)"
      />
    )
  }

  return (
    <div
      role="group"
      aria-label={label}
      data-slot="time-picker"
      className={cn(
        "inline-flex h-(--field-height-md) items-center gap-1 rounded-(--field-corner) bg-(--field-container) px-(--field-padding-x) text-sm text-(--field-label) shadow-(--field-elevation) transition-[background-color,box-shadow] duration-(--duration-fast) focus-within:bg-(--field-container-focus) focus-within:shadow-(--field-focus-shadow)",
        className
      )}
    >
      {segment("hour")}
      <span aria-hidden className="text-(--field-placeholder)">:</span>
      {segment("minute")}
      {hour12 ? (
        <button
          type="button"
          aria-label={pm ? "PM, switch to AM" : "AM, switch to PM"}
          onClick={() => set(h + (pm ? -12 : 12), m)}
          className="hit-target liquid-press ms-1 rounded-(--shape-full) bg-(--surface-container-high) px-2 py-0.5 text-caption font-medium outline-none focus-visible:shadow-(--focus-ring)"
        >
          {pm ? "PM" : "AM"}
        </button>
      ) : null}
    </div>
  )
}

export { TimePicker }