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