{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"shadcn-studio/calendar-21","type":"registry:component","title":"Calendar 21","description":"Advanced calendar with collapsible year/month selection and custom navigation","dependencies":["lucide-react"],"registryDependencies":["button","calendar","collapsible","scroll-area"],"files":[{"path":"src/registry/new-york/components/calendar/calendar-21.tsx","content":"'use client'\n\nimport { useEffect, useRef, useState } 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 '@/registry/new-york/ui/button'\nimport { Calendar } from '@/registry/new-york/ui/calendar'\nimport { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/registry/new-york/ui/collapsible'\nimport { ScrollArea } from '@/registry/new-york/ui/scroll-area'\n\nconst CalendarWithAdvanceSelectionDemo = () => {\n  const today = new Date()\n  const [month, setMonth] = useState(today)\n  const [date, setDate] = useState<Date | undefined>(today)\n  const [isYearView, setIsYearView] = useState(false)\n  const startDate = new Date(1980, 6)\n  const endDate = new Date(2030, 6)\n\n  const years = eachYearOfInterval({\n    start: startOfYear(startDate),\n    end: endOfYear(endDate)\n  })\n\n  return (\n    <div>\n      <Calendar\n        mode='single'\n        selected={date}\n        onSelect={setDate}\n        month={month}\n        onMonthChange={setMonth}\n        defaultMonth={new Date()}\n        startMonth={startDate}\n        endMonth={endDate}\n        className='overflow-hidden rounded-md border p-2'\n        classNames={{\n          month_caption: 'ml-2.5 mr-20 justify-start',\n          nav: 'flex absolute w-fit right-0 items-center'\n        }}\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                setIsYearView={setIsYearView}\n                startDate={startDate}\n                endDate={endDate}\n                years={years}\n                currentYear={month.getFullYear()}\n                currentMonth={month.getMonth()}\n                onMonthSelect={(selectedMonth: Date) => {\n                  setMonth(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}: {\n  className?: string\n  children: React.ReactNode\n  isYearView: boolean\n  setIsYearView: React.Dispatch<React.SetStateAction<boolean>>\n  startDate: Date\n  endDate: Date\n  years: Date[]\n  currentYear: number\n  currentMonth: number\n  onMonthSelect: (date: Date) => void\n}) {\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('[data-radix-scroll-area-viewport]') as HTMLElement\n\n      if (viewport) {\n        const yearTop = currentYearRef.current.offsetTop\n\n        viewport.scrollTop = yearTop\n      }\n\n      setTimeout(() => {\n        currentMonthButtonRef.current?.focus()\n      }, 100)\n    }\n  }, [isYearView])\n\n  return (\n    <div className='relative'>\n      <table className={className}>{children}</table>\n      {isYearView && (\n        <div className='bg-background absolute inset-0 z-20 -mx-2 -mb-2'>\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-7'\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}: {\n  isYearView: boolean\n  setIsYearView: React.Dispatch<React.SetStateAction<boolean>>\n} & React.HTMLAttributes<HTMLSpanElement>) {\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 }: { title: string; children: React.ReactNode; open?: boolean }) {\n  return (\n    <Collapsible className='border-t px-2 py-1.5' defaultOpen={open}>\n      <CollapsibleTrigger asChild>\n        <Button\n          className='flex w-full justify-start gap-2 text-sm font-medium hover:bg-transparent [&[data-state=open]>svg]:rotate-180'\n          variant='ghost'\n          size='sm'\n        >\n          <ChevronDownIcon\n            className='text-muted-foreground/80 shrink-0 transition-transform duration-200'\n            aria-hidden='true'\n          />\n          {title}\n        </Button>\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 CalendarWithAdvanceSelectionDemo\n","type":"registry:component","target":"components/shadcn-studio/calendar/calendar-21.tsx"}]}