{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "time-picker",
  "type": "registry:ui",
  "title": "TimePicker",
  "description": "Centered time selection wheels with optional AM/PM and seconds.",
  "dependencies": [],
  "registryDependencies": [
    "https://ghost-ui.com/r/ghost-tokens.json",
    "https://ghost-ui.com/r/foundation.json"
  ],
  "files": [
    {
      "path": "src/components/ui/time-picker.tsx",
      "type": "registry:component",
      "target": "@ui/time-picker.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport \"./foundation.css\";\nimport \"./time-picker.css\";\n\nexport type TimeOnlyValue = string | null;\n\nexport interface TimeOnlyConstraints {\n  min?: string;\n  max?: string;\n  minuteStep?: number;\n  required?: boolean;\n  showSeconds?: boolean;\n}\n\nexport function isValidTimeOnly(value: unknown, showSeconds = false): value is string {\n  return typeof value === \"string\" && (showSeconds\n    ? /^([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$/\n    : /^([01]\\d|2[0-3]):[0-5]\\d$/).test(value);\n}\n\nexport function isValidMinuteStep(step: number | undefined): boolean {\n  return step === undefined || (Number.isInteger(step) && step > 0 && step <= 60 && 60 % step === 0);\n}\n\nconst pad = (value: number) => String(value).padStart(2, \"0\");\nconst secondsOf = (value: string) => {\n  const [hour, minute, second = 0] = value.split(\":\").map(Number);\n  return hour * 3600 + minute * 60 + second;\n};\nconst timeOf = (seconds: number, showSeconds = false) => `${pad(Math.floor(seconds / 3600))}:${pad(Math.floor(seconds / 60) % 60)}${showSeconds ? `:${pad(seconds % 60)}` : \"\"}`;\n\nexport function isValidTimeConfiguration({ min, max, minuteStep = 1, showSeconds = false }: TimeOnlyConstraints): boolean {\n  if (!isValidMinuteStep(minuteStep)) return false;\n  if (min !== undefined && !isValidTimeOnly(min, showSeconds)) return false;\n  if (max !== undefined && !isValidTimeOnly(max, showSeconds)) return false;\n  if (min !== undefined && max !== undefined && min > max) return false;\n  const start = min ? secondsOf(min) : 0, end = max ? secondsOf(max) : 86399;\n  const minute = Math.floor(start / 60);\n  const first = minute % minuteStep === 0 ? (showSeconds ? start : minute * 60) : (minute + minuteStep - minute % minuteStep) * 60;\n  return first <= end;\n}\n\nexport function isAllowedTimeOnly(value: unknown, constraints: TimeOnlyConstraints = {}): value is string {\n  if (!isValidTimeOnly(value, constraints.showSeconds) || !isValidTimeConfiguration(constraints)) return false;\n  const { min, max, minuteStep = 1 } = constraints;\n  return (!min || value >= min) && (!max || value <= max) && Number(value.slice(3, 5)) % minuteStep === 0;\n}\n\nexport function timeOnlyError(value: unknown, constraints: TimeOnlyConstraints = {}): string | null {\n  if (!isValidTimeConfiguration(constraints)) return \"Time options are not available.\";\n  if (value == null || value === \"\") return constraints.required ? \"Choose a time.\" : null;\n  if (!isValidTimeOnly(value, constraints.showSeconds)) return `Enter a time in 24-hour ${constraints.showSeconds ? \"HH:mm:ss\" : \"HH:mm\"} format.`;\n  if (!isAllowedTimeOnly(value, constraints)) return \"Choose a time within the available range and minute step.\";\n  return null;\n}\n\nexport interface TimePickerProps extends TimeOnlyConstraints, Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n  value?: TimeOnlyValue;\n  defaultValue?: TimeOnlyValue;\n  onValueChange?: (value: string) => void;\n  hourCycle?: 12 | 24;\n  disabled?: boolean;\n  readOnly?: boolean;\n}\n\ntype TimeMatch = { hour?: number; minute?: number; second?: number; period?: number };\n\n// Search minutes, not all 86,400 seconds; a changed hour can clamp its lower units at a bound.\nfunction nearestTime(target: number, constraints: TimeOnlyConstraints, match: TimeMatch = {}): string | null {\n  const { min, max, minuteStep = 1, showSeconds = false } = constraints;\n  const lower = min ? secondsOf(min) : 0, upper = max ? secondsOf(max) : 86399;\n  let best: number | null = null;\n  for (let hour = 0; hour < 24; hour++) {\n    if (match.hour !== undefined && hour !== match.hour || match.period !== undefined && Math.floor(hour / 12) !== match.period) continue;\n    for (let minute = 0; minute < 60; minute += minuteStep) {\n      if (match.minute !== undefined && minute !== match.minute) continue;\n      const base = hour * 3600 + minute * 60;\n      const first = Math.max(0, lower - base), last = Math.min(showSeconds ? 59 : 0, upper - base);\n      if (first > last) continue;\n      const second = match.second ?? Math.max(first, Math.min(last, target % 60));\n      if (second < first || second > last) continue;\n      const candidate = base + second;\n      if (best === null || Math.abs(candidate - target) < Math.abs(best - target)) best = candidate;\n    }\n  }\n  return best === null ? null : timeOf(best, showSeconds);\n}\n\ninterface WheelOption { label: string; value: number; time: string | null }\n\nfunction TimeWheel({ label, value, options, disabled, readOnly, onChoose }: {\n  label: string; value: number; options: WheelOption[]; disabled: boolean; readOnly: boolean; onChoose: (time: string) => void;\n}) {\n  const id = React.useId();\n  const listRef = React.useRef<HTMLDivElement>(null);\n  const timer = React.useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n  const frame = React.useRef<number | undefined>(undefined);\n  const target = React.useRef<number | null>(null);\n  const latest = React.useRef({ value, options, onChoose });\n  latest.current = { value, options, onChoose };\n  const [preview, setPreview] = React.useState(value);\n  const typed = React.useRef({ text: \"\", at: 0 });\n  const available = options.filter(option => option.time !== null);\n  const index = options.findIndex(option => option.value === value);\n\n  function align() {\n    const list = listRef.current;\n    if (!list) return;\n    setPreview(latest.current.value);\n    const current = latest.current.options.findIndex(option => option.value === latest.current.value);\n    const row = list.firstElementChild as HTMLElement | null;\n    if (!row?.offsetHeight) return;\n    target.current = current * row.offsetHeight;\n    list.scrollTop = target.current;\n  }\n\n  React.useEffect(() => {\n    clearTimeout(timer.current);\n    frame.current = requestAnimationFrame(align);\n    const observer = typeof ResizeObserver !== \"undefined\" ? new ResizeObserver(align) : null;\n    if (listRef.current) observer?.observe(listRef.current);\n    return () => { clearTimeout(timer.current); if (frame.current !== undefined) cancelAnimationFrame(frame.current); observer?.disconnect(); };\n  }, [index, disabled, readOnly, options.map(option => option.time !== null).join(\"\")]);\n\n  function choose(option: WheelOption | undefined) {\n    clearTimeout(timer.current);\n    if (disabled || readOnly || !option?.time) { align(); return; }\n    latest.current.onChoose(option.time);\n    if (frame.current !== undefined) cancelAnimationFrame(frame.current);\n    frame.current = requestAnimationFrame(align);\n  }\n\n  return <section className=\"ui-time-picker-column\">\n    <h3 id={id} className=\"ui-time-picker-label\">{label}</h3>\n    <div ref={listRef} role=\"spinbutton\" tabIndex={disabled ? -1 : 0} aria-labelledby={id}\n      aria-valuemin={available[0]?.value} aria-valuemax={available.at(-1)?.value} aria-valuenow={preview}\n      aria-valuetext={options.find(option => option.value === preview)?.label} aria-disabled={disabled || undefined} aria-readonly={readOnly || undefined}\n      className=\"ui-time-picker-list\" data-base-ui-swipe-ignore=\"\" data-disabled={disabled || undefined} data-readonly={readOnly || undefined}\n      onScroll={event => {\n        const list = event.currentTarget, height = (list.firstElementChild as HTMLElement | null)?.offsetHeight;\n        if (!height || target.current !== null && Math.abs(list.scrollTop - target.current) < 1) return;\n        target.current = null;\n        if (disabled || readOnly) { align(); return; }\n        const next = Math.max(0, Math.min(options.length - 1, Math.round(list.scrollTop / height)));\n        setPreview(options[next].value);\n        clearTimeout(timer.current);\n        timer.current = setTimeout(() => {\n          const choices = latest.current.options;\n          const closest = choices.reduce<WheelOption | undefined>((best, option, optionIndex) => option.time && (!best || Math.abs(optionIndex - next) < Math.abs(choices.indexOf(best) - next)) ? option : best, undefined);\n          choose(closest);\n        }, 120);\n      }}\n      onKeyDown={event => {\n        if (disabled || readOnly || event.altKey || event.ctrlKey || event.metaKey) return;\n        const position = available.findIndex(option => option.value === value);\n        const delta = event.key === \"ArrowUp\" ? 1 : event.key === \"ArrowDown\" ? -1 : event.key === \"PageUp\" ? 5 : event.key === \"PageDown\" ? -5 : 0;\n        if (delta || event.key === \"Home\" || event.key === \"End\") {\n          event.preventDefault();\n          choose(available[event.key === \"Home\" ? 0 : event.key === \"End\" ? available.length - 1 : Math.max(0, Math.min(available.length - 1, position + delta))]);\n        } else if (/^[0-9ap]$/i.test(event.key)) {\n          event.preventDefault();\n          typed.current.text = Date.now() - typed.current.at < 700 ? typed.current.text + event.key : event.key;\n          typed.current.at = Date.now();\n          let option = available.find(item => Number.isNaN(Number(typed.current.text)) ? item.label.toLowerCase().startsWith(typed.current.text.toLowerCase()) : Number(item.label) === Number(typed.current.text));\n          if (!option) { typed.current.text = event.key; option = available.find(item => item.label.toLowerCase().startsWith(event.key.toLowerCase()) || Number(item.label) === Number(event.key)); }\n          choose(option);\n        } else if (event.key === \"Enter\" || event.key === \" \") { event.preventDefault(); choose(options.find(option => option.value === value)); }\n      }}>\n      {options.map(option => <div key={option.value} aria-hidden=\"true\" className=\"ui-time-picker-option\" data-value={option.value}\n        data-selected={option.value === preview || undefined} data-disabled={!option.time || undefined}\n        onClick={() => { listRef.current?.focus({ preventScroll: true }); choose(option); }}>{option.label}</div>)}\n    </div>\n  </section>;\n}\n\nexport const TimePicker = React.forwardRef<HTMLDivElement, TimePickerProps>(function TimePicker({\n  value, defaultValue = null, onValueChange, min, max, minuteStep = 1, required, showSeconds = false, hourCycle = 24,\n  disabled = false, readOnly = false, className, style, ...props\n}, ref) {\n  const constraints = { min, max, minuteStep, required, showSeconds };\n  const controlled = value !== undefined;\n  const [internalValue, setInternalValue] = React.useState<TimeOnlyValue>(defaultValue);\n  const [clock, setClock] = React.useState(0);\n  React.useEffect(() => { const now = new Date(); setClock(now.getHours() * 3600 + now.getMinutes() * 60 + (showSeconds ? now.getSeconds() : 0)); }, [showSeconds]);\n  const selected = controlled ? value ?? null : internalValue;\n  const valid = isValidTimeConfiguration(constraints) && (selected === null || isAllowedTimeOnly(selected, constraints));\n  const current = valid ? selected ?? nearestTime(clock, constraints) : null;\n  const [hour, minute, second = 0] = (current ?? \"00:00\").split(\":\").map(Number);\n  const period = Math.floor(hour / 12);\n  function choose(next: string) {\n    if (disabled || readOnly || !isAllowedTimeOnly(next, constraints)) return;\n    if (!controlled) setInternalValue(next);\n    if (next !== selected) onValueChange?.(next);\n  }\n  const columns = 2 + Number(showSeconds) + Number(hourCycle === 12);\n  const wheels: { label: string; value: number; options: WheelOption[] }[] = current ? [\n    { label: \"Hours\", value: hourCycle === 12 ? hour % 12 || 12 : hour, options: Array.from({ length: hourCycle }, (_, index) => {\n      const display = hourCycle === 12 ? index + 1 : index, actual = hourCycle === 12 ? display % 12 + period * 12 : display;\n      return { label: pad(display), value: display, time: nearestTime(actual * 3600 + minute * 60 + second, constraints, { hour: actual }) };\n    }) },\n    { label: \"Minutes\", value: minute, options: Array.from({ length: 60 / minuteStep }, (_, index) => {\n      const next = index * minuteStep;\n      return { label: pad(next), value: next, time: nearestTime(hour * 3600 + next * 60 + second, constraints, { hour, minute: next }) };\n    }) },\n    ...(showSeconds ? [{ label: \"Seconds\", value: second, options: Array.from({ length: 60 }, (_, next) => ({ label: pad(next), value: next, time: nearestTime(hour * 3600 + minute * 60 + next, constraints, { hour, minute, second: next }) })) }] : []),\n    ...(hourCycle === 12 ? [{ label: \"Period\", value: period, options: [\"AM\", \"PM\"].map((label, next) => ({ label, value: next, time: nearestTime((hour % 12 + next * 12) * 3600 + minute * 60 + second, constraints, { period: next }) })) }] : []),\n  ] : [];\n  return <div {...props} ref={ref} className={[\"ui-time-picker\", className].filter(Boolean).join(\" \")}\n    style={{ \"--time-picker-columns\": columns, ...style } as React.CSSProperties} data-invalid={!valid || undefined} data-disabled={disabled || undefined} data-preview={selected === null || undefined}>\n    {!current ? <p className=\"ui-error ui-error--caption\" role=\"alert\">Time options are not available.</p> : <div className=\"ui-time-picker-columns\">\n      {wheels.map(wheel => <TimeWheel key={wheel.label} {...wheel} disabled={disabled} readOnly={readOnly} onChoose={choose} />)}\n    </div>}\n  </div>;\n});\n\nTimePicker.displayName = \"TimePicker\";\n"
    },
    {
      "path": "src/components/ui/time-picker.css",
      "type": "registry:file",
      "target": "@ui/time-picker.css",
      "content": ".ui-time-picker { --time-picker-row: 44px; box-sizing: border-box; width: calc(var(--time-picker-columns, 2) * 80px); max-width: 100%; min-width: 0; color: var(--foreground); font-family: var(--font-sans); }\n.ui-time-picker-columns { position: relative; display: grid; grid-template-columns: repeat(var(--time-picker-columns, 2), minmax(0, 1fr)); gap: var(--space-1); }\n.ui-time-picker-columns::before { position: absolute; top: calc(24px + 2 * var(--time-picker-row)); inset-inline: 0; height: var(--time-picker-row); border-radius: var(--radius-control-base); background: var(--muted); content: \"\"; pointer-events: none; }\n.ui-time-picker-column { position: relative; min-width: 0; }\n.ui-time-picker-label { margin: 0 0 4px; color: var(--muted-foreground); font-size: var(--font-size-caption); font-weight: var(--font-weight-regular); line-height: 20px; text-align: center; }\n.ui-time-picker-list { box-sizing: border-box; height: calc(5 * var(--time-picker-row)); overflow-y: auto; overscroll-behavior: contain; scroll-snap-type: y mandatory; scrollbar-width: none; padding-block: calc(2 * var(--time-picker-row)); outline: none; border-radius: var(--radius-control-base); touch-action: pan-y; }\n.ui-time-picker-list::-webkit-scrollbar { display: none; }\n.ui-time-picker-list:focus-visible { outline: var(--focus-border-width) solid var(--ring); outline-offset: var(--focus-outline-offset); }\n.ui-time-picker-option { box-sizing: border-box; display: flex; height: var(--time-picker-row); align-items: center; justify-content: center; padding-inline: var(--space-2); color: var(--muted-foreground); font-size: var(--font-size-base); font-weight: var(--font-weight-regular); line-height: 24px; font-variant-numeric: tabular-nums; scroll-snap-align: center; user-select: none; cursor: pointer; }\n.ui-time-picker-option[data-selected] { color: var(--foreground); }\n@media (hover:hover) and (pointer:fine) { .ui-time-picker-option:hover:not([data-disabled]) { color: var(--foreground); } }\n.ui-time-picker-option[data-disabled] { opacity: var(--disabled-opacity, .5); cursor: not-allowed; }\n.ui-time-picker-list[data-disabled], .ui-time-picker-list[data-readonly] { overflow-y: hidden; }\n.ui-time-picker-list[data-disabled] { opacity: var(--disabled-opacity, .5); }\n.ui-time-picker-list[data-readonly] .ui-time-picker-option { cursor: default; }\n"
    }
  ]
}
