@surreal-ui/react 0.8.0 → 0.9.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 (128) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.js +955 -7227
  3. package/dist/utils.d.ts +9 -0
  4. package/dist/utils.js +14 -0
  5. package/package.json +25 -8
  6. package/CHANGELOG.md +0 -142
  7. package/src/components/GlassmorphismAlert.tsx +0 -43
  8. package/src/components/GlassmorphismButton.tsx +0 -20
  9. package/src/components/GlassmorphismCard.tsx +0 -43
  10. package/src/components/GlassmorphismEmptyState.tsx +0 -20
  11. package/src/components/GlassmorphismInputGroup.tsx +0 -148
  12. package/src/components/GlassmorphismMenubar.tsx +0 -96
  13. package/src/components/GlassmorphismScrollArea.tsx +0 -67
  14. package/src/components/GlassmorphismTable.tsx +0 -61
  15. package/src/components/GlassmorphismTypography.tsx +0 -31
  16. package/src/components/MinimalistAccordion.tsx +0 -215
  17. package/src/components/MinimalistAlert.tsx +0 -43
  18. package/src/components/MinimalistAlertDialog.tsx +0 -318
  19. package/src/components/MinimalistAspectRatio.tsx +0 -30
  20. package/src/components/MinimalistAvatar.tsx +0 -149
  21. package/src/components/MinimalistBadge.tsx +0 -49
  22. package/src/components/MinimalistBreadcrumb.tsx +0 -109
  23. package/src/components/MinimalistButton.tsx +0 -86
  24. package/src/components/MinimalistButtonGroup.tsx +0 -86
  25. package/src/components/MinimalistCalendar.tsx +0 -355
  26. package/src/components/MinimalistCard.tsx +0 -89
  27. package/src/components/MinimalistCarousel.tsx +0 -266
  28. package/src/components/MinimalistChart.tsx +0 -296
  29. package/src/components/MinimalistCheckbox.tsx +0 -117
  30. package/src/components/MinimalistCodeBlock.tsx +0 -203
  31. package/src/components/MinimalistCollapsible.tsx +0 -134
  32. package/src/components/MinimalistCommand.tsx +0 -344
  33. package/src/components/MinimalistContextMenu.tsx +0 -408
  34. package/src/components/MinimalistDialog.tsx +0 -273
  35. package/src/components/MinimalistDrawer.tsx +0 -220
  36. package/src/components/MinimalistDropdownMenu.tsx +0 -431
  37. package/src/components/MinimalistEmpty.tsx +0 -105
  38. package/src/components/MinimalistEmptyState.tsx +0 -34
  39. package/src/components/MinimalistField.tsx +0 -275
  40. package/src/components/MinimalistForm.tsx +0 -205
  41. package/src/components/MinimalistHoverCard.tsx +0 -48
  42. package/src/components/MinimalistInput.tsx +0 -24
  43. package/src/components/MinimalistInputGroup.tsx +0 -180
  44. package/src/components/MinimalistInputOtp.tsx +0 -189
  45. package/src/components/MinimalistItem.tsx +0 -182
  46. package/src/components/MinimalistKbd.tsx +0 -46
  47. package/src/components/MinimalistLabel.tsx +0 -19
  48. package/src/components/MinimalistMenubar.tsx +0 -491
  49. package/src/components/MinimalistNativeSelect.tsx +0 -77
  50. package/src/components/MinimalistNavigationMenu.tsx +0 -198
  51. package/src/components/MinimalistPagination.tsx +0 -152
  52. package/src/components/MinimalistPopover.tsx +0 -229
  53. package/src/components/MinimalistProgress.tsx +0 -39
  54. package/src/components/MinimalistRadioGroup.tsx +0 -115
  55. package/src/components/MinimalistRangeCalendar.tsx +0 -396
  56. package/src/components/MinimalistResizable.tsx +0 -54
  57. package/src/components/MinimalistScrollArea.tsx +0 -41
  58. package/src/components/MinimalistSelect.tsx +0 -262
  59. package/src/components/MinimalistSeparator.tsx +0 -45
  60. package/src/components/MinimalistSheet.tsx +0 -247
  61. package/src/components/MinimalistSidebar.tsx +0 -638
  62. package/src/components/MinimalistSkeleton.tsx +0 -16
  63. package/src/components/MinimalistSlider.tsx +0 -213
  64. package/src/components/MinimalistSonner.tsx +0 -41
  65. package/src/components/MinimalistSpinner.tsx +0 -39
  66. package/src/components/MinimalistSwitch.tsx +0 -92
  67. package/src/components/MinimalistTable.tsx +0 -65
  68. package/src/components/MinimalistTabs.tsx +0 -171
  69. package/src/components/MinimalistTextarea.tsx +0 -24
  70. package/src/components/MinimalistToggle.tsx +0 -74
  71. package/src/components/MinimalistToggleGroup.tsx +0 -197
  72. package/src/components/MinimalistTooltip.tsx +0 -172
  73. package/src/components/MinimalistTypography.tsx +0 -33
  74. package/src/components/NeubrutalismCard.tsx +0 -84
  75. package/src/components/NeubrutalismItem.tsx +0 -48
  76. package/src/components/ShadcnAccordion.tsx +0 -215
  77. package/src/components/ShadcnAlertDialog.tsx +0 -318
  78. package/src/components/ShadcnAspectRatio.tsx +0 -30
  79. package/src/components/ShadcnAvatar.tsx +0 -149
  80. package/src/components/ShadcnBadge.tsx +0 -49
  81. package/src/components/ShadcnBreadcrumb.tsx +0 -109
  82. package/src/components/ShadcnButton.tsx +0 -86
  83. package/src/components/ShadcnButtonGroup.tsx +0 -86
  84. package/src/components/ShadcnCalendar.tsx +0 -355
  85. package/src/components/ShadcnCarousel.tsx +0 -266
  86. package/src/components/ShadcnChart.tsx +0 -296
  87. package/src/components/ShadcnCheckbox.tsx +0 -117
  88. package/src/components/ShadcnCodeBlock.tsx +0 -203
  89. package/src/components/ShadcnCollapsible.tsx +0 -134
  90. package/src/components/ShadcnCommand.tsx +0 -344
  91. package/src/components/ShadcnContextMenu.tsx +0 -408
  92. package/src/components/ShadcnDialog.tsx +0 -273
  93. package/src/components/ShadcnDrawer.tsx +0 -220
  94. package/src/components/ShadcnDropdownMenu.tsx +0 -431
  95. package/src/components/ShadcnEmpty.tsx +0 -105
  96. package/src/components/ShadcnField.tsx +0 -275
  97. package/src/components/ShadcnForm.tsx +0 -205
  98. package/src/components/ShadcnHoverCard.tsx +0 -48
  99. package/src/components/ShadcnInput.tsx +0 -24
  100. package/src/components/ShadcnInputGroup.tsx +0 -180
  101. package/src/components/ShadcnInputOtp.tsx +0 -189
  102. package/src/components/ShadcnItem.tsx +0 -182
  103. package/src/components/ShadcnKbd.tsx +0 -46
  104. package/src/components/ShadcnLabel.tsx +0 -19
  105. package/src/components/ShadcnMenubar.tsx +0 -491
  106. package/src/components/ShadcnNativeSelect.tsx +0 -77
  107. package/src/components/ShadcnNavigationMenu.tsx +0 -198
  108. package/src/components/ShadcnPagination.tsx +0 -152
  109. package/src/components/ShadcnPopover.tsx +0 -229
  110. package/src/components/ShadcnProgress.tsx +0 -39
  111. package/src/components/ShadcnRadioGroup.tsx +0 -115
  112. package/src/components/ShadcnRangeCalendar.tsx +0 -396
  113. package/src/components/ShadcnSelect.tsx +0 -262
  114. package/src/components/ShadcnSeparator.tsx +0 -45
  115. package/src/components/ShadcnSheet.tsx +0 -247
  116. package/src/components/ShadcnSidebar.tsx +0 -638
  117. package/src/components/ShadcnSkeleton.tsx +0 -16
  118. package/src/components/ShadcnSlider.tsx +0 -213
  119. package/src/components/ShadcnSonner.tsx +0 -41
  120. package/src/components/ShadcnSpinner.tsx +0 -39
  121. package/src/components/ShadcnSwitch.tsx +0 -92
  122. package/src/components/ShadcnTabs.tsx +0 -171
  123. package/src/components/ShadcnTextarea.tsx +0 -24
  124. package/src/components/ShadcnToggle.tsx +0 -74
  125. package/src/components/ShadcnToggleGroup.tsx +0 -197
  126. package/src/components/ShadcnTooltip.tsx +0 -172
  127. package/src/index.ts +0 -124
  128. package/tsconfig.json +0 -10
