@dinachi/cli 1.0.0 → 1.3.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 (105) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/dist/index.js +244 -72
  3. package/package.json +5 -1
  4. package/templates/accordion/accordion.tsx +5 -1
  5. package/templates/agent/agent.tsx +35 -18
  6. package/templates/alert/alert.tsx +345 -0
  7. package/templates/alert/index.ts +16 -0
  8. package/templates/alert-dialog/alert-dialog.tsx +14 -3
  9. package/templates/animated-icon/animated-icon.tsx +1 -0
  10. package/templates/animated-list/animated-list.tsx +2 -1
  11. package/templates/animated-tabs/animated-tabs.tsx +4 -0
  12. package/templates/ask-bar/ask-bar.tsx +1061 -0
  13. package/templates/ask-bar/index.ts +1 -0
  14. package/templates/autocomplete/autocomplete.tsx +14 -4
  15. package/templates/avatar/avatar.tsx +3 -0
  16. package/templates/badge/badge.tsx +1 -0
  17. package/templates/breadcrumb/breadcrumb.tsx +606 -0
  18. package/templates/breadcrumb/index.ts +9 -0
  19. package/templates/button/button.tsx +1 -0
  20. package/templates/calendar/calendar.tsx +1155 -0
  21. package/templates/calendar/index.ts +15 -0
  22. package/templates/card/card.tsx +6 -1
  23. package/templates/carousel/carousel.tsx +7 -0
  24. package/templates/checkbox/checkbox.tsx +1 -0
  25. package/templates/checkbox-group/checkbox-group.tsx +1 -0
  26. package/templates/collapsible/collapsible.tsx +3 -1
  27. package/templates/combobox/combobox.tsx +15 -4
  28. package/templates/command-items/command-items.tsx +69 -0
  29. package/templates/command-items/index.ts +1 -0
  30. package/templates/command-palette/command-palette.tsx +954 -0
  31. package/templates/command-palette/index.ts +1 -0
  32. package/templates/compare-slider/compare-slider.tsx +1 -0
  33. package/templates/comparison-table/comparison-table.tsx +540 -0
  34. package/templates/comparison-table/index.ts +15 -0
  35. package/templates/context-menu/context-menu.tsx +61 -14
  36. package/templates/data-table/data-table.tsx +2077 -0
  37. package/templates/data-table/index.ts +14 -0
  38. package/templates/date-picker/date-picker.tsx +527 -0
  39. package/templates/date-picker/index.ts +2 -0
  40. package/templates/date-range-picker/date-range-picker.tsx +504 -0
  41. package/templates/date-range-picker/index.ts +2 -0
  42. package/templates/detail-list/detail-list.tsx +419 -0
  43. package/templates/detail-list/index.ts +12 -0
  44. package/templates/dialog/dialog.tsx +13 -3
  45. package/templates/drawer/drawer.tsx +19 -6
  46. package/templates/expandable-card/expandable-card.tsx +5 -1
  47. package/templates/field/field.tsx +5 -1
  48. package/templates/fieldset/fieldset.tsx +2 -0
  49. package/templates/form/form.tsx +1 -0
  50. package/templates/heat-table/heat-table.tsx +613 -0
  51. package/templates/heat-table/index.ts +9 -0
  52. package/templates/hold-to-confirm/hold-to-confirm.tsx +1 -0
  53. package/templates/input/input.tsx +1 -0
  54. package/templates/label/label.tsx +1 -0
  55. package/templates/link/link.tsx +1 -0
  56. package/templates/load-transition/load-transition.tsx +1 -0
  57. package/templates/marquee/marquee.tsx +1 -0
  58. package/templates/menu/menu.tsx +27 -6
  59. package/templates/menubar/menubar.tsx +15 -3
  60. package/templates/meter/meter.tsx +5 -1
  61. package/templates/month-picker/index.ts +1 -0
  62. package/templates/month-picker/month-picker.tsx +351 -0
  63. package/templates/navigation-menu/navigation-menu.tsx +13 -2
  64. package/templates/number-field/number-field.tsx +7 -1
  65. package/templates/number-ticker/number-ticker.tsx +3 -0
  66. package/templates/omnibar/index.ts +1 -0
  67. package/templates/omnibar/omnibar.tsx +1009 -0
  68. package/templates/otp-field/otp-field.tsx +4 -0
  69. package/templates/pagination/index.ts +8 -0
  70. package/templates/pagination/pagination.tsx +389 -0
  71. package/templates/popover/popover.tsx +19 -5
  72. package/templates/preview-card/preview-card.tsx +11 -3
  73. package/templates/progress/progress.tsx +5 -1
  74. package/templates/progress-ring/progress-ring.tsx +1 -0
  75. package/templates/radio/radio.tsx +3 -0
  76. package/templates/scroll-area/scroll-area.tsx +14 -4
  77. package/templates/scroll-progress/scroll-progress.tsx +1 -0
  78. package/templates/scroll-reveal/scroll-reveal.tsx +1 -0
  79. package/templates/select/select.tsx +18 -2
  80. package/templates/separator/separator.tsx +1 -0
  81. package/templates/skeleton/skeleton.tsx +1 -0
  82. package/templates/slider/slider.tsx +7 -0
  83. package/templates/sortable/sortable.tsx +3 -0
  84. package/templates/stagger-list/stagger-list.tsx +2 -0
  85. package/templates/streaming-text/streaming-text.tsx +1 -0
  86. package/templates/swipeable-row/swipeable-row.tsx +1 -0
  87. package/templates/switch/switch.tsx +2 -0
  88. package/templates/table/index.ts +13 -0
  89. package/templates/table/table.tsx +265 -0
  90. package/templates/tabs/tabs.tsx +5 -0
  91. package/templates/text/text.tsx +1 -0
  92. package/templates/text-morph/text-morph.tsx +2 -0
  93. package/templates/text-shimmer/text-shimmer.tsx +1 -0
  94. package/templates/textarea/textarea.tsx +1 -0
  95. package/templates/time-slots/index.ts +1 -0
  96. package/templates/time-slots/time-slots.tsx +589 -0
  97. package/templates/toast/toast.tsx +10 -1
  98. package/templates/toggle/toggle.tsx +1 -0
  99. package/templates/toggle-group/toggle-group.tsx +2 -0
  100. package/templates/toolbar/toolbar.tsx +6 -0
  101. package/templates/tooltip/tooltip.tsx +9 -3
  102. package/templates/week-strip/index.ts +1 -0
  103. package/templates/week-strip/week-strip.tsx +329 -0
  104. package/templates/year-view/index.ts +1 -0
  105. package/templates/year-view/year-view.tsx +362 -0
