| 1 | "use client"; |
| 2 | |
| 3 | import * as React from "react"; |
| 4 | import { cn } |
| 5 | import { |
| 6 | DayPicker, |
| 7 | getDefaultClassNames, |
| 8 | type DayButton, |
| 9 | type Locale, |
| 10 | } from "react-day-picker"; |
| 11 | |
| 12 | import { Button, buttonVariants |
| 13 | import { |
| 14 | ChevronLeftIcon, |
| 15 | ChevronRightIcon, |
| 16 | ChevronDownIcon, |
| 17 | } from "lucide-react"; |
| 18 | |
| 19 | function Calendar({ |
| 20 | className, |
| 21 | classNames, |
| 22 | showOutsideDays = true, |
| 23 | captionLayout = "label" |
| 24 | buttonVariant = "ghost" |
| 25 | locale, |
| 26 | formatters, |
| 27 | components, |
| 28 | ...props |
| 29 | }: React. |
| 30 | buttonVariant?: React |
| 31 | }) { |
| 32 | const defaultClassNames = |
| 33 | |
| 34 | return ( |
| 35 | <DayPicker |
| 36 | showOutsideDays={showOutsideDays} |
| 37 | className={cn( |
| 38 | "group/calendar bg-background p-2 [--cell-radius:var(--radius-md)] [--cell-size:--spacing(7)] in-data-[slot=card-content]:bg-transparent in-data-[slot=popover-content]:bg-transparent", |
| 39 | String.raw`rtl:**:[.rdp-button\_ |
| 40 | String.raw`rtl:**:[.rdp-button\_ |
| 41 | className, |
| 42 | )} |
| 43 | captionLayout={captionLayout} |
| 44 | locale={locale} |
| 45 | formatters={{ |
| 46 | formatMonthDropdown: (date) |
| 47 | date.toLocaleString(locale |
| 48 | ...formatters, |
| 49 | }} |
| 50 | classNames={{ |
| 51 | root: cn("w-fit |
| 52 | months: cn( |
| 53 | "relative flex flex-col gap-4 md:flex-row", |
| 54 | defaultClassNames.months, |
| 55 | ), |
| 56 | month: cn("flex w-full flex-col gap-4 |
| 57 | nav: cn( |
| 58 | "absolute inset-x-0 top-0 flex w-full items-center justify-between gap-1", |
| 59 | defaultClassNames.nav, |
| 60 | ), |
| 61 | button_previous: cn( |
| 62 | buttonVariants({ variant: |
| 63 | "size-(--cell-size) p-0 select-none aria-disabled:opacity-50", |
| 64 | defaultClassNames.button_previous, |
| 65 | ), |
| 66 | button_next: cn( |
| 67 | buttonVariants({ variant: |
| 68 | "size-(--cell-size) p-0 select-none aria-disabled:opacity-50", |
| 69 | defaultClassNames.button_next, |
| 70 | ), |
| 71 | month_caption: cn( |
| 72 | "flex h-(--cell-size) w-full items-center justify-center px-(--cell-size)", |
| 73 | defaultClassNames.month_caption, |
| 74 | ), |
| 75 | dropdowns: cn( |
| 76 | "flex h-(--cell-size) w-full items-center justify-center gap-1.5 text-sm font-medium", |
| 77 | defaultClassNames.dropdowns, |
| 78 | ), |
| 79 | dropdown_root: cn( |
| 80 | "relative rounded-(--cell-radius)", |
| 81 | defaultClassNames.dropdown_root, |
| 82 | ), |
| 83 | dropdown: cn( |
| 84 | "absolute inset-0 bg-popover opacity-0", |
| 85 | defaultClassNames.dropdown, |
| 86 | ), |
| 87 | caption_label: cn( |
| 88 | "font-medium select-none", |
| 89 | captionLayout === "label" |
| 90 | ? "text-sm" |
| 91 | : "flex items-center gap-1 rounded-(--cell-radius) text-sm [&>svg]:size-3.5 [&>svg]:text-muted-foreground", |
| 92 | defaultClassNames.caption_label, |
| 93 | ), |
| 94 | month_grid: cn("w-full border-collapse |
| 95 | weekdays: cn("flex |
| 96 | weekday: cn( |
| 97 | "flex-1 rounded-(--cell-radius) text-[0.8rem] font-normal text-muted-foreground select-none", |
| 98 | defaultClassNames.weekday, |
| 99 | ), |
| 100 | week: cn("mt-2 flex w-full |
| 101 | week_number_header: cn( |
| 102 | "w-(--cell-size) select-none", |
| 103 | defaultClassNames.week_number_header, |
| 104 | ), |
| 105 | week_number: cn( |
| 106 | "text-[0.8rem] text-muted-foreground select-none", |
| 107 | defaultClassNames.week_number, |
| 108 | ), |
| 109 | day: cn( |
| 110 | "group/day relative aspect-square h-full w-full rounded-(--cell-radius) p-0 text-center select-none [&:last-child[data-selected=true]_button]:rounded-r-(--cell-radius)", |
| 111 | props.showWeekNumber |
| 112 | ? "[&:nth-child(2)[data-selected=true]_button]:rounded-l-(--cell-radius)" |
| 113 | : "[&:first-child[data-selected=true]_button]:rounded-l-(--cell-radius)", |
| 114 | defaultClassNames.day, |
| 115 | ), |
| 116 | range_start: cn( |
| 117 | "relative isolate z-0 rounded-l-(--cell-radius) bg-muted after:absolute after:inset-y-0 after:right-0 after:w-4 after:bg-muted", |
| 118 | defaultClassNames.range_start, |
| 119 | ), |
| 120 | range_middle: cn("rounded-none |
| 121 | range_end: cn( |
| 122 | "relative isolate z-0 rounded-r-(--cell-radius) bg-muted after:absolute after:inset-y-0 after:left-0 after:w-4 after:bg-muted", |
| 123 | defaultClassNames.range_end, |
| 124 | ), |
| 125 | today: cn( |
| 126 | "rounded-(--cell-radius) bg-muted text-foreground data-[selected=true]:rounded-none", |
| 127 | defaultClassNames.today, |
| 128 | ), |
| 129 | outside: cn( |
| 130 | "text-muted-foreground aria-selected:text-muted-foreground", |
| 131 | defaultClassNames.outside, |
| 132 | ), |
| 133 | disabled: cn( |
| 134 | "text-muted-foreground opacity-50", |
| 135 | defaultClassNames.disabled, |
| 136 | ), |
| 137 | hidden: cn("invisible |
| 138 | ...classNames, |
| 139 | }} |
| 140 | components={{ |
| 141 | Root: ({ className |
| 142 | return ( |
| 143 | <div |
| 144 | data-slot="calendar" |
| 145 | ref={rootRef} |
| 146 | className={cn(className |
| 147 | {...props} |
| 148 | /> |
| 149 | ); |
| 150 | }, |
| 151 | Chevron: ({ className |
| 152 | if (orientation === " |
| 153 | return ( |
| 154 | <ChevronLeftIcon className={ |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | if (orientation === " |
| 159 | return ( |
| 160 | <ChevronRightIcon |
| 161 | className={cn(" |
| 162 | {...props} |
| 163 | /> |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | return ( |
| 168 | <ChevronDownIcon className={ |
| 169 | ); |
| 170 | }, |
| 171 | DayButton: ({ ... |
| 172 | <CalendarDayButton locale={ |
| 173 | ), |
| 174 | WeekNumber: ({ children |
| 175 | return ( |
| 176 | <td {...props |
| 177 | <div className="flex size-(--cell-size) items-center justify-center text-center |
| 178 | {children} |
| 179 | </div> |
| 180 | </td> |
| 181 | ); |
| 182 | }, |
| 183 | ...components, |
| 184 | }} |
| 185 | {...props} |
| 186 | /> |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | function CalendarDayButton({ |
| 191 | className, |
| 192 | day, |
| 193 | modifiers, |
| 194 | locale, |
| 195 | ...props |
| 196 | }: React. |
| 197 | const defaultClassNames = |
| 198 | |
| 199 | const ref = |
| 200 | React.useEffect(() |
| 201 | if (modifiers.focused |
| 202 | }, [modifiers.focused |
| 203 | |
| 204 | return ( |
| 205 | <Button |
| 206 | ref={ref} |
| 207 | variant="ghost" |
| 208 | size="icon" |
| 209 | data-day={day.date |
| 210 | data-selected-single={ |
| 211 | modifiers.selected && |
| 212 | !modifiers.range_start && |
| 213 | !modifiers.range_end && |
| 214 | !modifiers.range_middle |
| 215 | } |
| 216 | data-range-start={modifiers.range_start |
| 217 | data-range-end={modifiers.range_end |
| 218 | data-range-middle={modifiers.range_middle |
| 219 | className={cn( |
| 220 | "relative isolate z-10 flex aspect-square size-auto w-full min-w-(--cell-size) flex-col gap-1 border-0 leading-none font-normal group-data-[focused=true]/day:relative group-data-[focused=true]/day:z-10 group-data-[focused=true]/day:border-ring group-data-[focused=true]/day:ring-[3px] group-data-[focused=true]/day:ring-ring/50 data-[range-end=true]:rounded-(--cell-radius) data-[range-end=true]:rounded-r-(--cell-radius) data-[range-end=true]:bg-primary data-[range-end=true]:text-primary-foreground data-[range-middle=true]:rounded-none data-[range-middle=true]:bg-muted data-[range-middle=true]:text-foreground data-[range-start=true]:rounded-(--cell-radius) data-[range-start=true]:rounded-l-(--cell-radius) data-[range-start=true]:bg-primary data-[range-start=true]:text-primary-foreground data-[selected-single=true]:bg-primary data-[selected-single=true]:text-primary-foreground dark:hover:text-foreground [&>span]:text-xs [&>span]:opacity-70", |
| 221 | defaultClassNames.day, |
| 222 | className, |
| 223 | )} |
| 224 | {...props} |
| 225 | /> |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | export { Calendar, CalendarDayButton |