{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-calendar",
  "type": "registry:ui",
  "title": "react-tw/calendar.tsx",
  "description": "The month grid of the date picker as a React + Tailwind component, the look and keys of Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-date",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/calendar.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/calendar.tsx",
      "content": "import type { ComponentProps, ReactNode, Ref } from 'react';\nimport { calendarMonths, pickInRange, toIso, type Era } from '../core/date.ts';\nimport { Calendar as CalendarPrimitive } from '@purpurabog/ui/headless';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\n\n/**\n * The month grid of the date picker as a React + Tailwind component, the look and keys of\n * `<ui-calendar>` on the headless `Calendar` (Carbon flatpickr sizes, APG grid): month name, a\n * year box with ▲▼, a fixed 6-week grid, an optional \"Today\" button. A controlled view: you hold the chosen day(s) and\n * `focusDate` (the day with the roving tab stop, whose month shows) and hear `onFocusDateChange` /\n * `onPick`. ←/→ day, ↑/↓ week, Home/End week start/end, PageUp/PageDown month, Shift+Page year,\n * Enter/Space pick (←/→ swap right-to-left). `ref.focusDay()` puts focus on the day with the tab stop.\n * `months` (1 to 12) shows months side by side, each with its name over it (the header keeps the year), wrapping\n * one under the other where they don't fit; ‹ › turn the whole page, or one month with `pageBy=\"single\"`.\n * The header has Cally's month and year selects; `heading` replaces them (put `CalendarMonthSelect` / `CalendarYearSelect`\n * there, which follow the calendar), `previousIcon` / `nextIcon` the arrows' icons. Other months' days are empty cells unless `showOutsideDays`.\n * Cally's options: `today`, `weekdayFormat`, `showWeekNumbers`, `showOutsideDays`, `dayClassName`\n * (Cally `getDayParts`: classes for a day of yours), `onPageChange`.\n */\nexport type CalendarHandle = CalendarPrimitive.CalendarHandle;\n\nexport interface CalendarProps {\n    /** Chosen day (the start in a range), ISO yyyy-mm-dd. */\n    value?: string;\n    /** Range: the chosen end, ISO. */\n    endValue?: string;\n    range?: boolean;\n    /** Range: which end a pick sets (drives the preview). */\n    activeField?: 'start' | 'end';\n    focusDate: Date;\n    onFocusDateChange: (date: Date) => void;\n    onPick: (date: Date) => void;\n    /** Range: a pick began a range (its start), before `onPick`; Lit `ui-range-start` (Cally `rangestart`). */\n    onRangeStart?: (date: Date) => void;\n    /** Range: a pick finished a range (its end), before `onPick`; Lit `ui-range-end` (Cally `rangeend`). */\n    onRangeEnd?: (date: Date) => void;\n    min?: string;\n    max?: string;\n    isDateDisabled?: (date: Date) => boolean | string;\n    /** BCP 47 locale for names and labels (default `<html lang>`, as the DatePicker). */\n    locale?: string;\n    /** Years in พ.ศ. (`be`, default for Thai) or ค.ศ. */\n    era?: Era;\n    /** 0 = Sunday … 6 = Saturday. */\n    firstDay?: number;\n    showToday?: boolean;\n    /** Months side by side, 1 to 12 (default 1). */\n    months?: number;\n    /** How far ‹ › turn several months: the whole page (default) or one month (`single`). */\n    pageBy?: 'months' | 'single';\n    /** The day shown as today, ISO (default: the real today). */\n    today?: string;\n    /** The weekday headings: `narrow` or `short` (default: the translation's). */\n    weekdayFormat?: 'narrow' | 'short';\n    /** An ISO week number before each week. */\n    showWeekNumbers?: boolean;\n    /** Show other months' days, in six weeks (by default they are empty cells and only the weeks the month needs show). */\n    showOutsideDays?: boolean;\n    /** Classes of your own for a day (holidays, events …), after the calendar's; as Cally's `getDayParts`. */\n    dayClassName?: (date: Date) => string | undefined;\n    /** The month shown changed: its first and last day. */\n    onPageChange?: (page: { start: Date; end: Date }) => void;\n    /** Replaces the month and year selects (put `CalendarMonthSelect` / `CalendarYearSelect` here); Lit slot `heading`. */\n    heading?: ReactNode;\n    /** Replaces the previous button's arrow icon (the button, its label and size stay); Lit slot `previous`. */\n    previousIcon?: ReactNode;\n    /** Replaces the next button's arrow icon (the button, its label and size stay); Lit slot `next`. */\n    nextIcon?: ReactNode;\n    ref?: Ref<CalendarHandle>;\n    className?: string;\n}\n\n/** The arrows' own icons, sized and filled here, so an icon of yours (`previousIcon` / `nextIcon`) keeps its own look, as a slotted one in Lit. */\nconst prevArrow = (\n    <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className=\"size-4 fill-current\">\n        <path d=\"m5 8 5-5 .7.7L6.4 8l4.3 4.3-.7.7z\" />\n    </svg>\n);\nconst nextArrow = (\n    <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className=\"size-4 fill-current\">\n        <path d=\"m11 8-5 5-.7-.7L9.6 8 5.3 3.7 6 3z\" />\n    </svg>\n);\n\n/**\n * The month arrows and \"Today\" carry tabindex=\"0\" (the headless parts' default): Safari then focuses them on click (it doesn't focus a plain\n * button), so focus stays inside the calendar and the date picker doesn't take the click for leaving it.\n */\nconst navButton = cn(\n    'grid size-10 cursor-pointer touch-manipulation place-items-center rounded-[var(--ui-icon-control-radius,0px)] border-0 bg-transparent p-0 text-ink hover:bg-ghost-hover',\n    'transition-[background-color] duration-(--ui-duration-fast) ease-carbon', // Carbon flatpickr-prev-month / -next-month\n    'outline-none focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:outline-solid focus-visible:[outline-offset:var(--ui-focus-offset,-2px)] focus-visible:outline-focus',\n    'disabled:cursor-not-allowed disabled:bg-transparent disabled:text-ink-disabled',\n);\n\ninterface DayState {\n    outside: boolean;\n    today: boolean;\n    pressed: boolean;\n    inRange: boolean;\n    rangeEnd: boolean;\n    disabled: boolean;\n}\n\n/** A day's text colour, in Lit's rule order (later states win over earlier ones). */\nfunction dayColour(s: DayState) {\n    if (s.disabled) return 'text-ink-disabled';\n    if (s.inRange || (s.rangeEnd && !s.pressed)) return 'text-ink';\n    if (s.pressed) return 'text-on-primary';\n    if (s.today) return 'text-link';\n    return s.outside ? 'text-ink-helper' : 'text-ink';\n}\n\nfunction dayBackground(s: DayState) {\n    if (s.disabled) return 'bg-transparent';\n    if (s.rangeEnd && !s.pressed) return 'bg-[var(--_ui-calendar-surface,var(--ui-surface-1))]'; // the popup's surface (date-picker.tsx)\n    if (s.pressed) return 'bg-primary text-on-primary';\n    if (s.inRange) return 'bg-highlight';\n    // --ui-calendar-day-focus-background: the keyboard's day filled as under the pointer (Cally; Carbon: none)\n    return 'bg-transparent hover:bg-ghost-hover focus-visible:bg-[var(--ui-calendar-day-focus-background,transparent)]';\n}\n\nfunction dayRing(s: DayState) {\n    if (s.rangeEnd && !s.pressed) return 'z-1 outline-[length:var(--ui-focus-width,2px)] outline-solid -outline-offset-2 outline-focus';\n    if (s.pressed) {\n        // after a click a design that rings the keyboard only shows none (--ui-pointer-focus; Carbon: the ring stays)\n        return '[&:focus:not(:focus-visible)]:outline-[color:var(--ui-pointer-focus,var(--ui-calendar-selected-focus,var(--ui-layer-2)))] focus:z-[var(--ui-calendar-selected-focus-layer,auto)] focus:outline-solid focus:outline-[length:var(--ui-calendar-selected-focus-width,1px)] focus:outline-offset-[var(--ui-calendar-selected-focus-offset,-3px)] focus:outline-[color:var(--ui-calendar-selected-focus,var(--ui-layer-2))]';\n    }\n    return 'focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-[color:var(--ui-calendar-day-focus,var(--ui-primary))]';\n}\n\n/** A day's classes, decided here in Lit's rule order (later states win over earlier ones). */\nfunction dayClasses(s: DayState & { rangeStart?: boolean }) {\n    const colour = dayColour(s);\n    const background = dayBackground(s);\n    const ring = dayRing(s);\n    return cn(\n        'relative z-1 inline-flex h-10 w-full items-center justify-center cursor-pointer touch-manipulation border-0 p-0 type-body-sm outline-none',\n        'transition-colors duration-[var(--ui-calendar-day-duration,var(--ui-duration-fast))] ease-carbon', // Kanok: 0s, as Cally\n        // Fully round by default (Cally style)\n        'rounded-full',\n        // Range ribbon: flat inner, round caps\n        s.rangeStart && !s.rangeEnd && 'rounded-s-full! rounded-e-none!',\n        s.rangeEnd && !s.rangeStart && 'rounded-e-full! rounded-s-none!',\n        s.inRange && 'rounded-none! group-first/cell:rounded-s-full! group-last/cell:rounded-e-full!',\n        colour,\n        background,\n        ring,\n        s.today && \"font-semibold! after:absolute after:start-1/2 after:bottom-[5px] after:-translate-x-1/2 after:size-1 after:rounded-full after:bg-current after:content-['']\",\n        s.today && s.pressed && 'after:bg-on-primary',\n        s.pressed && 'font-semibold', // as <ui-calendar>'s chosen days\n        s.disabled && 'cursor-not-allowed line-through opacity-50',\n        'forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText] forced-colors:aria-pressed:[forced-color-adjust:none]',\n    );\n}\n\n/** A heading select (Cally `calendar-select-month` / `-year`), set as `<ui-calendar-select-*>`: as wide as its choice, a chevron after it.\n * It, the arrows, the days and \"Today\" take a tap at once (`touch-manipulation`: no double-tap zoom to wait for; Cally's buttons too). */\nconst headingSelect = cn(\n    'box-border h-7 cursor-pointer touch-manipulation appearance-none rounded-[var(--ui-radius-sm,6px)] border-0 bg-transparent ps-1.5 pe-5 text-ink type-body-emphasis [font-size:var(--ui-field-font-size,0.9375rem)]! font-semibold! [field-sizing:content]',\n    'transition-[background-color] duration-(--ui-duration-fast) ease-carbon hover:bg-canvas-hover',\n    'outline-none focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:outline-solid focus-visible:-outline-offset-1 focus-visible:outline-focus',\n    'forced-colors:border forced-colors:border-solid forced-colors:border-[ButtonText]',\n);\n\nconst chevron = (\n    <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className=\"pointer-events-none absolute end-1 size-3 fill-ink-muted forced-colors:fill-[ButtonText]\">\n        <path d=\"M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z\" />\n    </svg>\n);\n\ntype HeadingSelectProps = Omit<ComponentProps<'select'>, 'value' | 'defaultValue' | 'children'>;\n\nexport interface CalendarMonthSelectProps extends HeadingSelectProps {\n    /** Month names `long` (October, default) or `short` (Oct); Lit `format-month`. */\n    formatMonth?: 'long' | 'short';\n}\n\nexport interface CalendarYearSelectProps extends HeadingSelectProps {\n    /** How many years to offer, around the year shown (default 20); Lit `max-years`. */\n    maxYears?: number;\n}\n\n/** The heading selects' names, in the enclosing calendar's language (as `<ui-calendar>` gives its selects). */\nfunction useHeadingLabels() {\n    const m = useTranslation(CalendarPrimitive.useCalendarContext().locale).datePicker;\n    return { month: m.month ?? 'Month', year: m.year };\n}\n\n/**\n * The month select of the calendar's header (`<ui-calendar-select-month>`, Cally `calendar-select-month`): the months of\n * the year shown, those wholly outside `min` / `max` off; a choice shows that month. Put it in `heading`; it follows the calendar around it.\n */\nexport function CalendarMonthSelect({ className, 'aria-label': label, ...props }: Readonly<CalendarMonthSelectProps>) {\n    const labels = useHeadingLabels();\n    return (\n        <span className=\"relative inline-flex items-center\">\n            <CalendarPrimitive.MonthSelect {...props} aria-label={label ?? labels.month} className={cn(headingSelect, className)} />\n            {chevron}\n        </span>\n    );\n}\n\n/**\n * The year select of the calendar's header (`<ui-calendar-select-year>`, Cally `calendar-select-year`): `maxYears` years\n * around the year shown, cut to `min` / `max`, in the calendar's era. Put it in `heading`; it follows the calendar around it.\n */\nexport function CalendarYearSelect({ className, 'aria-label': label, ...props }: Readonly<CalendarYearSelectProps>) {\n    const labels = useHeadingLabels();\n    return (\n        <span className=\"relative inline-flex items-center\">\n            <CalendarPrimitive.YearSelect {...props} aria-label={label ?? labels.year} className={cn(headingSelect, className)} />\n            {chevron}\n        </span>\n    );\n}\n\n/** Seamless continuous range ribbon behind day buttons (Cally style). */\nfunction DayRibbon() {\n    const s = CalendarPrimitive.useCellContext();\n    const showRibbon = s.inRange || (s.rangeStart && !s.rangeEnd) || (s.rangeEnd && !s.rangeStart);\n    if (!showRibbon) return null;\n    return (\n        <span\n            aria-hidden=\"true\"\n            className={cn(\n                'pointer-events-none absolute inset-y-[2px] z-0 bg-highlight',\n                s.inRange && 'inset-x-0 group-first/cell:rounded-s-full group-last/cell:rounded-e-full',\n                s.rangeStart && !s.rangeEnd && 'start-1/2 end-0 group-last/cell:rounded-e-full',\n                s.rangeEnd && !s.rangeStart && 'start-0 end-1/2 group-first/cell:rounded-s-full',\n            )}\n        />\n    );\n}\n\n/** A day's button, its classes from where it stands, then the app's own (`dayClassName`). */\nfunction CalendarDay({ own }: Readonly<{ own?: (date: Date) => string | undefined }>) {\n    const s = CalendarPrimitive.useCellContext();\n    return <CalendarPrimitive.Day className={cn(dayClasses({ outside: s.outsideMonth, today: s.today, pressed: s.selected, inRange: s.inRange, rangeStart: s.rangeStart, rangeEnd: s.rangeEnd, disabled: s.disabled }), own?.(s.date))} />;\n}\n\n/** The week-number column (`showWeekNumbers`): quieter than the days, as `<ui-calendar>`. */\nconst weekNumberCells = '[&_[data-week-number]]:h-10 [&_[data-week-number]]:p-0 [&_[data-week-number]]:type-caption [&_[data-week-number]]:font-normal [&_[data-week-number]]:text-ink-helper';\n\nexport function Calendar({\n    value = '',\n    endValue = '',\n    range = false,\n    activeField = 'start',\n    focusDate,\n    onFocusDateChange,\n    onPick,\n    onRangeStart,\n    onRangeEnd,\n    min = '',\n    max = '',\n    isDateDisabled,\n    locale,\n    era,\n    firstDay = 0,\n    showToday = false,\n    months = 1,\n    pageBy = 'months',\n    today,\n    weekdayFormat,\n    showWeekNumbers = false,\n    showOutsideDays = false,\n    dayClassName,\n    onPageChange,\n    heading,\n    previousIcon,\n    nextIcon,\n    ref,\n    className,\n}: Readonly<CalendarProps>) {\n    const m = useTranslation(locale).datePicker;\n    const several = calendarMonths(months) > 1;\n    // a range's start / end event before the pick, as <ui-calendar> (Cally's rangestart / rangeend come before its change)\n    const pick = (date: Date) => {\n        if (range) {\n            const ends = pickInRange({ start: value, end: endValue, active: activeField }, toIso(date)).done ? onRangeEnd : onRangeStart;\n            ends?.(date);\n        }\n        onPick(date);\n    };\n    const grid = (\n        <CalendarPrimitive.Grid className={cn('mx-auto w-full max-w-[280px] table-fixed border-collapse', showWeekNumbers && cn('max-w-[320px]', weekNumberCells))}>\n            <CalendarPrimitive.GridHeader>{(d) => <CalendarPrimitive.HeaderCell date={d} className=\"h-10 p-0 type-body-sm font-medium! text-ink-muted\" />}</CalendarPrimitive.GridHeader>\n            <CalendarPrimitive.GridBody>\n                {(d) => (\n                    <CalendarPrimitive.Cell date={d} className=\"group/cell relative h-10 p-0 pb-px text-center align-middle\">\n                        <DayRibbon />\n                        <CalendarDay own={dayClassName} />\n                    </CalendarPrimitive.Cell>\n                )}\n            </CalendarPrimitive.GridBody>\n        </CalendarPrimitive.Grid>\n    );\n    // several months as <ui-calendar months>: each grid under its name, side by side, wrapping one under the other where they don't fit\n    const monthView = (\n        <div className={cn('w-full max-w-[280px] flex-[0_0_280px]', showWeekNumbers && 'max-w-[320px] basis-[320px]')}>\n            <CalendarPrimitive.MonthTitle className=\"mb-[var(--ui-space-xs,8px)] flex h-7 items-center justify-center type-body-emphasis text-[0.9375rem]! font-semibold! text-ink\" />\n            {grid}\n        </div>\n    );\n    return (\n        <CalendarPrimitive.Root\n            handle={ref}\n            value={value}\n            endValue={endValue}\n            range={range}\n            activeField={activeField}\n            focusDate={focusDate}\n            onFocusDateChange={onFocusDateChange}\n            onPick={pick}\n            min={min}\n            max={max}\n            isDateDisabled={isDateDisabled}\n            locale={locale}\n            era={era}\n            firstDay={firstDay}\n            months={months}\n            pageBy={pageBy}\n            today={today}\n            weekdayFormat={weekdayFormat}\n            showWeekNumbers={showWeekNumbers}\n            showOutsideDays={showOutsideDays}\n            onPageChange={onPageChange}\n            className={cn('block font-sans', className)}\n        >\n            <div className=\"flex items-center justify-between\">\n                <CalendarPrimitive.PrevButton aria-label={m.previousMonth} className={navButton}>\n                    {previousIcon ?? prevArrow}\n                </CalendarPrimitive.PrevButton>\n                <div className=\"flex h-7 items-center gap-1 type-body-emphasis\">\n                    {/* several months: the year only (each grid has its month's name) */}\n                    {heading ?? (\n                        <>\n                            {several ? null : <CalendarMonthSelect />}\n                            <CalendarYearSelect />\n                        </>\n                    )}\n                </div>\n                <CalendarPrimitive.NextButton aria-label={m.nextMonth} className={navButton}>\n                    {nextIcon ?? nextArrow}\n                </CalendarPrimitive.NextButton>\n            </div>\n            <CalendarPrimitive.Heading className=\"sr-only\" />\n            {several ? (\n                <div className=\"flex flex-wrap items-start justify-center gap-[var(--ui-space-lg,24px)]\">\n                    <CalendarPrimitive.Months>{() => monthView}</CalendarPrimitive.Months>\n                </div>\n            ) : (\n                grid\n            )}\n            {showToday ? (\n                <div className=\"mt-1 flex justify-end border-0 border-t border-solid border-hairline\">\n                    <CalendarPrimitive.TodayButton\n                        className={cn(\n                            'h-8 cursor-pointer touch-manipulation border-0 bg-transparent px-4 text-link type-body-sm hover:bg-ghost-hover',\n                            'outline-none focus-visible:rounded-[var(--ui-focus-radius,0px)] focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:outline-solid focus-visible:[outline-offset:var(--ui-focus-offset,-2px)] focus-visible:outline-focus',\n                            'disabled:cursor-not-allowed disabled:bg-transparent disabled:text-ink-disabled',\n                        )}\n                    >\n                        {m.today}\n                    </CalendarPrimitive.TodayButton>\n                </div>\n            ) : null}\n        </CalendarPrimitive.Root>\n    );\n}\n"
    }
  ]
}
