|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + ChevronLeftIcon, |
| 5 | + ChevronRightIcon, |
| 6 | + ChevronsUpDownIcon, |
| 7 | +} from "lucide-react"; |
| 8 | +import type * as React from "react"; |
| 9 | +import { DayPicker } from "react-day-picker"; |
| 10 | + |
| 11 | +import { cn } from "@coss/ui/lib/utils"; |
| 12 | + |
| 13 | +const buttonClassNames = |
| 14 | + "relative flex size-(--cell-size) text-base sm:text-sm items-center justify-center rounded-lg text-foreground not-in-data-selected:hover:bg-accent disabled:pointer-events-none disabled:opacity-64 [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0"; |
| 15 | + |
| 16 | +function Calendar({ |
| 17 | + className, |
| 18 | + classNames, |
| 19 | + showOutsideDays = true, |
| 20 | + components: userComponents, |
| 21 | + mode = "single", |
| 22 | + ...props |
| 23 | +}: React.ComponentProps<typeof DayPicker>) { |
| 24 | + const defaultClassNames = { |
| 25 | + button_next: buttonClassNames, |
| 26 | + button_previous: buttonClassNames, |
| 27 | + caption_label: |
| 28 | + "text-base sm:text-sm font-medium flex items-center gap-2 h-full", |
| 29 | + day: "size-(--cell-size) text-sm py-px", |
| 30 | + day_button: cn( |
| 31 | + buttonClassNames, |
| 32 | + "in-[[data-selected]:not(.range-middle)]:transition-[color,background-color,border-radius,box-shadow] in-data-disabled:pointer-events-none focus-visible:z-1 in-data-selected:bg-primary in-data-selected:text-primary-foreground in-data-disabled:text-muted-foreground/70 in-data-disabled:line-through in-data-outside:text-muted-foreground/70 in-data-selected:in-data-outside:text-primary-foreground outline-none focus-visible:ring-ring/50 focus-visible:ring-[3px] in-[.range-start:not(.range-end)]:rounded-e-none in-[.range-end:not(.range-start)]:rounded-s-none in-[.range-middle]:rounded-none in-[.range-middle]:in-data-selected:bg-accent in-[.range-middle]:in-data-selected:text-foreground", |
| 33 | + ), |
| 34 | + dropdown: "absolute bg-popover inset-0 opacity-0", |
| 35 | + dropdown_root: |
| 36 | + "relative has-focus:border-ring has-focus:ring-ring/50 has-focus:ring-[3px] border border-input shadow-xs/5 rounded-lg px-[calc(--spacing(3)-1px)] h-9 sm:h-8 [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:-me-1", |
| 37 | + dropdowns: |
| 38 | + "w-full flex items-center text-base sm:text-sm justify-center h-(--cell-size) gap-1.5 *:[span]:font-medium", |
| 39 | + hidden: "invisible", |
| 40 | + month: "w-full", |
| 41 | + month_caption: |
| 42 | + "relative mx-(--cell-size) px-1 mb-1 flex h-(--cell-size) items-center justify-center z-2", |
| 43 | + months: "relative flex flex-col sm:flex-row gap-2", |
| 44 | + nav: "absolute top-0 flex w-full justify-between z-1", |
| 45 | + outside: |
| 46 | + "text-muted-foreground data-selected:bg-accent/50 data-selected:text-muted-foreground", |
| 47 | + range_end: "range-end", |
| 48 | + range_middle: "range-middle", |
| 49 | + range_start: "range-start", |
| 50 | + today: |
| 51 | + "*:after:pointer-events-none *:after:absolute *:after:bottom-1 *:after:start-1/2 *:after:z-1 *:after:size-[3px] *:after:-translate-x-1/2 *:after:rounded-full *:after:bg-primary [&[data-selected]:not(.range-middle)>*]:after:bg-background [&[data-disabled]>*]:after:bg-foreground/30 *:after:transition-colors", |
| 52 | + week_number: |
| 53 | + "size-(--cell-size) p-0 text-xs font-medium text-muted-foreground/70", |
| 54 | + weekday: |
| 55 | + "size-(--cell-size) p-0 text-xs font-medium text-muted-foreground/70", |
| 56 | + }; |
| 57 | + const mergedClassNames: typeof defaultClassNames = Object.keys( |
| 58 | + defaultClassNames, |
| 59 | + ).reduce( |
| 60 | + (acc, key) => { |
| 61 | + const userClass = classNames?.[key as keyof typeof classNames]; |
| 62 | + const baseClass = |
| 63 | + defaultClassNames[key as keyof typeof defaultClassNames]; |
| 64 | + |
| 65 | + acc[key as keyof typeof defaultClassNames] = userClass |
| 66 | + ? cn(baseClass, userClass) |
| 67 | + : baseClass; |
| 68 | + |
| 69 | + return acc; |
| 70 | + }, |
| 71 | + { ...defaultClassNames } as typeof defaultClassNames, |
| 72 | + ); |
| 73 | + |
| 74 | + const defaultComponents = { |
| 75 | + Chevron: ({ |
| 76 | + className, |
| 77 | + orientation, |
| 78 | + ...props |
| 79 | + }: { |
| 80 | + className?: string; |
| 81 | + orientation?: "left" | "right" | "up" | "down"; |
| 82 | + }) => { |
| 83 | + if (orientation === "left") { |
| 84 | + return ( |
| 85 | + <ChevronLeftIcon |
| 86 | + className={cn(className, "rtl:rotate-180")} |
| 87 | + {...props} |
| 88 | + aria-hidden="true" |
| 89 | + /> |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + if (orientation === "right") { |
| 94 | + return ( |
| 95 | + <ChevronRightIcon |
| 96 | + className={cn(className, "rtl:rotate-180")} |
| 97 | + {...props} |
| 98 | + aria-hidden="true" |
| 99 | + /> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <ChevronsUpDownIcon |
| 105 | + className={className} |
| 106 | + {...props} |
| 107 | + aria-hidden="true" |
| 108 | + /> |
| 109 | + ); |
| 110 | + }, |
| 111 | + }; |
| 112 | + |
| 113 | + const mergedComponents = { |
| 114 | + ...defaultComponents, |
| 115 | + ...userComponents, |
| 116 | + }; |
| 117 | + |
| 118 | + const dayPickerProps = { |
| 119 | + className: cn( |
| 120 | + "w-fit [--cell-size:--spacing(10)] sm:[--cell-size:--spacing(9)]", |
| 121 | + className, |
| 122 | + ), |
| 123 | + classNames: mergedClassNames, |
| 124 | + components: mergedComponents, |
| 125 | + "data-slot": "calendar", |
| 126 | + formatters: { |
| 127 | + formatMonthDropdown: (date: Date) => |
| 128 | + date.toLocaleString("default", { month: "short" }), |
| 129 | + } as React.ComponentProps<typeof DayPicker>["formatters"], |
| 130 | + mode, |
| 131 | + showOutsideDays, |
| 132 | + ...props, |
| 133 | + }; |
| 134 | + |
| 135 | + return ( |
| 136 | + <DayPicker |
| 137 | + {...(dayPickerProps as React.ComponentProps<typeof DayPicker>)} |
| 138 | + /> |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +export { Calendar }; |
0 commit comments