@@ -41,6 +41,7 @@ const MenuTrigger = React.forwardRef<
41
41
  "data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
42
42
  className
43
43
  )}
44
+ data-slot="menu-trigger"
44
45
  {...props}
45
46
  />
46
47
  ))
@@ -48,7 +49,7 @@ MenuTrigger.displayName = "MenuTrigger"
48
49
 
49
50
  const MenuPortal: React.FC<
50
51
  React.ComponentProps<typeof MenuPrimitive.Portal>
51
- > = (props) => <MenuPrimitive.Portal {...props} />
52
+ > = (props) => <MenuPrimitive.Portal data-slot="menu-portal" {...props} />
52
53
  MenuPortal.displayName = "MenuPortal"
53
54
 
54
55
  const MenuPositioner = React.forwardRef<
@@ -59,17 +60,23 @@ const MenuPositioner = React.forwardRef<
59
60
  ref={ref}
60
61
  className={cn("z-50 outline-none", className)}
61
62
  sideOffset={6}
63
+ data-slot="menu-positioner"
62
64
  {...props}
63
65
  />
64
66
  ))
65
67
  MenuPositioner.displayName = "MenuPositioner"
66
68
 
69
+ type MenuPlacement = Pick<
70
+ React.ComponentProps<typeof MenuPrimitive.Positioner>,
71
+ "side" | "align" | "sideOffset" | "alignOffset"
72
+ >
73
+
67
74
  const MenuContent = React.forwardRef<
68
75
  React.ComponentRef<typeof MenuPrimitive.Popup>,
69
- React.ComponentProps<typeof MenuPrimitive.Popup>
70
- >(({ className, ...props }, ref) => (
76
+ React.ComponentProps<typeof MenuPrimitive.Popup> & MenuPlacement
77
+ >(({ className, side, align, sideOffset, alignOffset, ...props }, ref) => (
71
78
  <MenuPortal>
72
- <MenuPositioner>
79
+ <MenuPositioner side={side} align={align} sideOffset={sideOffset ?? 6} alignOffset={alignOffset}>
73
80
  <MenuPrimitive.Popup
74
81
  ref={useMergedRef(ref, React.useContext(MenuAgentContext)?.popup)}
75
82
  className={cn(
@@ -81,6 +88,7 @@ const MenuContent = React.forwardRef<
81
88
  "motion-reduce:data-[starting-style]:scale-100 motion-reduce:data-[ending-style]:scale-100",
82
89
  className
83
90
  )}
91
+ data-slot="menu-content"
84
92
  {...props}
85
93
  />
86
94
  </MenuPositioner>
@@ -104,6 +112,7 @@ const MenuItem = React.forwardRef<
104
112
  inset && "pl-8",
105
113
  className
106
114
  )}
115
+ data-slot="menu-item"
107
116
  {...props}
108
117
  />
109
118
  ))
@@ -121,6 +130,7 @@ const MenuLinkItem = React.forwardRef<
121
130
  "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
122
131
  className
123
132
  )}
133
+ data-slot="menu-link-item"
124
134
  {...props}
125
135
  />
126
136
  ))
@@ -139,6 +149,7 @@ const MenuCheckboxItem = React.forwardRef<
139
149
  "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
140
150
  className
141
151
  )}
152
+ data-slot="menu-checkbox-item"
142
153
  {...props}
143
154
  >
144
155
  <MenuPrimitive.CheckboxItemIndicator className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
