{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "date-picker",
  "type": "registry:ui",
  "title": "Date Picker",
  "description": "The date-picker primitive.",
  "dependencies": [
    "lucide-react@^1.49.0"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/calendar.json",
    "https://ui.klarhimmel.com/r/input-group.json",
    "https://ui.klarhimmel.com/r/popover.json",
    "https://ui.klarhimmel.com/r/theme.json"
  ],
  "files": [
    {
      "path": "src/ui/date-picker.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/ui/date-picker.tsx",
      "content": "import { forwardRef, useEffect, useId, useState } from 'react';\nimport { CalendarDays } from 'lucide-react';\nimport { Calendar, parseIso, readDate, type CalendarProps } from './calendar';\nimport { InputGroup, InputGroupButton, InputGroupInput } from './input-group';\nimport { Popover, PopoverContent, PopoverTrigger } from './popover';\n\nexport interface DatePickerProps extends Pick<CalendarProps, 'min' | 'max' | 'isDisabled' | 'today' | 'firstDay' | 'locale'> {\n  /** The chosen day, \"YYYY-MM-DD\", or \"\" for none. */\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  /** Submits the chosen day, as \"YYYY-MM-DD\", with a form (a hidden input). */\n  name?: string;\n  id?: string;\n  placeholder?: string;\n  disabled?: boolean;\n  invalid?: boolean;\n  'aria-label'?: string;\n  'aria-labelledby'?: string;\n  'aria-describedby'?: string;\n  /** Words for the button, the panel, the calendar's buttons and the two messages. Pass them translated. */\n  labels?: { open: string; panel: string; previous: string; next: string; today: string; unreadable: string; outOfRange: string };\n  className?: string;\n}\n\nconst WORDS = { open: 'Choose date', panel: 'Choose date', previous: 'Previous month', next: 'Next month', today: 'today', unreadable: 'Enter a date like 2026-11-16 or 16/11/2026.', outOfRange: 'That date cannot be chosen.' };\n\n/**\n * A date you can type or pick. The field takes 2026-11-16 or day first (16/11/2026) and keeps the day as ISO text; the button opens\n * the calendar in a dialog (the WAI-ARIA date picker dialog): focus lands on the chosen day, Escape closes it and returns focus to the\n * button, choosing a day closes it. Text that is not a date, or a day that cannot be chosen, is marked and said in words.\n */\nexport const DatePicker = forwardRef<HTMLInputElement, DatePickerProps>(function DatePicker(\n  { value, defaultValue = '', onValueChange, name, id, placeholder = 'YYYY-MM-DD', disabled, invalid, labels, min, max, isDisabled, today, firstDay, locale, className, ...aria },\n  ref,\n) {\n  const w = { ...WORDS, ...labels };\n  const auto = useId();\n  const pid = id ?? auto;\n  const [own, setOwn] = useState(defaultValue);\n  const chosen = value ?? own;\n  const [text, setText] = useState(chosen);\n  const [problem, setProblem] = useState<'unreadable' | 'outOfRange' | null>(null);\n  const [open, setOpen] = useState(false);\n  useEffect(() => {\n    setText(chosen);\n    setProblem(null);\n  }, [chosen]);\n  const blocked = (iso: string) => (min !== undefined && iso < min) || (max !== undefined && iso > max) || !!isDisabled?.(iso);\n  const set = (iso: string) => {\n    setOwn(iso);\n    if (iso !== chosen) onValueChange?.(iso);\n  };\n  /** Reads the text when the person leaves the field or presses Enter. Empty clears the date. */\n  const commit = () => {\n    if (text.trim() === '') {\n      setProblem(null);\n      set('');\n      return;\n    }\n    const iso = readDate(text);\n    if (!iso) setProblem('unreadable');\n    else if (blocked(iso)) setProblem('outOfRange');\n    else {\n      setProblem(null);\n      setText(iso);\n      set(iso);\n    }\n  };\n  const bad = invalid || problem !== null;\n  return (\n    <div className={className}>\n      <InputGroup invalid={bad} disabled={disabled}>\n        <InputGroupInput\n          ref={ref}\n          id={pid}\n          inputMode=\"numeric\"\n          autoComplete=\"off\"\n          placeholder={placeholder}\n          value={text}\n          disabled={disabled}\n          aria-invalid={bad || undefined}\n          aria-describedby={[aria['aria-describedby'], problem ? `${pid}-e` : ''].filter(Boolean).join(' ') || undefined}\n          aria-label={aria['aria-label']}\n          aria-labelledby={aria['aria-labelledby']}\n          onChange={(e) => setText(e.target.value)}\n          onBlur={commit}\n          onKeyDown={(e) => {\n            if (e.key === 'Enter') commit();\n            else if (e.key === 'ArrowDown' && e.altKey) setOpen(true);\n          }}\n        />\n        <Popover open={open} onOpenChange={setOpen}>\n          <PopoverTrigger asChild>\n            <InputGroupButton aria-label={w.open} aria-haspopup=\"dialog\" aria-expanded={open} disabled={disabled}>\n              <CalendarDays aria-hidden />\n            </InputGroupButton>\n          </PopoverTrigger>\n          <PopoverContent label={w.panel} align=\"end\" className=\"w-auto p-3\" onOpenAutoFocus={(e) => e.preventDefault()}>\n            <Calendar\n              autoFocus\n              value={parseIso(chosen) ? chosen : ''}\n              onValueChange={(iso) => {\n                set(iso);\n                setOpen(false);\n              }}\n              min={min}\n              max={max}\n              isDisabled={isDisabled}\n              today={today}\n              firstDay={firstDay}\n              locale={locale}\n              labels={{ previous: w.previous, next: w.next, today: w.today }}\n            />\n          </PopoverContent>\n        </Popover>\n      </InputGroup>\n      {problem && (\n        <p id={`${pid}-e`} className=\"mt-1.5 text-kh-small text-kh-ink\">\n          {w[problem]}\n        </p>\n      )}\n      {name && <input type=\"hidden\" name={name} value={parseIso(chosen) ? chosen : ''} />}\n    </div>\n  );\n});\n"
    }
  ]
}
