panelui-native 0.25.0 → 0.28.1

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 (193) hide show
  1. package/lib/module/components/accordion/index.js +8 -8
  2. package/lib/module/components/accordion/index.js.map +1 -1
  3. package/lib/module/components/alert/index.js +2 -2
  4. package/lib/module/components/alert/index.js.map +1 -1
  5. package/lib/module/components/area-chart/index.js +182 -50
  6. package/lib/module/components/area-chart/index.js.map +1 -1
  7. package/lib/module/components/attachment/index.js +2 -1
  8. package/lib/module/components/attachment/index.js.map +1 -1
  9. package/lib/module/components/avatar/index.js +3 -3
  10. package/lib/module/components/avatar/index.js.map +1 -1
  11. package/lib/module/components/badge/index.js +4 -4
  12. package/lib/module/components/badge/index.js.map +1 -1
  13. package/lib/module/components/bar-chart/index.js +197 -56
  14. package/lib/module/components/bar-chart/index.js.map +1 -1
  15. package/lib/module/components/bottom-sheet/index.js +15 -10
  16. package/lib/module/components/bottom-sheet/index.js.map +1 -1
  17. package/lib/module/components/breadcrumb/index.js +6 -6
  18. package/lib/module/components/breadcrumb/index.js.map +1 -1
  19. package/lib/module/components/button/index.js +11 -6
  20. package/lib/module/components/button/index.js.map +1 -1
  21. package/lib/module/components/calendar/index.js +557 -0
  22. package/lib/module/components/calendar/index.js.map +1 -0
  23. package/lib/module/components/carousel/index.js +680 -0
  24. package/lib/module/components/carousel/index.js.map +1 -0
  25. package/lib/module/components/chip/index.js +4 -4
  26. package/lib/module/components/chip/index.js.map +1 -1
  27. package/lib/module/components/date-picker/index.js +158 -0
  28. package/lib/module/components/date-picker/index.js.map +1 -0
  29. package/lib/module/components/dialog/index.js +7 -2
  30. package/lib/module/components/dialog/index.js.map +1 -1
  31. package/lib/module/components/direction/index.js +2 -1
  32. package/lib/module/components/direction/index.js.map +1 -1
  33. package/lib/module/components/empty-state/index.js +2 -2
  34. package/lib/module/components/empty-state/index.js.map +1 -1
  35. package/lib/module/components/field/index.js +5 -5
  36. package/lib/module/components/field/index.js.map +1 -1
  37. package/lib/module/components/flow/index.js +3 -3
  38. package/lib/module/components/flow/index.js.map +1 -1
  39. package/lib/module/components/frame/index.js +5 -5
  40. package/lib/module/components/frame/index.js.map +1 -1
  41. package/lib/module/components/input-group/index.js +3 -2
  42. package/lib/module/components/input-group/index.js.map +1 -1
  43. package/lib/module/components/item/index.js +9 -9
  44. package/lib/module/components/item/index.js.map +1 -1
  45. package/lib/module/components/label/index.js +4 -4
  46. package/lib/module/components/label/index.js.map +1 -1
  47. package/lib/module/components/line-chart/index.js +167 -44
  48. package/lib/module/components/line-chart/index.js.map +1 -1
  49. package/lib/module/components/map/index.js +7 -7
  50. package/lib/module/components/map/index.js.map +1 -1
  51. package/lib/module/components/marker/index.js +3 -3
  52. package/lib/module/components/marker/index.js.map +1 -1
  53. package/lib/module/components/message/index.js +7 -7
  54. package/lib/module/components/message/index.js.map +1 -1
  55. package/lib/module/components/message-scroller/index.js +5 -4
  56. package/lib/module/components/message-scroller/index.js.map +1 -1
  57. package/lib/module/components/popover/index.js +39 -7
  58. package/lib/module/components/popover/index.js.map +1 -1
  59. package/lib/module/components/radio-group/index.js +2 -2
  60. package/lib/module/components/radio-group/index.js.map +1 -1
  61. package/lib/module/components/section-rail/index.js +4 -4
  62. package/lib/module/components/section-rail/index.js.map +1 -1
  63. package/lib/module/components/select/index.js +10 -4
  64. package/lib/module/components/select/index.js.map +1 -1
  65. package/lib/module/components/steps/index.js +4 -4
  66. package/lib/module/components/steps/index.js.map +1 -1
  67. package/lib/module/components/tabs/index.js +7 -7
  68. package/lib/module/components/tabs/index.js.map +1 -1
  69. package/lib/module/components/timeline/index.js +4 -4
  70. package/lib/module/components/timeline/index.js.map +1 -1
  71. package/lib/module/components/toast/index.js +2 -2
  72. package/lib/module/components/toast/index.js.map +1 -1
  73. package/lib/module/components/toggle-button/index.js +5 -5
  74. package/lib/module/components/toggle-button/index.js.map +1 -1
  75. package/lib/module/components/tooltip/index.js +492 -0
  76. package/lib/module/components/tooltip/index.js.map +1 -0
  77. package/lib/module/components/typography/index.js +2 -2
  78. package/lib/module/components/typography/index.js.map +1 -1
  79. package/lib/module/hooks/index.js +1 -0
  80. package/lib/module/hooks/index.js.map +1 -1
  81. package/lib/module/hooks/use-back-handler.js +37 -0
  82. package/lib/module/hooks/use-back-handler.js.map +1 -0
  83. package/lib/module/icons/index.js +63 -0
  84. package/lib/module/icons/index.js.map +1 -1
  85. package/lib/module/index.js +5 -1
  86. package/lib/module/index.js.map +1 -1
  87. package/lib/module/primitives/keyboard-avoider.js +2 -1
  88. package/lib/module/primitives/keyboard-avoider.js.map +1 -1
  89. package/lib/module/primitives/text.js +25 -1
  90. package/lib/module/primitives/text.js.map +1 -1
  91. package/lib/module/utils/date.js +386 -0
  92. package/lib/module/utils/date.js.map +1 -0
  93. package/lib/typescript/src/components/accordion/index.d.ts.map +1 -1
  94. package/lib/typescript/src/components/alert/index.d.ts.map +1 -1
  95. package/lib/typescript/src/components/area-chart/index.d.ts +41 -1
  96. package/lib/typescript/src/components/area-chart/index.d.ts.map +1 -1
  97. package/lib/typescript/src/components/attachment/index.d.ts.map +1 -1
  98. package/lib/typescript/src/components/bar-chart/index.d.ts +44 -2
  99. package/lib/typescript/src/components/bar-chart/index.d.ts.map +1 -1
  100. package/lib/typescript/src/components/bottom-sheet/index.d.ts.map +1 -1
  101. package/lib/typescript/src/components/breadcrumb/index.d.ts.map +1 -1
  102. package/lib/typescript/src/components/button/index.d.ts.map +1 -1
  103. package/lib/typescript/src/components/calendar/index.d.ts +108 -0
  104. package/lib/typescript/src/components/calendar/index.d.ts.map +1 -0
  105. package/lib/typescript/src/components/carousel/index.d.ts +179 -0
  106. package/lib/typescript/src/components/carousel/index.d.ts.map +1 -0
  107. package/lib/typescript/src/components/chip/index.d.ts.map +1 -1
  108. package/lib/typescript/src/components/date-picker/index.d.ts +91 -0
  109. package/lib/typescript/src/components/date-picker/index.d.ts.map +1 -0
  110. package/lib/typescript/src/components/dialog/index.d.ts.map +1 -1
  111. package/lib/typescript/src/components/direction/index.d.ts.map +1 -1
  112. package/lib/typescript/src/components/empty-state/index.d.ts.map +1 -1
  113. package/lib/typescript/src/components/field/index.d.ts.map +1 -1
  114. package/lib/typescript/src/components/frame/index.d.ts.map +1 -1
  115. package/lib/typescript/src/components/input-group/index.d.ts.map +1 -1
  116. package/lib/typescript/src/components/item/index.d.ts.map +1 -1
  117. package/lib/typescript/src/components/label/index.d.ts.map +1 -1
  118. package/lib/typescript/src/components/line-chart/index.d.ts +41 -2
  119. package/lib/typescript/src/components/line-chart/index.d.ts.map +1 -1
  120. package/lib/typescript/src/components/map/index.d.ts.map +1 -1
  121. package/lib/typescript/src/components/marker/index.d.ts.map +1 -1
  122. package/lib/typescript/src/components/message/index.d.ts.map +1 -1
  123. package/lib/typescript/src/components/message-scroller/index.d.ts.map +1 -1
  124. package/lib/typescript/src/components/popover/index.d.ts.map +1 -1
  125. package/lib/typescript/src/components/select/index.d.ts.map +1 -1
  126. package/lib/typescript/src/components/steps/index.d.ts.map +1 -1
  127. package/lib/typescript/src/components/tabs/index.d.ts.map +1 -1
  128. package/lib/typescript/src/components/timeline/index.d.ts.map +1 -1
  129. package/lib/typescript/src/components/toast/index.d.ts.map +1 -1
  130. package/lib/typescript/src/components/toggle-button/index.d.ts.map +1 -1
  131. package/lib/typescript/src/components/tooltip/index.d.ts +125 -0
  132. package/lib/typescript/src/components/tooltip/index.d.ts.map +1 -0
  133. package/lib/typescript/src/components/typography/index.d.ts.map +1 -1
  134. package/lib/typescript/src/hooks/index.d.ts +1 -0
  135. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  136. package/lib/typescript/src/hooks/use-back-handler.d.ts +19 -0
  137. package/lib/typescript/src/hooks/use-back-handler.d.ts.map +1 -0
  138. package/lib/typescript/src/icons/index.d.ts +3 -0
  139. package/lib/typescript/src/icons/index.d.ts.map +1 -1
  140. package/lib/typescript/src/index.d.ts +8 -4
  141. package/lib/typescript/src/index.d.ts.map +1 -1
  142. package/lib/typescript/src/primitives/keyboard-avoider.d.ts.map +1 -1
  143. package/lib/typescript/src/primitives/text.d.ts +20 -0
  144. package/lib/typescript/src/primitives/text.d.ts.map +1 -1
  145. package/lib/typescript/src/utils/date.d.ts +116 -0
  146. package/lib/typescript/src/utils/date.d.ts.map +1 -0
  147. package/package.json +1 -1
  148. package/src/components/accordion/index.tsx +8 -12
  149. package/src/components/alert/index.tsx +2 -2
  150. package/src/components/area-chart/index.tsx +208 -55
  151. package/src/components/attachment/index.tsx +2 -2
  152. package/src/components/avatar/index.tsx +3 -3
  153. package/src/components/badge/index.tsx +6 -6
  154. package/src/components/bar-chart/index.tsx +214 -54
  155. package/src/components/bottom-sheet/index.tsx +17 -16
  156. package/src/components/breadcrumb/index.tsx +6 -8
  157. package/src/components/button/index.tsx +12 -8
  158. package/src/components/calendar/index.tsx +703 -0
  159. package/src/components/carousel/index.tsx +788 -0
  160. package/src/components/chip/index.tsx +4 -6
  161. package/src/components/date-picker/index.tsx +237 -0
  162. package/src/components/dialog/index.tsx +7 -2
  163. package/src/components/direction/index.tsx +2 -1
  164. package/src/components/empty-state/index.tsx +2 -2
  165. package/src/components/field/index.tsx +3 -5
  166. package/src/components/flow/index.tsx +3 -3
  167. package/src/components/frame/index.tsx +5 -7
  168. package/src/components/input-group/index.tsx +3 -2
  169. package/src/components/item/index.tsx +9 -9
  170. package/src/components/label/index.tsx +2 -2
  171. package/src/components/line-chart/index.tsx +189 -50
  172. package/src/components/map/index.tsx +7 -9
  173. package/src/components/marker/index.tsx +3 -3
  174. package/src/components/message/index.tsx +7 -7
  175. package/src/components/message-scroller/index.tsx +5 -4
  176. package/src/components/popover/index.tsx +43 -7
  177. package/src/components/radio-group/index.tsx +2 -2
  178. package/src/components/section-rail/index.tsx +4 -4
  179. package/src/components/select/index.tsx +10 -4
  180. package/src/components/steps/index.tsx +4 -4
  181. package/src/components/tabs/index.tsx +7 -9
  182. package/src/components/timeline/index.tsx +4 -4
  183. package/src/components/toast/index.tsx +2 -2
  184. package/src/components/toggle-button/index.tsx +5 -7
  185. package/src/components/tooltip/index.tsx +610 -0
  186. package/src/components/typography/index.tsx +2 -2
  187. package/src/hooks/index.ts +1 -0
  188. package/src/hooks/use-back-handler.ts +37 -0
  189. package/src/icons/index.tsx +45 -0
  190. package/src/index.ts +45 -0
  191. package/src/primitives/keyboard-avoider.tsx +2 -1
  192. package/src/primitives/text.tsx +28 -1
  193. package/src/utils/date.ts +446 -0