@@ -153,7 +164,7 @@ const MenuRadioGroup = React.forwardRef<
153
164
  React.ComponentRef<typeof MenuPrimitive.RadioGroup>,
154
165
  React.ComponentProps<typeof MenuPrimitive.RadioGroup>
155
166
  >(({ className, ...props }, ref) => (
156
- <MenuPrimitive.RadioGroup ref={ref} className={className} {...props} />
167
+ <MenuPrimitive.RadioGroup ref={ref} className={className} data-slot="menu-radio-group" {...props} />
157
168
  ))
158
169
  MenuRadioGroup.displayName = "MenuRadioGroup"
159
170
 
@@ -170,6 +181,7 @@ const MenuRadioItem = React.forwardRef<
170
181
  "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
171
182
  className
172
183
  )}
184
+ data-slot="menu-radio-item"
173
185
  {...props}
174
186
  >
175
187
  <MenuPrimitive.RadioItemIndicator className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
@@ -184,7 +196,7 @@ const MenuGroup = React.forwardRef<
184
196
  React.ComponentRef<typeof MenuPrimitive.Group>,
185
197
  React.ComponentProps<typeof MenuPrimitive.Group>
186
198
  >(({ className, ...props }, ref) => (
187
- <MenuPrimitive.Group ref={ref} className={className} {...props} />
199
+ <MenuPrimitive.Group ref={ref} className={className} data-slot="menu-group" {...props} />
188
200
  ))
189
201
  MenuGroup.displayName = "MenuGroup"
190
202
 
@@ -197,6 +209,7 @@ const MenuLabel = React.forwardRef<
197
209
  <MenuPrimitive.GroupLabel
198
210
  ref={ref}
199
211
  className={cn("px-2 py-1.5 text-sm font-medium", inset && "pl-8", className)}
212
+ data-slot="menu-label"
200
213
  {...props}
201
214
  />
202
215
  ))
@@ -209,6 +222,7 @@ const MenuSeparator = React.forwardRef<
209
222
  <MenuPrimitive.Separator
210
223
  ref={ref}
211
224
  className={cn("-mx-1 my-1 h-px bg-border", className)}
225
+ data-slot="menu-separator"
212
226
  {...props}
213
227
  />
214
228
  ))
@@ -220,6 +234,7 @@ const MenuShortcut = ({
220
234
  }: React.HTMLAttributes<HTMLSpanElement>) => (
221
235
  <span
222
236
  className={cn("ml-auto text-xs tracking-widest text-muted-foreground", className)}
237
+ data-slot="menu-shortcut"
223
238
  {...props}
224
239
  />
225
240
  )
@@ -247,6 +262,7 @@ const MenuSubTrigger = React.forwardRef<
247
262
  inset && "pl-8",
248
263
  className
249
264
  )}
265
+ data-slot="menu-sub-trigger"
250
266
  {...props}
251
267
  >
252
268
  {children}
@@ -272,6 +288,7 @@ const MenuSubContent = React.forwardRef<
272
288
  "motion-reduce:data-[starting-style]:scale-100 motion-reduce:data-[ending-style]:scale-100",
273
289
  className
274
290
  )}
291
+ data-slot="menu-sub-content"
275
292
  {...props}
276
293
  />
277
294
  </MenuPrimitive.Positioner>
@@ -298,6 +315,7 @@ const MenuInput = React.forwardRef<
298
315
  "disabled:cursor-not-allowed disabled:opacity-50",
299
316
  className
300
317
  )}
318
+ data-slot="menu-input"
301
319
  {...props}
302
320
  />
303
321
  {children}
@@ -317,6 +335,7 @@ const MenuClear = React.forwardRef<
317
335
  "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
318
336
  className
319
337
  )}
338
+ data-slot="menu-clear"
320
339
  {...props}
321
340
  >
322
341
  {children ?? <X className="h-4 w-4" />}
@@ -331,6 +350,7 @@ const MenuEmpty = React.forwardRef<
331
350
  <MenuPrimitive.Empty
332
351
  ref={ref}
333
352
  className={cn("py-6 text-center text-sm text-muted-foreground", className)}
353
+ data-slot="menu-empty"
334
354
  {...props}
335
355
  />
336
356
  ))
@@ -347,6 +367,7 @@ const MenuList = React.forwardRef<
347
367
  "[&>[data-filter-section]:not([hidden])~[data-filter-section]:not([hidden])>[data-filter-separator]]:block",
348
368
  className
349
369
  )}
370
+ data-slot="menu-list"
350
371
  {...props}
351
372
  />
352
373
  ))
@@ -17,6 +17,7 @@ const Menubar = React.forwardRef<
17
17
  "flex h-10 items-center space-x-1 rounded-md border bg-background p-1",
18
18
  className
19
19
  )}
20
+ data-slot="menubar"
20
21
  {...props}
21
22
  />
22
23
  ))
@@ -58,6 +59,7 @@ const MenubarTrigger = React.forwardRef<
58
59
  "data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
59
60
  className
60
61
  )}
