{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "calendar",
  "type": "registry:ui",
  "title": "Calendar",
  "description": "React calendar for Tailwind and shadcn: single, range or multiple dates, two months, preset ranges, a hover preview of the range and full arrow-key support.",
  "author": "MiniDev UI <https://ui.minidev.pro>",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://ui.minidev.pro/r/button.json",
    "utils"
  ],
  "files": [
    {
      "path": "registry/ui/calendar.tsx",
      "type": "registry:ui",
      "target": "components/ui/calendar.tsx",
      "content": "\"use client\"\nimport * as React from \"react\"\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\"\nimport { ChevronLeftIcon, ChevronRightIcon } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { Button } from \"@/components/ui/button\"\n\n/**\n * Calendar — a month grid for picking one date, a range or several dates, with arrow-key navigation and a range preview on hover.\n *\n * Use it inline or inside a Popover when people need to see the month: bookings, report periods, availability. Set `mode` to\n * \"single\", \"range\" or \"multiple\"; `value` is then a `Date`, a `{ from, to }` range or a `Date[]`. `months={2}` shows two months\n * side by side from the `sm` breakpoint, and `presets` adds a rail of ready-made ranges in range mode. Bound it with `min`, `max`\n * and `disabled`. Month and day names come from `Intl.DateTimeFormat` in `locale`. It has no frame, so it sits flush in a\n * Popover or card; on its own add `className=\"rounded-xl border border-border bg-surface shadow-raised\"`. For a typed native field use DatePicker or\n * DateRangePicker, for a date plus a time use DateTimePicker, and to show events use EventCalendar.\n *\n * @example\n * const [range, setRange] = React.useState<DateRange>()\n * <Calendar mode=\"range\" months={2} value={range} onValueChange={setRange} defaultMonth={new Date(2026, 10, 1)} />\n */\n\ntype CalendarMode = \"single\" | \"range\" | \"multiple\"\n/** A date range. `to` stays undefined until the second day is picked. */\ntype DateRange = { from: Date; to?: Date }\ntype CalendarValue<M extends CalendarMode> = M extends \"range\" ? DateRange : M extends \"multiple\" ? Date[] : Date\ntype CalendarPreset = { label: string; range: DateRange }\n\ntype CalendarProps<M extends CalendarMode = \"single\"> = {\n  /** \"single\" picks one day, \"range\" a from/to pair, \"multiple\" any set of days. */\n  mode?: M\n  /** Controlled selection: a Date, a `{ from, to }` range or a Date[], to match `mode`. */\n  value?: CalendarValue<M> | null\n  /** Starting selection when uncontrolled. */\n  defaultValue?: CalendarValue<M> | null\n  /** Fires with the whole new selection on every pick. */\n  onValueChange?: (value: CalendarValue<M>) => void\n  /** Months shown at once. Two sit side by side from the sm breakpoint and stack on phones. */\n  months?: 1 | 2\n  /** The first month shown. Defaults to the selection's month, else the current month. */\n  defaultMonth?: Date\n  /** Earliest day that can be picked. */\n  min?: Date\n  /** Latest day that can be picked. */\n  max?: Date\n  /** Days that cannot be picked: a list of dates or a test function. */\n  disabled?: Date[] | ((date: Date) => boolean)\n  /** First column of the week: 0 is Sunday, 1 is Monday. */\n  weekStartsOn?: 0 | 1\n  /** BCP 47 locale for month, weekday and day labels. Fixed by default so server and client render the same text. */\n  locale?: string\n  /** Ready-made ranges shown in a rail beside the grid. Range mode only. */\n  presets?: CalendarPreset[]\n  /** Show the days of the previous and next month in muted text. */\n  showOutsideDays?: boolean\n  className?: string\n}\n\nconst SNAPPY = { type: \"spring\", bounce: 0.18, duration: 0.42 } as const\n// Exit is faster than entry.\nconst SLIDE = { enter: (d: number) => ({ x: d * 24, opacity: 0 }), center: { x: 0, opacity: 1 }, exit: (d: number) => ({ x: d * -24, opacity: 0, transition: { duration: 0.16 } }) }\n// Date math on local midnights. k() is a sortable day key (months count from 0): 20261014 is 14 Nov 2026.\nconst at = (y: number, m: number, d: number) => new Date(y, m, d)\nconst addDays = (d: Date, n: number) => at(d.getFullYear(), d.getMonth(), d.getDate() + n)\nconst day = (d: Date) => addDays(d, 0)\nconst firstOf = (d: Date) => at(d.getFullYear(), d.getMonth(), 1)\n// Clamp the day so 31 Jan plus one month is 28 Feb, not 3 Mar.\nconst addMonths = (d: Date, n: number) => at(d.getFullYear(), d.getMonth() + n, Math.min(d.getDate(), at(d.getFullYear(), d.getMonth() + n + 1, 0).getDate()))\nconst k = (d: Date) => d.getFullYear() * 10000 + d.getMonth() * 100 + d.getDate()\nconst fromK = (n: number) => at(Math.floor(n / 10000), Math.floor(n / 100) % 100, n % 100)\nconst monthDiff = (a: Date, b: Date) => (b.getFullYear() - a.getFullYear()) * 12 + b.getMonth() - a.getMonth()\nconst noSubscribe = () => () => {}\n\nfunction Calendar<M extends CalendarMode = \"single\">({\n  mode = \"single\" as M,\n  value,\n  defaultValue = null,\n  onValueChange,\n  months = 1,\n  defaultMonth,\n  min,\n  max,\n  disabled,\n  weekStartsOn = 0,\n  locale = \"en-US\",\n  presets,\n  showOutsideDays = true,\n  className,\n}: CalendarProps<M>) {\n  const reduce = useReducedMotion()\n  const root = React.useRef<HTMLDivElement>(null)\n  const pendingFocus = React.useRef(false)\n  // Today is client-only: null on the server and during hydration, so static HTML never disagrees with the browser.\n  const today = React.useSyncExternalStore(noSubscribe, () => k(new Date()), () => null)\n  const [inner, setInner] = React.useState(defaultValue)\n  const [view, setView] = React.useState<Date | null>(null)\n  const [dir, setDir] = React.useState(1)\n  const [focus, setFocus] = React.useState<number | null>(null)\n  const [hover, setHover] = React.useState<number | null>(null)\n\n  const fmt = React.useMemo(() => {\n    const f = (o: Intl.DateTimeFormatOptions) => new Intl.DateTimeFormat(locale, o)\n    // 4 Jan 2026 is a Sunday.\n    const weekdays = Array.from({ length: 7 }, (_, i) => at(2026, 0, 4 + ((i + weekStartsOn) % 7)))\n    return { caption: f({ month: \"long\", year: \"numeric\" }), full: f({ dateStyle: \"full\" }), num: f({ day: \"numeric\" }), weekdays: weekdays.map((d) => [f({ weekday: \"short\" }).format(d), f({ weekday: \"long\" }).format(d)]) }\n  }, [locale, weekStartsOn])\n\n  const sel = value !== undefined ? value : inner\n  const single = mode === \"single\" && sel instanceof Date ? sel : null\n  const range = mode === \"range\" && sel && !(sel instanceof Date) && !Array.isArray(sel) ? (sel as DateRange) : null\n  const multi = mode === \"multiple\" && Array.isArray(sel) ? sel : []\n  const anchor = defaultMonth ?? single ?? range?.from ?? multi[0] ?? (today ? fromK(today) : null)\n  const first = view ?? (anchor ? firstOf(anchor) : null)\n\n  React.useEffect(() => {\n    if (!pendingFocus.current || !first) return\n    pendingFocus.current = false\n    root.current?.querySelector<HTMLElement>(`[data-view=\"${k(first)}\"] [data-day=\"${focus}\"]:not([data-outside])`)?.focus()\n  })\n\n  const off = (d: Date) =>\n    (min && d < day(min)) || (max && d > day(max)) || (Array.isArray(disabled) ? disabled.some((x) => k(x) === k(d)) : disabled?.(d)) || false\n\n  if (!first) return <div data-slot=\"calendar\" className={cn(\"min-h-80 min-w-64 p-3\", className)} />\n\n  const panels = Array.from({ length: months }, (_, i) => addMonths(first, i))\n  const inView = (key: number | null | undefined) => key != null && key >= k(first) && key < k(addMonths(first, months))\n  const tabKey = [focus, single && k(single), range && k(range.from), multi[0] && k(multi[0]), today].find(inView) ?? k(first)\n\n  const shiftTo = (n: Date) => {\n    setDir(monthDiff(first, n) < 0 ? -1 : 1)\n    setView(n)\n  }\n  const reveal = (d: Date) => {\n    const diff = monthDiff(first, d)\n    if (diff < 0) shiftTo(firstOf(d))\n    else if (diff >= months) shiftTo(addMonths(firstOf(d), 1 - months))\n  }\n  const moveFocus = (d: Date) => {\n    if (min && d < min) d = day(min)\n    if (max && d > max) d = day(max)\n    reveal(d)\n    setFocus(k(d))\n    pendingFocus.current = true\n  }\n  const commit = (next: CalendarValue<M>) => {\n    if (value === undefined) setInner(next)\n    onValueChange?.(next)\n  }\n  const pick = (d: Date) => {\n    if (off(d)) return\n    setFocus(k(d))\n    reveal(d)\n    if (mode === \"range\") commit((!range || range.to || k(d) < k(range.from) ? { from: d } : { from: range.from, to: d }) as CalendarValue<M>)\n    else if (mode === \"multiple\") commit((multi.some((x) => k(x) === k(d)) ? multi.filter((x) => k(x) !== k(d)) : [...multi, d].sort((a, b) => +a - +b)) as CalendarValue<M>)\n    else commit(d as CalendarValue<M>)\n  }\n  const onKey = (e: React.KeyboardEvent, d: Date) => {\n    const wd = (d.getDay() - weekStartsOn + 7) % 7\n    const step = e.shiftKey ? 12 : 1 // Shift+PageUp/PageDown moves a year\n    const to: Record<string, Date> = { ArrowLeft: addDays(d, -1), ArrowRight: addDays(d, 1), ArrowUp: addDays(d, -7), ArrowDown: addDays(d, 7), Home: addDays(d, -wd), End: addDays(d, 6 - wd), PageUp: addMonths(d, -step), PageDown: addMonths(d, step) }\n    if (!to[e.key]) return\n    e.preventDefault()\n    moveFocus(to[e.key])\n  }\n\n  // The band runs from `lo` to `hi`: the committed range, or a preview from `from` to the hovered or focused day.\n  const preview = range && !range.to && hover != null\n  const lo = range ? (preview ? Math.min(k(range.from), hover) : range.to && k(range.from)) : null\n  const hi = range ? (preview ? Math.max(k(range.from), hover) : range.to && k(range.to)) : null\n  const prevOff = min ? k(first) <= k(firstOf(min)) : false\n  const nextOff = max ? monthDiff(first, max) < months : false\n  const captions = panels.map((m) => fmt.caption.format(m))\n\n  return (\n    <div\n      ref={root}\n      data-slot=\"calendar\"\n      className={cn(\"inline-flex flex-col gap-3 p-3 text-fg sm:flex-row\", className)}\n    >\n      {mode === \"range\" && presets?.length ? (\n        <div role=\"group\" aria-label=\"Preset ranges\" className=\"flex w-0 min-w-full gap-1 overflow-x-auto border-b border-border pb-2 sm:w-36 sm:min-w-0 sm:flex-col sm:overflow-visible sm:border-r sm:border-b-0 sm:pr-3 sm:pb-0\">\n          {presets.map((p) => {\n            const on = !!range?.to && k(range.from) === k(p.range.from) && !!p.range.to && k(range.to) === k(p.range.to)\n            return (\n              <button\n                key={p.label}\n                type=\"button\"\n                aria-pressed={on}\n                onClick={() => {\n                  commit(p.range as CalendarValue<M>)\n                  setFocus(k(p.range.from))\n                  if (!inView(k(p.range.from))) shiftTo(firstOf(p.range.from))\n                }}\n                className={cn(\n                  \"h-8 shrink-0 rounded-lg px-2.5 text-left text-[0.8125rem] whitespace-nowrap outline-none transition-[background-color,color] duration-[70ms] ease-hairline focus-visible:ring-2 focus-visible:ring-accent\",\n                  on ? \"bg-accent-soft font-medium text-accent-fg\" : \"text-fg-muted hover:bg-sunken hover:text-fg\"\n                )}\n              >\n                {p.label}\n              </button>\n            )\n          })}\n        </div>\n      ) : null}\n      <div className=\"relative\">\n        <span aria-live=\"polite\" className=\"sr-only\">{captions.join(\", \")}</span>\n        <Button variant=\"ghost\" size=\"icon-sm\" aria-label=\"Previous month\" disabled={prevOff} onClick={() => shiftTo(addMonths(first, -1))} className=\"absolute top-0 left-0 z-10\">\n          <ChevronLeftIcon />\n        </Button>\n        <Button variant=\"ghost\" size=\"icon-sm\" aria-label=\"Next month\" disabled={nextOff} onClick={() => shiftTo(addMonths(first, 1))} className=\"absolute top-0 right-0 z-10\">\n          <ChevronRightIcon />\n        </Button>\n        <AnimatePresence initial={false} mode=\"popLayout\" custom={dir}>\n          <motion.div\n            key={k(first)}\n            data-view={k(first)}\n            custom={dir}\n            variants={SLIDE}\n            initial=\"enter\"\n            animate=\"center\"\n            exit={reduce ? { opacity: 0, transition: { duration: 0 } } : \"exit\"}\n            transition={reduce ? { duration: 0 } : SNAPPY}\n            className=\"flex flex-col gap-4 sm:flex-row sm:gap-6\"\n          >\n            {panels.map((m, pi) => {\n              const start = addDays(m, -((m.getDay() - weekStartsOn + 7) % 7))\n              return (\n                <div key={pi} className=\"flex flex-col gap-2\">\n                  <div aria-hidden className=\"flex h-8 items-center justify-center text-sm font-medium\">{captions[pi]}</div>\n                  <div\n                    role=\"grid\"\n                    aria-label={captions[pi]}\n                    aria-multiselectable={mode !== \"single\" || undefined}\n                    onPointerLeave={() => setHover(null)}\n                    className=\"flex flex-col gap-1\"\n                  >\n                    <div role=\"row\" className=\"grid grid-cols-7\">\n                      {fmt.weekdays.map(([s, l]) => (\n                        <div key={l} role=\"columnheader\" aria-label={l} className=\"flex h-8 items-center justify-center text-xs text-fg-subtle\">{s}</div>\n                      ))}\n                    </div>\n                    {Array.from({ length: 6 }, (_, w) => (\n                      <div key={w} role=\"row\" className=\"grid grid-cols-7\">\n                        {Array.from({ length: 7 }, (_, col) => {\n                          const d = addDays(start, w * 7 + col)\n                          const kd = k(d)\n                          const outside = d.getMonth() !== m.getMonth()\n                          if (outside && !showOutsideDays) return <div key={col} role=\"gridcell\" className=\"size-9\" />\n                          const picked = single ? kd === k(single) : range ? kd === k(range.from) || (!!range.to && kd === k(range.to)) : multi.some((x) => k(x) === kd)\n                          const band = lo != null && hi != null && lo !== hi && kd >= lo && kd <= hi\n                          const isOff = off(d)\n                          return (\n                            <div key={col} role=\"gridcell\" aria-selected={picked || (band && !preview) || undefined} className=\"relative\">\n                              {band ? (\n                                <span aria-hidden className={cn(\"absolute inset-y-0\", kd === lo ? \"right-0 left-1/2\" : kd === hi ? \"right-1/2 left-0\" : \"inset-x-0\", preview ? \"bg-accent/5\" : \"bg-accent/10\", col === 0 && \"rounded-l-lg\", col === 6 && \"rounded-r-lg\")} />\n                              ) : null}\n                              <button\n                                type=\"button\"\n                                data-day={kd}\n                                data-outside={outside || undefined}\n                                tabIndex={!outside && kd === tabKey ? 0 : -1}\n                                aria-label={fmt.full.format(d)}\n                                aria-current={kd === today ? \"date\" : undefined}\n                                aria-disabled={isOff || undefined}\n                                onClick={() => pick(d)}\n                                onKeyDown={(e) => onKey(e, d)}\n                                onPointerEnter={() => setHover(kd)}\n                                onFocus={() => setHover(kd)}\n                                className={cn(\n                                  \"relative flex size-9 items-center justify-center rounded-lg text-sm tabular-nums outline-none transition-[background-color,color] duration-[70ms] ease-hairline\",\n                                  \"focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-surface\",\n                                  picked ? \"bg-accent font-medium text-on-accent shadow-ink hover:bg-accent-hover\" : band ? \"hover:bg-accent/15\" : \"hover:bg-sunken\",\n                                  !picked && outside && \"text-fg-subtle\",\n                                  !picked && kd === today && \"font-semibold text-accent-fg\",\n                                  kd === today && \"after:absolute after:bottom-1 after:size-1 after:rounded-full after:bg-current\",\n                                  preview && kd === hover && !picked && \"ring-1 ring-accent-line ring-inset\",\n                                  isOff && \"cursor-not-allowed text-fg-subtle opacity-50 hover:bg-transparent\"\n                                )}\n                              >\n                                {fmt.num.format(d)}\n                              </button>\n                            </div>\n                          )\n                        })}\n                      </div>\n                    ))}\n                  </div>\n                </div>\n              )\n            })}\n          </motion.div>\n        </AnimatePresence>\n      </div>\n    </div>\n  )\n}\n\nexport { Calendar }\nexport type { CalendarProps, CalendarPreset, DateRange }\n"
    }
  ],
  "categories": [
    "forms"
  ],
  "docs": "import { Calendar } from \"@/components/ui/calendar\"\nNeeds the MiniDev tokens stylesheet (https://ui.minidev.pro/r/styles.css) imported after Tailwind.\nDocs: https://ui.minidev.pro/docs/calendar · Markdown for agents: https://ui.minidev.pro/docs/calendar.md",
  "meta": {
    "tier": "free",
    "kind": "ui",
    "category": "forms",
    "import": "@/components/ui/calendar",
    "npmImport": "minidev-ui-kit/ui/calendar",
    "exports": [
      "Calendar"
    ],
    "related": [
      "popover",
      "date-picker",
      "date-range-picker",
      "date-time-picker",
      "event-calendar"
    ],
    "docsUrl": "https://ui.minidev.pro/docs/calendar",
    "markdownUrl": "https://ui.minidev.pro/docs/calendar.md",
    "keywords": [
      "shadcn calendar",
      "react date range picker",
      "react calendar component"
    ]
  }
}
