calendar.tsx 8.3 KB

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