{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "calendar-21",
  "type": "registry:component",
  "title": "Calendar 21",
  "description": "Calendar 21. A calendar component for displaying and selecting dates.",
  "dependencies": [
    "date-fns",
    "lucide-react",
    "react-day-picker"
  ],
  "registryDependencies": [
    "button",
    "calendar",
    "collapsible",
    "scroll-area"
  ],
  "files": [
    {
      "path": "components/watermelon/calendar-21.tsx",
      "type": "registry:component",
      "content": "'use client'\n\nimport {\n  type ComponentProps,\n  type Dispatch,\n  type HTMLAttributes,\n  type ReactNode,\n  type SetStateAction,\n  useEffect,\n  useRef,\n  useState\n} from 'react'\n\nimport { eachMonthOfInterval, eachYearOfInterval, endOfYear, format, isAfter, isBefore, startOfYear } from 'date-fns'\nimport { ChevronDownIcon } from 'lucide-react'\nimport type { CaptionLabelProps, MonthGridProps } from 'react-day-picker'\n\nimport { Button } from '@/components/base-ui/button'\nimport { Calendar } from '@/components/base-ui/calendar'\nimport { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/base-ui/collapsible'\nimport { ScrollArea } from '@/components/base-ui/scroll-area'\n\ntype CalendarClassNames = NonNullable<ComponentProps<typeof Calendar>['classNames']>\n\ntype MonthGridPanelProps = {\n  children: ReactNode\n  className?: string\n  currentMonth: number\n  currentYear: number\n  endDate: Date\n  isYearView: boolean\n  onMonthSelect: (selectedMonth: Date) => void\n  startDate: Date\n  years: readonly Date[]\n}\n\ntype CaptionLabelButtonProps = {\n  isYearView: boolean\n  setIsYearView: Dispatch<SetStateAction<boolean>>\n} & HTMLAttributes<HTMLSpanElement>\n\ntype CollapsibleYearProps = {\n  children: ReactNode\n  open?: boolean\n  title: string\n}\n\nconst initialSelectedDate: Date = new Date()\nconst minimumAvailableMonth: Date = new Date(1980, 6)\nconst maximumAvailableMonth: Date = new Date(2030, 6)\nconst yearOptions: readonly Date[] = eachYearOfInterval({\n  start: startOfYear(minimumAvailableMonth),\n  end: endOfYear(maximumAvailableMonth)\n})\n\nconst calendarClassNames = {\n  month_caption: 'ml-2.5 mr-20 justify-start',\n  nav: 'flex absolute w-fit right-0 items-center',\n  today: '!bg-transparent',\n  day_button: '!ring-0 !ring-offset-0 focus:!ring-0 focus-visible:!ring-0'\n} satisfies CalendarClassNames\n\nconst Calendar21 = () => {\n  const [visibleMonth, setVisibleMonth] = useState<Date>(initialSelectedDate)\n  const [selectedDate, setSelectedDate] = useState<Date | undefined>(\n    initialSelectedDate\n  )\n  const [isYearView, setIsYearView] = useState<boolean>(false)\n\n  return (\n    <div>\n      <Calendar\n        mode='single'\n        selected={selectedDate}\n        onSelect={setSelectedDate}\n        month={visibleMonth}\n        onMonthChange={setVisibleMonth}\n        defaultMonth={initialSelectedDate}\n        startMonth={minimumAvailableMonth}\n        endMonth={maximumAvailableMonth}\n        className='overflow-hidden !border-0 !bg-transparent p-3 transition-all !ring-0 !ring-offset-0 focus:!ring-0 focus:!ring-offset-0 focus-visible:!ring-0 focus-visible:!ring-offset-0 [&_*]:!ring-0 [&_*]:!ring-offset-0 [&_*]:focus:!ring-0 [&_*]:focus-visible:!ring-0 [&_.rdp-day_today]:!bg-transparent'\n        classNames={calendarClassNames}\n        components={{\n          CaptionLabel: (props: CaptionLabelProps) => (\n            <CaptionLabel isYearView={isYearView} setIsYearView={setIsYearView} {...props} />\n          ),\n          MonthGrid: (props: MonthGridProps) => {\n            return (\n              <MonthGrid\n                className={props.className}\n                isYearView={isYearView}\n                startDate={minimumAvailableMonth}\n                endDate={maximumAvailableMonth}\n                years={yearOptions}\n                currentYear={visibleMonth.getFullYear()}\n                currentMonth={visibleMonth.getMonth()}\n                onMonthSelect={(selectedMonth: Date) => {\n                  setVisibleMonth(selectedMonth)\n                  setIsYearView(false)\n                }}\n              >\n                {props.children}\n              </MonthGrid>\n            )\n          }\n        }}\n      />\n      <p className='text-muted-foreground mt-4 text-center text-xs' role='region'>\n        Calendar with advance selection{' '}\n        <a href='https://originui.com/calendar-date-picker' className='hover:text-primary underline' target='_blank'>\n          Origin UI\n        </a>\n      </p>\n    </div>\n  )\n}\n\nfunction MonthGrid({\n  className,\n  children,\n  isYearView,\n  startDate,\n  endDate,\n  years,\n  currentYear,\n  currentMonth,\n  onMonthSelect\n}: MonthGridPanelProps) {\n  const currentYearRef = useRef<HTMLDivElement>(null)\n  const currentMonthButtonRef = useRef<HTMLButtonElement>(null)\n  const scrollAreaRef = useRef<HTMLDivElement>(null)\n\n  useEffect(() => {\n    if (isYearView && currentYearRef.current && scrollAreaRef.current) {\n      const viewport = scrollAreaRef.current.querySelector<HTMLElement>(\n        '[data-radix-scroll-area-viewport]'\n      )\n\n      if (viewport) {\n        const yearTop = currentYearRef.current.offsetTop\n\n        viewport.scrollTop = yearTop\n      }\n\n      const focusTimeoutId = window.setTimeout(() => {\n        currentMonthButtonRef.current?.focus()\n      }, 100)\n\n      return () => window.clearTimeout(focusTimeoutId)\n    }\n  }, [isYearView])\n\n  return (\n    <div className='relative'>\n      <table className={className}>{children}</table>\n      {isYearView && (\n        <div className='absolute inset-0 z-20 -mx-3 -mb-3 rounded-b-[1.75rem] bg-background'>\n          <ScrollArea ref={scrollAreaRef} className='h-full'>\n            {years.map(year => {\n              const months = eachMonthOfInterval({\n                start: startOfYear(year),\n                end: endOfYear(year)\n              })\n\n              const isCurrentYear = year.getFullYear() === currentYear\n\n              return (\n                <div key={year.getFullYear()} ref={isCurrentYear ? currentYearRef : undefined}>\n                  <CollapsibleYear title={year.getFullYear().toString()} open={isCurrentYear}>\n                    <div className='grid grid-cols-3 gap-2'>\n                      {months.map(month => {\n                        const isDisabled = isBefore(month, startDate) || isAfter(month, endDate)\n                        const isCurrentMonth = month.getMonth() === currentMonth && year.getFullYear() === currentYear\n\n                        return (\n                          <Button\n                            key={month.getTime()}\n                            ref={isCurrentMonth ? currentMonthButtonRef : undefined}\n                            variant={isCurrentMonth ? 'default' : 'outline'}\n                            size='sm'\n                            className='h-8 rounded-full border-border/60'\n                            disabled={isDisabled}\n                            onClick={() => onMonthSelect(month)}\n                          >\n                            {format(month, 'MMM')}\n                          </Button>\n                        )\n                      })}\n                    </div>\n                  </CollapsibleYear>\n                </div>\n              )\n            })}\n          </ScrollArea>\n        </div>\n      )}\n    </div>\n  )\n}\n\nfunction CaptionLabel({\n  children,\n  isYearView,\n  setIsYearView\n}: CaptionLabelButtonProps) {\n  return (\n    <Button\n      className='data-[state=open]:text-muted-foreground/80 -ms-2 flex items-center gap-2 text-sm font-medium hover:bg-transparent [&[data-state=open]>svg]:rotate-180'\n      variant='ghost'\n      size='sm'\n      onClick={() => setIsYearView(prev => !prev)}\n      data-state={isYearView ? 'open' : 'closed'}\n    >\n      {children}\n      <ChevronDownIcon\n        className='text-muted-foreground/80 shrink-0 transition-transform duration-200'\n        aria-hidden='true'\n      />\n    </Button>\n  )\n}\n\nfunction CollapsibleYear({ title, children, open }: CollapsibleYearProps) {\n  return (\n    <Collapsible className='border-t border-border/60 px-2 py-1.5' defaultOpen={open}>\n      <CollapsibleTrigger className='flex w-full items-center justify-start gap-2 rounded-lg px-2 py-1.5 text-sm font-medium hover:bg-muted/30 data-[state=open]:[&_svg]:rotate-180'>\n        <ChevronDownIcon\n          className='text-muted-foreground/80 shrink-0 transition-transform duration-200'\n          aria-hidden='true'\n        />\n        {title}\n      </CollapsibleTrigger>\n      <CollapsibleContent className='data-[state=closed]:animate-collapsible-up data-[state=open]:animate-collapsible-down overflow-hidden px-3 py-1 text-sm transition-all'>\n        {children}\n      </CollapsibleContent>\n    </Collapsible>\n  )\n}\n\nexport default Calendar21\n"
    }
  ]
}
