{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "calendar",
  "title": "Calendar",
  "description": "A day or a range. Arrow keys move by day, Page Up and Down by month.",
  "registryDependencies": [
    "https://creance.pages.dev/r/creance-base.json"
  ],
  "files": [
    {
      "path": "calendar/Calendar.css",
      "content": ".cr-calendar {\n  display: inline-grid;\n  gap: var(--cr-space-2);\n  padding: 18px;\n  border-radius: var(--cr-radius-xl);\n  color: var(--cr-color-text-default);\n  font-family: var(--cr-font-family-sans);\n}\n\n.cr-calendar__head {\n  display: flex;\n  align-items: center;\n  justify-content: space-between;\n  padding: 0 var(--cr-space-1);\n}\n\n.cr-calendar__title {\n  margin: 0;\n  font-size: var(--cr-font-size-md);\n  font-weight: var(--cr-font-weight-semibold);\n}\n\n.cr-calendar__nav {\n  display: grid;\n  place-items: center;\n  width: 32px;\n  height: 32px;\n  border: 0;\n  border-radius: var(--cr-radius-full);\n  background: transparent;\n  color: inherit;\n  cursor: pointer;\n}\n\n.cr-calendar__nav:hover {\n  background: var(--cr-color-bg-subtle);\n}\n\n.cr-calendar__grid {\n  border-collapse: collapse;\n}\n\n.cr-calendar__grid th {\n  height: 32px;\n  color: var(--cr-color-text-muted);\n  font-size: var(--cr-font-size-xs);\n  font-weight: var(--cr-font-weight-semibold);\n}\n\n.cr-calendar__grid td {\n  padding: 1px 0;\n}\n\n.cr-calendar__day {\n  display: grid;\n  place-items: center;\n  width: 40px;\n  height: 40px;\n  border: 0;\n  border-radius: var(--cr-radius-full);\n  background: transparent;\n  color: inherit;\n  font: inherit;\n  font-size: var(--cr-font-size-md);\n  font-variant-numeric: tabular-nums;\n  cursor: pointer;\n}\n\n.cr-calendar__day:hover {\n  background: var(--cr-color-bg-subtle);\n}\n\n.cr-calendar__day[data-outside] {\n  color: var(--cr-color-text-muted);\n}\n\n.cr-calendar__day[aria-current='date'] {\n  box-shadow: inset 0 0 0 1.5px var(--cr-color-ink-bg);\n}\n\n.cr-calendar__day[data-between] {\n  border-radius: 0;\n  background: var(--cr-color-ink-subtle);\n}\n\n.cr-calendar__day[data-selected] {\n  background: var(--cr-color-ink-bg);\n  color: var(--cr-color-ink-fg);\n  font-weight: var(--cr-font-weight-semibold);\n}\n\n.cr-calendar__day[aria-disabled] {\n  cursor: not-allowed;\n  opacity: 0.35;\n  text-decoration: line-through;\n}\n\n.cr-calendar__day:focus-visible,\n.cr-calendar__nav:focus-visible {\n  outline: var(--cr-focus-width) solid var(--cr-color-focus);\n  outline-offset: var(--cr-focus-offset);\n}\n",
      "type": "registry:file",
      "target": "@components/creance/calendar/Calendar.css"
    },
    {
      "path": "calendar/Calendar.tsx",
      "content": "'use client';\n\nimport { useEffect, useMemo, useRef, useState, type KeyboardEvent } from 'react';\nimport { cx } from '@/lib/creance/cx';\nimport { ChevronIcon, ChevronLeftIcon } from '@/lib/creance/icons';\nimport { addDays, addMonths, compare, monthGrid, sameDay, toDay } from './dates';\nimport { useNow } from '@/lib/creance/useNow';\nimport './Calendar.css';\n\nexport interface DateRange {\n  start: Date;\n  end: Date;\n}\n\ninterface Base {\n  /** Which month shows first. Defaults to the selection, or today. */\n  defaultMonth?: Date;\n  /** 0 Sunday, 1 Monday. Default 1. */\n  weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;\n  /** BCP 47 locale for month and weekday names. Defaults to the browser's. */\n  locale?: string;\n  min?: Date;\n  max?: Date;\n  isDateDisabled?: (date: Date) => boolean;\n  /** Override \"today\", for tests and screenshots. */\n  today?: Date;\n  className?: string;\n  'aria-label'?: string;\n}\n\nexport type CalendarProps = Base &\n  (\n    | { mode?: 'single'; value?: Date | null; defaultValue?: Date | null; onValueChange?: (date: Date) => void }\n    | { mode: 'range'; value?: DateRange | null; defaultValue?: DateRange | null; onValueChange?: (range: DateRange) => void }\n  );\n\n/**\n * A month grid. Arrow keys move by day and week, Home and End to the week's ends, Page Up and Down\n * by month (with Shift, by year), Enter or Space picks. A range takes two picks: start, then end.\n */\nexport function Calendar(props: CalendarProps) {\n  const { weekStartsOn = 1, locale, min, max, isDateDisabled, className } = props;\n  // Today is read after mount: a clock read while rendering differs between server and browser.\n  const now = useNow(60_000, false);\n  const today = props.today ? toDay(props.today) : now === undefined ? undefined : toDay(new Date(now));\n  const range = props.mode === 'range';\n\n  const [ownSingle, setOwnSingle] = useState<Date | null>(range ? null : ((props.defaultValue as Date | null) ?? null));\n  const [ownRange, setOwnRange] = useState<DateRange | null>(range ? ((props.defaultValue as DateRange | null) ?? null) : null);\n  const single = range ? null : props.value === undefined ? ownSingle : (props.value as Date | null);\n  const span = range ? (props.value === undefined ? ownRange : (props.value as DateRange | null)) : null;\n  // While choosing a range, the first pick waits here for the second.\n  const [anchor, setAnchor] = useState<Date | null>(null);\n  const [hover, setHover] = useState<Date | null>(null);\n\n  const start = props.defaultMonth ?? single ?? span?.start ?? today;\n  const [ownFocused, setFocused] = useState(start && toDay(start));\n  const [ownMonth, setMonth] = useState(ownFocused);\n  // Until the person moves, both follow the starting month, which may only be known after mount.\n  const focused = ownFocused ?? (start && toDay(start));\n  const month = ownMonth ?? focused;\n  const grid = useMemo(() => (month ? monthGrid(month, weekStartsOn) : []), [month, weekStartsOn]);\n  const gridRef = useRef<HTMLTableElement>(null);\n  const moved = useRef(false);\n\n  useEffect(() => {\n    if (moved.current) gridRef.current?.querySelector<HTMLButtonElement>('[tabindex=\"0\"]')?.focus();\n  }, [focused]);\n\n  // No value, no defaultMonth and no today prop: the month is only known once mounted.\n  if (!month || !focused) return <div className={cx('cr-calendar cr-popover', className)} aria-busy=\"true\" />;\n\n  const fmt = (o: Intl.DateTimeFormatOptions) => new Intl.DateTimeFormat(locale, o);\n  const monthTitle = fmt({ month: 'long', year: 'numeric' }).format(month);\n  const dayName = fmt({ weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' });\n  const weekdays = grid[0]!.map((d) => ({ short: fmt({ weekday: 'narrow' }).format(d), long: fmt({ weekday: 'long' }).format(d) }));\n\n  const disabled = (d: Date) => (min && compare(d, min) < 0) || (max && compare(d, max) > 0) || !!isDateDisabled?.(d);\n\n  const go = (d: Date) => {\n    moved.current = true;\n    setFocused(d);\n    if (d.getMonth() !== month.getMonth() || d.getFullYear() !== month.getFullYear()) setMonth(d);\n  };\n\n  const pick = (d: Date) => {\n    if (disabled(d)) return;\n    if (props.mode === 'range') {\n      if (!anchor) {\n        setAnchor(d);\n        return;\n      }\n      const next = compare(d, anchor) < 0 ? { start: d, end: anchor } : { start: anchor, end: d };\n      setAnchor(null);\n      if (props.value === undefined) setOwnRange(next);\n      props.onValueChange?.(next);\n    } else {\n      if (props.value === undefined) setOwnSingle(d);\n      props.onValueChange?.(d);\n    }\n  };\n\n  const onKeyDown = (e: KeyboardEvent) => {\n    const offset = (focused.getDay() - weekStartsOn + 7) % 7;\n    const next: Record<string, () => Date> = {\n      ArrowLeft: () => addDays(focused, -1),\n      ArrowRight: () => addDays(focused, 1),\n      ArrowUp: () => addDays(focused, -7),\n      ArrowDown: () => addDays(focused, 7),\n      Home: () => addDays(focused, -offset),\n      End: () => addDays(focused, 6 - offset),\n      PageUp: () => addMonths(focused, e.shiftKey ? -12 : -1),\n      PageDown: () => addMonths(focused, e.shiftKey ? 12 : 1),\n    };\n    if (next[e.key]) {\n      e.preventDefault();\n      go(next[e.key]!());\n    } else if (e.key === 'Escape' && anchor) {\n      setAnchor(null);\n    }\n  };\n\n  // The range shown: the chosen one, or the one being chosen.\n  const shownRange = anchor\n    ? (() => {\n        const other = hover ?? focused;\n        return compare(other, anchor) < 0 ? { start: other, end: anchor } : { start: anchor, end: other };\n      })()\n    : span;\n\n  return (\n    <div className={cx('cr-calendar cr-popover', className)}>\n      <div className=\"cr-calendar__head\">\n        <button type=\"button\" className=\"cr-calendar__nav\" aria-label=\"Previous month\" onClick={() => go(addMonths(focused, -1))}>\n          <ChevronLeftIcon />\n        </button>\n        <h2 className=\"cr-calendar__title\" aria-live=\"polite\">\n          {monthTitle}\n        </h2>\n        <button type=\"button\" className=\"cr-calendar__nav\" aria-label=\"Next month\" onClick={() => go(addMonths(focused, 1))}>\n          <ChevronIcon />\n        </button>\n      </div>\n      <table ref={gridRef} role=\"grid\" aria-label={props['aria-label'] ?? monthTitle} className=\"cr-calendar__grid\" onKeyDown={onKeyDown}>\n        <thead>\n          <tr>\n            {weekdays.map((w) => (\n              <th key={w.long} scope=\"col\" abbr={w.long}>\n                {w.short}\n              </th>\n            ))}\n          </tr>\n        </thead>\n        <tbody>\n          {grid.map((week, w) => (\n            <tr key={w}>\n              {week.map((d) => {\n                const outside = d.getMonth() !== month.getMonth();\n                const selected = range\n                  ? !!shownRange && (sameDay(d, shownRange.start) || sameDay(d, shownRange.end))\n                  : sameDay(d, single);\n                const between = !!shownRange && compare(d, shownRange.start) > 0 && compare(d, shownRange.end) < 0;\n                const off = disabled(d);\n                return (\n                  <td key={d.getTime()} role=\"gridcell\" aria-selected={selected || between || undefined}>\n                    <button\n                      type=\"button\"\n                      className=\"cr-calendar__day\"\n                      tabIndex={sameDay(d, focused) ? 0 : -1}\n                      aria-label={dayName.format(d)}\n                      aria-current={sameDay(d, today) ? 'date' : undefined}\n                      aria-disabled={off || undefined}\n                      data-outside={outside || undefined}\n                      data-selected={selected || undefined}\n                      data-between={between || undefined}\n                      data-start={(shownRange && sameDay(d, shownRange.start)) || undefined}\n                      data-end={(shownRange && sameDay(d, shownRange.end)) || undefined}\n                      onClick={() => {\n                        go(d);\n                        pick(d);\n                      }}\n                      onMouseEnter={() => anchor && setHover(d)}\n                    >\n                      {d.getDate()}\n                    </button>\n                  </td>\n                );\n              })}\n            </tr>\n          ))}\n        </tbody>\n      </table>\n    </div>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/creance/calendar/Calendar.tsx"
    },
    {
      "path": "calendar/dates.ts",
      "content": "// Calendar dates are local days. Times are ignored; every helper returns noon to dodge DST edges.\nexport const day = (y: number, m: number, d: number) => new Date(y, m, d, 12);\nexport const toDay = (d: Date) => day(d.getFullYear(), d.getMonth(), d.getDate());\nexport const addDays = (d: Date, n: number) => day(d.getFullYear(), d.getMonth(), d.getDate() + n);\nexport const sameDay = (a?: Date | null, b?: Date | null) =>\n  !!a && !!b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();\nexport const compare = (a: Date, b: Date) => toDay(a).getTime() - toDay(b).getTime();\n\n/** Same day of month, clamped: Jan 31 + 1 month is Feb 28 or 29. */\nexport function addMonths(d: Date, n: number) {\n  const first = day(d.getFullYear(), d.getMonth() + n, 1);\n  const last = day(first.getFullYear(), first.getMonth() + 1, 0).getDate();\n  return day(first.getFullYear(), first.getMonth(), Math.min(d.getDate(), last));\n}\n\n/** Six rows of seven days covering the month, starting on `weekStartsOn` (0 Sunday, 1 Monday). */\nexport function monthGrid(month: Date, weekStartsOn: number) {\n  const first = day(month.getFullYear(), month.getMonth(), 1);\n  const start = addDays(first, -((first.getDay() - weekStartsOn + 7) % 7));\n  return Array.from({ length: 6 }, (_, w) => Array.from({ length: 7 }, (_, i) => addDays(start, w * 7 + i)));\n}\n",
      "type": "registry:ui",
      "target": "@components/creance/calendar/dates.ts"
    }
  ],
  "categories": [
    "molecule",
    "forms"
  ],
  "type": "registry:ui"
}