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