@surreal-ui/react 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/dist/index.js +11307 -0
  3. package/package.json +38 -0
  4. package/src/components/GlassmorphismAlert.tsx +40 -0
  5. package/src/components/GlassmorphismButton.tsx +20 -0
  6. package/src/components/GlassmorphismCard.tsx +43 -0
  7. package/src/components/GlassmorphismEmptyState.tsx +20 -0
  8. package/src/components/GlassmorphismInputGroup.tsx +148 -0
  9. package/src/components/GlassmorphismMenubar.tsx +96 -0
  10. package/src/components/GlassmorphismScrollArea.tsx +67 -0
  11. package/src/components/GlassmorphismTable.tsx +61 -0
  12. package/src/components/GlassmorphismTypography.tsx +31 -0
  13. package/src/components/MinimalistAccordion.tsx +215 -0
  14. package/src/components/MinimalistAlert.tsx +40 -0
  15. package/src/components/MinimalistAlertDialog.tsx +317 -0
  16. package/src/components/MinimalistAspectRatio.tsx +30 -0
  17. package/src/components/MinimalistAvatar.tsx +149 -0
  18. package/src/components/MinimalistBadge.tsx +49 -0
  19. package/src/components/MinimalistBreadcrumb.tsx +105 -0
  20. package/src/components/MinimalistButton.tsx +86 -0
  21. package/src/components/MinimalistButtonGroup.tsx +85 -0
  22. package/src/components/MinimalistCalendar.tsx +352 -0
  23. package/src/components/MinimalistCard.tsx +89 -0
  24. package/src/components/MinimalistCarousel.tsx +266 -0
  25. package/src/components/MinimalistChart.tsx +296 -0
  26. package/src/components/MinimalistCheckbox.tsx +117 -0
  27. package/src/components/MinimalistCodeBlock.tsx +203 -0
  28. package/src/components/MinimalistCollapsible.tsx +134 -0
  29. package/src/components/MinimalistCommand.tsx +344 -0
  30. package/src/components/MinimalistContextMenu.tsx +400 -0
  31. package/src/components/MinimalistDialog.tsx +269 -0
  32. package/src/components/MinimalistDrawer.tsx +216 -0
  33. package/src/components/MinimalistDropdownMenu.tsx +431 -0
  34. package/src/components/MinimalistEmpty.tsx +105 -0
  35. package/src/components/MinimalistEmptyState.tsx +34 -0
  36. package/src/components/MinimalistField.tsx +275 -0
  37. package/src/components/MinimalistForm.tsx +203 -0
  38. package/src/components/MinimalistHoverCard.tsx +48 -0
  39. package/src/components/MinimalistInput.tsx +24 -0
  40. package/src/components/MinimalistInputGroup.tsx +180 -0
  41. package/src/components/MinimalistInputOtp.tsx +189 -0
  42. package/src/components/MinimalistItem.tsx +181 -0
  43. package/src/components/MinimalistKbd.tsx +46 -0
  44. package/src/components/MinimalistLabel.tsx +19 -0
  45. package/src/components/MinimalistMenubar.tsx +491 -0
  46. package/src/components/MinimalistNativeSelect.tsx +77 -0
  47. package/src/components/MinimalistNavigationMenu.tsx +198 -0
  48. package/src/components/MinimalistPagination.tsx +152 -0
  49. package/src/components/MinimalistPopover.tsx +228 -0
  50. package/src/components/MinimalistProgress.tsx +39 -0
  51. package/src/components/MinimalistRadioGroup.tsx +115 -0
  52. package/src/components/MinimalistRangeCalendar.tsx +396 -0
  53. package/src/components/MinimalistResizable.tsx +54 -0
  54. package/src/components/MinimalistScrollArea.tsx +41 -0
  55. package/src/components/MinimalistSelect.tsx +265 -0
  56. package/src/components/MinimalistSeparator.tsx +45 -0
  57. package/src/components/MinimalistSheet.tsx +247 -0
  58. package/src/components/MinimalistSidebar.tsx +637 -0
  59. package/src/components/MinimalistSkeleton.tsx +16 -0
  60. package/src/components/MinimalistSlider.tsx +213 -0
  61. package/src/components/MinimalistSonner.tsx +41 -0
  62. package/src/components/MinimalistSpinner.tsx +39 -0
  63. package/src/components/MinimalistSwitch.tsx +92 -0
  64. package/src/components/MinimalistTable.tsx +65 -0
  65. package/src/components/MinimalistTabs.tsx +166 -0
  66. package/src/components/MinimalistTextarea.tsx +24 -0
  67. package/src/components/MinimalistToggle.tsx +74 -0
  68. package/src/components/MinimalistToggleGroup.tsx +196 -0
  69. package/src/components/MinimalistTooltip.tsx +172 -0
  70. package/src/components/MinimalistTypography.tsx +33 -0
  71. package/src/components/NeubrutalismCard.tsx +3 -0
  72. package/src/components/NeubrutalismItem.tsx +48 -0
  73. package/src/components/ShadcnAccordion.tsx +215 -0
  74. package/src/components/ShadcnAlertDialog.tsx +317 -0
  75. package/src/components/ShadcnAspectRatio.tsx +30 -0
  76. package/src/components/ShadcnAvatar.tsx +149 -0
  77. package/src/components/ShadcnBadge.tsx +49 -0
  78. package/src/components/ShadcnBreadcrumb.tsx +105 -0
  79. package/src/components/ShadcnButton.tsx +86 -0
  80. package/src/components/ShadcnButtonGroup.tsx +85 -0
  81. package/src/components/ShadcnCalendar.tsx +352 -0
  82. package/src/components/ShadcnCarousel.tsx +266 -0
  83. package/src/components/ShadcnChart.tsx +296 -0
  84. package/src/components/ShadcnCheckbox.tsx +117 -0
  85. package/src/components/ShadcnCodeBlock.tsx +203 -0
  86. package/src/components/ShadcnCollapsible.tsx +134 -0
  87. package/src/components/ShadcnCommand.tsx +344 -0
  88. package/src/components/ShadcnContextMenu.tsx +400 -0
  89. package/src/components/ShadcnDialog.tsx +269 -0
  90. package/src/components/ShadcnDrawer.tsx +216 -0
  91. package/src/components/ShadcnDropdownMenu.tsx +431 -0
  92. package/src/components/ShadcnEmpty.tsx +105 -0
  93. package/src/components/ShadcnField.tsx +275 -0
  94. package/src/components/ShadcnForm.tsx +203 -0
  95. package/src/components/ShadcnHoverCard.tsx +48 -0
  96. package/src/components/ShadcnInput.tsx +24 -0
  97. package/src/components/ShadcnInputGroup.tsx +180 -0
  98. package/src/components/ShadcnInputOtp.tsx +189 -0
  99. package/src/components/ShadcnItem.tsx +181 -0
  100. package/src/components/ShadcnKbd.tsx +46 -0
  101. package/src/components/ShadcnLabel.tsx +19 -0
  102. package/src/components/ShadcnMenubar.tsx +491 -0
  103. package/src/components/ShadcnNativeSelect.tsx +77 -0
  104. package/src/components/ShadcnNavigationMenu.tsx +198 -0
  105. package/src/components/ShadcnPagination.tsx +152 -0
  106. package/src/components/ShadcnPopover.tsx +228 -0
  107. package/src/components/ShadcnProgress.tsx +39 -0
  108. package/src/components/ShadcnRadioGroup.tsx +115 -0
  109. package/src/components/ShadcnRangeCalendar.tsx +396 -0
  110. package/src/components/ShadcnSelect.tsx +265 -0
  111. package/src/components/ShadcnSeparator.tsx +45 -0
  112. package/src/components/ShadcnSheet.tsx +247 -0
  113. package/src/components/ShadcnSidebar.tsx +637 -0
  114. package/src/components/ShadcnSkeleton.tsx +16 -0
  115. package/src/components/ShadcnSlider.tsx +213 -0
  116. package/src/components/ShadcnSonner.tsx +41 -0
  117. package/src/components/ShadcnSpinner.tsx +39 -0
  118. package/src/components/ShadcnSwitch.tsx +92 -0
  119. package/src/components/ShadcnTabs.tsx +166 -0
  120. package/src/components/ShadcnTextarea.tsx +24 -0
  121. package/src/components/ShadcnToggle.tsx +74 -0
  122. package/src/components/ShadcnToggleGroup.tsx +196 -0
  123. package/src/components/ShadcnTooltip.tsx +172 -0
  124. package/src/index.ts +124 -0
  125. package/tsconfig.json +10 -0