@@ -103,6 +103,51 @@ export function MinusIcon({ size = 14, color, strokeWidth = 3, ...props }: IconP
103
103
  );
104
104
  }
105
105
 
106
+ export function ChevronUpIcon({ size = 16, color, ...props }: IconProps) {
107
+ const resolved = useResolvedColor(color, '#737373');
108
+ return (
109
+ <Svg width={size} height={size} viewBox="0 0 24 24" fill="none" {...props}>
110
+ <Path
111
+ d="m18 15-6-6-6 6"
112
+ stroke={resolved}
113
+ strokeWidth={2}
114
+ strokeLinecap="round"
115
+ strokeLinejoin="round"
116
+ />
117
+ </Svg>
118
+ );
119
+ }
120
+
121
+ export function CalendarIcon({ size = 16, color, ...props }: IconProps) {
122
+ const resolved = useResolvedColor(color, '#737373');
123
+ return (
124
+ <Svg width={size} height={size} viewBox="0 0 24 24" fill="none" {...props}>
125
+ <Path
126
+ d="M8 2v4M16 2v4M3 10h18M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2Z"
127
+ stroke={resolved}
128
+ strokeWidth={2}
129
+ strokeLinecap="round"
130
+ strokeLinejoin="round"
131
+ />
132
+ </Svg>
133
+ );
134
+ }
135
+
136
+ export function BookmarkIcon({ size = 16, color, ...props }: IconProps) {
137
+ const resolved = useResolvedColor(color, '#737373');
138
+ return (
139
+ <Svg width={size} height={size} viewBox="0 0 24 24" fill="none" {...props}>
140
+ <Path
141
+ d="m19 21-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2Z"
142
+ stroke={resolved}
143
+ strokeWidth={2}
144
+ strokeLinecap="round"
145
+ strokeLinejoin="round"
146
+ />
147
+ </Svg>
148
+ );
149
+ }
150
+
106
151
  export function ChevronDownIcon({ size = 16, color, ...props }: IconProps) {
107
152
  const resolved = useResolvedColor(color, '#737373');
108
153
  return (
package/src/index.ts CHANGED
@@ -79,6 +79,7 @@ export {
79
79
  type AreaChartProps,
80
80
  type AreaChartHandle,
81
81
  type AreaChartGridProps,
82
+ type AreaChartHeaderProps,
82
83
  type AreaChartAreaProps,
83
84
  type AreaChartXAxisProps,
84
85
  type AreaChartYAxisProps,
@@ -93,6 +94,7 @@ export {
93
94
  useBarChart,
94
95
  type BarChartProps,
95
96
  type BarChartHandle,
97
+ type BarChartHeaderProps,
96
98
  type BarChartGridProps,
97
99
  type BarChartBarProps,
98
100
  type BarChartXAxisProps,
@@ -133,6 +135,11 @@ export {
133
135
  type ToggleButtonVariant,
134
136
  type ToggleSelectionMode,
135
137
  } from './components/toggle-button';
138
+ export {
139
+ DatePicker,
140
+ type DatePickerProps,
141
+ type DatePickerMode,
142
+ } from './components/date-picker';
136
143
  export {
137
144
  Dialog,
138
145
  type DialogProps,
@@ -246,6 +253,30 @@ export {
246
253
  type TabsTriggerProps,
247
254
  type TabsContentProps,
248
255
  } from './components/tabs';
256
+ export {
257
+ Carousel,
258
+ useCarouselState,
259
+ type CarouselProps,
260
+ type CarouselHandle,
261
+ type CarouselContentProps,
262
+ type CarouselItemProps,
263
+ type CarouselCaptionProps,
264
+ type CarouselDotsProps,
265
+ type CarouselArrowProps,
266
+ type CarouselControlsProps,
267
+ type CarouselVariant,
268
+ type CarouselOrientation,
269
+ type CarouselAlign,
270
+ } from './components/carousel';
271
+ export {
272
+ Calendar,
273
+ type CalendarProps,
274
+ type CalendarMode,
275
+ type CalendarCaptionLayout,
276
+ type CalendarDisabled,
277
+ type CalendarSelection,
278
+ type DateRange,
279
+ } from './components/calendar';
249
280
  export { Card, type CardProps } from './components/card';
250
281
  export { Checkbox, type CheckboxProps } from './components/checkbox';
251
282
  export {
@@ -320,6 +351,7 @@ export {
320
351
  useLineChart,
321
352
  type LineChartProps,
322
353
  type LineChartHandle,
354
+ type LineChartHeaderProps,
323
355
  type LineChartGridProps,
324
356
  type LineChartLineProps,
325
357
  type LineChartAreaProps,
@@ -470,6 +502,16 @@ export {
470
502
  type ToastIndicatorProps,
471
503
  type ToastCloseProps,
472
504
  } from './components/toast';
505
+ export {
506
+ Tooltip,
507
+ type TooltipProps,
508
+ type TooltipTriggerProps,
509
+ type TooltipContentProps,
510
+ type TooltipArrowProps,
511
+ type TooltipPlacement,
512
+ type TooltipAlign,
513
+ type TooltipOpenOn,
514
+ } from './components/tooltip';
473
515
  export {
474
516
  Typography,
475
517
  type TypographyProps,
@@ -485,12 +527,15 @@ export {
485
527
  AppleIcon,
486
528
  ArrowUpRightIcon,
487
529
  BellIcon,
530
+ BookmarkIcon,
531
+ CalendarIcon,
488
532
  CardIcon,
489
533
  CheckCircleIcon,
490
534
  CheckIcon,
491
535
  ChevronDownIcon,
492
536
  ChevronLeftIcon,
493
537
  ChevronRightIcon,
538
+ ChevronUpIcon,
494
539
  DownloadIcon,
495
540
  EllipsisIcon,
496
541
  FacebookIcon,
@@ -27,6 +27,7 @@ import {
27
27
  useKeyboardAvoidance,
28
28
  type KeyboardAvoidanceMode,
29
29
  } from '../hooks/use-keyboard-avoidance';
30
+ import { textChildren } from './text';
30
31
 
31
32
  export interface KeyboardAvoiderProps extends ViewProps {
32
33
  className?: string;
@@ -86,7 +87,7 @@ export function KeyboardAvoider({
86
87
  className={className}
87
88
  style={[style, avoidance.animatedStyle]}
88
89
  >
89
- {children}
90
+ {textChildren(children)}
90
91
  </Animated.View>
91
92
  );
92
93
  }
@@ -1,4 +1,4 @@
1
- import { forwardRef } from 'react';
1
+ import { Children, forwardRef, type ReactElement, type ReactNode } from 'react';
2
2
  import { Text as RNText, type Text as RNTextType, type TextProps as RNTextProps } from 'react-native';
3
3
  import { tv, type VariantProps } from 'tailwind-variants';
4
4
  import { useDirection } from '../hooks/use-direction';
@@ -63,3 +63,30 @@ export const Text = forwardRef<RNTextType, TextProps>(
63
63
  );
64
64
 
65
65
  Text.displayName = 'Text';
66
+
67
+ /**
68
+ * Wraps the bare text among `children` so it can live in a plain view.
69
+ *
70
+ * React Native will not render a string outside a `<Text>`, and a component
71
+ * that takes `children` has no say in whether it is handed one — so anything
72
+ * putting `children` into a `View`, a `Pressable` or a scroller has to allow
73
+ * for it. Every part in the library that does runs its children through here.
74
+ *
75
+ * Per child, and not all-or-nothing. A part is nearly always given an element
76
+ * and some text together — an icon and a label, an arrow and a line of text —
77
+ * which makes `children` an *array*, so a `typeof children === 'string'` check
78
+ * never matches and the text underneath reaches the view bare. That is the
79
+ * shape this exists to make impossible.
80
+ *
81
+ * `render` is there because the wrapper is rarely the plain default: a button
82
+ * wants its own label variant, a sheet header wants a heading. Pass one, and
83
+ * the text arrives dressed the way the part would have dressed it anyway.
84
+ */
85
+ export function textChildren(
86
+ children: ReactNode,
87
+ render: (text: string | number) => ReactElement = (text) => <Text>{text}</Text>
88
+ ): ReactNode {
89
+ return Children.map(children, (child) =>
90
+ typeof child === 'string' || typeof child === 'number' ? render(child) : child
91
+ );
92
+ }
@@ -0,0 +1,446 @@
1
+ /**
2
+ * Date helpers for the calendar.
3
+ *
4
+ * No date library. Everything a month grid needs is arithmetic on `Date`, and
5
+ * a dependency that ships a full timezone database to render seven columns is
6
+ * a poor trade for a library whose whole runtime is three small packages.
7
+ *
8
+ * Two rules hold throughout:
9
+ *
10
+ * - **Days are compared at local midnight.** A `Date` carries a time, and two
11
+ * values on the same day are not equal unless that time is stripped. Every
12
+ * comparison here goes through `startOfDay`.
13
+ * - **Month arithmetic is done with `setMonth`, not by adding days.** The
14
+ * platform already knows that a month is 28, 29, 30 or 31 days long.
15
+ */
16
+
17
+ /** Names of the weekdays and months, in the caller's locale. */
18
+ export type DateLocale = string | undefined;
19
+
20
+ export function startOfDay(date: Date): Date {
21
+ const copy = new Date(date);
22
+ copy.setHours(0, 0, 0, 0);
23
+ return copy;
24
+ }
25
+
26
+ export function isSameDay(a: Date | null | undefined, b: Date | null | undefined): boolean {
27
+ if (!a || !b) return false;
28
+ return (
29
+ a.getFullYear() === b.getFullYear() &&
30
+ a.getMonth() === b.getMonth() &&
31
+ a.getDate() === b.getDate()
32
+ );
33
+ }
34
+
35
+ export function isSameMonth(a: Date, b: Date): boolean {
36
+ return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth();
37
+ }
38
+
39
+ export function startOfMonth(date: Date): Date {
40
+ return new Date(date.getFullYear(), date.getMonth(), 1);
41
+ }
42
+
43
+ /**
44
+ * Day 0 of the *next* month, which the platform resolves to the last day of
45
+ * this one — so a leap February needs no special case.
46
+ */
47
+ export function endOfMonth(date: Date): Date {
48
+ return new Date(date.getFullYear(), date.getMonth() + 1, 0);
49
+ }
50
+
51
+ export function addMonths(date: Date, months: number): Date {
52
+ const copy = new Date(date.getFullYear(), date.getMonth(), 1);
53
+ copy.setMonth(copy.getMonth() + months);
54
+ return copy;
55
+ }
56
+
57
+ export function addDays(date: Date, days: number): Date {
58
+ const copy = new Date(date);
59
+ copy.setDate(copy.getDate() + days);
60
+ return copy;
61
+ }
62
+
63
+ /** Whole days from `a` to `b`, ignoring the time of day on both. */
64
+ export function daysBetween(a: Date, b: Date): number {
65
+ const MS = 24 * 60 * 60 * 1000;
66
+ return Math.round((startOfDay(b).getTime() - startOfDay(a).getTime()) / MS);
67
+ }
68
+
69
+ export function isBefore(a: Date, b: Date): boolean {
70
+ return startOfDay(a).getTime() < startOfDay(b).getTime();
71
+ }
72
+
73
+ export function isAfter(a: Date, b: Date): boolean {
74
+ return startOfDay(a).getTime() > startOfDay(b).getTime();
75
+ }
76
+
77
+ /** Inclusive at both ends, which is what a selected range means to a reader. */
78
+ export function isWithin(date: Date, from: Date, to: Date): boolean {
79
+ const time = startOfDay(date).getTime();
80
+ const start = startOfDay(from).getTime();
81
+ const end = startOfDay(to).getTime();
82
+ return time >= Math.min(start, end) && time <= Math.max(start, end);
83
+ }
84
+
85
+ export function clampDate(date: Date, min?: Date, max?: Date): Date {
86
+ if (min && isBefore(date, min)) return min;
87
+ if (max && isAfter(date, max)) return max;
88
+ return date;
89
+ }
90
+
91
+ /**
92
+ * The six-week grid for a month.
93
+ *
94
+ * Always six rows, and always starting on the chosen first day of the week, so
95
+ * the calendar does not change height as the months are paged through. A grid
96
+ * that grows a row in March and loses it again in April makes everything below
97
+ * it jump, and the days themselves appear to move between months.
98
+ *
99
+ * The leading and trailing cells belong to the neighbouring months; whether
100
+ * they are drawn or left blank is the caller's decision.
101
+ */
102
+ export function monthGrid(month: Date, weekStartsOn: number): Date[][] {
103
+ const first = startOfMonth(month);
104
+ // How far back to the most recent `weekStartsOn`. The `+ 7` keeps the
105
+ // modulo positive when the week starts later than the first of the month.
106
+ const lead = (first.getDay() - weekStartsOn + 7) % 7;
107
+ const start = addDays(first, -lead);
108
+
109
+ return Array.from({ length: 6 }, (_unusedWeek, week) =>
110
+ Array.from({ length: 7 }, (_unusedDay, day) => addDays(start, week * 7 + day))
111
+ );
112
+ }
113
+
114
+ /**
115
+ * Formatting, guarded.
116
+ *
117
+ * `Intl` is on modern Hermes, but the data behind it is not guaranteed —
118
+ * builds without the full ICU set throw or return something unusable for
119
+ * locales they do not carry. Every formatter here falls back to English rather
120
+ * than taking the screen down over a month name.
121
+ */
122
+ function format(date: Date, locale: DateLocale, options: Intl.DateTimeFormatOptions): string | null {
123
+ try {
124
+ return new Intl.DateTimeFormat(locale, options).format(date);
125
+ } catch {
126
+ return null;
127
+ }
128
+ }
129
+
130
+ /**
131
+ * A locale tag carrying an explicit calendar and Latin digits.
132
+ *
133
+ * The calendar has to go in the *tag*, not in the options bag. Hermes builds
134
+ * its `Intl` on the platform's own date formatter, which reads the Unicode
135
+ * extension on a locale identifier but does not reliably honour `calendar` as
136
+ * an option — so `{ calendar: 'gregory' }` was quietly ignored and an Arabic
137
+ * device kept answering in Hijri. Any extension already on the tag is dropped
138
+ * first, so a caller passing `ar-u-ca-islamic` cannot end up with two.
139
+ */
140
+ function withCalendar(locale: DateLocale, calendar: 'gregory' | 'islamic'): string {
141
+ const base = (locale ?? 'en').split('-u-')[0] || 'en';
142
+ return `${base}-u-ca-${calendar}-nu-latn`;
143
+ }
144
+
145
+ /**
146
+ * Gregorian formatting, pinned.
147
+ *
148
+ * Asking for a month name in Arabic without saying which calendar gets a Hijri
149
+ * one back, because that is what the locale resolves to — right for the
150
+ * language, wrong for a grid whose cells are counting Gregorian days. The
151
+ * digits are pinned too, so the caption and the numbers under it are in one
152
+ * set.
153
+ */
154
+ function formatGregory(
155
+ date: Date,
156
+ locale: DateLocale,
157
+ options: Intl.DateTimeFormatOptions
158
+ ): string | null {
159
+ return (
160
+ format(date, withCalendar(locale, 'gregory'), options) ??
161
+ // A build that cannot parse the extension at all still gets asked, in the
162
+ // hope that it honours the option instead.
163
+ format(date, locale, { ...options, calendar: 'gregory', numberingSystem: 'latn' })
164
+ );
165
+ }
166
+
167
+ /* -------------------------------------------------------------------------- */
168
+ /* Calendar systems */
169
+ /* -------------------------------------------------------------------------- */
170
+
171
+ /**
172
+ * Which calendar the months and day numbers are counted in.
173
+ *
174
+ * `auto` takes whatever the locale resolves to, which on an Arabic device is
175
+ * usually islamic. It is not the default: a grid whose month boundaries move
176
+ * with the device's language is a surprise, and the caller who wants that can
177
+ * ask for it.
178
+ */
179
+ export type CalendarSystem = 'gregory' | 'islamic' | 'auto';
180
+
181
+ /** Year, month and day as the chosen calendar counts them. 1-based month. */
182
+ export interface CalendarParts {
183
+ year: number;
184
+ month: number;
185
+ day: number;
186
+ }
187
+
188
+ /** What `auto` resolves to for a locale, falling back to the Gregorian. */
189
+ export function resolveCalendar(system: CalendarSystem, locale: DateLocale): 'gregory' | 'islamic' {
190
+ if (system !== 'auto') return system;
191
+ try {
192
+ const resolved = new Intl.DateTimeFormat(locale).resolvedOptions().calendar ?? '';
193
+ return resolved.startsWith('islamic') ? 'islamic' : 'gregory';
194
+ } catch {
195
+ return 'gregory';
196
+ }
197
+ }
198
+
199
+ /**
200
+ * A date's year, month and day in the given calendar.
201
+ *
202
+ * `formatToParts` rather than a parsed string, because the order and the
203
+ * separators are the locale's business and we only want the numbers. The
204
+ * numbering system is pinned to Latin so the parts come back parseable
205
+ * whatever language is on the device, and so the cells and the caption above
206
+ * them end up in one set of digits.
207
+ */
208
+ export function calendarParts(
209
+ date: Date,
210
+ system: 'gregory' | 'islamic',
211
+ locale: DateLocale
212
+ ): CalendarParts {
213
+ if (system === 'gregory') {
214
+ return { year: date.getFullYear(), month: date.getMonth() + 1, day: date.getDate() };
215
+ }
216
+ try {
217
+ const parts = new Intl.DateTimeFormat(withCalendar(locale, 'islamic'), {
218
+ year: 'numeric',
219
+ month: 'numeric',
220
+ day: 'numeric',
221
+ }).formatToParts(date);
222
+ const read = (type: string) => Number(parts.find((part) => part.type === type)?.value);
223
+ const year = read('year');
224
+ const month = read('month');
225
+ const day = read('day');
226
+ if (!Number.isFinite(year) || !Number.isFinite(month) || !Number.isFinite(day)) {
227
+ throw new Error('unusable');
228
+ }
229
+ return { year, month, day };
230
+ } catch {
231
+ // No islamic data in this build. Gregorian is wrong, but it is coherent —
232
+ // the grid, the caption and the cells will at least agree with each other.
233
+ return { year: date.getFullYear(), month: date.getMonth() + 1, day: date.getDate() };
234
+ }
235
+ }
236
+
237
+ export function isSameCalendarMonth(
238
+ a: Date,
239
+ b: Date,
240
+ system: 'gregory' | 'islamic',
241
+ locale: DateLocale
242
+ ): boolean {
243
+ if (system === 'gregory') return isSameMonth(a, b);
244
+ const one = calendarParts(a, system, locale);
245
+ const two = calendarParts(b, system, locale);
246
+ return one.year === two.year && one.month === two.month;
247
+ }
248
+
249
+ /**
250
+ * The first day of the calendar month `date` falls in.
251
+ *
252
+ * Walked back a day at a time rather than computed. A Hijri month is 29 or 30
253
+ * days depending on the year, and the platform's own calendar data already
254
+ * knows which — stepping until the day number reads 1 asks it, instead of
255
+ * shipping a table that will disagree with the device.
256
+ */
257
+ export function startOfCalendarMonth(
258
+ date: Date,
259
+ system: 'gregory' | 'islamic',
260
+ locale: DateLocale
261
+ ): Date {
262
+ if (system === 'gregory') return startOfMonth(date);
263
+ let cursor = startOfDay(date);
264
+ for (let step = 0; step < 31; step += 1) {
265
+ if (calendarParts(cursor, system, locale).day === 1) return cursor;
266
+ cursor = addDays(cursor, -1);
267
+ }
268
+ return cursor;
269
+ }
270
+
271
+ /** Days in the calendar month `date` falls in. */
272
+ export function daysInCalendarMonth(
273
+ date: Date,
274
+ system: 'gregory' | 'islamic',
275
+ locale: DateLocale
276
+ ): number {
277
+ if (system === 'gregory') return endOfMonth(date).getDate();
278
+ const start = startOfCalendarMonth(date, system, locale);
279
+ for (let length = 28; length <= 31; length += 1) {
280
+ if (calendarParts(addDays(start, length), system, locale).day === 1) return length;
281
+ }
282
+ return 30;
283
+ }
284
+
285
+ /** Page by whole calendar months, forwards or back. */
286
+ export function addCalendarMonths(
287
+ date: Date,
288
+ months: number,
289
+ system: 'gregory' | 'islamic',
290
+ locale: DateLocale
291
+ ): Date {
292
+ if (system === 'gregory') return addMonths(date, months);
293
+ let cursor = startOfCalendarMonth(date, system, locale);
294
+ for (let step = 0; step < Math.abs(months); step += 1) {
295
+ cursor =
296
+ months > 0
297
+ ? // One past the end of this month is the first of the next.
298
+ addDays(cursor, daysInCalendarMonth(cursor, system, locale))
299
+ : // One before the start of this month is somewhere in the previous
300
+ // one, which then gets walked back to its own first day.
301
+ startOfCalendarMonth(addDays(cursor, -1), system, locale);
302
+ }
303
+ return cursor;
304
+ }
305
+
306
+ /** The day-of-month a cell shows. */
307
+ export function calendarDayNumber(
308
+ date: Date,
309
+ system: 'gregory' | 'islamic',
310
+ locale: DateLocale
311
+ ): number {
312
+ return system === 'gregory' ? date.getDate() : calendarParts(date, system, locale).day;
313
+ }
314
+
315
+ /** "Ramadan 1447" or "March 2026", in whichever calendar is in force. */
316
+ export function calendarMonthLabel(
317
+ date: Date,
318
+ system: 'gregory' | 'islamic',
319
+ locale: DateLocale
320
+ ): string {
321
+ if (system === 'gregory') return monthLabel(date, locale);
322
+ return (
323
+ format(date, withCalendar(locale, 'islamic'), { month: 'long', year: 'numeric' }) ??
324
+ monthLabel(date, locale)
325
+ );
326
+ }
327
+
328
+ /**
329
+ * The names of a calendar's months, in order, for the dropdown.
330
+ *
331
+ * Sampled from a real year in that calendar rather than from twelve Gregorian
332
+ * firsts — the old code did the latter and got twelve arbitrary Hijri names in
333
+ * whatever order the Gregorian 1sts happened to fall, then indexed them by
334
+ * Gregorian month, which meant nothing at all.
335
+ */
336
+ export function calendarMonthNames(
337
+ reference: Date,
338
+ system: 'gregory' | 'islamic',
339
+ locale: DateLocale,
340
+ style: 'long' | 'short' = 'short'
341
+ ): string[] {
342
+ if (system === 'gregory') return monthNames(locale, style);
343
+ const names: string[] = [];
344
+ let cursor = startOfCalendarMonth(reference, system, locale);
345
+ // Back to month 1 of this year, then forward through all twelve.
346
+ cursor = addCalendarMonths(cursor, -(calendarParts(cursor, system, locale).month - 1), system, locale);
347
+ for (let month = 0; month < 12; month += 1) {
348
+ names.push(
349
+ format(cursor, withCalendar(locale, 'islamic'), { month: style }) ?? String(month + 1)
350
+ );
351
+ cursor = addCalendarMonths(cursor, 1, system, locale);
352
+ }
353
+ return names;
354
+ }
355
+
356
+ /** The whole date read out, in whichever calendar is in force. */
357
+ export function calendarLongDate(
358
+ date: Date,
359
+ system: 'gregory' | 'islamic',
360
+ locale: DateLocale
361
+ ): string {
362
+ if (system === 'gregory') return longDate(date, locale);
363
+ return (
364
+ format(date, withCalendar(locale, 'islamic'), {
365
+ weekday: 'long',
366
+ day: 'numeric',
367
+ month: 'long',
368
+ year: 'numeric',
369
+ }) ?? longDate(date, locale)
370
+ );
371
+ }
372
+
373
+ /** The compact form a trigger shows, in whichever calendar is in force. */
374
+ export function calendarShortDate(
375
+ date: Date,
376
+ system: 'gregory' | 'islamic',
377
+ locale: DateLocale
378
+ ): string {
379
+ if (system === 'gregory') return shortDate(date, locale);
380
+ return (
381
+ format(date, withCalendar(locale, 'islamic'), {
382
+ day: 'numeric',
383
+ month: 'short',
384
+ year: 'numeric',
385
+ }) ?? shortDate(date, locale)
386
+ );
387
+ }
388
+
389
+ const MONTHS_EN = [
390
+ 'January',
391
+ 'February',
392
+ 'March',
393
+ 'April',
394
+ 'May',
395
+ 'June',
396
+ 'July',
397
+ 'August',
398
+ 'September',
399
+ 'October',
400
+ 'November',
401
+ 'December',
402
+ ];
403
+
404
+ const WEEKDAYS_EN = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
405
+
406
+ /** Month names in order, for the caption and its dropdown. */
407
+ export function monthNames(locale: DateLocale, style: 'long' | 'short' = 'long'): string[] {
408
+ return MONTHS_EN.map((fallback, month) => {
409
+ const formatted = formatGregory(new Date(2021, month, 1), locale, { month: style });
410
+ return formatted ?? (style === 'short' ? fallback.slice(0, 3) : fallback);
411
+ });
412
+ }
413
+
414
+ /** Column headings, rotated so the first one is `weekStartsOn`. */
415
+ export function weekdayNames(locale: DateLocale, weekStartsOn: number): string[] {
416
+ // 2021-08-01 was a Sunday, so adding the index lands on each weekday in turn.
417
+ return Array.from({ length: 7 }, (_unused, day) => {
418
+ const index = (day + weekStartsOn) % 7;
419
+ const formatted = formatGregory(new Date(2021, 7, 1 + index), locale, { weekday: 'short' });
420
+ return formatted ?? WEEKDAYS_EN[index]!;
421
+ });
422
+ }
423
+
424
+ /** "March 2026", for the caption. */
425
+ export function monthLabel(date: Date, locale: DateLocale): string {
426
+ return (
427
+ formatGregory(date, locale, { month: 'long', year: 'numeric' }) ??
428
+ `${MONTHS_EN[date.getMonth()]} ${date.getFullYear()}`
429
+ );
430
+ }
431
+
432
+ /** "12 March 2026" — the whole date, for a screen reader to read out. */
433
+ export function longDate(date: Date, locale: DateLocale): string {
434
+ return (
435
+ formatGregory(date, locale, { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' }) ??
436
+ `${WEEKDAYS_EN[date.getDay()]} ${date.getDate()} ${MONTHS_EN[date.getMonth()]} ${date.getFullYear()}`
437
+ );
438
+ }
439
+
440
+ /** "12 Mar 2026" — the compact form a trigger shows. */
441
+ export function shortDate(date: Date, locale: DateLocale): string {
442
+ return (
443
+ formatGregory(date, locale, { day: 'numeric', month: 'short', year: 'numeric' }) ??
444
+ `${date.getDate()} ${MONTHS_EN[date.getMonth()]!.slice(0, 3)} ${date.getFullYear()}`
445
+ );
446
+ }