{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "time-picker",
  "title": "Time Picker",
  "description": "A time picker with a popover of scrollable hour, minute and AM/PM columns: 12 or 24-hour clock, minute steps, min/max times, a Now shortcut and full keyboard support.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/time-picker/time-picker.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type TimePickerSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface TimePickerProps {\n  /** 24-hour \"HH:mm\", e.g. \"09:30\" or \"17:45\". */\n  value?: string | null;\n  defaultValue?: string | null;\n  onValueChange?: (value: string | null) => void;\n  hourCycle?: 12 | 24;\n  minuteStep?: number;\n  /** Earliest selectable time, \"HH:mm\". */\n  minTime?: string;\n  /** Latest selectable time, \"HH:mm\". */\n  maxTime?: string;\n  placeholder?: string;\n  label?: string;\n  helperText?: string;\n  clearable?: boolean;\n  size?: TimePickerSize;\n  theme?: \"dark\" | \"light\";\n  width?: number | string;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.28)\", bg: \"rgba(255,255,255,0.03)\", border: \"rgba(255,255,255,0.12)\", borderHover: \"rgba(255,255,255,0.24)\", focus: \"rgba(245,244,241,0.85)\", menuBg: \"#0E0E0E\", menuBorder: \"rgba(255,255,255,0.1)\", hover: \"rgba(255,255,255,0.08)\", sel: \"#F5F4F1\", selText: \"#0A0A0A\", shadow: \"0 24px 60px rgba(0,0,0,0.55)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.28)\", bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.16)\", borderHover: \"rgba(10,10,10,0.3)\", focus: \"rgba(10,10,10,0.85)\", menuBg: \"#FFFFFF\", menuBorder: \"rgba(10,10,10,0.14)\", hover: \"rgba(10,10,10,0.06)\", sel: \"#0A0A0A\", selText: \"#FFFFFF\", shadow: \"0 20px 50px rgba(0,0,0,0.18)\" },\n};\n\nconst SIZES: Record<TimePickerSize, { font: number; height: number; padX: number; radius: number; cell: number; col: number }> = {\n  sm: { font: 13, height: 36, padX: 12, radius: 11, cell: 30, col: 46 },\n  md: { font: 14.5, height: 44, padX: 14, radius: 13, cell: 34, col: 52 },\n  lg: { font: 16, height: 52, padX: 16, radius: 15, cell: 38, col: 58 },\n};\n\n// Rows visible in each column; the selected row scrolls to the middle one.\nconst VISIBLE_ROWS = 5;\n\nconst pad = (n: number) => String(n).padStart(2, \"0\");\n\n/** \"HH:mm\" -> minutes since midnight, or null if it isn't a valid time. */\nfunction parseTime(value?: string | null): number | null {\n  const m = /^(\\d{1,2}):(\\d{2})$/.exec(value ?? \"\");\n  if (!m) return null;\n  const h = Number(m[1]);\n  const min = Number(m[2]);\n  return h < 24 && min < 60 ? h * 60 + min : null;\n}\n\nconst toValue = (t: number) => `${pad(Math.floor(t / 60))}:${pad(t % 60)}`;\n\nfunction formatTime(t: number, hourCycle: 12 | 24) {\n  const h = Math.floor(t / 60);\n  const m = t % 60;\n  if (hourCycle === 24) return `${pad(h)}:${pad(m)}`;\n  return `${h % 12 || 12}:${pad(m)} ${h < 12 ? \"AM\" : \"PM\"}`;\n}\n\nfunction ClockIcon({ size }: { size: number }) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <circle cx=\"8\" cy=\"8\" r=\"5.75\" />\n      <path d=\"M8 5v3.25l2 1.25\" />\n    </svg>\n  );\n}\n\ninterface ColumnItem {\n  key: string;\n  label: string;\n  disabled: boolean;\n}\n\ninterface ColumnProps {\n  id: string;\n  label: string;\n  /** Small caption above the column; the AM/PM column has none. */\n  heading?: string;\n  items: ColumnItem[];\n  selectedKey: string | null;\n  onSelect: (key: string) => void;\n  onEnter: () => void;\n  columnRef: (el: HTMLDivElement | null) => void;\n  p: (typeof PALETTES)[\"dark\"];\n  s: (typeof SIZES)[\"md\"];\n}\n\n// One scrollable listbox (hours, minutes or AM/PM). The selected row gets a filled pill that slides\n// between rows (shared layoutId), and ArrowUp/ArrowDown step through the enabled rows.\nfunction Column({ id, label, heading = label, items, selectedKey, onSelect, onEnter, columnRef, p, s }: ColumnProps) {\n  const enabled = items.filter((it) => !it.disabled);\n  const step = (dir: 1 | -1 | \"first\" | \"last\") => {\n    if (enabled.length === 0) return;\n    if (dir === \"first\") return onSelect(enabled[0].key);\n    if (dir === \"last\") return onSelect(enabled[enabled.length - 1].key);\n    const i = enabled.findIndex((it) => it.key === selectedKey);\n    const next = i === -1 ? (dir === 1 ? 0 : enabled.length - 1) : (i + dir + enabled.length) % enabled.length;\n    onSelect(enabled[next].key);\n  };\n\n  return (\n    <div className=\"flex flex-col items-center\" style={{ width: s.col }}>\n      <div className=\"font-medium\" style={{ height: 24, lineHeight: \"24px\", color: p.faint, fontSize: s.font - 2.5 }} aria-hidden=\"true\">\n        {heading}\n      </div>\n      <div\n        ref={columnRef}\n        role=\"listbox\"\n        aria-label={label}\n        tabIndex={0}\n        aria-activedescendant={selectedKey ? `${id}-${selectedKey}` : undefined}\n        onKeyDown={(e) => {\n          const keys: Record<string, 1 | -1 | \"first\" | \"last\"> = { ArrowDown: 1, ArrowUp: -1, Home: \"first\", End: \"last\" };\n          if (e.key in keys) {\n            e.preventDefault();\n            step(keys[e.key]);\n          } else if (e.key === \"Enter\") {\n            e.preventDefault();\n            onEnter();\n          }\n        }}\n        className=\"group/col relative w-full overflow-y-auto outline-none [scrollbar-width:none] [&::-webkit-scrollbar]:hidden\"\n        style={{\n          height: s.cell * VISIBLE_ROWS,\n          paddingBlock: s.cell * Math.floor(VISIBLE_ROWS / 2),\n          maskImage: \"linear-gradient(to bottom, transparent, #000 32%, #000 68%, transparent)\",\n          WebkitMaskImage: \"linear-gradient(to bottom, transparent, #000 32%, #000 68%, transparent)\",\n          // The fade mask would clip a ring drawn around the column, so keyboard focus rings the\n          // selected pill instead (see the pill below).\n          [\"--tp-ring\" as string]: p.focus,\n          [\"--tp-gap\" as string]: p.menuBg,\n        }}\n      >\n        {items.map((it) => {\n          const isSel = it.key === selectedKey;\n          return (\n            <div\n              key={it.key}\n              id={`${id}-${it.key}`}\n              role=\"option\"\n              aria-selected={isSel}\n              aria-disabled={it.disabled || undefined}\n              onClick={() => !it.disabled && onSelect(it.key)}\n              className=\"relative flex items-center justify-center rounded-full\"\n              style={{ height: s.cell, margin: \"0 4px\", fontSize: s.font - 0.5, fontWeight: isSel ? 700 : 500, fontVariantNumeric: \"tabular-nums\", color: isSel ? p.selText : p.text, opacity: it.disabled ? 0.3 : 1, cursor: it.disabled ? \"default\" : \"pointer\", transition: \"color 0.15s ease, background 0.12s ease\" }}\n              onMouseOver={(e) => !isSel && !it.disabled && (e.currentTarget.style.background = p.hover)}\n              onMouseOut={(e) => (e.currentTarget.style.background = \"transparent\")}\n            >\n              {isSel && <motion.span layoutId={`${id}-pill`} aria-hidden=\"true\" className=\"absolute inset-0 rounded-full group-focus-visible/col:[box-shadow:0_0_0_2px_var(--tp-gap),0_0_0_3.5px_var(--tp-ring)]\" style={{ background: p.sel }} transition={{ type: \"spring\", stiffness: 520, damping: 38 }} />}\n              <span className=\"relative\">{it.label}</span>\n            </div>\n          );\n        })}\n      </div>\n    </div>\n  );\n}\n\nexport function TimePicker({\n  value,\n  defaultValue = null,\n  onValueChange,\n  hourCycle = 12,\n  minuteStep = 5,\n  minTime,\n  maxTime,\n  placeholder = \"Pick a time\",\n  label,\n  helperText,\n  clearable = true,\n  size = \"md\",\n  theme = \"dark\",\n  width = 240,\n  disabled = false,\n  className,\n}: TimePickerProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const triggerRef = React.useRef<HTMLButtonElement>(null);\n  const columnRefs = React.useRef<(HTMLDivElement | null)[]>([]);\n  const [internal, setInternal] = React.useState<string | null>(defaultValue);\n  const [open, setOpen] = React.useState(false);\n  const [hovered, setHovered] = React.useState(false);\n  const isControlled = value !== undefined;\n  const time = parseTime(isControlled ? value : internal);\n\n  const lo = parseTime(minTime) ?? 0;\n  const hi = parseTime(maxTime) ?? 24 * 60 - 1;\n  const step = Math.min(Math.max(1, Math.round(minuteStep)), 30);\n  const allowed = (t: number) => t >= lo && t <= hi;\n\n  const minutes = React.useMemo(() => {\n    const list = Array.from({ length: Math.ceil(60 / step) }, (_, i) => i * step);\n    // A controlled value off the step grid (say 09:07) still gets its own row.\n    if (time !== null && !list.includes(time % 60)) list.push(time % 60);\n    return list.sort((a, b) => a - b);\n  }, [step, time]);\n\n  const hour24 = time === null ? null : Math.floor(time / 60);\n  const minute = time === null ? null : time % 60;\n  const isPm = hour24 !== null && hour24 >= 12;\n\n  const commit = (t: number | null) => {\n    const next = t === null ? null : toValue(t);\n    if (!isControlled) setInternal(next);\n    onValueChange?.(next);\n  };\n\n  // Keep the chosen hour/minute when possible; if that lands outside min/max, take the closest\n  // allowed minute in the same hour instead.\n  const setTime = (h: number, m: number) => {\n    let t = h * 60 + m;\n    if (!allowed(t)) {\n      const inHour = minutes.map((mm) => h * 60 + mm).filter(allowed);\n      if (inHour.length === 0) return;\n      t = inHour.reduce((best, c) => (Math.abs(c - t) < Math.abs(best - t) ? c : best));\n    }\n    commit(t);\n  };\n\n  const hourAllowed = (h: number) => minutes.some((mm) => allowed(h * 60 + mm));\n  const hourValues = hourCycle === 24 ? Array.from({ length: 24 }, (_, i) => i) : [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];\n  const to24 = (h: number, pm: boolean) => (hourCycle === 24 ? h : (h % 12) + (pm ? 12 : 0));\n\n  const hourItems: ColumnItem[] = hourValues.map((h) => ({ key: String(h), label: hourCycle === 24 ? pad(h) : String(h), disabled: !hourAllowed(to24(h, isPm)) }));\n  const minuteItems: ColumnItem[] = minutes.map((mm) => ({ key: String(mm), label: pad(mm), disabled: !allowed((hour24 ?? Math.floor(lo / 60)) * 60 + mm) }));\n  const periodItems: ColumnItem[] = [\"AM\", \"PM\"].map((k) => ({ key: k, label: k, disabled: !hourValues.some((h) => hourAllowed(to24(h, k === \"PM\"))) }));\n\n  const firstAllowedHour = () => hourValues.map((h) => to24(h, isPm)).find(hourAllowed) ?? Math.floor(lo / 60);\n\n  const pickHour = (key: string) => setTime(to24(Number(key), isPm), minute ?? 0);\n  const pickMinute = (key: string) => setTime(hour24 ?? firstAllowedHour(), Number(key));\n  const pickPeriod = (key: string) => {\n    const pm = key === \"PM\";\n    const h = hour24 === null ? (pm ? 12 : 0) : (hour24 % 12) + (pm ? 12 : 0);\n    if (hourAllowed(h)) return setTime(h, minute ?? 0);\n    const first = hourValues.map((hv) => to24(hv, pm)).find(hourAllowed);\n    if (first !== undefined) setTime(first, minute ?? 0);\n  };\n\n  const now = () => {\n    const d = new Date();\n    const t = d.getHours() * 60 + Math.floor(d.getMinutes() / step) * step;\n    commit(Math.min(Math.max(t, lo), hi));\n  };\n\n  const openedAt = React.useRef(0);\n  const openMenu = () => {\n    if (disabled) return;\n    openedAt.current = performance.now();\n    setOpen(true);\n  };\n\n  const closeMenu = (returnFocus = true) => {\n    setOpen(false);\n    if (returnFocus) triggerRef.current?.focus();\n  };\n\n  const selectedKeys = [hour24 === null ? null : String(hourCycle === 24 ? hour24 : hour24 % 12 || 12), minute === null ? null : String(minute), hour24 === null ? null : isPm ? \"PM\" : \"AM\"];\n\n  // Centre each column on its selected row: instantly when the menu opens, smoothly afterwards.\n  React.useEffect(() => {\n    if (!open) return;\n    const instant = performance.now() - openedAt.current < 80;\n    columnRefs.current.forEach((col, i) => {\n      const key = selectedKeys[i];\n      if (!col || key === null) return;\n      const row = col.querySelector<HTMLElement>(`[id=\"${uid}-c${i}-${key}\"]`);\n      if (!row) return;\n      const top = row.offsetTop - (col.clientHeight - row.offsetHeight) / 2;\n      col.scrollTo({ top, behavior: instant ? \"auto\" : \"smooth\" });\n    });\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [open, time, hourCycle]);\n\n  React.useEffect(() => {\n    if (!open) return;\n    const onDown = (e: MouseEvent) => {\n      if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);\n    };\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key === \"Escape\") {\n        setOpen(false);\n        triggerRef.current?.focus();\n      }\n    };\n    document.addEventListener(\"mousedown\", onDown);\n    document.addEventListener(\"keydown\", onKey);\n    const t = setTimeout(() => columnRefs.current[0]?.focus({ preventScroll: true }), 40);\n    return () => {\n      document.removeEventListener(\"mousedown\", onDown);\n      document.removeEventListener(\"keydown\", onKey);\n      clearTimeout(t);\n    };\n  }, [open]);\n\n  const hasValue = time !== null;\n  const borderColor = open ? p.focus : hovered ? p.borderHover : p.border;\n  const columns = hourCycle === 12 ? 3 : 2;\n\n  return (\n    <div ref={rootRef} className={cn(\"relative inline-flex flex-col gap-2\", className)} style={{ width, maxWidth: \"100%\", fontFamily: \"Inter, sans-serif\", opacity: disabled ? 0.5 : 1 }}>\n      {label && (\n        <label id={`${uid}-label`} className=\"font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: s.font }}>\n          {label}\n        </label>\n      )}\n\n      <div className=\"relative\">\n        <button\n          ref={triggerRef}\n          type=\"button\"\n          aria-haspopup=\"dialog\"\n          aria-expanded={open}\n          aria-labelledby={label ? `${uid}-label` : undefined}\n          disabled={disabled}\n          onClick={() => (open ? closeMenu(false) : openMenu())}\n          onMouseEnter={() => setHovered(true)}\n          onMouseLeave={() => setHovered(false)}\n          className=\"flex w-full items-center text-left outline-none\"\n          style={{ height: s.height, gap: 10, padding: `0 ${s.padX}px`, borderRadius: s.radius, background: p.bg, border: `1px solid ${borderColor}`, boxShadow: open ? `0 0 0 3px color-mix(in srgb, ${p.focus} 14%, transparent)` : \"0 0 0 0 transparent\", color: hasValue ? p.text : p.muted, fontSize: s.font, fontWeight: 500, fontVariantNumeric: \"tabular-nums\", cursor: disabled ? \"default\" : \"pointer\", transition: \"border-color 0.18s ease, box-shadow 0.18s ease\" }}\n        >\n          <span className=\"flex shrink-0\" style={{ color: p.muted }}>\n            <ClockIcon size={s.font + 2} />\n          </span>\n          <span className=\"min-w-0 flex-1 truncate\">{hasValue ? formatTime(time, hourCycle) : placeholder}</span>\n          {clearable && hasValue && !disabled && (\n            <span\n              role=\"button\"\n              tabIndex={-1}\n              aria-label=\"Clear time\"\n              onClick={(e) => {\n                e.stopPropagation();\n                commit(null);\n              }}\n              className=\"flex shrink-0 cursor-pointer items-center justify-center rounded-full\"\n              style={{ width: 20, height: 20, background: p.hover, color: p.muted }}\n            >\n              <svg width=\"9\" height=\"9\" viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\">\n                <path d=\"M3 3L9 9M9 3L3 9\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" />\n              </svg>\n            </span>\n          )}\n        </button>\n\n        <AnimatePresence>\n          {open && (\n            <motion.div\n              role=\"dialog\"\n              aria-label=\"Choose time\"\n              className=\"absolute left-0 z-50\"\n              style={{ top: \"calc(100% + 8px)\", padding: 12, borderRadius: s.radius + 6, border: `1px solid ${p.menuBorder}`, background: p.menuBg, boxShadow: p.shadow, transformOrigin: \"top left\" }}\n              initial={{ opacity: 0, y: -6, scale: 0.97 }}\n              animate={{ opacity: 1, y: 0, scale: 1 }}\n              exit={{ opacity: 0, y: -6, scale: 0.97 }}\n              transition={{ duration: 0.16, ease: [0.16, 1, 0.3, 1] }}\n            >\n              <div className=\"flex\" style={{ gap: 4, width: columns * s.col + (columns - 1) * 4 }}>\n                <Column id={`${uid}-c0`} label=\"Hour\" items={hourItems} selectedKey={selectedKeys[0]} onSelect={pickHour} onEnter={() => closeMenu()} columnRef={(el) => (columnRefs.current[0] = el)} p={p} s={s} />\n                <Column id={`${uid}-c1`} label=\"Min\" items={minuteItems} selectedKey={selectedKeys[1]} onSelect={pickMinute} onEnter={() => closeMenu()} columnRef={(el) => (columnRefs.current[1] = el)} p={p} s={s} />\n                {hourCycle === 12 && <Column id={`${uid}-c2`} label=\"AM or PM\" heading=\"\" items={periodItems} selectedKey={selectedKeys[2]} onSelect={pickPeriod} onEnter={() => closeMenu()} columnRef={(el) => (columnRefs.current[2] = el)} p={p} s={s} />}\n              </div>\n\n              <div className=\"flex items-center justify-between\" style={{ marginTop: 10, paddingTop: 10, borderTop: `1px solid ${p.menuBorder}` }}>\n                <button type=\"button\" onClick={now} className=\"cursor-pointer border-none bg-transparent p-0 font-semibold outline-none hover:underline\" style={{ color: p.text, fontSize: s.font - 1.5 }}>\n                  Now\n                </button>\n                {clearable && hasValue && (\n                  <button type=\"button\" onClick={() => commit(null)} className=\"cursor-pointer border-none bg-transparent p-0 font-medium outline-none hover:underline\" style={{ color: p.muted, fontSize: s.font - 1.5 }}>\n                    Clear\n                  </button>\n                )}\n              </div>\n            </motion.div>\n          )}\n        </AnimatePresence>\n      </div>\n\n      {helperText && <span style={{ color: p.muted, fontSize: s.font - 1.5, lineHeight: 1.4 }}>{helperText}</span>}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}