62
+ data-slot="menubar-trigger"
61
63
  {...props}
62
64
  />
63
65
  ))
@@ -65,7 +67,7 @@ MenubarTrigger.displayName = "MenubarTrigger"
65
67
 
66
68
  const MenubarPortal: React.FC<
67
69
  React.ComponentProps<typeof Menu.Portal>
68
- > = (props) => <Menu.Portal {...props} />
70
+ > = (props) => <Menu.Portal data-slot="menubar-portal" {...props} />
69
71
  MenubarPortal.displayName = "MenubarPortal"
70
72
 
71
73
  const MenubarPositioner = React.forwardRef<
@@ -76,6 +78,7 @@ const MenubarPositioner = React.forwardRef<
76
78
  ref={ref}
77
79
  className={cn("z-50 outline-none", className)}
78
80
  sideOffset={4}
81
+ data-slot="menubar-positioner"
79
82
  {...props}
80
83
  />
81
84
  ))
@@ -97,6 +100,7 @@ const MenubarContent = React.forwardRef<
97
100
  "motion-reduce:data-[starting-style]:scale-100 motion-reduce:data-[ending-style]:scale-100",
98
101
  className
99
102
  )}
103
+ data-slot="menubar-content"
100
104
  {...props}
101
105
  />
102
106
  ))
@@ -118,6 +122,7 @@ const MenubarItem = React.forwardRef<
118
122
  inset && "pl-8",
119
123
  className
120
124
  )}
125
+ data-slot="menubar-item"
121
126
  {...props}
122
127
  />
123
128
  ))
@@ -136,6 +141,7 @@ const MenubarCheckboxItem = React.forwardRef<
136
141
  "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
137
142
  className
138
143
  )}
144
+ data-slot="menubar-checkbox-item"
139
145
  {...props}
140
146
  >
141
147
  <Menu.CheckboxItemIndicator className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
@@ -150,7 +156,7 @@ const MenubarRadioGroup = React.forwardRef<
150
156
  React.ComponentRef<typeof Menu.RadioGroup>,
151
157
  React.ComponentProps<typeof Menu.RadioGroup>
152
158
  >(({ className, ...props }, ref) => (
153
- <Menu.RadioGroup ref={ref} className={className} {...props} />
159
+ <Menu.RadioGroup ref={ref} className={className} data-slot="menubar-radio-group" {...props} />
154
160
  ))
155
161
  MenubarRadioGroup.displayName = "MenubarRadioGroup"
156
162
 
@@ -167,6 +173,7 @@ const MenubarRadioItem = React.forwardRef<
167
173
  "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
168
174
  className
169
175
  )}
176
+ data-slot="menubar-radio-item"
170
177
  {...props}
171
178
  >
172
179
  <Menu.RadioItemIndicator className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
@@ -181,7 +188,7 @@ const MenubarGroup = React.forwardRef<
181
188
  React.ComponentRef<typeof Menu.Group>,
182
189
  React.ComponentProps<typeof Menu.Group>
183
190
  >(({ className, ...props }, ref) => (
184
- <Menu.Group ref={ref} className={className} {...props} />
191
+ <Menu.Group ref={ref} className={className} data-slot="menubar-group" {...props} />
185
192
  ))
186
193
  MenubarGroup.displayName = "MenubarGroup"
187
194
 
@@ -198,6 +205,7 @@ const MenubarLabel = React.forwardRef<
198
205
  inset && "pl-8",
199
206
  className
200
207
  )}
208
+ data-slot="menubar-label"
201
209
  {...props}
202
210
  />
203
211
  ))
@@ -210,6 +218,7 @@ const MenubarSeparator = React.forwardRef<
210
218
  <Menu.Separator
211
219
  ref={ref}
212
220
  className={cn("-mx-1 my-1 h-px bg-border", className)}
221
+ data-slot="menubar-separator"
213
222
  {...props}
214
223
  />
215
224
  ))
@@ -224,6 +233,7 @@ const MenubarShortcut = ({
224
233
  "ml-auto text-xs tracking-widest text-muted-foreground",
225
234
  className
226
235
  )}
236
+ data-slot="menubar-shortcut"
227
237
  {...props}
228
238
  />
229
239
  )
@@ -251,6 +261,7 @@ const MenubarSubTrigger = React.forwardRef<
251
261
  inset && "pl-8",
252
262
  className
253
263
  )}
264
+ data-slot="menubar-sub-trigger"
254
265
  {...props}
255
266
  >
256
267
  {children}
@@ -277,6 +288,7 @@ const MenubarSubContent = React.forwardRef<
277
288
  "motion-reduce:data-[starting-style]:scale-100 motion-reduce:data-[ending-style]:scale-100",
278
289
  className
279
290
  )}
291
+ data-slot="menubar-sub-content"
280
292
  {...props}
281
293
  />
282
294
  </Menu.Positioner>
@@ -8,7 +8,7 @@ const Meter = React.forwardRef<
8
8
  React.ComponentRef<typeof MeterPrimitive.Root>,
