|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as React from "react"; |
| 4 | +import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/20/solid"; |
| 5 | +import { format } from "date-fns"; |
| 6 | +import { DayPicker, useDayPicker } from "react-day-picker"; |
| 7 | +import { cn } from "~/utils/cn"; |
| 8 | + |
| 9 | +export type CalendarProps = React.ComponentProps<typeof DayPicker>; |
| 10 | + |
| 11 | +const navButtonClass = |
| 12 | + "size-7 rounded-[3px] bg-secondary border border-charcoal-600 text-text-bright hover:bg-charcoal-600 hover:border-charcoal-550 transition inline-flex items-center justify-center"; |
| 13 | + |
| 14 | +function CustomMonthCaption({ calendarMonth }: { calendarMonth: { date: Date } }) { |
| 15 | + const { goToMonth, nextMonth, previousMonth } = useDayPicker(); |
| 16 | + |
| 17 | + return ( |
| 18 | + <div className="flex w-full items-center justify-between px-1"> |
| 19 | + <button |
| 20 | + type="button" |
| 21 | + className={navButtonClass} |
| 22 | + disabled={!previousMonth} |
| 23 | + onClick={() => previousMonth && goToMonth(previousMonth)} |
| 24 | + aria-label="Go to previous month" |
| 25 | + > |
| 26 | + <ChevronLeftIcon className="size-4" /> |
| 27 | + </button> |
| 28 | + <div className="flex items-center gap-2"> |
| 29 | + <select |
| 30 | + className="rounded border border-charcoal-600 bg-charcoal-750 px-2 py-1 text-sm text-text-bright focus:border-charcoal-500 focus:outline-none" |
| 31 | + value={calendarMonth.date.getMonth()} |
| 32 | + onChange={(e) => { |
| 33 | + const newDate = new Date(calendarMonth.date); |
| 34 | + newDate.setMonth(parseInt(e.target.value)); |
| 35 | + goToMonth(newDate); |
| 36 | + }} |
| 37 | + > |
| 38 | + {Array.from({ length: 12 }, (_, i) => ( |
| 39 | + <option key={i} value={i}> |
| 40 | + {format(new Date(2000, i), "MMM")} |
| 41 | + </option> |
| 42 | + ))} |
| 43 | + </select> |
| 44 | + <select |
| 45 | + className="rounded border border-charcoal-600 bg-charcoal-750 px-2 py-1 text-sm text-text-bright focus:border-charcoal-500 focus:outline-none" |
| 46 | + value={calendarMonth.date.getFullYear()} |
| 47 | + onChange={(e) => { |
| 48 | + const newDate = new Date(calendarMonth.date); |
| 49 | + newDate.setFullYear(parseInt(e.target.value)); |
| 50 | + goToMonth(newDate); |
| 51 | + }} |
| 52 | + > |
| 53 | + {Array.from({ length: 100 }, (_, i) => { |
| 54 | + const year = new Date().getFullYear() - 50 + i; |
| 55 | + return ( |
| 56 | + <option key={year} value={year}> |
| 57 | + {year} |
| 58 | + </option> |
| 59 | + ); |
| 60 | + })} |
| 61 | + </select> |
| 62 | + </div> |
| 63 | + <button |
| 64 | + type="button" |
| 65 | + className={navButtonClass} |
| 66 | + disabled={!nextMonth} |
| 67 | + onClick={() => nextMonth && goToMonth(nextMonth)} |
| 68 | + aria-label="Go to next month" |
| 69 | + > |
| 70 | + <ChevronRightIcon className="size-4" /> |
| 71 | + </button> |
| 72 | + </div> |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +export function Calendar({ |
| 77 | + className, |
| 78 | + classNames, |
| 79 | + showOutsideDays = true, |
| 80 | + ...props |
| 81 | +}: CalendarProps) { |
| 82 | + return ( |
| 83 | + <DayPicker |
| 84 | + showOutsideDays={showOutsideDays} |
| 85 | + weekStartsOn={1} |
| 86 | + className={cn("p-3", className)} |
| 87 | + classNames={{ |
| 88 | + months: "flex flex-col sm:flex-row gap-2", |
| 89 | + month: "flex flex-col gap-4", |
| 90 | + month_caption: "flex justify-center pt-1 relative items-center w-full", |
| 91 | + caption_label: "sr-only", |
| 92 | + nav: "hidden", |
| 93 | + month_grid: "w-full border-collapse", |
| 94 | + weekdays: "flex", |
| 95 | + weekday: "text-text-dimmed rounded-md w-8 font-normal text-[0.8rem]", |
| 96 | + week: "flex w-full mt-2", |
| 97 | + day: "relative p-0 text-center text-sm focus-within:relative focus-within:z-20 [&:has([aria-selected])]:bg-charcoal-700 [&:has([aria-selected].day-outside)]:bg-charcoal-700/50 [&:has([aria-selected].day-range-end)]:rounded-r-md first:[&:has([aria-selected])]:rounded-l-md last:[&:has([aria-selected])]:rounded-r-md", |
| 98 | + day_button: cn( |
| 99 | + "size-8 p-0 font-normal text-text-bright rounded-md", |
| 100 | + "hover:bg-charcoal-700 hover:text-text-bright", |
| 101 | + "focus:bg-charcoal-700 focus:text-text-bright focus:outline-none", |
| 102 | + "aria-selected:opacity-100" |
| 103 | + ), |
| 104 | + range_start: "day-range-start rounded-l-md", |
| 105 | + range_end: "day-range-end rounded-r-md", |
| 106 | + selected: |
| 107 | + "bg-indigo-600 text-text-bright hover:bg-indigo-600 hover:text-text-bright focus:bg-indigo-600 focus:text-text-bright rounded-md", |
| 108 | + today: "bg-charcoal-700 text-text-bright rounded-md", |
| 109 | + outside: |
| 110 | + "day-outside text-text-dimmed opacity-50 aria-selected:bg-charcoal-700/50 aria-selected:text-text-dimmed aria-selected:opacity-30", |
| 111 | + disabled: "text-text-dimmed opacity-50", |
| 112 | + range_middle: "aria-selected:bg-charcoal-700 aria-selected:text-text-bright", |
| 113 | + hidden: "invisible", |
| 114 | + dropdowns: "flex gap-2 items-center justify-center", |
| 115 | + dropdown: |
| 116 | + "bg-charcoal-750 border border-charcoal-600 rounded px-2 py-1 text-sm text-text-bright focus:outline-none focus:border-charcoal-500", |
| 117 | + ...classNames, |
| 118 | + }} |
| 119 | + components={{ |
| 120 | + MonthCaption: CustomMonthCaption, |
| 121 | + }} |
| 122 | + {...props} |
| 123 | + /> |
| 124 | + ); |
| 125 | +} |
| 126 | +Calendar.displayName = "Calendar"; |
0 commit comments