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 ↓.
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.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/time-picker.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install cnCopy 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:
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
- 1Field · Text field tokens.
- 2Segments · Hours and minutes (spinbuttons).
- 3Period · AM/PM toggle (12-hour).
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value / onChange | "HH:MM" | "09:00" | 24-hour time. |
hour12 | boolean | false | 12-hour display with AM/PM. |
step | number | 5 | Minutes 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.
| Token | Points to | Light | Dark |
|---|---|---|---|
--field-height-sm | var(--size-control-sm) | 32px | same |
--field-height-md | var(--size-control-md) | 40px | same |
--field-height-lg | var(--size-control-lg) | 48px | same |
--field-padding-x | var(--space-inset-control-x) | 12px | same |
--field-padding-x-text | var(--space-inset-control-x-lg) | 16px | same |
--field-padding-y | var(--space-inset-control-y) | 8px | same |
--field-corner | var(--shape-full) | 999px | same |
--field-corner-multiline | var(--shape-md) | 14px | same |
--field-container | var(--surface-container) | oklch(0.965 0.005 95) | oklch(0.12 0 0) |
--field-container-focus | var(--surface-container-lowest) | oklch(1 0 0) | oklch(0.14 0 0) |
--field-label | var(--on-surface) | oklch(0.22 0.01 260) | oklch(1 0 0) |
--field-container-hover | color-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-disabled | var(--color-bg-disabled) | color-mix(in oklch, var(--color-fg-default) var(--state-disabled-container-mix), transparent) | same |
--field-label-disabled | var(--color-fg-disabled) | oklch(0.62 0.01 95) | oklch(0.53 0 0) |
--field-placeholder | var(--on-surface-variant) | oklch(0.45 0.01 95) | oklch(0.68 0 0) |
--field-stroke-width | var(--stroke-width-control) | 0px | same |
--field-stroke-color | var(--stroke-color-default) | oklch(0.22 0.01 260 / 18%) | oklch(1 0 0 / 12%) |
--field-elevation | var(--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-width | var(--state-field-stroke) | 3px | same |
--field-focus-shadow | var(--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-shadow | var(--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-gap | var(--space-1) | 4px | same |
--field-action-label | var(--field-placeholder) | oklch(0.45 0.01 95) | oklch(0.68 0 0) |
--field-action-label-hover | var(--field-label) | oklch(0.22 0.01 260) | oklch(1 0 0) |
--field-action-container-hover | var(--color-bg-hover) | oklch(0.965 0.005 95) | oklch(0.22 0 0) |
--field-action-container-pressed | var(--color-bg-pressed) | oklch(0.94 0.007 95) | oklch(0.27 0 0) |
--field-choice-container-hover | color-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
| Trigger | What happens | Tokens |
|---|---|---|
| Focus | Field 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
"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 }