@@ -1,49 +0,0 @@
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;
@@ -1,109 +0,0 @@
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({
75
- children,
76
- className,
77
- ...props
78
- }: BreadcrumbSeparatorProps) {
79
- return (
80
- <li
81
- data-slot="breadcrumb-separator"
82
- role="presentation"
83
- aria-hidden="true"
84
- className={cn('[&>svg]:size-3.5', className)}
85
- {...props}
86
- >
87
- {children ?? <ChevronRight className="size-3.5" />}
88
- </li>
89
- );
90
- }
91
-
92
- export interface BreadcrumbEllipsisProps extends React.ComponentProps<'span'> {}
93
-
94
- export function BreadcrumbEllipsis({ className, ...props }: BreadcrumbEllipsisProps) {
95
- return (
96
- <span
97
- data-slot="breadcrumb-ellipsis"
98
- role="presentation"
99
- aria-hidden="true"
100
- className={cn('size-5 [&>svg]:size-4 flex items-center justify-center', className)}
101
- {...props}
102
- >
103
- <MoreHorizontal className="size-4" />
104
- <span className="sr-only">More</span>
105
- </span>
106
- );
107
- }
108
-
109
- export default ShadcnBreadcrumb;
@@ -1,86 +0,0 @@
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;
@@ -1,86 +0,0 @@
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<
47
- HTMLDivElement,
48
- ButtonGroupSeparatorProps
49
- >(({ className, orientation = 'vertical', ...props }, ref) => {
50
- return (
51
- <div
52
- ref={ref}
53
- role="separator"
54
- aria-orientation={orientation}
55
- data-slot="button-group-separator"
56
- data-orientation={orientation}
57
- className={cn(
58
- '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',
59
- className
60
- )}
61
- {...props}
62
- />
63
- );
64
- });
65
- ButtonGroupSeparator.displayName = 'ButtonGroupSeparator';
66
-
67
- export interface ShadcnButtonGroupTextProps extends React.ComponentProps<'div'> {}
68
-
69
- export const ShadcnButtonGroupText = React.forwardRef<HTMLDivElement, ButtonGroupTextProps>(
70
- ({ className, ...props }, ref) => {
71
- return (
72
- <div
73
- ref={ref}
74
- data-slot="button-group-text"
75
- className={cn(
76
- "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",
77
- className
78
- )}
79
- {...props}
80
- />
81
- );
82
- }
83
- );
84
- ButtonGroupText.displayName = 'ButtonGroupText';
85
-
86
- export default ShadcnButtonGroup;
@@ -1,355 +0,0 @@
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<
6
- React.HTMLAttributes<HTMLDivElement>,
7
- 'onChange'
8
- > {
9
- value?: Date | string | null;
10
- defaultValue?: Date | string | null;
11
- locale?: string;
12
- weekdayFormat?: 'short' | 'narrow' | 'long';
13
- captionLayout?: 'label' | 'dropdown' | 'dropdown-months' | 'dropdown-years';
14
- minDate?: Date;
15
- maxDate?: Date;
16
- disableDaysOutsideMonth?: boolean;
17
- onChange?: (date: Date) => void;
18
- }
19
-
20
- export function ShadcnCalendar({
21
- value: controlledValue,
22
- defaultValue = null,
23
- locale = 'en-US',
24
- weekdayFormat = 'short',
25
- captionLayout = 'label',
26
- minDate,
27
- maxDate,
28
- disableDaysOutsideMonth = false,
29
- onChange,
30
- className,
31
- ...props
32
- }: CalendarProps) {
33
- const [internalDate, setInternalDate] = React.useState<Date | null>(() => {
34
- if (controlledValue) return new Date(controlledValue);
35
- if (defaultValue) return new Date(defaultValue);
36
- return null;
37
- });
38
-
39
- const selectedDate =
40
- controlledValue !== undefined
41
- ? controlledValue
42
- ? new Date(controlledValue)
43
- : null
44
- : internalDate;
45
-
46
- const [currentMonth, setCurrentMonth] = React.useState<Date>(() => {
47
- const initial = selectedDate || new Date();
48
- return new Date(initial.getFullYear(), initial.getMonth(), 1);
49
- });
50
-
51
- const monthNames = React.useMemo(
52
- () => [
53
- 'January',
54
- 'February',
55
- 'March',
56
- 'April',
57
- 'May',
58
- 'June',
59
- 'July',
60
- 'August',
61
- 'September',
62
- 'October',
63
- 'November',
64
- 'December'
65
- ],
66
- []
67
- );
68
-
69
- const weekdays = React.useMemo(() => {
70
- const days: string[] = [];
71
- const temp = new Date(2023, 0, 1); // Sunday
72
- for (let i = 0; i < 7; i++) {
73
- days.push(new Intl.DateTimeFormat(locale, { weekday: weekdayFormat }).format(temp));
74
- temp.setDate(temp.getDate() + 1);
75
- }
76
- return days;
77
- }, [locale, weekdayFormat]);
78
-
79
- const monthYearLabel = React.useMemo(() => {
80
- const month = new Intl.DateTimeFormat(locale, { month: 'long' }).format(currentMonth);
81
- const year = new Intl.DateTimeFormat(locale, { year: 'numeric' }).format(currentMonth);
82
- return { month, year };
83
- }, [locale, currentMonth]);
84
-
85
- const yearList = React.useMemo(() => {
86
- const startYear = currentMonth.getFullYear() - 10;
87
- return Array.from({ length: 21 }, (_, i) => startYear + i);
88
- }, [currentMonth]);
89
-
90
- const calendarGrid = React.useMemo(() => {
91
- const year = currentMonth.getFullYear();
92
- const month = currentMonth.getMonth();
93
- const firstDay = new Date(year, month, 1);
94
- const lastDay = new Date(year, month + 1, 0);
95
-
96
- const startOffset = firstDay.getDay();
97
- const daysInMonth = lastDay.getDate();
98
- const prevMonthLastDay = new Date(year, month, 0).getDate();
99
-
100
- const today = new Date();
101
- today.setHours(0, 0, 0, 0);
102
-
103
- const weeks: Array<
104
- Array<{
105
- date: Date;
106
- isCurrentMonth: boolean;
107
- isToday: boolean;
108
- isSelected: boolean;
109
- isDisabled: boolean;
110
- }>
111
- > = [];
112
- let currentWeek: Array<{
113
- date: Date;
114
- isCurrentMonth: boolean;
115
- isToday: boolean;
116
- isSelected: boolean;
117
- isDisabled: boolean;
118
- }> = [];
119
-
120
- for (let i = startOffset - 1; i >= 0; i--) {
121
- const d = new Date(year, month - 1, prevMonthLastDay - i);
122
- d.setHours(0, 0, 0, 0);
123
- currentWeek.push({
124
- date: d,
125
- isCurrentMonth: false,
126
- isToday: d.getTime() === today.getTime(),
127
- isSelected:
128
- !!selectedDate &&
129
- d.getFullYear() === selectedDate.getFullYear() &&
130
- d.getMonth() === selectedDate.getMonth() &&
131
- d.getDate() === selectedDate.getDate(),
132
- isDisabled:
133
- disableDaysOutsideMonth || (!!minDate && d < minDate) || (!!maxDate && d > maxDate)
134
- });
135
- }
136
-
137
- for (let day = 1; day <= daysInMonth; day++) {
138
- const d = new Date(year, month, day);
139
- d.setHours(0, 0, 0, 0);
140
- const isSelected =
141
- !!selectedDate &&
142
- d.getFullYear() === selectedDate.getFullYear() &&
143
- d.getMonth() === selectedDate.getMonth() &&
144
- d.getDate() === selectedDate.getDate();
145
- currentWeek.push({
146
- date: d,
147
- isCurrentMonth: true,
148
- isToday: d.getTime() === today.getTime(),
149
- isSelected,
150
- isDisabled: (!!minDate && d < minDate) || (!!maxDate && d > maxDate)
151
- });
152
-
153
- if (currentWeek.length === 7) {
154
- weeks.push(currentWeek);
155
- currentWeek = [];
156
- }
157
- }
158
-
159
- if (currentWeek.length > 0) {
160
- let nextDay = 1;
161
- while (currentWeek.length < 7) {
162
- const d = new Date(year, month + 1, nextDay);
163
- d.setHours(0, 0, 0, 0);
164
- currentWeek.push({
165
- date: d,
166
- isCurrentMonth: false,
167
- isToday: d.getTime() === today.getTime(),
168
- isSelected:
169
- !!selectedDate &&
170
- d.getFullYear() === selectedDate.getFullYear() &&
171
- d.getMonth() === selectedDate.getMonth() &&
172
- d.getDate() === selectedDate.getDate(),
173
- isDisabled:
174
- disableDaysOutsideMonth || (!!minDate && d < minDate) || (!!maxDate && d > maxDate)
175
- });
176
- nextDay++;
177
- }
178
- weeks.push(currentWeek);
179
- }
180
-
181
- return weeks;
182
- }, [currentMonth, selectedDate, disableDaysOutsideMonth, minDate, maxDate]);
183
-
184
- const handleSelect = (date: Date, disabled: boolean) => {
185
- if (disabled) return;
186
- if (controlledValue === undefined) {
187
- setInternalDate(date);
188
- }
189
- onChange?.(date);
190
- };
191
-
192
- return (
193
- <div
194
- data-slot="calendar"
195
- className={cn(
196
- 'p-3 rounded-lg border border-border bg-background text-foreground shadow-sm inline-block select-none',
197
- className
198
- )}
199
- {...props}
200
- >
201
- <div className="flex flex-col gap-4">
202
- <div className="relative flex items-center justify-between pt-1">
203
- <div className="flex items-center gap-1.5 text-sm font-semibold pl-1">
204
- {captionLayout === 'dropdown' || captionLayout === 'dropdown-months' ? (
205
- <div className="relative flex items-center">
206
- <select
207
- aria-label="Select month"
208
- value={currentMonth.getMonth()}
209
- onChange={(e) => {
210
- const m = parseInt(e.target.value, 10);
211
- setCurrentMonth(new Date(currentMonth.getFullYear(), m, 1));
212
- }}
213
- 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"
214
- >
215
- {monthNames.map((name, idx) => (
216
- <option key={name} value={idx}>
217
- {name}
218
- </option>
219
- ))}
220
- </select>
221
- <ChevronDown className="pointer-events-none absolute right-1.5 size-3.5 text-muted-foreground" />
222
- </div>
223
- ) : (
224
- <span>{monthYearLabel.month}</span>
225
- )}
226
-
227
- {captionLayout === 'dropdown' || captionLayout === 'dropdown-years' ? (
228
- <div className="relative flex items-center">
229
- <select
230
- aria-label="Select year"
231
- value={currentMonth.getFullYear()}
232
- onChange={(e) => {
233
- const y = parseInt(e.target.value, 10);
234
- setCurrentMonth(new Date(y, currentMonth.getMonth(), 1));
235
- }}
236
- 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"
237
- >
238
- {yearList.map((y) => (
239
- <option key={y} value={y}>
240
- {y}
241
- </option>
242
- ))}
243
- </select>
244
- <ChevronDown className="pointer-events-none absolute right-1.5 size-3.5 text-muted-foreground" />
245
- </div>
246
- ) : (
247
- <span>{monthYearLabel.year}</span>
248
- )}
249
- </div>
250
-
251
- <div className="flex items-center gap-1">
252
- <button
253
- type="button"
254
- onClick={() =>
255
- setCurrentMonth(
256
- new Date(currentMonth.getFullYear(), currentMonth.getMonth() - 1, 1)
257
- )
258
- }
259
- aria-label="Previous Month"
260
- 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"
261
- >
262
- <ChevronLeft className="size-4" />
263
- </button>
264
- <button
265
- type="button"
266
- onClick={() =>
267
- setCurrentMonth(
268
- new Date(currentMonth.getFullYear(), currentMonth.getMonth() + 1, 1)
269
- )
270
- }
271
- aria-label="Next Month"
272
- 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"
273
- >
274
- <ChevronRight className="size-4" />
275
- </button>
276
- </div>
277
- </div>
278
-
279
- <div className="w-full">
280
- <div className="grid grid-cols-7 mb-1">
281
- {weekdays.map((day, idx) => (
282
- <div
283
- key={idx}
284
- className="size-8 flex items-center justify-center text-[0.8rem] font-medium text-muted-foreground"
285
- >
286
- {day.slice(0, 2)}
287
- </div>
288
- ))}
289
- </div>
290
-
291
- <div className="grid grid-cols-7 gap-y-1">
292
- {calendarGrid.flatMap((week, wIdx) =>
293
- week.map((cell, cIdx) => (
294
- <button
295
- key={`${wIdx}-${cIdx}`}
296
- type="button"
297
- disabled={cell.isDisabled}
298
- onClick={() => handleSelect(cell.date, cell.isDisabled)}
299
- className={cn(
300
- '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',
301
- !cell.isCurrentMonth && 'text-muted-foreground opacity-40',
302
- cell.isToday &&
303
- !cell.isSelected &&
304
- 'bg-accent text-accent-foreground font-semibold',
305
- cell.isSelected &&
306
- 'bg-primary text-primary-foreground font-medium hover:bg-primary hover:text-primary-foreground focus:bg-primary focus:text-primary-foreground',
307
- !cell.isSelected &&
308
- !cell.isDisabled &&
309
- 'hover:bg-accent hover:text-accent-foreground',
310
- cell.isDisabled && 'pointer-events-none opacity-20'
311
- )}
312
- >
313
- {cell.date.getDate()}
314
- </button>
315
- ))
316
- )}
317
- </div>
318
- </div>
319
- </div>
320
- </div>
321
- );
322
- }
323
-
324
- // Subcomponent exports
325
- export const ShadcnCalendarHeader = React.forwardRef<
326
- HTMLDivElement,
327
- React.HTMLAttributes<HTMLDivElement>
328
- >(({ className, ...props }, ref) => (
329
- <div
330
- ref={ref}
331
- className={cn(
332
- 'flex h-7 w-full items-center justify-center gap-1.5 text-sm font-medium',
333
- className
334
- )}
335
- {...props}
336
- />
337
- ));
338
- CalendarHeader.displayName = 'CalendarHeader';
339
-
340
- export const CalendarMonths = React.forwardRef<
341
- HTMLDivElement,
342
- React.HTMLAttributes<HTMLDivElement>
343
- >(({ className, ...props }, ref) => (
344
- <div ref={ref} className={cn('relative flex flex-col gap-4 md:flex-row', className)} {...props} />
345
- ));
346
- CalendarMonths.displayName = 'CalendarMonths';
347
-
348
- export const CalendarMonth = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
349
- ({ className, ...props }, ref) => (
350
- <div ref={ref} className={cn('flex w-full flex-col gap-4', className)} {...props} />
351
- )
352
- );
353
- CalendarMonth.displayName = 'CalendarMonth';
354
-
355
- export default ShadcnCalendar;