{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "calendar-model",
  "type": "registry:ui",
  "title": "Calendar Model",
  "description": "The pure half of `Calendar`: dates as plain `YYYY-MM-DD` strings, the month grid, the keyboard moves of the WAI-ARIA date grid, and the bounds.",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "form",
    "primitive"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/calendar-model.ts",
      "target": "components/ui/calendar-model.ts",
      "type": "registry:ui",
      "content": "export type CalendarDate = string;\n\n/** 0 is Sunday, 1 is Monday … 6 is Saturday, as `Date#getUTCDay`. */\nexport type Weekday = 0 | 1 | 2 | 3 | 4 | 5 | 6;\n\nconst ISO_RE = /^(\\d{4})-(\\d{2})-(\\d{2})$/;\n\n/** True when `value` is a real `YYYY-MM-DD` day (no 31 April). */\nexport function isCalendarDate(value: unknown): value is CalendarDate {\n  if (typeof value !== 'string') return false;\n  const m = ISO_RE.exec(value);\n  if (!m) return false;\n  const [y, mo, d] = [Number(m[1]), Number(m[2]), Number(m[3])];\n  if (mo < 1 || mo > 12 || d < 1) return false;\n  return d <= daysInMonth(y, mo);\n}\n\nfunction parts(date: CalendarDate): [number, number, number] {\n  const m = ISO_RE.exec(date);\n  if (!m) throw new RangeError(`Not a YYYY-MM-DD date: ${date}`);\n  return [Number(m[1]), Number(m[2]), Number(m[3])];\n}\n\nfunction pad(n: number, width = 2): string {\n  return String(n).padStart(width, '0');\n}\n\n/** Build a `YYYY-MM-DD` from a year, a 1-based month and a day. */\nexport function toCalendarDate(year: number, month: number, day: number): CalendarDate {\n  return `${pad(year, 4)}-${pad(month)}-${pad(day)}`;\n}\n\n/** Days in a 1-based month. */\nexport function daysInMonth(year: number, month: number): number {\n  return new Date(Date.UTC(year, month, 0)).getUTCDate();\n}\n\nfunction fromUtc(ms: number): CalendarDate {\n  const d = new Date(ms);\n  return toCalendarDate(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate());\n}\n\nfunction toUtc(date: CalendarDate): number {\n  const [y, m, d] = parts(date);\n  return Date.UTC(y, m - 1, d);\n}\n\n/**\n * A UTC-midnight `Date` for the day, for handing to `Intl.DateTimeFormat`\n * with `timeZone: 'UTC'`. Never show it in a local zone.\n */\nexport function toUtcDate(date: CalendarDate): Date {\n  return new Date(toUtc(date));\n}\n\n/** The weekday of a day. */\nexport function weekdayOf(date: CalendarDate): Weekday {\n  return new Date(toUtc(date)).getUTCDay() as Weekday;\n}\n\n/** The day `n` days later (or earlier, for a negative `n`). */\nexport function addDays(date: CalendarDate, n: number): CalendarDate {\n  return fromUtc(toUtc(date) + n * 86_400_000);\n}\n\n/**\n * The same day `n` months later, kept inside the month: 31 January plus one\n * month is 28 or 29 February, never 3 March.\n */\nexport function addMonths(date: CalendarDate, n: number): CalendarDate {\n  const [y, m, d] = parts(date);\n  const index = y * 12 + (m - 1) + n;\n  const year = Math.floor(index / 12);\n  const month = index - year * 12 + 1;\n  return toCalendarDate(year, month, Math.min(d, daysInMonth(year, month)));\n}\n\n/** The first day of the day's month. */\nexport function startOfMonth(date: CalendarDate): CalendarDate {\n  return `${date.slice(0, 7)}-01`;\n}\n\n/** The last day of the day's month. */\nexport function endOfMonth(date: CalendarDate): CalendarDate {\n  const [y, m] = parts(date);\n  return toCalendarDate(y, m, daysInMonth(y, m));\n}\n\n/** Both days fall in the same month of the same year. */\nexport function isSameMonth(a: CalendarDate, b: CalendarDate): boolean {\n  return a.slice(0, 7) === b.slice(0, 7);\n}\n\n/** -1, 0 or 1. `YYYY-MM-DD` sorts as text, so this is a string compare. */\nexport function compareDates(a: CalendarDate, b: CalendarDate): -1 | 0 | 1 {\n  return a < b ? -1 : a > b ? 1 : 0;\n}\n\n/** The day within `[min, max]`; either bound may be left out. */\nexport function clampDate(date: CalendarDate, min?: CalendarDate, max?: CalendarDate): CalendarDate {\n  if (min && date < min) return min;\n  if (max && date > max) return max;\n  return date;\n}\n\n/** The first day of the week that holds `date`. */\nexport function startOfWeek(date: CalendarDate, weekStartsOn: Weekday): CalendarDate {\n  return addDays(date, -((weekdayOf(date) - weekStartsOn + 7) % 7));\n}\n\n/** The last day of the week that holds `date`. */\nexport function endOfWeek(date: CalendarDate, weekStartsOn: Weekday): CalendarDate {\n  return addDays(startOfWeek(date, weekStartsOn), 6);\n}\n\n/**\n * The month as six weeks of seven days, starting on `weekStartsOn`, with the\n * neighbouring months' days filling the edges. Always six rows, so the grid\n * keeps its height from one month to the next and nothing below it moves.\n */\nexport function monthGrid(month: CalendarDate, weekStartsOn: Weekday): CalendarDate[][] {\n  const first = startOfWeek(startOfMonth(month), weekStartsOn);\n  return Array.from({ length: 6 }, (_, w) =>\n    Array.from({ length: 7 }, (_, d) => addDays(first, w * 7 + d)),\n  );\n}\n\n/** The seven weekdays in grid order, starting on `weekStartsOn`. */\nexport function weekdayOrder(weekStartsOn: Weekday): Weekday[] {\n  return Array.from({ length: 7 }, (_, i) => ((weekStartsOn + i) % 7) as Weekday);\n}\n\n/**\n * Regions whose calendars start the week on Sunday or Saturday, for runtimes\n * without `Intl.Locale#getWeekInfo`. Everyone else starts on Monday (ISO 8601).\n * From CLDR's `firstDay` supplemental data.\n */\nconst SUNDAY_REGIONS = new Set([\n  'AG', 'AS', 'BD', 'BR', 'BS', 'BT', 'BW', 'BZ', 'CA', 'CN', 'CO', 'DM', 'DO', 'ET', 'GT', 'GU',\n  'HK', 'HN', 'ID', 'IL', 'IN', 'JM', 'JP', 'KE', 'KH', 'KR', 'LA', 'MH', 'MM', 'MO', 'MT', 'MX',\n  'MZ', 'NI', 'NP', 'PA', 'PE', 'PH', 'PK', 'PR', 'PT', 'PY', 'SA', 'SG', 'SV', 'TH', 'TT', 'TW',\n  'UM', 'US', 'VE', 'VI', 'WS', 'YE', 'ZA', 'ZW',\n]);\nconst SATURDAY_REGIONS = new Set(['AE', 'AF', 'BH', 'DJ', 'DZ', 'EG', 'IQ', 'IR', 'JO', 'KW', 'LY', 'OM', 'QA', 'SD', 'SY']);\n\ninterface WeekInfoLocale {\n  getWeekInfo?: () => { firstDay: number };\n  weekInfo?: { firstDay: number };\n  maximize: () => Intl.Locale;\n}\n\n/**\n * The first day of the week for a locale: `Intl.Locale#getWeekInfo` where the\n * runtime has it, else the CLDR region table above. `en-US` is Sunday,\n * `en-GB` Monday, `ar-EG` Saturday.\n */\nexport function weekStartFor(locale?: string): Weekday {\n  let loc: WeekInfoLocale;\n  try {\n    loc = new Intl.Locale(locale ?? new Intl.DateTimeFormat().resolvedOptions().locale) as unknown as WeekInfoLocale;\n  } catch {\n    return 1;\n  }\n  const info = loc.getWeekInfo?.() ?? loc.weekInfo;\n  if (info) return (info.firstDay % 7) as Weekday;\n  const region = String(loc.maximize().region);\n  if (SUNDAY_REGIONS.has(region)) return 0;\n  if (SATURDAY_REGIONS.has(region)) return 6;\n  return 1;\n}\n\n/** Today as a `YYYY-MM-DD` in a time zone (the runtime's own when left out). */\nexport function todayIn(timeZone?: string, now: Date = new Date()): CalendarDate {\n  const fmt = new Intl.DateTimeFormat('en-CA', {\n    timeZone,\n    year: 'numeric',\n    month: '2-digit',\n    day: '2-digit',\n  });\n  const part = Object.fromEntries(fmt.formatToParts(now).map((p) => [p.type, p.value]));\n  return `${part.year}-${part.month}-${part.day}`;\n}\n\nexport interface DateBounds {\n  min?: CalendarDate;\n  max?: CalendarDate;\n  /** A day that is inside the bounds but cannot be picked: full, closed, a holiday. */\n  isDateUnavailable?: (date: CalendarDate) => boolean;\n}\n\n/** Outside `[min, max]`: not shown as a choice at all. */\nexport function isOutOfRange(date: CalendarDate, { min, max }: DateBounds): boolean {\n  return (min !== undefined && date < min) || (max !== undefined && date > max);\n}\n\n/** The day cannot be picked, for either reason. */\nexport function isDateDisabled(date: CalendarDate, bounds: DateBounds): boolean {\n  return isOutOfRange(date, bounds) || Boolean(bounds.isDateUnavailable?.(date));\n}\n\n/** The first day from `from` (inclusive) to the end of its month that can be picked, or null. */\nexport function firstEnabledInMonth(from: CalendarDate, bounds: DateBounds): CalendarDate | null {\n  const end = endOfMonth(from);\n  for (let d = from; d <= end; d = addDays(d, 1)) {\n    if (!isDateDisabled(d, bounds)) return d;\n  }\n  return null;\n}\n\n/** The keys the date grid answers to. */\nexport type GridKey =\n  | 'ArrowLeft'\n  | 'ArrowRight'\n  | 'ArrowUp'\n  | 'ArrowDown'\n  | 'Home'\n  | 'End'\n  | 'PageUp'\n  | 'PageDown';\n\nconst GRID_KEYS = new Set<string>([\n  'ArrowLeft',\n  'ArrowRight',\n  'ArrowUp',\n  'ArrowDown',\n  'Home',\n  'End',\n  'PageUp',\n  'PageDown',\n]);\n\n/** The key moves focus in the date grid. */\nexport function isGridKey(key: string): key is GridKey {\n  return GRID_KEYS.has(key);\n}\n\nexport interface MoveOptions {\n  weekStartsOn: Weekday;\n  /** Right-to-left: the left arrow goes to the next day. */\n  rtl?: boolean;\n  /** Shift held: Page Up / Page Down move a year, not a month. */\n  shift?: boolean;\n  min?: CalendarDate;\n  max?: CalendarDate;\n}\n\n/**\n * Where focus goes from `date` on `key`, as the WAI-ARIA date picker grid\n * does it: arrows by a day or a week, Home and End to the week's edges,\n * Page Up and Page Down by a month (a year with Shift). Arrows follow the\n * reading direction. The result stays inside `[min, max]`, so focus never\n * lands on a day that is not drawn as a choice.\n */\nexport function moveFocus(date: CalendarDate, key: GridKey, opts: MoveOptions): CalendarDate {\n  const forward = opts.rtl ? 'ArrowLeft' : 'ArrowRight';\n  let next: CalendarDate;\n  switch (key) {\n    case 'ArrowLeft':\n    case 'ArrowRight':\n      next = addDays(date, key === forward ? 1 : -1);\n      break;\n    case 'ArrowUp':\n      next = addDays(date, -7);\n      break;\n    case 'ArrowDown':\n      next = addDays(date, 7);\n      break;\n    case 'Home':\n      next = startOfWeek(date, opts.weekStartsOn);\n      break;\n    case 'End':\n      next = endOfWeek(date, opts.weekStartsOn);\n      break;\n    case 'PageUp':\n      next = addMonths(date, opts.shift ? -12 : -1);\n      break;\n    case 'PageDown':\n      next = addMonths(date, opts.shift ? 12 : 1);\n      break;\n  }\n  return clampDate(next, opts.min, opts.max);\n}\n\n/**\n * @interlace/ui — calendar-model\n *\n * The pure half of `Calendar`: dates as plain `YYYY-MM-DD` strings, the\n * month grid, the keyboard moves of the WAI-ARIA date grid, and the bounds.\n *\n * A calendar day is not an instant. A `Date` carries a time and a zone, and\n * \"the 3rd\" turned into a `Date` at midnight in one zone is the 2nd in the\n * next one west, which is how a booking page ends up a day off for half its\n * visitors. So a day here is the string a form posts and a server stores,\n * and every piece of arithmetic runs on the UTC calendar, where no day is\n * 23 or 25 hours long.\n *\n * Nothing in this file touches the DOM or React, so a server can repeat any\n * of it: the same grid, the same \"is this day bookable\".\n */\n\n/** A calendar day, `YYYY-MM-DD`. */\n\n// @interlace/calendar-model v1.0.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/calendar-model\n// What changed since: https://ds.interlace.tools/c/calendar-model#history\n// Generated banner — keep it, the upgrade diff reads this version.\n"
    }
  ],
  "meta": {
    "tier": "primitive",
    "client": false,
    "minViewport": null,
    "loading": false,
    "version": "1.0.0",
    "since": "2.0.0"
  },
  "docs": "## @interlace/calendar-model\n\nInstalled to `components/ui/calendar-model.ts`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/calendar-model';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/calendar-model\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