9
9
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Root>
10
10
  >(({ className, ...props }, ref) => (
11
- <MeterPrimitive.Root ref={ref} className={cn("grid gap-2", className)} {...props} />
11
+ <MeterPrimitive.Root data-slot="meter" ref={ref} className={cn("grid gap-2", className)} {...props} />
12
12
  ))
13
13
  Meter.displayName = "Meter"
14
14
 
@@ -17,6 +17,7 @@ const MeterTrack = React.forwardRef<
17
17
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Track>
18
18
  >(({ className, ...props }, ref) => (
19
19
  <MeterPrimitive.Track
20
+ data-slot="meter-track"
20
21
  ref={ref}
21
22
  className={cn("relative h-2 w-full overflow-hidden rounded-full bg-secondary", className)}
22
23
  {...props}
@@ -29,6 +30,7 @@ const MeterIndicator = React.forwardRef<
29
30
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Indicator>
30
31
  >(({ className, ...props }, ref) => (
31
32
  <MeterPrimitive.Indicator
33
+ data-slot="meter-indicator"
32
34
  ref={ref}
33
35
  className={cn("h-full rounded-full bg-primary transition-[width] duration-[var(--motion-duration-base,200ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))] motion-reduce:transition-none", className)}
34
36
  {...props}
@@ -41,6 +43,7 @@ const MeterLabel = React.forwardRef<
41
43
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Label>
42
44
  >(({ className, ...props }, ref) => (
43
45
  <MeterPrimitive.Label
46
+ data-slot="meter-label"
44
47
  ref={ref}
45
48
  className={cn("text-sm font-medium leading-none", className)}
46
49
  {...props}
@@ -53,6 +56,7 @@ const MeterValue = React.forwardRef<
53
56
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Value>
54
57
  >(({ className, ...props }, ref) => (
55
58
  <MeterPrimitive.Value
59
+ data-slot="meter-value"
56
60
  ref={ref}
57
61
  className={cn("text-xs text-muted-foreground", className)}
58
62
  {...props}
@@ -0,0 +1 @@
1
+ export { MonthPicker, type MonthPickerProps } from "./month-picker";
@@ -0,0 +1,351 @@
1
+ "use client"
2
+
3
+ import * as React from "react";
4
+ import { format, type Locale } from "date-fns";
5
+ import { ChevronLeft, ChevronRight } from "lucide-react";
6
+ import { cn } from "@/lib/utils";
7
+ import { changeAsAgent, useAgentControl, useAgentState, useHasAgent, useMergedRef } from "@/components/ui/agent";
8
+ import { type CalendarChangeDetails, type DateRange, addMonths, fromISODate, monthsBetween, parseDate } from "@/components/ui/calendar";
9
+
10
+ /** A month is a Date on its first day, at local midnight. Forms carry it as YYYY-MM. */
11
+ const toISOMonth = (d: Date) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`;
12
+ const monthOf = (d: Date) => new Date(d.getFullYear(), d.getMonth(), 1);
13
+ const sameMonth = (a: Date | null | undefined, b: Date | null | undefined) =>
14
+ !!a && !!b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth();
15
+
16
+ function changeDetails(reason: string, event?: Event): CalendarChangeDetails {
17
+ const details: CalendarChangeDetails = {
18
+ reason,
19
+ event,
20
+ isCanceled: false,
21
+ cancel() {
22
+ details.isCanceled = true;
23
+ },
24
+ };
25
+ return details;
26
+ }
27
+
28
+ const EMPTY: DateRange = { from: null, to: null };
29
+
30
+ interface MonthPickerBaseProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "defaultValue" | "onChange" | "dir"> {
31
+ /** The earliest month that can be picked. */
32
+ min?: Date;
33
+ /** The latest month that can be picked. */
34
+ max?: Date;
35
+ /** Months that can't be picked, besides those outside `min` and `max`. Called with the first of the month. */
36
+ isMonthDisabled?: (month: Date) => boolean;
37
+ disabled?: boolean;
38
+ /** A date-fns locale, for month names. */
39
+ locale?: Locale;
40
+ dir?: "ltr" | "rtl";
41
+ /** Defaults to the current day. Its month carries a ring. */
42
+ today?: Date;
43
+ /** Submits YYYY-MM, or YYYY-MM/YYYY-MM for a range. */
44
+ name?: string;
45
+ required?: boolean;
46
+ }
47
+
48
+ export type MonthPickerProps = MonthPickerBaseProps &
49
+ (
50
+ | {
51
+ mode?: "single";
52
+ value?: Date | null;
53
+ defaultValue?: Date | null;
54
+ onValueChange?: (value: Date | null, details: CalendarChangeDetails) => void;
55
+ }
56
+ | {
57
+ mode: "range";
58
+ value?: DateRange;
59
+ defaultValue?: DateRange;
60
+ onValueChange?: (value: DateRange, details: CalendarChangeDetails) => void;
61
+ }
62
+ );
63
+
64
+ type Value = Date | null | DateRange;
65
+
66
+ const KEYS: Record<string, number> = { ArrowLeft: -1, ArrowRight: 1, ArrowUp: -3, ArrowDown: 3, PageUp: -12, PageDown: 12 };
67
+
68
+ /**
69
+ * A year of months in three columns, for billing and reporting periods where days are
70
+ * noise. In range mode two picks make a span, banded the way Calendar bands days.
71
+ */
72
+ const MonthPicker = React.forwardRef<HTMLDivElement, MonthPickerProps>((props, ref) => {
73
+ const {
74
+ mode = "single",
75
+ value: valueProp,
76
+ defaultValue,
77
+ onValueChange,
78
+ min: minProp,
79
+ max: maxProp,
80
+ isMonthDisabled,
81
+ disabled,
82
+ locale,
83
+ dir,
84
+ today: todayProp,
85
+ name,
86
+ required,
87
+ className,
88
+ "aria-label": ariaLabel = mode === "range" ? "Months" : "Month",
89
+ ...rest
90
+ } = props;
91
+ const range = mode === "range";
92
+ const hasAgent = useHasAgent();
93
+
94
+ const thisMonth = monthOf(todayProp ?? new Date());
95
+ const min = minProp && monthOf(minProp);
96
+ const max = maxProp && monthOf(maxProp);
97
+ const [value, change] = useAgentState<Value, CalendarChangeDetails>(
98
+ valueProp as Value | undefined,
99
+ (defaultValue as Value | undefined) ?? (range ? EMPTY : null),
100
+ onValueChange as ((v: Value, d: CalendarChangeDetails) => void) | undefined
101
+ );
102
+ const from = range ? (value as DateRange).from && monthOf((value as DateRange).from!) : value && monthOf(value as Date);
103
+ const to = range ? (value as DateRange).to && monthOf((value as DateRange).to!) : from;
104
+
105
+ const outside = (m: Date) => (!!min && m < min) || (!!max && m > max);
106
+ const blocked = (m: Date) => !!disabled || outside(m) || !!isMonthDisabled?.(m);
107
+ const clamp = (m: Date) => (min && m < min ? min : max && m > max ? max : m);
108
+
109
+ /* ---- which month has focus; its year is the one on screen ---- */
110
+ const anchor = () => clamp(from ?? thisMonth);
111
+ const [focus, setFocus] = React.useState(anchor);
112
+ const key = `${from ? toISOMonth(from) : ""}/${to ? toISOMonth(to) : ""}`;
113
+ const [seen, setSeen] = React.useState(key);
114
+ if (seen !== key) {
115
+ setSeen(key);
116
+ // A value set from outside brings its year into view, unless one end is already on screen.
117
+ if (from && ![from, to].some((m) => m?.getFullYear() === focus.getFullYear())) setFocus(anchor());
118
+ }
119
+ const year = focus.getFullYear();
120
+
121
+ const root = React.useRef<HTMLDivElement>(null);
122
+ const moved = React.useRef(false);
123
+ React.useEffect(() => {
124
+ if (!moved.current) return;
125
+ moved.current = false;
126
+ root.current?.querySelector<HTMLElement>(`[data-month="${toISOMonth(focus)}"]`)?.focus();
127
+ }, [focus.getTime()]);
128
+
129
+ const commit = (next: Value, details: CalendarChangeDetails) => {
130
+ const eq = (a: Date | null | undefined, b: Date | null | undefined) => (!a && !b) || sameMonth(a, b);
131
+ const same = range ? eq((next as DateRange).from, from) && eq((next as DateRange).to, to) : eq(next as Date | null, from);
132
+ if (!same) change(next, details);
133
+ };
134
+
135
+ const pick = (m: Date, event?: Event) => {
136
+ if (blocked(m)) return;
137
+ setFocus(m);
138
+ if (!range) return commit(m, changeDetails("month-press", event));
139
+ const r = value as DateRange;
140
+ // A third pick, or one before the start, starts a new span.
141
+ const next = !r.from || r.to || m < from! ? { from: m, to: null } : { from: r.from, to: m };
142
+ commit(next, changeDetails("month-press", event));
143
+ };
144
+
145
+ /* ---- agent ---- */
146
+ const monthNames = React.useMemo(
147
+ () => Array.from({ length: 12 }, (_, i) => [format(new Date(2000, i, 1), "MMMM", { locale }).toLowerCase(), "january february march april may june july august september october november december".split(" ")[i]]),
148
+ [locale]
149
+ );
150
+ const readMonth = (item: unknown): Date | null => {
151
+ if (item instanceof Date) return isNaN(item.getTime()) ? null : monthOf(item);
152
+ if (typeof item !== "string") return null;
153
+ const t = item.trim().toLowerCase();
154
+ const iso = /^(\d{4})-(\d{1,2})$/.exec(t);
155
+ if (iso && +iso[2] >= 1 && +iso[2] <= 12) return new Date(+iso[1], +iso[2] - 1, 1);
156
+ const named = /^([^\d\s.,]+)\.?,?\s*(\d{4})?$/.exec(t);
157
+ if (named && named[1].length >= 3) {
158
+ const i = monthNames.findIndex((names) => names.some((n) => n.startsWith(named[1])));
159
+ if (i >= 0) return new Date(named[2] ? +named[2] : thisMonth.getFullYear(), i, 1);
160
+ }
161
+ const day = fromISODate(t) ?? parseDate(t, { today: todayProp ?? new Date() });
162
+ return day && monthOf(day);
163
+ };
164
+ const refusal = (m: Date): string | null => {
165
+ if (min && m < min) return `The earliest is ${toISOMonth(min)}.`;
166
+ if (max && m > max) return `The latest is ${toISOMonth(max)}.`;
167
+ if (isMonthDisabled?.(m)) return `${toISOMonth(m)} can't be picked.`;
168
+ return null;
169
+ };
170
+
171
+ /** A value set from outside that names a month that can't be picked. */
172
+ const wrong = (m: Date | null | undefined) => !!m && !!refusal(m);
173
+ const problem = [from, to].map((m) => m && refusal(m)).find(Boolean) ?? null;
174
+
175
+ useAgentControl(() => ({
176
+ kind: "date",
177
+ element: root.current,
178
+ invalid: !!problem || undefined,
179
+ errors: problem ? [problem] : undefined,
180
+ value: range ? (from ? [toISOMonth(from), ...((value as DateRange).to ? [toISOMonth(to!)] : [])] : undefined) : from ? toISOMonth(from) : undefined,
181
+ multiple: range,
182
+ disabled,
183
+ required,
184
+ actions: {
185
+ setValue: (wanted: unknown) => {
186
+ if (wanted === null || wanted === "") return changeAsAgent(commit, range ? EMPTY : null);
187
+ const parts = !range
188
+ ? [wanted]
189
+ : Array.isArray(wanted)
190
+ ? wanted
191
+ : typeof wanted === "string"
192
+ ? // "jun to nov 2026": a month with no year takes the one after it.
193
+ wanted.split(/\s*(?:\/|,| to )\s*/i).map((p, _, all) => (/\d/.test(p) ? p : `${p} ${/\d{4}$/.exec(all[all.length - 1])?.[0] ?? ""}`))
194
+ : [wanted];
195
+ const months = parts.map(readMonth);
196
+ const bad = parts.findIndex((_, i) => !months[i]);
197
+ if (bad >= 0) return `"${String(parts[bad])}" is not a month. Send YYYY-MM, or a name such as jun 2026.`;
198
+ const why = months.map((m) => refusal(m!)).find(Boolean);
199
+ if (why) return why;
200
+ if (!range) return changeAsAgent(commit, months[0]);
201
+ if (months.length !== 2) return "Send two months, from and to.";
202
+ if (months[1]! < months[0]!) return "The span must end on or after its start.";
203
+ return changeAsAgent(commit, { from: months[0], to: months[1] });
204
+ },
205
+ clear: () => changeAsAgent(commit, range ? EMPTY : null),
206
+ },
207
+ }));
208
+
209
+ /* ---- render ---- */
210
+ const months = Array.from({ length: 12 }, (_, i) => new Date(year, i, 1));
211
+ const done = range && from && (value as DateRange).to;
212
+ const count = done ? monthsBetween(from!, to!) + 1 : 0;
213
+ const short = (d: Date) => format(d, "MMM", { locale });
214
+ const submitted = range ? (done ? `${toISOMonth(from!)}/${toISOMonth(to!)}` : "") : from ? toISOMonth(from) : "";
215
+ const step = (n: number) => setFocus((f) => clamp(new Date(f.getFullYear() + n, f.getMonth(), 1)));
216
+
217
+ const navButton = (n: -1 | 1) => (
218
+ <button
219
+ type="button"
220
+ aria-label={n < 0 ? "Previous year" : "Next year"}
221
+ data-agent-part={hasAgent ? "" : undefined}
222
+ disabled={disabled || (n < 0 ? !!min && year <= min.getFullYear() : !!max && year >= max.getFullYear())}
223
+ onClick={() => step(n)}
224
+ className={cn(
225
+ "inline-flex size-8 items-center justify-center rounded-md text-muted-foreground",
226
+ "hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
227
+ "disabled:pointer-events-none disabled:opacity-40"
228
+ )}
229
+ >
230
+ {n < 0 ? <ChevronLeft className="size-4 rtl:rotate-180" aria-hidden /> : <ChevronRight className="size-4 rtl:rotate-180" aria-hidden />}
231
+ </button>
232
+ );
233
+
234
+ return (
235
+ <div
236
+ {...rest}
237
+ ref={useMergedRef(ref, root)}
238
+ role="group"
239
+ aria-label={ariaLabel}
240
+ aria-disabled={disabled || undefined}
241
+ dir={dir}
242
+ data-slot="month-picker"
243
+ data-invalid={problem ? "" : undefined}
244
+ className={cn("relative w-full max-w-80", className)}
245
+ >
246
+ <div data-slot="month-picker-header" className="mb-2.5 flex items-center">
247
+ {navButton(-1)}
248
+ <div data-slot="month-picker-year" aria-live="polite" className="flex-1 text-center text-sm font-semibold tabular-nums">
249
+ {year}
250
+ </div>
251
+ {navButton(1)}
252
+ </div>
253
+ <div
254
+ data-slot="month-picker-grid"
255
+ className="grid grid-cols-3 gap-y-1.5"
256
+ onKeyDown={(e) => {
257
+ let n = KEYS[e.key];
258
+ if (e.key === "Home") n = -focus.getMonth();
259
+ if (e.key === "End") n = 11 - focus.getMonth();
260
+ if (n === undefined) return;
261
+ if ((e.key === "ArrowLeft" || e.key === "ArrowRight") && root.current?.closest("[dir]")?.getAttribute("dir") === "rtl") n = -n;
262
+ e.preventDefault();
263
+ const home = e.key === "Home" || e.key === "End";
264
+ const next = home ? clamp(addMonths(focus, n)) : addMonths(focus, n);
265
+ if (outside(next)) return;
266
+ moved.current = true;
267
+ setFocus(next);
268
+ }}
269
+ >
270
+ {months.map((m) => {
271
+ const ends = sameMonth(m, from) || sameMonth(m, to);
272
+ const within = !!done && m >= from! && m <= to!;
273
+ const col = m.getMonth() % 3;
274
+ const bad = ends && wrong(m);
275
+ return (
276
+ <span
277
+ key={m.getMonth()}
278
+ data-range={done && within ? (sameMonth(m, from) ? "start" : sameMonth(m, to) ? "end" : "middle") : undefined}
279
+ className={cn(
280
+ "grid h-11 place-items-center",
281
+ done && within && from! < to! && "bg-secondary",
282
+ done && within && (sameMonth(m, from) || col === 0) && "rounded-s-xl",
283
+ done && within && (sameMonth(m, to) || col === 2) && "rounded-e-xl"
284
+ )}
285
+ >
286
+ <button
287
+ type="button"
288
+ data-slot="month-picker-month"
289
+ data-month={toISOMonth(m)}
290
+ data-agent-part={hasAgent ? "" : undefined}
291
+ data-selected={(ends && !bad) || undefined}
292
+ data-invalid={bad || undefined}
293
+ data-current={sameMonth(m, thisMonth) || undefined}
294
+ tabIndex={sameMonth(m, focus) ? 0 : -1}
295
+ aria-label={`${format(m, "MMMM yyyy", { locale })}${bad ? ", can't be picked" : ""}`}
296
+ aria-current={sameMonth(m, thisMonth) ? "date" : undefined}
297
+ aria-pressed={ends || within}
298
+ aria-disabled={blocked(m) || undefined}
299
+ onClick={(e) => pick(m, e.nativeEvent)}
300
+ className={cn(
301
+ "h-10 w-full max-w-[5.25rem] rounded-[0.625rem] border border-transparent text-[0.8125rem] outline-none transition-colors duration-150 motion-reduce:transition-none",
302
+ "hover:bg-accent hover:text-accent-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
303
+ // This month is ringed with a border, so the focus ring stays free for focus.
304
+ "data-[current]:border-muted-foreground",
305
+ "data-[selected]:border-transparent data-[selected]:bg-primary data-[selected]:font-medium data-[selected]:text-primary-foreground",
306
+ "aria-disabled:pointer-events-none",
307
+ bad ? "border-destructive bg-destructive/10 font-medium text-destructive" : "aria-disabled:opacity-30"
308
+ )}
309
+ >
310
+ {short(m)}
311
+ </button>
312
+ </span>
313
+ );
314
+ })}
315
+ </div>
316
+ {range && (
317
+ <p data-slot="month-picker-readout" aria-live="polite" className="mt-3 min-h-5 border-t pt-3 text-[0.8125rem]">
318
+ {done ? (
319
+ <>
320
+ {short(from!)}
321
+ {from!.getFullYear() !== to!.getFullYear() && ` ${from!.getFullYear()}`} to {short(to!)} {to!.getFullYear()}{" "}
322
+ <span className="text-muted-foreground">
323
+ · {count} {count === 1 ? "month" : "months"}
324
+ </span>
325
+ </>
326
+ ) : from ? (
327
+ <>
328
+ {short(from)} {from.getFullYear()} <span className="text-muted-foreground">to ...</span>
329
+ </>
330
+ ) : null}
331
+ </p>
332
+ )}
333
+ {(name || required) && (
334
+ <input
335
+ aria-hidden
336
+ tabIndex={-1}
337
+ name={name}
338
+ required={required}
339
+ disabled={disabled}
340
+ value={submitted}
341
+ onChange={() => {}}
342
+ onFocus={() => root.current?.querySelector<HTMLElement>("[data-month][tabindex='0']")?.focus()}
343
+ className="pointer-events-none absolute bottom-0 start-0 size-px opacity-0"
344
+ />
345
+ )}
346
+ </div>
347
+ );
348
+ });
349
+ MonthPicker.displayName = "MonthPicker";
350
+
351
+ export { MonthPicker };