{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "date-picker",
  "type": "registry:ui",
  "title": "Date Picker",
  "description": "A button that shows a date, or a range of dates, and opens a calendar in a popover to choose it. Values are \"YYYY-MM-DD\" strings.",
  "categories": [
    "ui"
  ],
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/button",
    "@rivier-ui/calendar",
    "@rivier-ui/cn",
    "@rivier-ui/popover",
    "@rivier-ui/use-controllable-state"
  ],
  "files": [
    {
      "path": "registry/ui/date-picker.tsx",
      "type": "registry:ui",
      "target": "components/ui/date-picker.tsx",
      "content": "\"use client\";\n/**\n * @name date-picker\n * @category ui\n * @description A button that shows a date, or a range of dates, and opens a calendar in a popover to choose it. Values are \"YYYY-MM-DD\" strings.\n * @when A date or a stay inside a form or a toolbar, where a calendar that is always open would take too much room. When the calendar is the main thing on the screen use calendar itself. Never the browser's native date input. Name the button by passing `aria-labelledby` with the id of its visible label: the chosen date is added to the name.\n * @example\n * // One day, uncontrolled, submitted with a form as \"date\"\n * <p id=\"date-label\">Date</p>\n * <DatePicker defaultValue=\"2026-11-14\" name=\"date\" locale=\"en-GB\">\n *   <DatePickerTrigger aria-labelledby=\"date-label\" placeholder=\"Choose a date\" />\n *   <DatePickerCalendar min=\"2026-11-01\" disabled={(day) => (closed.has(day) ? \"nothing open\" : false)} />\n * </DatePicker>\n *\n * // A range, controlled, two months where there is room\n * const [stay, setStay] = useState<CalendarRange>({ start: null, end: null });\n * <DateRangePicker value={stay} onValueChange={setStay} startName=\"from\" endName=\"to\" locale=\"en-GB\">\n *   <DatePickerTrigger aria-label=\"Stay\" placeholder=\"Arrival – departure\" />\n *   <DatePickerCalendar numberOfMonths={2} />\n * </DateRangePicker>\n */\nimport { CalendarDays } from \"lucide-react\";\nimport { createContext, use, useId, type ComponentProps, type ReactNode } from \"react\";\nimport { buttonVariants } from \"@/components/ui/button\";\nimport { Calendar, formatDay, type CalendarOptions, type CalendarRange } from \"@/components/ui/calendar\";\nimport { Popover, PopoverContent, PopoverTrigger } from \"@/components/ui/popover\";\nimport { cn } from \"@/lib/cn\";\nimport { useControllableState } from \"@/lib/use-controllable-state\";\n\ntype PickerValue = string | null | CalendarRange;\n\ntype PickerContextValue = {\n  mode: \"single\" | \"range\";\n  value: PickerValue;\n  setValue: (value: PickerValue) => void;\n  setOpen: (open: boolean) => void;\n  locale: string;\n  format: Intl.DateTimeFormatOptions;\n  disabled: boolean;\n  valueId: string;\n};\n\nconst PickerContext = createContext<PickerContextValue | null>(null);\n\nfunction usePicker(part: string): PickerContextValue {\n  const context = use(PickerContext);\n  if (!context) throw new Error(`<${part}> must be used inside <DatePicker> or <DateRangePicker>.`);\n  return context;\n}\n\ntype PickerCommon = {\n  /** Whether the calendar is open. Controlled. */\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  /** The language tag for the date on the button and, by default, for the calendar. Default \"en\". */\n  locale?: string;\n  /** How the date is written on the button: Intl.DateTimeFormat options. Default: the medium date style. */\n  format?: Intl.DateTimeFormatOptions;\n  disabled?: boolean;\n  /** The trigger and the calendar. Default: a DatePickerTrigger and a DatePickerCalendar. */\n  children?: ReactNode;\n};\n\nfunction PickerRoot({\n  mode,\n  value,\n  setValue,\n  open,\n  defaultOpen = false,\n  onOpenChange,\n  locale = \"en\",\n  format = { dateStyle: \"medium\" },\n  disabled = false,\n  children,\n  fields,\n}: PickerCommon & { mode: \"single\" | \"range\"; value: PickerValue; setValue: (value: PickerValue) => void; fields: ReactNode }) {\n  const valueId = useId();\n  const [isOpen, setOpen] = useControllableState({ value: open, defaultValue: defaultOpen, onChange: onOpenChange });\n  return (\n    <PickerContext value={{ mode, value, setValue, setOpen, locale, format, disabled, valueId }}>\n      <Popover open={isOpen} onOpenChange={setOpen}>\n        {children ?? (\n          <>\n            <DatePickerTrigger />\n            <DatePickerCalendar />\n          </>\n        )}\n      </Popover>\n      {fields}\n    </PickerContext>\n  );\n}\n\nexport type DatePickerProps = PickerCommon & {\n  /** The chosen day as \"YYYY-MM-DD\", or null. Controlled. */\n  value?: string | null;\n  defaultValue?: string | null;\n  onValueChange?: (value: string | null) => void;\n  /** The field's name in a form: the day is submitted with it (\"\" when none is chosen). */\n  name?: string;\n};\n\n/** One day. The calendar closes when a day is chosen, and focus goes back to the button. */\nexport function DatePicker({ value, defaultValue = null, onValueChange, name, ...props }: DatePickerProps) {\n  const [current, setCurrent] = useControllableState<string | null>({ value, defaultValue, onChange: onValueChange });\n  return (\n    <PickerRoot\n      mode=\"single\"\n      value={current}\n      setValue={(next) => setCurrent(next as string | null)}\n      fields={name ? <input type=\"hidden\" name={name} value={current ?? \"\"} disabled={props.disabled} /> : null}\n      {...props}\n    />\n  );\n}\n\nexport type DateRangePickerProps = PickerCommon & {\n  /** The chosen range. Either end is null until it is chosen. Controlled. */\n  value?: CalendarRange;\n  defaultValue?: CalendarRange;\n  onValueChange?: (value: CalendarRange) => void;\n  /** The names the start and the end are submitted with in a form. */\n  startName?: string;\n  endName?: string;\n};\n\nconst EMPTY_RANGE: CalendarRange = { start: null, end: null };\n\n/** A range of days. The first press sets the start, the second the end; then the calendar closes. */\nexport function DateRangePicker({ value, defaultValue = EMPTY_RANGE, onValueChange, startName, endName, ...props }: DateRangePickerProps) {\n  const [current, setCurrent] = useControllableState<CalendarRange>({ value, defaultValue, onChange: onValueChange });\n  return (\n    <PickerRoot\n      mode=\"range\"\n      value={current}\n      setValue={(next) => setCurrent(next as CalendarRange)}\n      fields={\n        <>\n          {startName ? <input type=\"hidden\" name={startName} value={current.start ?? \"\"} disabled={props.disabled} /> : null}\n          {endName ? <input type=\"hidden\" name={endName} value={current.end ?? \"\"} disabled={props.disabled} /> : null}\n        </>\n      }\n      {...props}\n    />\n  );\n}\n\nexport type DatePickerTriggerProps = Omit<ComponentProps<\"button\">, \"children\" | \"value\"> & {\n  /** Shown while nothing is chosen. */\n  placeholder?: string;\n  /** Shown in place of the end of a range that has only its start. */\n  openEnd?: string;\n};\n\n/**\n * The button. It shows the chosen date written for the locale, or the placeholder. With `aria-labelledby`\n * (the id of a visible label) its accessible name is the label followed by the date, so that a screen\n * reader says both; with neither that nor `aria-label`, its name is the date or the placeholder alone.\n */\nexport function DatePickerTrigger({ className, placeholder = \"Choose a date\", openEnd = \"…\", \"aria-labelledby\": labelledBy, \"aria-label\": ariaLabel, ...props }: DatePickerTriggerProps) {\n  const picker = usePicker(\"DatePickerTrigger\");\n  let text: string | null = null;\n  if (picker.mode === \"single\") {\n    const day = picker.value as string | null;\n    text = day ? formatDay(day, picker.locale, picker.format) : null;\n  } else {\n    const { start, end } = picker.value as CalendarRange;\n    if (start && end) {\n      const formatter = new Intl.DateTimeFormat(picker.locale, { ...picker.format, timeZone: \"UTC\" });\n      text = formatter.formatRange(Date.parse(`${start}T00:00:00Z`), Date.parse(`${end}T00:00:00Z`));\n    } else if (start) {\n      text = `${formatDay(start, picker.locale, picker.format)} – ${openEnd}`;\n    }\n  }\n  return (\n    <PopoverTrigger asChild>\n      <button\n        type=\"button\"\n        data-slot=\"date-picker-trigger\"\n        data-placeholder={text === null ? \"\" : undefined}\n        disabled={picker.disabled}\n        aria-labelledby={labelledBy ? `${labelledBy} ${picker.valueId}` : undefined}\n        aria-label={ariaLabel && !labelledBy ? `${ariaLabel}: ${text ?? placeholder}` : undefined}\n        className={cn(buttonVariants({ variant: \"outline\" }), \"max-w-full min-w-0 justify-start border-input font-normal data-placeholder:text-muted-foreground\", className)}\n        {...props}\n      >\n        <CalendarDays aria-hidden=\"true\" strokeWidth={1.5} className=\"text-muted-foreground\" />\n        {/* dir=\"auto\": a date keeps its own order inside a page of the other direction. */}\n        <span id={picker.valueId} dir=\"auto\" className=\"truncate\">\n          {text ?? placeholder}\n        </span>\n      </button>\n    </PopoverTrigger>\n  );\n}\n\nexport type DatePickerCalendarProps = CalendarOptions &\n  Pick<ComponentProps<typeof PopoverContent>, \"align\" | \"side\" | \"sideOffset\"> & {\n    className?: string;\n    /** The popover's accessible name. Translate it with the interface. */\n    label?: string;\n    /** Parts of your own inside the calendar, in place of its default layout (see calendar). */\n    children?: ReactNode;\n  };\n\n/**\n * The popover with the calendar in it. It takes the calendar's own options (`min`, `max`, `disabled`,\n * `marks`, `numberOfMonths`, `labels`, `weekStartsOn`, `today`). On opening, focus goes to the chosen day\n * (or today); Escape or a press outside closes it. Two months are shown only where the screen has room.\n */\nexport function DatePickerCalendar({ className, label = \"Calendar\", align = \"start\", side, sideOffset, children, locale, numberOfMonths = 1, ...options }: DatePickerCalendarProps) {\n  const picker = usePicker(\"DatePickerCalendar\");\n  const shared = { ...options, locale: locale ?? picker.locale, numberOfMonths };\n  return (\n    <PopoverContent\n      data-slot=\"date-picker-content\"\n      aria-label={label}\n      align={align}\n      side={side}\n      sideOffset={sideOffset}\n      className={cn(\"p-0\", numberOfMonths === 2 ? \"w-[min(40rem,calc(100vw-1rem))]\" : \"w-auto\", className)}\n      onOpenAutoFocus={(event) => {\n        const day = (event.currentTarget as HTMLElement).querySelector<HTMLElement>('[data-day][tabindex=\"0\"]');\n        if (!day) return;\n        event.preventDefault();\n        day.focus();\n      }}\n      onClick={(event) => {\n        // One day: pressing a day closes the calendar, also when it is the day already chosen.\n        const day = (event.target as HTMLElement).closest(\"[data-day]\");\n        if (picker.mode === \"single\" && day && day.getAttribute(\"aria-disabled\") !== \"true\") picker.setOpen(false);\n      }}\n    >\n      {picker.mode === \"single\" ? (\n        <Calendar\n          {...shared}\n          mode=\"single\"\n          value={picker.value as string | null}\n          onValueChange={picker.setValue}\n        >\n          {children}\n        </Calendar>\n      ) : (\n        <Calendar\n          {...shared}\n          mode=\"range\"\n          value={picker.value as CalendarRange}\n          onValueChange={(range) => {\n            picker.setValue(range);\n            if (range.start && range.end) picker.setOpen(false);\n          }}\n        >\n          {children}\n        </Calendar>\n      )}\n    </PopoverContent>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "A date or a stay inside a form or a toolbar, where a calendar that is always open would take too much room. When the calendar is the main thing on the screen use calendar itself. Never the browser's native date input. Name the button by passing `aria-labelledby` with the id of its visible label: the chosen date is added to the name.",
    "example": "// One day, uncontrolled, submitted with a form as \"date\"\n<p id=\"date-label\">Date</p>\n<DatePicker defaultValue=\"2026-11-14\" name=\"date\" locale=\"en-GB\">\n  <DatePickerTrigger aria-labelledby=\"date-label\" placeholder=\"Choose a date\" />\n  <DatePickerCalendar min=\"2026-11-01\" disabled={(day) => (closed.has(day) ? \"nothing open\" : false)} />\n</DatePicker>\n\n// A range, controlled, two months where there is room\nconst [stay, setStay] = useState<CalendarRange>({ start: null, end: null });\n<DateRangePicker value={stay} onValueChange={setStay} startName=\"from\" endName=\"to\" locale=\"en-GB\">\n  <DatePickerTrigger aria-label=\"Stay\" placeholder=\"Arrival – departure\" />\n  <DatePickerCalendar numberOfMonths={2} />\n</DateRangePicker>",
    "client": true
  }
}