@@ -0,0 +1,49 @@
1
+ import * as React from 'react';
2
+ import { cn, cva, type VariantProps } from '@surreal-ui/core';
3
+
4
+ export const shadcnBadgeVariants = cva(
5
+ 'h-5 gap-1 rounded-4xl border border-transparent px-2 py-0.5 text-xs font-medium transition-all has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&>svg]:size-3! group/badge inline-flex w-fit shrink-0 items-center justify-center overflow-hidden whitespace-nowrap transition-colors focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 [&>svg]:pointer-events-none',
6
+ {
7
+ variants: {
8
+ variant: {
9
+ default: 'bg-primary text-primary-foreground [a]:hover:bg-primary/80',
10
+ secondary: 'bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80',
11
+ destructive:
12
+ 'bg-destructive/10 text-destructive focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:focus-visible:ring-destructive/40 [a]:hover:bg-destructive/20',
13
+ outline: 'border-border text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground',
14
+ ghost: 'hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50',
15
+ link: 'text-primary underline-offset-4 hover:underline',
16
+ },
17
+ },
18
+ defaultVariants: {
19
+ variant: 'default',
20
+ },
21
+ },
22
+ );
23
+
24
+ export type BadgeVariant = VariantProps<typeof badgeVariants>['variant'];
25
+
26
+ export interface ShadcnBadgeProps
27
+ extends
28
+ React.HTMLAttributes<HTMLSpanElement | HTMLAnchorElement>,
29
+ VariantProps<typeof badgeVariants> {
30
+ href?: string;
31
+ }
32
+
33
+ export const ShadcnBadge = React.forwardRef<HTMLElement, BadgeProps>(
34
+ ({ className, variant = 'default', href, ...props }, ref) => {
35
+ const Comp = href ? 'a' : 'span';
36
+ return (
37
+ <Comp
38
+ ref={ref as any}
39
+ data-slot="badge"
40
+ href={href}
41
+ className={cn(shadcnBadgeVariants({ variant }), className)}
42
+ {...(props as any)}
43
+ />
44
+ );
45
+ },
46
+ );
47
+ Badge.displayName = 'Badge';
48
+
49
+ export default ShadcnBadge;
@@ -0,0 +1,105 @@
1
+ import * as React from 'react';
2
+ import { ChevronRight, MoreHorizontal } from 'lucide-react';
3
+ import { cn } from '@surreal-ui/core';
4
+
5
+ export interface ShadcnBreadcrumbProps extends React.ComponentProps<'nav'> {}
6
+
7
+ export function ShadcnBreadcrumb({ className, ...props }: BreadcrumbProps) {
8
+ return (
9
+ <nav
10
+ data-slot="breadcrumb"
11
+ aria-label="breadcrumb"
12
+ className={cn('cn-breadcrumb', className)}
13
+ {...props}
14
+ />
15
+ );
16
+ }
17
+
18
+ export interface ShadcnBreadcrumbListProps extends React.ComponentProps<'ol'> {}
19
+
20
+ export function ShadcnBreadcrumbList({ className, ...props }: BreadcrumbListProps) {
21
+ return (
22
+ <ol
23
+ data-slot="breadcrumb-list"
24
+ className={cn(
25
+ 'gap-1.5 text-sm text-muted-foreground flex flex-wrap items-center wrap-break-word',
26
+ className,
27
+ )}
28
+ {...props}
29
+ />
30
+ );
31
+ }
32
+
33
+ export interface ShadcnBreadcrumbItemProps extends React.ComponentProps<'li'> {}
34
+
35
+ export function ShadcnBreadcrumbItem({ className, ...props }: BreadcrumbItemProps) {
36
+ return (
37
+ <li
38
+ data-slot="breadcrumb-item"
39
+ className={cn('gap-1 inline-flex items-center', className)}
40
+ {...props}
41
+ />
42
+ );
43
+ }
44
+
45
+ export interface ShadcnBreadcrumbLinkProps extends React.ComponentProps<'a'> {}
46
+
47
+ export function ShadcnBreadcrumbLink({ className, ...props }: BreadcrumbLinkProps) {
48
+ return (
49
+ <a
50
+ data-slot="breadcrumb-link"
51
+ className={cn('transition-colors hover:text-foreground', className)}
52
+ {...props}
53
+ />
54
+ );
55
+ }
56
+
57
+ export interface BreadcrumbPageProps extends React.ComponentProps<'span'> {}
58
+
59
+ export function BreadcrumbPage({ className, ...props }: BreadcrumbPageProps) {
60
+ return (
61
+ <span
62
+ data-slot="breadcrumb-page"
63
+ role="link"
64
+ aria-disabled="true"
65
+ aria-current="page"
66
+ className={cn('font-normal text-foreground', className)}
67
+ {...props}
68
+ />
69
+ );
70
+ }
71
+
72
+ export interface ShadcnBreadcrumbSeparatorProps extends React.ComponentProps<'li'> {}
73
+
74
+ export function ShadcnBreadcrumbSeparator({ children, className, ...props }: BreadcrumbSeparatorProps) {
75
+ return (
76
+ <li
77
+ data-slot="breadcrumb-separator"
78
+ role="presentation"
79
+ aria-hidden="true"
80
+ className={cn('[&>svg]:size-3.5', className)}
81
+ {...props}
82
+ >
83
+ {children ?? <ChevronRight className="size-3.5" />}
84
+ </li>
85
+ );
86
+ }
87
+
88
+ export interface BreadcrumbEllipsisProps extends React.ComponentProps<'span'> {}
89
+
90
+ export function BreadcrumbEllipsis({ className, ...props }: BreadcrumbEllipsisProps) {
91
+ return (
92
+ <span
93
+ data-slot="breadcrumb-ellipsis"
94
+ role="presentation"
95
+ aria-hidden="true"
96
+ className={cn('size-5 [&>svg]:size-4 flex items-center justify-center', className)}
97
+ {...props}
98
+ >
99
+ <MoreHorizontal className="size-4" />
100
+ <span className="sr-only">More</span>
101
+ </span>
102
+ );
103
+ }
104
+
105
+ export default ShadcnBreadcrumb;
@@ -0,0 +1,86 @@
1
+ import * as React from 'react';
2
+ import { cn, cva, type VariantProps } from '@surreal-ui/core';
3
+
4
+ export const shadcnButtonVariants = cva(
5
+ "rounded-lg border border-transparent bg-clip-padding text-sm font-medium focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg:not([class*='size-'])]:size-4 group/button inline-flex shrink-0 items-center justify-center whitespace-nowrap transition-all outline-none select-none disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0",
6
+ {
7
+ variants: {
8
+ variant: {
9
+ default: 'bg-primary text-primary-foreground [a]:hover:bg-primary/80',
10
+ outline:
11
+ 'border-border bg-background hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:border-input dark:bg-input/30 dark:hover:bg-input/50',
12
+ secondary:
13
+ 'bg-secondary text-secondary-foreground hover:bg-secondary/80 aria-expanded:bg-secondary aria-expanded:text-secondary-foreground',
14
+ ghost:
15
+ 'hover:bg-muted hover:text-foreground aria-expanded:bg-muted aria-expanded:text-foreground dark:hover:bg-muted/50',
16
+ destructive:
17
+ 'bg-destructive/10 text-destructive hover:bg-destructive/20 focus-visible:border-destructive/40 focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:hover:bg-destructive/30 dark:focus-visible:ring-destructive/40',
18
+ link: 'text-primary underline-offset-4 hover:underline',
19
+ },
20
+ size: {
21
+ default:
22
+ 'h-8 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2',
23
+ xs: "h-6 gap-1 rounded-[min(var(--radius-md),10px)] px-2 text-xs in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3",
24
+ sm: "h-7 gap-1 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] in-data-[slot=button-group]:rounded-lg has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
25
+ lg: 'h-9 gap-1.5 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2',
26
+ icon: 'size-8',
27
+ 'icon-xs':
28
+ "size-6 rounded-[min(var(--radius-md),10px)] in-data-[slot=button-group]:rounded-lg [&_svg:not([class*='size-'])]:size-3",
29
+ 'icon-sm':
30
+ 'size-7 rounded-[min(var(--radius-md),12px)] in-data-[slot=button-group]:rounded-lg',
31
+ 'icon-lg': 'size-9',
32
+ },
33
+ },
34
+ defaultVariants: {
35
+ variant: 'default',
36
+ size: 'default',
37
+ },
38
+ },
39
+ );
40
+
41
+ export interface ShadcnButtonProps
42
+ extends React.ButtonHTMLAttributes<HTMLButtonElement>, VariantProps<typeof buttonVariants> {
43
+ href?: string;
44
+ target?: string;
45
+ rel?: string;
46
+ }
47
+
48
+ export const ShadcnButton = React.forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(
49
+ ({ className, variant, size, href, disabled, type = 'button', children, ...props }, ref) => {
50
+ const classes = cn(shadcnButtonVariants({ variant, size, className }));
51
+
52
+ if (href) {
53
+ return (
54
+ <a
55
+ ref={ref as React.ForwardedRef<HTMLAnchorElement>}
56
+ data-slot="button"
57
+ className={classes}
58
+ href={disabled ? undefined : href}
59
+ aria-disabled={disabled}
60
+ role={disabled ? 'link' : undefined}
61
+ tabIndex={disabled ? -1 : undefined}
62
+ {...(props as React.AnchorHTMLAttributes<HTMLAnchorElement>)}
63
+ >
64
+ {children}
65
+ </a>
66
+ );
67
+ }
68
+
69
+ return (
70
+ <button
71
+ ref={ref as React.ForwardedRef<HTMLButtonElement>}
72
+ data-slot="button"
73
+ className={classes}
74
+ type={type}
75
+ disabled={disabled}
76
+ {...props}
77
+ >
78
+ {children}
79
+ </button>
80
+ );
81
+ },
82
+ );
83
+
84
+ Button.displayName = 'Button';
85
+
86
+ export default ShadcnButton;
@@ -0,0 +1,85 @@
1
+ import * as React from 'react';
2
+ import { cn, cva, type VariantProps } from '@surreal-ui/core';
3
+
4
+ export const shadcnButtonGroupVariants = cva(
5
+ "has-[>[data-slot=button-group]]:gap-2 has-[select[aria-hidden=true]:last-child]:[&>[data-slot=select-trigger]:last-of-type]:rounded-r-lg flex w-fit items-stretch [&>*]:focus-visible:relative [&>*]:focus-visible:z-10 [&>[data-slot=select-trigger]:not([class*='w-'])]:w-fit [&>input]:flex-1",
6
+ {
7
+ variants: {
8
+ orientation: {
9
+ horizontal:
10
+ '[&>[data-slot]:not(:has(~[data-slot]))]:rounded-r-lg! [&>[data-slot]]:rounded-r-none [&>[data-slot]~[data-slot]]:rounded-l-none [&>[data-slot]~[data-slot]]:border-l-0',
11
+ vertical:
12
+ '[&>[data-slot]:not(:has(~[data-slot]))]:rounded-b-lg! flex-col [&>[data-slot]]:rounded-b-none [&>[data-slot]~[data-slot]]:rounded-t-none [&>[data-slot]~[data-slot]]:border-t-0',
13
+ },
14
+ },
15
+ defaultVariants: {
16
+ orientation: 'horizontal',
17
+ },
18
+ },
19
+ );
20
+
21
+ export type ButtonGroupOrientation = VariantProps<typeof buttonGroupVariants>['orientation'];
22
+
23
+ export interface ShadcnButtonGroupProps
24
+ extends React.ComponentProps<'div'>, VariantProps<typeof buttonGroupVariants> {}
25
+
26
+ export const ShadcnButtonGroup = React.forwardRef<HTMLDivElement, ButtonGroupProps>(
27
+ ({ className, orientation = 'horizontal', ...props }, ref) => {
28
+ return (
29
+ <div
30
+ ref={ref}
31
+ role="group"
32
+ data-slot="button-group"
33
+ data-orientation={orientation}
34
+ className={cn(shadcnButtonGroupVariants({ orientation }), className)}
35
+ {...props}
36
+ />
37
+ );
38
+ },
39
+ );
40
+ ButtonGroup.displayName = 'ButtonGroup';
41
+
42
+ export interface ShadcnButtonGroupSeparatorProps extends React.ComponentProps<'div'> {
43
+ orientation?: 'horizontal' | 'vertical';
44
+ }
45
+
46
+ export const ShadcnButtonGroupSeparator = React.forwardRef<HTMLDivElement, ButtonGroupSeparatorProps>(
47
+ ({ className, orientation = 'vertical', ...props }, ref) => {
48
+ return (
49
+ <div
50
+ ref={ref}
51
+ role="separator"
52
+ aria-orientation={orientation}
53
+ data-slot="button-group-separator"
54
+ data-orientation={orientation}
55
+ className={cn(
56
+ 'bg-input relative self-stretch data-[orientation=horizontal]:mx-px data-[orientation=horizontal]:w-auto data-[orientation=vertical]:my-px data-[orientation=vertical]:h-auto',
57
+ className,
58
+ )}
59
+ {...props}
60
+ />
61
+ );
62
+ },
63
+ );
64
+ ButtonGroupSeparator.displayName = 'ButtonGroupSeparator';
65
+
66
+ export interface ShadcnButtonGroupTextProps extends React.ComponentProps<'div'> {}
67
+
68
+ export const ShadcnButtonGroupText = React.forwardRef<HTMLDivElement, ButtonGroupTextProps>(
69
+ ({ className, ...props }, ref) => {
70
+ return (
71
+ <div
72
+ ref={ref}
73
+ data-slot="button-group-text"
74
+ className={cn(
75
+ "gap-2 rounded-lg border bg-muted px-2.5 text-sm font-medium [&_svg:not([class*='size-'])]:size-4 flex items-center [&_svg]:pointer-events-none",
76
+ className,
77
+ )}
78
+ {...props}
79
+ />
80
+ );
81
+ },
82
+ );
83
+ ButtonGroupText.displayName = 'ButtonGroupText';
84
+
85
+ export default ShadcnButtonGroup;
@@ -0,0 +1,352 @@
1
+ import * as React from 'react';
2
+ import { ChevronLeft, ChevronRight, ChevronDown } from 'lucide-react';
3
+ import { cn } from '@surreal-ui/core';
4
+
5
+ export interface ShadcnCalendarProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
6
+ value?: Date | string | null;
7
+ defaultValue?: Date | string | null;
8
+ locale?: string;
9
+ weekdayFormat?: 'short' | 'narrow' | 'long';
10
+ captionLayout?: 'label' | 'dropdown' | 'dropdown-months' | 'dropdown-years';
11
+ minDate?: Date;
12
+ maxDate?: Date;
13
+ disableDaysOutsideMonth?: boolean;
14
+ onChange?: (date: Date) => void;
15
+ }
16
+
17
+ export function ShadcnCalendar({
18
+ value: controlledValue,
19
+ defaultValue = null,
20
+ locale = 'en-US',
21
+ weekdayFormat = 'short',
22
+ captionLayout = 'label',
23
+ minDate,
24
+ maxDate,
25
+ disableDaysOutsideMonth = false,
26
+ onChange,
27
+ className,
28
+ ...props
29
+ }: CalendarProps) {
30
+ const [internalDate, setInternalDate] = React.useState<Date | null>(() => {
31
+ if (controlledValue) return new Date(controlledValue);
32
+ if (defaultValue) return new Date(defaultValue);
33
+ return null;
34
+ });
35
+
36
+ const selectedDate =
37
+ controlledValue !== undefined
38
+ ? controlledValue
39
+ ? new Date(controlledValue)
40
+ : null
41
+ : internalDate;
42
+
43
+ const [currentMonth, setCurrentMonth] = React.useState<Date>(() => {
44
+ const initial = selectedDate || new Date();
45
+ return new Date(initial.getFullYear(), initial.getMonth(), 1);
46
+ });
47
+
48
+ const monthNames = React.useMemo(
49
+ () => [
50
+ 'January',
51
+ 'February',
52
+ 'March',
53
+ 'April',
54
+ 'May',
55
+ 'June',
56
+ 'July',
57
+ 'August',
58
+ 'September',
59
+ 'October',
60
+ 'November',
61
+ 'December',
62
+ ],
63
+ [],
64
+ );
65
+
66
+ const weekdays = React.useMemo(() => {
67
+ const days: string[] = [];
68
+ const temp = new Date(2023, 0, 1); // Sunday
69
+ for (let i = 0; i < 7; i++) {
70
+ days.push(new Intl.DateTimeFormat(locale, { weekday: weekdayFormat }).format(temp));
71
+ temp.setDate(temp.getDate() + 1);
72
+ }
73
+ return days;
74
+ }, [locale, weekdayFormat]);
75
+
76
+ const monthYearLabel = React.useMemo(() => {
77
+ const month = new Intl.DateTimeFormat(locale, { month: 'long' }).format(currentMonth);
78
+ const year = new Intl.DateTimeFormat(locale, { year: 'numeric' }).format(currentMonth);
79
+ return { month, year };
80
+ }, [locale, currentMonth]);
81
+
82
+ const yearList = React.useMemo(() => {
83
+ const startYear = currentMonth.getFullYear() - 10;
84
+ return Array.from({ length: 21 }, (_, i) => startYear + i);
85
+ }, [currentMonth]);
86
+
87
+ const calendarGrid = React.useMemo(() => {
88
+ const year = currentMonth.getFullYear();
89
+ const month = currentMonth.getMonth();
90
+ const firstDay = new Date(year, month, 1);
91
+ const lastDay = new Date(year, month + 1, 0);
92
+
93
+ const startOffset = firstDay.getDay();
94
+ const daysInMonth = lastDay.getDate();
95
+ const prevMonthLastDay = new Date(year, month, 0).getDate();
96
+
97
+ const today = new Date();
98
+ today.setHours(0, 0, 0, 0);
99
+
100
+ const weeks: Array<
101
+ Array<{
102
+ date: Date;
103
+ isCurrentMonth: boolean;
104
+ isToday: boolean;
105
+ isSelected: boolean;
106
+ isDisabled: boolean;
107
+ }>
108
+ > = [];
109
+ let currentWeek: Array<{
110
+ date: Date;
111
+ isCurrentMonth: boolean;
112
+ isToday: boolean;
113
+ isSelected: boolean;
114
+ isDisabled: boolean;
115
+ }> = [];
116
+
117
+ for (let i = startOffset - 1; i >= 0; i--) {
118
+ const d = new Date(year, month - 1, prevMonthLastDay - i);
119
+ d.setHours(0, 0, 0, 0);
120
+ currentWeek.push({
121
+ date: d,
122
+ isCurrentMonth: false,
123
+ isToday: d.getTime() === today.getTime(),
124
+ isSelected:
125
+ !!selectedDate &&
126
+ d.getFullYear() === selectedDate.getFullYear() &&
127
+ d.getMonth() === selectedDate.getMonth() &&
128
+ d.getDate() === selectedDate.getDate(),
129
+ isDisabled:
130
+ disableDaysOutsideMonth || (!!minDate && d < minDate) || (!!maxDate && d > maxDate),
131
+ });
132
+ }
133
+
134
+ for (let day = 1; day <= daysInMonth; day++) {
135
+ const d = new Date(year, month, day);
136
+ d.setHours(0, 0, 0, 0);
137
+ const isSelected =
138
+ !!selectedDate &&
139
+ d.getFullYear() === selectedDate.getFullYear() &&
140
+ d.getMonth() === selectedDate.getMonth() &&
141
+ d.getDate() === selectedDate.getDate();
142
+ currentWeek.push({
143
+ date: d,
144
+ isCurrentMonth: true,
145
+ isToday: d.getTime() === today.getTime(),
146
+ isSelected,
147
+ isDisabled: (!!minDate && d < minDate) || (!!maxDate && d > maxDate),
148
+ });
149
+
150
+ if (currentWeek.length === 7) {
151
+ weeks.push(currentWeek);
152
+ currentWeek = [];
153
+ }
154
+ }
155
+
156
+ if (currentWeek.length > 0) {
157
+ let nextDay = 1;
158
+ while (currentWeek.length < 7) {
159
+ const d = new Date(year, month + 1, nextDay);
160
+ d.setHours(0, 0, 0, 0);
161
+ currentWeek.push({
162
+ date: d,
163
+ isCurrentMonth: false,
164
+ isToday: d.getTime() === today.getTime(),
165
+ isSelected:
166
+ !!selectedDate &&
167
+ d.getFullYear() === selectedDate.getFullYear() &&
168
+ d.getMonth() === selectedDate.getMonth() &&
169
+ d.getDate() === selectedDate.getDate(),
170
+ isDisabled:
171
+ disableDaysOutsideMonth || (!!minDate && d < minDate) || (!!maxDate && d > maxDate),
172
+ });
173
+ nextDay++;
174
+ }
175
+ weeks.push(currentWeek);
176
+ }
177
+
178
+ return weeks;
179
+ }, [currentMonth, selectedDate, disableDaysOutsideMonth, minDate, maxDate]);
180
+
181
+ const handleSelect = (date: Date, disabled: boolean) => {
182
+ if (disabled) return;
183
+ if (controlledValue === undefined) {
184
+ setInternalDate(date);
185
+ }
186
+ onChange?.(date);
187
+ };
188
+
189
+ return (
190
+ <div
191
+ data-slot="calendar"
192
+ className={cn(
193
+ 'p-3 rounded-lg border border-border bg-background text-foreground shadow-sm inline-block select-none',
194
+ className,
195
+ )}
196
+ {...props}
197
+ >
198
+ <div className="flex flex-col gap-4">
199
+ <div className="relative flex items-center justify-between pt-1">
200
+ <div className="flex items-center gap-1.5 text-sm font-semibold pl-1">
201
+ {captionLayout === 'dropdown' || captionLayout === 'dropdown-months' ? (
202
+ <div className="relative flex items-center">
203
+ <select
204
+ aria-label="Select month"
205
+ value={currentMonth.getMonth()}
206
+ onChange={(e) => {
207
+ const m = parseInt(e.target.value, 10);
208
+ setCurrentMonth(new Date(currentMonth.getFullYear(), m, 1));
209
+ }}
210
+ className="appearance-none bg-transparent hover:bg-accent hover:text-accent-foreground text-sm font-medium py-1 ps-2 pe-6 rounded-md border border-input cursor-pointer focus:outline-none focus:ring-1 focus:ring-ring"
211
+ >
212
+ {monthNames.map((name, idx) => (
213
+ <option key={name} value={idx}>
214
+ {name}
215
+ </option>
216
+ ))}
217
+ </select>
218
+ <ChevronDown className="pointer-events-none absolute right-1.5 size-3.5 text-muted-foreground" />
219
+ </div>
220
+ ) : (
221
+ <span>{monthYearLabel.month}</span>
222
+ )}
223
+
224
+ {captionLayout === 'dropdown' || captionLayout === 'dropdown-years' ? (
225
+ <div className="relative flex items-center">
226
+ <select
227
+ aria-label="Select year"
228
+ value={currentMonth.getFullYear()}
229
+ onChange={(e) => {
230
+ const y = parseInt(e.target.value, 10);
231
+ setCurrentMonth(new Date(y, currentMonth.getMonth(), 1));
232
+ }}
233
+ className="appearance-none bg-transparent hover:bg-accent hover:text-accent-foreground text-sm font-medium py-1 ps-2 pe-6 rounded-md border border-input cursor-pointer focus:outline-none focus:ring-1 focus:ring-ring"
234
+ >
235
+ {yearList.map((y) => (
236
+ <option key={y} value={y}>
237
+ {y}
238
+ </option>
239
+ ))}
240
+ </select>
241
+ <ChevronDown className="pointer-events-none absolute right-1.5 size-3.5 text-muted-foreground" />
242
+ </div>
243
+ ) : (
244
+ <span>{monthYearLabel.year}</span>
245
+ )}
246
+ </div>
247
+
248
+ <div className="flex items-center gap-1">
249
+ <button
250
+ type="button"
251
+ onClick={() =>
252
+ setCurrentMonth(
253
+ new Date(currentMonth.getFullYear(), currentMonth.getMonth() - 1, 1),
254
+ )
255
+ }
256
+ aria-label="Previous Month"
257
+ className="inline-flex size-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
258
+ >
259
+ <ChevronLeft className="size-4" />
260
+ </button>
261
+ <button
262
+ type="button"
263
+ onClick={() =>
264
+ setCurrentMonth(
265
+ new Date(currentMonth.getFullYear(), currentMonth.getMonth() + 1, 1),
266
+ )
267
+ }
268
+ aria-label="Next Month"
269
+ className="inline-flex size-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
270
+ >
271
+ <ChevronRight className="size-4" />
272
+ </button>
273
+ </div>
274
+ </div>
275
+
276
+ <div className="w-full">
277
+ <div className="grid grid-cols-7 mb-1">
278
+ {weekdays.map((day, idx) => (
279
+ <div
280
+ key={idx}
281
+ className="size-8 flex items-center justify-center text-[0.8rem] font-medium text-muted-foreground"
282
+ >
283
+ {day.slice(0, 2)}
284
+ </div>
285
+ ))}
286
+ </div>
287
+
288
+ <div className="grid grid-cols-7 gap-y-1">
289
+ {calendarGrid.flatMap((week, wIdx) =>
290
+ week.map((cell, cIdx) => (
291
+ <button
292
+ key={`${wIdx}-${cIdx}`}
293
+ type="button"
294
+ disabled={cell.isDisabled}
295
+ onClick={() => handleSelect(cell.date, cell.isDisabled)}
296
+ className={cn(
297
+ 'size-8 flex items-center justify-center rounded-md text-sm font-normal transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring',
298
+ !cell.isCurrentMonth && 'text-muted-foreground opacity-40',
299
+ cell.isToday &&
300
+ !cell.isSelected &&
301
+ 'bg-accent text-accent-foreground font-semibold',
302
+ cell.isSelected &&
303
+ 'bg-primary text-primary-foreground font-medium hover:bg-primary hover:text-primary-foreground focus:bg-primary focus:text-primary-foreground',
304
+ !cell.isSelected &&
305
+ !cell.isDisabled &&
306
+ 'hover:bg-accent hover:text-accent-foreground',
307
+ cell.isDisabled && 'pointer-events-none opacity-20',
308
+ )}
309
+ >
310
+ {cell.date.getDate()}
311
+ </button>
312
+ )),
313
+ )}
314
+ </div>
315
+ </div>
316
+ </div>
317
+ </div>
318
+ );
319
+ }
320
+
321
+ // Subcomponent exports
322
+ export const ShadcnCalendarHeader = React.forwardRef<
323
+ HTMLDivElement,
324
+ React.HTMLAttributes<HTMLDivElement>
325
+ >(({ className, ...props }, ref) => (
326
+ <div
327
+ ref={ref}
328
+ className={cn(
329
+ 'flex h-7 w-full items-center justify-center gap-1.5 text-sm font-medium',
330
+ className,
331
+ )}
332
+ {...props}
333
+ />
334
+ ));
335
+ CalendarHeader.displayName = 'CalendarHeader';
336
+
337
+ export const CalendarMonths = React.forwardRef<
338
+ HTMLDivElement,
339
+ React.HTMLAttributes<HTMLDivElement>
340
+ >(({ className, ...props }, ref) => (
341
+ <div ref={ref} className={cn('relative flex flex-col gap-4 md:flex-row', className)} {...props} />
342
+ ));
343
+ CalendarMonths.displayName = 'CalendarMonths';
344
+
345
+ export const CalendarMonth = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
346
+ ({ className, ...props }, ref) => (
347
+ <div ref={ref} className={cn('flex w-full flex-col gap-4', className)} {...props} />
348
+ ),
349
+ );
350
+ CalendarMonth.displayName = 'CalendarMonth';
351
+
352
+ export default ShadcnCalendar;