{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "mini-calendar",
  "title": "Mini Calendar",
  "description": "Notion Calendar's month picker: week numbers, today, the visible days highlighted and a way back to today.",
  "dependencies": [
    "date-fns",
    "lucide-react",
    "react-day-picker@^9"
  ],
  "registryDependencies": [
    "@calendarcn/calendar-core",
    "tooltip"
  ],
  "files": [
    {
      "path": "src/components/mini-calendar.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { isSameDay, isSameMonth } from \"date-fns\";\nimport { ChevronDownIcon, ChevronUpIcon, Undo2Icon } from \"lucide-react\";\nimport {\n  DayPicker,\n  getDefaultClassNames,\n  type DayButton,\n} from \"react-day-picker\";\n\nimport { cn } from \"@/lib/utils\";\nimport { useToday } from \"@/hooks/use-now\";\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipTrigger,\n} from \"@/components/ui/tooltip\";\nimport type { WeekStartsOn } from \"@/components/calendar-types\";\n\n/**\n * Stand-in for \"today\" on the server and during hydration, where the viewer's\n * date is unknown. It lies outside any month the calendar shows, so no day is\n * marked as today and server and client markup match (React error #418).\n */\nconst UNKNOWN_TODAY = new Date(0);\n\n/** Class the cells of the days the calendar view shows get */\nconst IN_VIEW_DAY_CLASS = \"in-view-day\";\n\n/**\n * The month navigation buttons (back to today, previous, next): a 24px\n * ghost button whose hover matches the days'\n */\nconst NAV_BUTTON_CLASS =\n  \"inline-flex size-6 shrink-0 items-center justify-center rounded-sm p-0 whitespace-nowrap outline-none select-none transition-none hover:bg-calendar-day-hover focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_svg]:pointer-events-none [&_svg]:shrink-0\";\n\ninterface MiniCalendarProps {\n  /**\n   * Day the calendar is on. Picks the month shown when there are no\n   * `visibleDays` (month view, agenda).\n   */\n  currentDate?: Date;\n  /**\n   * Days the calendar view shows. They're highlighted, and the month of the\n   * last one is shown.\n   */\n  visibleDays?: Date[];\n  /** Called with the day picked, or with today from \"Go back to today\" */\n  onDateSelect?: (date: Date) => void;\n  /** First day of the week, 0 (Sunday) to 6. Defaults to Sunday */\n  weekStartsOn?: WeekStartsOn;\n  className?: string;\n}\n\n/**\n * Month the mini calendar should display for the current navigation state.\n * Week and day views anchor to the LAST visible day, matching the header\n * (a week crossing into a new month shows the new month, like Notion\n * Calendar). Month view passes no visible days and anchors to `currentDate`.\n */\nfunction getAnchorMonth(\n  currentDate: Date | undefined,\n  visibleDays: Date[] | undefined,\n): Date | undefined {\n  if (visibleDays && visibleDays.length > 0) {\n    return visibleDays[visibleDays.length - 1];\n  }\n  return currentDate;\n}\n\n/**\n * Notion Calendar's mini calendar: week numbers, today in red, the visible\n * days highlighted, up/down chevrons to browse months, and a button back to\n * today once you browse away.\n */\nexport function MiniCalendar({\n  currentDate,\n  visibleDays,\n  onDateSelect,\n  weekStartsOn = 0,\n  className,\n}: MiniCalendarProps) {\n  // `null` on the server and during hydration\n  const today = useToday();\n  const [displayedMonth, setDisplayedMonth] = React.useState<Date>(\n    () => getAnchorMonth(currentDate, visibleDays) ?? today ?? new Date(),\n  );\n\n  // Re-sync the displayed month whenever the calendar navigates. The user can\n  // browse months freely with the chevrons; the next navigation (keyboard,\n  // header buttons, horizontal scroll, day click, view switch) snaps it back.\n  React.useEffect(() => {\n    const anchor = getAnchorMonth(currentDate, visibleDays);\n    if (!anchor) return;\n    setDisplayedMonth((prev) => (isSameMonth(prev, anchor) ? prev : anchor));\n  }, [currentDate, visibleDays]);\n\n  // Treat an unknown today as this month, so no \"back to today\" control renders\n  const isTodayMonth = today === null || isSameMonth(displayedMonth, today);\n\n  const monthLabel = isTodayMonth\n    ? undefined\n    : displayedMonth.toLocaleDateString(\"default\", {\n        month: \"long\",\n        year: \"numeric\",\n      });\n\n  const goBackToToday = () => {\n    if (!today) return;\n    setDisplayedMonth(today);\n    onDateSelect?.(today);\n  };\n\n  // Highlight the days the calendar view shows\n  const modifiers = React.useMemo(() => {\n    if (!visibleDays || visibleDays.length === 0) return undefined;\n\n    return {\n      inView: (date: Date) => visibleDays.some((d) => isSameDay(d, date)),\n    };\n  }, [visibleDays]);\n\n  const modifiersClassNames = React.useMemo(() => {\n    if (!modifiers) return undefined;\n    return { inView: IN_VIEW_DAY_CLASS };\n  }, [modifiers]);\n\n  const defaultClassNames = getDefaultClassNames();\n\n  return (\n    <DayPicker\n      mode=\"single\"\n      month={displayedMonth}\n      onMonthChange={setDisplayedMonth}\n      selected={today ?? undefined}\n      today={today ?? UNKNOWN_TODAY}\n      onSelect={(date) => {\n        if (date) {\n          onDateSelect?.(date);\n        }\n      }}\n      weekStartsOn={weekStartsOn}\n      showOutsideDays\n      showWeekNumber\n      fixedWeeks\n      captionLayout=\"label\"\n      modifiers={modifiers}\n      modifiersClassNames={modifiersClassNames}\n      className={cn(\n        \"group/calendar bg-transparent px-2 py-1 [--cell-size:--spacing(6)]\",\n        className,\n      )}\n      formatters={{\n        formatWeekNumber: (weekNumber) => String(weekNumber),\n      }}\n      classNames={{\n        root: cn(\"w-full relative\", defaultClassNames.root),\n        months: cn(\"flex gap-2 flex-col\", defaultClassNames.months),\n        month: cn(\"flex flex-col w-full gap-0\", defaultClassNames.month),\n        nav: cn(\n          \"flex w-full items-center justify-end gap-0\",\n          defaultClassNames.nav,\n        ),\n        month_caption: cn(\"hidden\", defaultClassNames.month_caption),\n        weekdays: cn(\"flex\", defaultClassNames.weekdays),\n        weekday: cn(\n          \"text-muted-foreground rounded-sm flex-1 font-normal text-xxs select-none\",\n          defaultClassNames.weekday,\n        ),\n        week: cn(\n          \"flex w-full items-center relative group/week mb-0.5\",\n          defaultClassNames.week,\n        ),\n        week_number_header: cn(\n          \"select-none w-5\",\n          defaultClassNames.week_number_header,\n        ),\n        week_number: cn(\n          \"text-xxs select-none text-muted-foreground w-5\",\n          defaultClassNames.week_number,\n        ),\n        day: cn(\n          \"relative flex items-center justify-center w-full h-7 p-0 text-center [&:last-child[data-selected=true]_button]:before:rounded-r-md group/day select-none\",\n          // The visible days share one band, rounded at its ends\n          \"[&.in-view-day]:bg-calendar-current-week\",\n          \"[&.in-view-day:not(.in-view-day+.in-view-day)]:rounded-l-md\",\n          \"[&.in-view-day:not(:has(+.in-view-day))]:rounded-r-md\",\n          // The first day column comes after the week number\n          \"[&:nth-child(2)[data-selected=true]_button]:before:rounded-l-md\",\n          defaultClassNames.day,\n        ),\n        today: cn(\"rounded-sm\", defaultClassNames.today),\n        outside: cn(\n          \"text-muted-foreground/40 aria-selected:text-muted-foreground\",\n          defaultClassNames.outside,\n        ),\n        disabled: cn(\n          \"text-muted-foreground opacity-50\",\n          defaultClassNames.disabled,\n        ),\n        hidden: cn(\"invisible\", defaultClassNames.hidden),\n      }}\n      components={{\n        Root: ({ className, rootRef, ...props }) => {\n          return (\n            <div\n              data-slot=\"mini-calendar\"\n              ref={rootRef}\n              className={cn(className)}\n              {...props}\n            />\n          );\n        },\n        Nav: ({\n          className,\n          onPreviousClick,\n          onNextClick,\n          // eslint-disable-next-line @typescript-eslint/no-unused-vars\n          previousMonth,\n          // eslint-disable-next-line @typescript-eslint/no-unused-vars\n          nextMonth,\n          ...navProps\n        }) => {\n          return (\n            <nav\n              className={cn(\"flex w-full items-center\", className)}\n              {...navProps}\n            >\n              {/* Week number space - same as week_number_header */}\n              <div className=\"w-5\" />\n\n              {/* Month label - spans 4 columns worth (Su-We) */}\n              {monthLabel && (\n                <span className=\"text-foreground flex-[4] text-sm font-medium\">\n                  {monthLabel}\n                </span>\n              )}\n              {!monthLabel && <span className=\"flex-[4]\" />}\n\n              {/* Th column - Undo button or empty space */}\n              {monthLabel ? (\n                <div className=\"flex-1 flex justify-center\">\n                  <Tooltip>\n                    <TooltipTrigger asChild>\n                      <button\n                        type=\"button\"\n                        className={NAV_BUTTON_CLASS}\n                        aria-label=\"Go back to today\"\n                        onClick={goBackToToday}\n                      >\n                        <Undo2Icon className=\"size-4\" />\n                      </button>\n                    </TooltipTrigger>\n                    <TooltipContent side=\"bottom\">\n                      Go back to today\n                    </TooltipContent>\n                  </Tooltip>\n                </div>\n              ) : (\n                <span className=\"flex-1\" />\n              )}\n\n              {/* Fr column - Up chevron */}\n              <div className=\"flex-1 flex justify-center\">\n                <Tooltip>\n                  <TooltipTrigger asChild>\n                    <button\n                      type=\"button\"\n                      className={NAV_BUTTON_CLASS}\n                      aria-label=\"Go to previous month\"\n                      onClick={onPreviousClick}\n                    >\n                      <ChevronUpIcon className=\"size-4\" />\n                    </button>\n                  </TooltipTrigger>\n                  <TooltipContent side=\"bottom\">\n                    Go to previous month\n                  </TooltipContent>\n                </Tooltip>\n              </div>\n\n              {/* Sa column - Down chevron */}\n              <div className=\"flex-1 flex justify-center\">\n                <Tooltip>\n                  <TooltipTrigger asChild>\n                    <button\n                      type=\"button\"\n                      className={NAV_BUTTON_CLASS}\n                      aria-label=\"Go to next month\"\n                      onClick={onNextClick}\n                    >\n                      <ChevronDownIcon className=\"size-4\" />\n                    </button>\n                  </TooltipTrigger>\n                  <TooltipContent side=\"bottom\">\n                    Go to next month\n                  </TooltipContent>\n                </Tooltip>\n              </div>\n            </nav>\n          );\n        },\n        DayButton: MiniCalendarDayButton,\n        WeekNumber: ({ children, ...props }) => {\n          return (\n            <td {...props}>\n              <div className=\"flex h-(--cell-size) w-5 items-center justify-center text-center text-xxs\">\n                {children}\n              </div>\n            </td>\n          );\n        },\n      }}\n    />\n  );\n}\n\n/**\n * A day of the mini calendar. The button fills its cell, so the pointer never\n * crosses a dead zone between days; the visible 28px box is its `::before`.\n */\nfunction MiniCalendarDayButton({\n  className,\n  day,\n  modifiers,\n  ...props\n}: React.ComponentProps<typeof DayButton>) {\n  const defaultClassNames = getDefaultClassNames();\n\n  const ref = React.useRef<HTMLButtonElement>(null);\n  React.useEffect(() => {\n    if (modifiers.focused) ref.current?.focus();\n  }, [modifiers.focused]);\n\n  const isInView = \"inView\" in modifiers && modifiers.inView === true;\n\n  return (\n    <button\n      ref={ref}\n      type=\"button\"\n      data-day={day.date.toLocaleDateString()}\n      data-selected-single={modifiers.selected}\n      data-outside={modifiers.outside}\n      data-today={modifiers.today}\n      data-in-view={isInView}\n      className={cn(\n        // Hit area: the whole cell plus half of the week row's mb-0.5 gap above\n        // and below, so the pointer never crosses a dead zone between days\n        \"absolute inset-x-0 -inset-y-px isolate flex size-auto shrink-0 items-center justify-center gap-2 rounded-none text-sm leading-none font-normal whitespace-nowrap transition-none outline-none\",\n        \"text-foreground hover:text-accent-foreground data-[outside=true]:text-muted-foreground/40 data-[selected-single=true]:text-primary-foreground data-[today=true]:hover:!text-primary-foreground\",\n        // Visible box: a size-7 ::before centered in the hit area, behind the label\n        \"before:absolute before:inset-0 before:-z-10 before:m-auto before:size-7 before:rounded-sm hover:before:bg-calendar-day-hover data-[in-view=true]:hover:before:bg-calendar-day-hover-current-week data-[selected-single=true]:before:bg-primary data-[today=true]:hover:before:!bg-primary\",\n        // Keyboard focus ring on the visible box, not the hit area: a 2px\n        // inset border-ring, like EVENT_FOCUS_RING_CLASS\n        \"focus-visible:before:border-2 focus-visible:before:border-ring\",\n        defaultClassNames.day,\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}