{
  "schemaVersion": 1,
  "target": "react",
  "name": "date-picker",
  "version": "0.1.1",
  "category": "data",
  "title": "Date Picker",
  "description": "A button that opens the Calendar in a Popover, for a single date or a range, with presets such as Last 7 days.",
  "tags": [
    "date",
    "picker",
    "range",
    "form",
    "popover"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {},
  "registryDependencies": [
    {
      "name": "calendar",
      "version": "0.1.0"
    },
    {
      "name": "popover",
      "version": "0.2.2"
    },
    {
      "name": "button",
      "version": "0.2.1"
    }
  ],
  "docsPath": "/components/date-picker",
  "props": [
    {
      "name": "mode",
      "type": "\"single\" | \"range\"",
      "default": "\"single\"",
      "description": "One date, or a range with a Done footer."
    },
    {
      "name": "value / defaultValue / onValueChange",
      "type": "Date | null, or { from, to }",
      "description": "Selection, controlled or not."
    },
    {
      "name": "presets",
      "type": "{ label, value }[]",
      "description": "Quick picks beside the calendar. value can be a function of today. singleDatePresets and rangeDatePresets ship as defaults."
    },
    {
      "name": "placeholder",
      "type": "string",
      "description": "Trigger text when nothing is picked."
    },
    {
      "name": "numberOfMonths",
      "type": "1 | 2",
      "default": "2",
      "description": "Range mode on wide screens; narrow screens always show one month."
    },
    {
      "name": "disabled / minDate / maxDate / weekStartsOn / locale",
      "type": "Calendar props",
      "description": "Passed to the Calendar."
    },
    {
      "name": "id / aria-label / className / triggerDisabled",
      "type": "string / string / string / boolean",
      "description": "Trigger attributes. Pair id with a <label htmlFor>."
    },
    {
      "name": "align",
      "type": "\"start\" | \"center\" | \"end\"",
      "default": "\"start\"",
      "description": "Popover alignment against the trigger."
    }
  ],
  "accessibility": [
    "The trigger is a Base UI Popover trigger (aria-expanded, aria-controls); its text is the picked date or the placeholder.",
    "Opening focuses the selected day (or today) in the grid, Escape closes, and focus returns to the trigger.",
    "Presets are toggle buttons with aria-pressed showing which one matches the current value.",
    "In range mode a polite live summary reads the span and day count (\"Nov 8 to 14, 7 days\"); Done stays disabled until an end date is picked.",
    "Dates are formatted with Intl.DateTimeFormat, including formatRange for ranges."
  ],
  "states": [
    "empty",
    "single picked",
    "range picked",
    "picking end date",
    "open"
  ],
  "files": [
    {
      "path": "ui/date-picker.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useMemo, useState } from \"react\";\nimport { Button } from \"./button\";\nimport { addDays, addMonths, Calendar, type CalendarProps, countDays, type DateRange, isSameDay, startOfDay } from \"./calendar\";\nimport { Popover, PopoverContent, PopoverTrigger } from \"./popover\";\n\nfunction cx(...classes: (string | false | null | undefined)[]) {\n  return classes.filter(Boolean).join(\" \");\n}\n\nexport interface DatePreset<Value> {\n  label: string;\n  /** The value, or a function that computes it from today when the preset is picked. */\n  value: Value | ((today: Date) => Value);\n}\n\n/** Today, Tomorrow, and In a week. */\nexport const singleDatePresets: DatePreset<Date>[] = [\n  { label: \"Today\", value: (today) => today },\n  { label: \"Tomorrow\", value: (today) => addDays(today, 1) },\n  { label: \"In a week\", value: (today) => addDays(today, 7) },\n];\n\n/** Today, Last 7 days, Last 30 days, This month, and Last month. */\nexport const rangeDatePresets: DatePreset<DateRange>[] = [\n  { label: \"Today\", value: (today) => ({ from: today, to: today }) },\n  { label: \"Last 7 days\", value: (today) => ({ from: addDays(today, -6), to: today }) },\n  { label: \"Last 30 days\", value: (today) => ({ from: addDays(today, -29), to: today }) },\n  { label: \"This month\", value: (today) => ({ from: new Date(today.getFullYear(), today.getMonth(), 1), to: today }) },\n  {\n    label: \"Last month\",\n    value: (today) => {\n      const start = addMonths(new Date(today.getFullYear(), today.getMonth(), 1), -1);\n      return { from: start, to: new Date(start.getFullYear(), start.getMonth() + 1, 0) };\n    },\n  },\n];\n\nfunction useWide() {\n  const [wide, setWide] = useState(true);\n  useEffect(() => {\n    const query = window.matchMedia(\"(min-width: 640px)\");\n    const update = () => setWide(query.matches);\n    update();\n    query.addEventListener(\"change\", update);\n    return () => query.removeEventListener(\"change\", update);\n  }, []);\n  return wide;\n}\n\ntype Shared = Pick<CalendarProps, \"disabled\" | \"minDate\" | \"maxDate\" | \"weekStartsOn\" | \"locale\"> & {\n  /** Text in the trigger when nothing is picked. */\n  placeholder?: string;\n  /** Turns the trigger off. */\n  triggerDisabled?: boolean;\n  /** Accessible name for the trigger when no visible label is next to it. */\n  \"aria-label\"?: string;\n  id?: string;\n  /** Classes for the trigger button. */\n  className?: string;\n  /** Popover alignment against the trigger. */\n  align?: \"start\" | \"center\" | \"end\";\n};\n\nexport interface DatePickerSingleProps extends Shared {\n  mode?: \"single\";\n  value?: Date | null;\n  defaultValue?: Date | null;\n  onValueChange?: (value: Date | null) => void;\n  /** Quick picks beside the calendar. Pass `singleDatePresets` for the defaults. */\n  presets?: DatePreset<Date>[];\n}\n\nexport interface DatePickerRangeProps extends Shared {\n  mode: \"range\";\n  value?: DateRange;\n  defaultValue?: DateRange;\n  onValueChange?: (value: DateRange) => void;\n  /** Quick picks beside the calendar. Pass `rangeDatePresets` for the defaults. */\n  presets?: DatePreset<DateRange>[];\n  /** Months shown on wide screens. Narrow screens always show one. */\n  numberOfMonths?: 1 | 2;\n}\n\nexport type DatePickerProps = DatePickerSingleProps | DatePickerRangeProps;\n\nfunction resolve<Value>(preset: DatePreset<Value>, today: Date): Value {\n  return typeof preset.value === \"function\" ? (preset.value as (today: Date) => Value)(today) : preset.value;\n}\n\n/**\n * A button that opens a `Calendar` in a `Popover`, for one date or a range, with optional presets.\n * A single date closes the popover when picked; a range stays open until Done, so either end can\n * be adjusted.\n */\nexport function DatePicker(props: DatePickerProps) {\n  const {\n    mode = \"single\",\n    placeholder = mode === \"range\" ? \"Pick a date range\" : \"Pick a date\",\n    triggerDisabled,\n    disabled,\n    minDate,\n    maxDate,\n    weekStartsOn,\n    locale,\n    id,\n    className,\n    align = \"start\",\n  } = props;\n  const wide = useWide();\n  const [open, setOpen] = useState(false);\n  const [today, setToday] = useState(() => startOfDay(new Date()));\n  const [innerSingle, setInnerSingle] = useState<Date | null>(mode === \"single\" ? ((props as DatePickerSingleProps).defaultValue ?? null) : null);\n  const [innerRange, setInnerRange] = useState<DateRange>(\n    mode === \"range\" ? ((props as DatePickerRangeProps).defaultValue ?? { from: null, to: null }) : { from: null, to: null },\n  );\n  const single = mode === \"single\" ? ((props as DatePickerSingleProps).value !== undefined ? (props as DatePickerSingleProps).value ?? null : innerSingle) : null;\n  const range = mode === \"range\" ? ((props as DatePickerRangeProps).value ?? innerRange) : { from: null, to: null };\n  const [month, setMonth] = useState<Date>(() => (mode === \"single\" ? single : range.from) ?? today);\n\n  const dateFormat = useMemo(() => new Intl.DateTimeFormat(locale, { month: \"short\", day: \"numeric\", year: \"numeric\" }), [locale]);\n  const summaryFormat = useMemo(() => new Intl.DateTimeFormat(locale, { month: \"short\", day: \"numeric\" }), [locale]);\n\n  function setSingle(value: Date | null) {\n    const p = props as DatePickerSingleProps;\n    if (p.value === undefined) setInnerSingle(value);\n    p.onValueChange?.(value);\n  }\n\n  function setRange(value: DateRange) {\n    const p = props as DatePickerRangeProps;\n    if (p.value === undefined) setInnerRange(value);\n    p.onValueChange?.(value);\n  }\n\n  function onOpenChange(next: boolean) {\n    if (next) {\n      // Re-read the clock and show the month of the current value each time the picker opens.\n      const now = startOfDay(new Date());\n      setToday(now);\n      setMonth((mode === \"single\" ? single : range.from) ?? now);\n    }\n    setOpen(next);\n  }\n\n  let label: string | null = null;\n  if (mode === \"single\" && single) label = dateFormat.format(single);\n  if (mode === \"range\" && range.from) label = range.to ? dateFormat.formatRange(range.from, range.to) : `${dateFormat.format(range.from)} to …`;\n\n  const presets = (props.presets ?? []) as DatePreset<Date | DateRange>[];\n  const months = mode === \"range\" && wide ? ((props as DatePickerRangeProps).numberOfMonths ?? 2) : 1;\n\n  function isActive(preset: DatePreset<Date | DateRange>) {\n    const value = resolve(preset, today);\n    if (value instanceof Date) return isSameDay(value, single);\n    return isSameDay(value.from, range.from) && isSameDay(value.to, range.to);\n  }\n\n  function pick(preset: DatePreset<Date | DateRange>) {\n    const value = resolve(preset, today);\n    if (value instanceof Date) {\n      setSingle(value);\n      setOpen(false);\n    } else {\n      setRange(value);\n      // Show the end of the range; with two months, it lands in the right-hand one.\n      const end = value.to ?? value.from;\n      if (end) setMonth(months === 2 ? addMonths(end, -1) : end);\n    }\n  }\n\n  const calendarShared = { disabled, minDate, maxDate, weekStartsOn, locale, today, month, onMonthChange: setMonth, autoFocus: true };\n\n  return (\n    <Popover open={open} onOpenChange={onOpenChange}>\n      <PopoverTrigger\n        disabled={triggerDisabled}\n        id={id}\n        aria-label={props[\"aria-label\"] ? `${props[\"aria-label\"]}${label ? `, ${label}` : \"\"}` : undefined}\n        render={<Button variant=\"secondary\" />}\n        className={cx(\n          \"min-w-56 justify-start px-3 font-normal tabular-nums data-[popup-open]:border-zinc-950/20 dark:data-[popup-open]:border-white/25\",\n          className,\n        )}\n      >\n        <CalendarGlyph />\n        <span className={cx(\"truncate\", !label && \"text-zinc-500\")}>{label ?? placeholder}</span>\n      </PopoverTrigger>\n      <PopoverContent align={align} className=\"w-auto max-w-[calc(100vw-2rem)] p-0\">\n        <div className={cx(\"flex\", presets.length > 0 && \"max-sm:flex-col\")}>\n          {presets.length > 0 ? (\n            <div\n              role=\"group\"\n              aria-label=\"Presets\"\n              className={cx(\n                \"flex gap-0.5 border-zinc-950/[0.07] p-2 dark:border-white/[0.08]\",\n                // On narrow screens the row scrolls instead of widening the popover past the calendar.\n                \"max-sm:w-0 max-sm:min-w-full max-sm:overflow-x-auto max-sm:border-b sm:w-36 sm:flex-col sm:border-r\",\n              )}\n            >\n              {presets.map((preset) => {\n                const active = isActive(preset);\n                return (\n                  <button\n                    key={preset.label}\n                    type=\"button\"\n                    aria-pressed={active}\n                    onClick={() => pick(preset)}\n                    className={cx(\n                      \"h-8 shrink-0 rounded-md px-2.5 text-left text-sm whitespace-nowrap\",\n                      \"transition-[background-color,color] duration-150 ease-out motion-reduce:transition-none\",\n                      \"focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-sky-500\",\n                      active\n                        ? \"bg-zinc-950/[0.06] font-medium text-zinc-950 dark:bg-white/[0.09] dark:text-white\"\n                        : \"text-zinc-600 hover:bg-zinc-950/[0.04] hover:text-zinc-950 dark:text-zinc-400 dark:hover:bg-white/[0.06] dark:hover:text-white\",\n                    )}\n                  >\n                    {preset.label}\n                  </button>\n                );\n              })}\n            </div>\n          ) : null}\n          <div className=\"grid\">\n            <div className=\"p-3\">\n              {mode === \"single\" ? (\n                <Calendar\n                  {...calendarShared}\n                  mode=\"single\"\n                  value={single}\n                  onValueChange={(value) => {\n                    setSingle(value);\n                    setOpen(false);\n                  }}\n                />\n              ) : (\n                <Calendar {...calendarShared} mode=\"range\" value={range} onValueChange={setRange} numberOfMonths={months as 1 | 2} />\n              )}\n            </div>\n            {mode === \"range\" ? (\n              <div className=\"flex items-center gap-2 border-t border-zinc-950/[0.07] py-2.5 ps-4 pe-2.5 dark:border-white/[0.08]\">\n                <p aria-live=\"polite\" className=\"min-w-0 truncate text-xs text-zinc-500 tabular-nums\">\n                  {range.from && range.to\n                    ? `${summaryFormat.formatRange(range.from, range.to)}, ${countDays(range.from, range.to)} ${countDays(range.from, range.to) === 1 ? \"day\" : \"days\"}`\n                    : range.from\n                      ? \"Pick an end date\"\n                      : \"Pick a start date\"}\n                </p>\n                <Button variant=\"ghost\" size=\"sm\" className=\"ms-auto\" disabled={!range.from} onClick={() => setRange({ from: null, to: null })}>\n                  Clear\n                </Button>\n                <Button size=\"sm\" disabled={!!range.from && !range.to} onClick={() => setOpen(false)}>\n                  Done\n                </Button>\n              </div>\n            ) : null}\n          </div>\n        </div>\n      </PopoverContent>\n    </Popover>\n  );\n}\n\nfunction CalendarGlyph() {\n  return (\n    <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" fill=\"none\" className=\"text-zinc-500\">\n      <rect x=\"2.25\" y=\"3.25\" width=\"11.5\" height=\"10.5\" rx=\"2\" stroke=\"currentColor\" strokeWidth=\"1.5\" />\n      <path d=\"M2.5 6.75h11M5.5 1.75v3M10.5 1.75v3\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" />\n    </svg>\n  );\n}\n"
    }
  ],
  "example": {
    "content": "\"use client\";\n\nimport { DatePicker, rangeDatePresets, singleDatePresets } from \"@/components/shaken/ui/date-picker\";\n\nexport default function DatePickerExample() {\n  return (\n    <div className=\"flex flex-wrap items-end justify-center gap-6\">\n      <div className=\"grid gap-1.5\">\n        <label htmlFor=\"usage-range\" className=\"text-sm font-medium text-zinc-950 dark:text-white\">\n          Usage period\n        </label>\n        <DatePicker id=\"usage-range\" mode=\"range\" presets={rangeDatePresets} />\n      </div>\n      <div className=\"grid gap-1.5\">\n        <label htmlFor=\"key-expiry\" className=\"text-sm font-medium text-zinc-950 dark:text-white\">\n          Key expires on\n        </label>\n        <DatePicker id=\"key-expiry\" presets={singleDatePresets} minDate={new Date()} />\n      </div>\n    </div>\n  );\n}\n"
  }
}
