Open History to see the last commit
1import { DayPicker } from "@daypicker/react";
2import { ChevronLeftIcon, ChevronRightIcon, ChevronsUpDownIcon } from "lucide-react";
3import type * as React from "react";
4import { useFormattingLocale } from "~/i18n/I18nProvider";
5import { cn } from "~/lib/utils";
6
7const buttonClassNames =
8 "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";
9
10const defaultComponents = {
11 Chevron: ({
12 className,
13 orientation,
14 ...props
15 }: {
16 className?: string;
17 orientation?: "left" | "right" | "up" | "down";
18 }): React.ReactElement => {
19 if (orientation === "left") {
20 return (
21 <ChevronLeftIcon
22 className={cn(className, "rtl:rotate-180")}
23 {...props}
24 aria-hidden="true"
25 />
26 );
27 }
28
29 if (orientation === "right") {
30 return (
31 <ChevronRightIcon
32 className={cn(className, "rtl:rotate-180")}
33 {...props}
34 aria-hidden="true"
35 />
36 );
37 }
38
39 return <ChevronsUpDownIcon className={className} {...props} aria-hidden="true" />;
40 },
41};
42
43// DayPicker otherwise formats and labels with date-fns enUS, whatever the app language.
44function localizedFormatters(locale: string | undefined, timeZone: string | undefined) {
45 // DayPicker's grid is always Gregorian, so the month names must be too (fa-IR defaults to Persian).
46 const calendar = "gregory";
47 const caption = new Intl.DateTimeFormat(locale, {
48 month: "long",
49 year: "numeric",
50 calendar,
51 timeZone,
52 });
53 const monthDropdown = new Intl.DateTimeFormat(locale, { month: "short", calendar, timeZone });
54 const weekday = new Intl.DateTimeFormat(locale, { weekday: "short", timeZone });
55 const formatters = {
56 formatCaption: (month: Date) => caption.format(month),
57 formatMonthDropdown: (month: Date) => monthDropdown.format(month),
58 };
59 // Intl has no two-letter weekday, so English keeps DayPicker's own "Mo".
60 if (weekday.resolvedOptions().locale.startsWith("en")) return formatters;
61 return { ...formatters, formatWeekdayName: (day: Date) => weekday.format(day) };
62}
63
64export function Calendar({
65 className,
66 classNames,
67 showOutsideDays = true,
68 components: userComponents,
69 formatters: userFormatters,
70 labels: userLabels,
71 mode = "single",
72 timeZone,
73 ...props
74}: React.ComponentProps<typeof DayPicker>): React.ReactElement {
75 const localized = localizedFormatters(useFormattingLocale(), timeZone);
76 const defaultClassNames = {
77 button_next: buttonClassNames,
78 button_previous: buttonClassNames,
79 caption_label: "text-base sm:text-sm font-medium flex items-center gap-2 h-full",
80 day: "size-(--cell-size) text-sm py-px",
81 day_button: cn(
82 buttonClassNames,
83 "in-data-disabled:pointer-events-none in-[.range-middle]:rounded-none in-[.range-end:not(.range-start)]:rounded-s-none in-[.range-start:not(.range-end)]:rounded-e-none in-[.range-middle]:in-data-selected:bg-accent in-data-selected:bg-primary in-[.range-middle]:in-data-selected:text-foreground in-data-disabled:text-muted-foreground/72 in-data-outside:text-muted-foreground/72 in-data-selected:in-data-outside:text-primary-foreground in-data-selected:text-primary-foreground in-data-disabled:line-through outline-none in-[[data-selected]:not(.range-middle)]:transition-[border-radius,box-shadow] focus-visible:z-1 focus-visible:ring-[3px] focus-visible:ring-inset focus-visible:ring-ring/50",
84 ),
85 dropdown: "absolute bg-popover inset-0 opacity-0",
86 dropdown_root:
87 "relative has-focus:border-ring has-focus:ring-ring/50 has-focus:ring-[3px] has-focus:ring-inset 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",
88 dropdowns:
89 "w-full flex items-center text-base sm:text-sm justify-center h-(--cell-size) gap-1.5 *:[span]:font-medium",
90 hidden: "invisible",
91 month: "w-full",
92 month_caption:
93 "relative mx-(--cell-size) px-1 mb-1 flex h-(--cell-size) items-center justify-center z-2",
94 months: "relative flex flex-col sm:flex-row gap-2",
95 nav: "absolute top-0 flex w-full justify-between z-1",
96 outside: "text-muted-foreground data-selected:bg-accent/50 data-selected:text-muted-foreground",
97 range_end: "range-end",
98 range_middle: "range-middle",
99 range_start: "range-start",
100 today:
101 "*: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",
102 week_number: "size-(--cell-size) p-0 text-xs font-medium text-muted-foreground/72",
103 weekday: "size-(--cell-size) p-0 text-xs font-medium text-muted-foreground/72",
104 };
105 const mergedClassNames: typeof defaultClassNames = Object.keys(defaultClassNames).reduce(
106 (acc, key) => {
107 const userClass = classNames?.[key as keyof typeof classNames];
108 const baseClass = defaultClassNames[key as keyof typeof defaultClassNames];
109
110 acc[key as keyof typeof defaultClassNames] = userClass ? cn(baseClass, userClass) : baseClass;
111
112 return acc;
113 },
114 { ...defaultClassNames } as typeof defaultClassNames,
115 );
116
117 const mergedComponents = {
118 ...defaultComponents,
119 ...userComponents,
120 };
121
122 const dayPickerProps = {
123 className: cn("w-fit [--cell-size:--spacing(10)] sm:[--cell-size:--spacing(9)]", className),
124 classNames: mergedClassNames,
125 components: mergedComponents,
126 "data-slot": "calendar",
127 formatters: { ...localized, ...userFormatters },
128 labels: { labelGrid: localized.formatCaption, ...userLabels },
129 mode,
130 showOutsideDays,
131 timeZone,
132 ...props,
133 };
134
135 return <DayPicker {...(dayPickerProps as React.ComponentProps<typeof DayPicker>)} />;
136}