panelui-native 0.22.6 → 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.
- package/README.md +1 -0
- package/lib/module/components/accordion/index.js +8 -8
- package/lib/module/components/accordion/index.js.map +1 -1
- package/lib/module/components/alert/index.js +2 -2
- package/lib/module/components/alert/index.js.map +1 -1
- package/lib/module/components/area-chart/index.js +958 -0
- package/lib/module/components/area-chart/index.js.map +1 -0
- package/lib/module/components/attachment/index.js +2 -1
- package/lib/module/components/attachment/index.js.map +1 -1
- package/lib/module/components/avatar/index.js +4 -4
- package/lib/module/components/avatar/index.js.map +1 -1
- package/lib/module/components/badge/index.js +4 -4
- package/lib/module/components/badge/index.js.map +1 -1
- package/lib/module/components/bar-chart/index.js +994 -0
- package/lib/module/components/bar-chart/index.js.map +1 -0
- package/lib/module/components/bottom-sheet/index.js +213 -24
- package/lib/module/components/bottom-sheet/index.js.map +1 -1
- package/lib/module/components/breadcrumb/index.js +275 -0
- package/lib/module/components/breadcrumb/index.js.map +1 -0
- package/lib/module/components/button/index.js +11 -6
- package/lib/module/components/button/index.js.map +1 -1
- package/lib/module/components/calendar/index.js +557 -0
- package/lib/module/components/calendar/index.js.map +1 -0
- package/lib/module/components/carousel/index.js +680 -0
- package/lib/module/components/carousel/index.js.map +1 -0
- package/lib/module/components/chip/index.js +6 -6
- package/lib/module/components/chip/index.js.map +1 -1
- package/lib/module/components/date-picker/index.js +158 -0
- package/lib/module/components/date-picker/index.js.map +1 -0
- package/lib/module/components/dialog/index.js +7 -2
- package/lib/module/components/dialog/index.js.map +1 -1
- package/lib/module/components/direction/index.js +15 -20
- package/lib/module/components/direction/index.js.map +1 -1
- package/lib/module/components/empty-state/index.js +2 -2
- package/lib/module/components/empty-state/index.js.map +1 -1
- package/lib/module/components/field/index.js +5 -5
- package/lib/module/components/field/index.js.map +1 -1
- package/lib/module/components/flow/index.js +3 -3
- package/lib/module/components/flow/index.js.map +1 -1
- package/lib/module/components/frame/index.js +22 -5
- package/lib/module/components/frame/index.js.map +1 -1
- package/lib/module/components/heatmap-chart/index.js +28 -8
- package/lib/module/components/heatmap-chart/index.js.map +1 -1
- package/lib/module/components/input-group/index.js +5 -4
- package/lib/module/components/input-group/index.js.map +1 -1
- package/lib/module/components/item/index.js +9 -9
- package/lib/module/components/item/index.js.map +1 -1
- package/lib/module/components/label/index.js +4 -4
- package/lib/module/components/label/index.js.map +1 -1
- package/lib/module/components/line-chart/index.js +165 -184
- package/lib/module/components/line-chart/index.js.map +1 -1
- package/lib/module/components/loader/index.js +758 -0
- package/lib/module/components/loader/index.js.map +1 -0
- package/lib/module/components/map/basemap.js +265 -0
- package/lib/module/components/map/basemap.js.map +1 -0
- package/lib/module/components/map/index.js +850 -0
- package/lib/module/components/map/index.js.map +1 -0
- package/lib/module/components/map/maplibre.js +87 -0
- package/lib/module/components/map/maplibre.js.map +1 -0
- package/lib/module/components/marker/index.js +3 -3
- package/lib/module/components/marker/index.js.map +1 -1
- package/lib/module/components/message/index.js +15 -14
- package/lib/module/components/message/index.js.map +1 -1
- package/lib/module/components/message-scroller/index.js +5 -4
- package/lib/module/components/message-scroller/index.js.map +1 -1
- package/lib/module/components/otp-input/index.js +2 -2
- package/lib/module/components/popover/index.js +39 -7
- package/lib/module/components/popover/index.js.map +1 -1
- package/lib/module/components/progress/index.js +8 -2
- package/lib/module/components/progress/index.js.map +1 -1
- package/lib/module/components/radio-group/index.js +2 -2
- package/lib/module/components/radio-group/index.js.map +1 -1
- package/lib/module/components/ring-chart/index.js +423 -0
- package/lib/module/components/ring-chart/index.js.map +1 -0
- package/lib/module/components/section-rail/index.js +6 -6
- package/lib/module/components/section-rail/index.js.map +1 -1
- package/lib/module/components/select/index.js +10 -4
- package/lib/module/components/select/index.js.map +1 -1
- package/lib/module/components/separator/index.js +76 -22
- package/lib/module/components/separator/index.js.map +1 -1
- package/lib/module/components/shimmer/index.js +10 -1
- package/lib/module/components/shimmer/index.js.map +1 -1
- package/lib/module/components/slider/index.js +19 -5
- package/lib/module/components/slider/index.js.map +1 -1
- package/lib/module/components/steps/index.js +5 -5
- package/lib/module/components/steps/index.js.map +1 -1
- package/lib/module/components/switch/index.js +6 -1
- package/lib/module/components/switch/index.js.map +1 -1
- package/lib/module/components/tabs/index.js +7 -7
- package/lib/module/components/tabs/index.js.map +1 -1
- package/lib/module/components/timeline/index.js +4 -4
- package/lib/module/components/timeline/index.js.map +1 -1
- package/lib/module/components/toast/index.js +2 -2
- package/lib/module/components/toast/index.js.map +1 -1
- package/lib/module/components/toggle-button/index.js +5 -5
- package/lib/module/components/toggle-button/index.js.map +1 -1
- package/lib/module/components/tooltip/index.js +492 -0
- package/lib/module/components/tooltip/index.js.map +1 -0
- package/lib/module/components/typography/index.js +7 -2
- package/lib/module/components/typography/index.js.map +1 -1
- package/lib/module/hooks/index.js +1 -0
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/use-back-handler.js +37 -0
- package/lib/module/hooks/use-back-handler.js.map +1 -0
- package/lib/module/hooks/use-direction.js +43 -0
- package/lib/module/hooks/use-direction.js.map +1 -0
- package/lib/module/icons/index.js +180 -2
- package/lib/module/icons/index.js.map +1 -1
- package/lib/module/index.js +12 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/primitives/keyboard-avoider.js +2 -1
- package/lib/module/primitives/keyboard-avoider.js.map +1 -1
- package/lib/module/primitives/text.js +55 -11
- package/lib/module/primitives/text.js.map +1 -1
- package/lib/module/utils/chart.js +279 -0
- package/lib/module/utils/chart.js.map +1 -0
- package/lib/module/utils/date.js +386 -0
- package/lib/module/utils/date.js.map +1 -0
- package/lib/typescript/src/components/accordion/index.d.ts.map +1 -1
- package/lib/typescript/src/components/alert/index.d.ts.map +1 -1
- package/lib/typescript/src/components/area-chart/index.d.ts +237 -0
- package/lib/typescript/src/components/area-chart/index.d.ts.map +1 -0
- package/lib/typescript/src/components/attachment/index.d.ts.map +1 -1
- package/lib/typescript/src/components/bar-chart/index.d.ts +270 -0
- package/lib/typescript/src/components/bar-chart/index.d.ts.map +1 -0
- package/lib/typescript/src/components/bottom-sheet/index.d.ts +75 -4
- package/lib/typescript/src/components/bottom-sheet/index.d.ts.map +1 -1
- package/lib/typescript/src/components/breadcrumb/index.d.ts +100 -0
- package/lib/typescript/src/components/breadcrumb/index.d.ts.map +1 -0
- package/lib/typescript/src/components/button/index.d.ts.map +1 -1
- package/lib/typescript/src/components/calendar/index.d.ts +108 -0
- package/lib/typescript/src/components/calendar/index.d.ts.map +1 -0
- package/lib/typescript/src/components/carousel/index.d.ts +179 -0
- package/lib/typescript/src/components/carousel/index.d.ts.map +1 -0
- package/lib/typescript/src/components/chip/index.d.ts.map +1 -1
- package/lib/typescript/src/components/date-picker/index.d.ts +91 -0
- package/lib/typescript/src/components/date-picker/index.d.ts.map +1 -0
- package/lib/typescript/src/components/dialog/index.d.ts.map +1 -1
- package/lib/typescript/src/components/direction/index.d.ts +11 -10
- package/lib/typescript/src/components/direction/index.d.ts.map +1 -1
- package/lib/typescript/src/components/empty-state/index.d.ts.map +1 -1
- package/lib/typescript/src/components/field/index.d.ts.map +1 -1
- package/lib/typescript/src/components/frame/index.d.ts +17 -0
- package/lib/typescript/src/components/frame/index.d.ts.map +1 -1
- package/lib/typescript/src/components/heatmap-chart/index.d.ts +22 -3
- package/lib/typescript/src/components/heatmap-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/input-group/index.d.ts.map +1 -1
- package/lib/typescript/src/components/item/index.d.ts.map +1 -1
- package/lib/typescript/src/components/label/index.d.ts.map +1 -1
- package/lib/typescript/src/components/line-chart/index.d.ts +42 -2
- package/lib/typescript/src/components/line-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/loader/index.d.ts +38 -0
- package/lib/typescript/src/components/loader/index.d.ts.map +1 -0
- package/lib/typescript/src/components/map/basemap.d.ts +76 -0
- package/lib/typescript/src/components/map/basemap.d.ts.map +1 -0
- package/lib/typescript/src/components/map/index.d.ts +367 -0
- package/lib/typescript/src/components/map/index.d.ts.map +1 -0
- package/lib/typescript/src/components/map/maplibre.d.ts +193 -0
- package/lib/typescript/src/components/map/maplibre.d.ts.map +1 -0
- package/lib/typescript/src/components/marker/index.d.ts.map +1 -1
- package/lib/typescript/src/components/message/index.d.ts.map +1 -1
- package/lib/typescript/src/components/message-scroller/index.d.ts.map +1 -1
- package/lib/typescript/src/components/popover/index.d.ts.map +1 -1
- package/lib/typescript/src/components/progress/index.d.ts.map +1 -1
- package/lib/typescript/src/components/ring-chart/index.d.ts +156 -0
- package/lib/typescript/src/components/ring-chart/index.d.ts.map +1 -0
- package/lib/typescript/src/components/select/index.d.ts.map +1 -1
- package/lib/typescript/src/components/separator/index.d.ts +36 -1
- package/lib/typescript/src/components/separator/index.d.ts.map +1 -1
- package/lib/typescript/src/components/shimmer/index.d.ts.map +1 -1
- package/lib/typescript/src/components/slider/index.d.ts.map +1 -1
- package/lib/typescript/src/components/steps/index.d.ts.map +1 -1
- package/lib/typescript/src/components/switch/index.d.ts.map +1 -1
- package/lib/typescript/src/components/tabs/index.d.ts.map +1 -1
- package/lib/typescript/src/components/timeline/index.d.ts.map +1 -1
- package/lib/typescript/src/components/toast/index.d.ts.map +1 -1
- package/lib/typescript/src/components/toggle-button/index.d.ts.map +1 -1
- package/lib/typescript/src/components/tooltip/index.d.ts +125 -0
- package/lib/typescript/src/components/tooltip/index.d.ts.map +1 -0
- package/lib/typescript/src/components/typography/index.d.ts +6 -0
- package/lib/typescript/src/components/typography/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/index.d.ts +1 -0
- package/lib/typescript/src/hooks/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/use-back-handler.d.ts +19 -0
- package/lib/typescript/src/hooks/use-back-handler.d.ts.map +1 -0
- package/lib/typescript/src/hooks/use-direction.d.ts +20 -0
- package/lib/typescript/src/hooks/use-direction.d.ts.map +1 -0
- package/lib/typescript/src/icons/index.d.ts +8 -0
- package/lib/typescript/src/icons/index.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +14 -4
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/primitives/keyboard-avoider.d.ts.map +1 -1
- package/lib/typescript/src/primitives/text.d.ts +20 -0
- package/lib/typescript/src/primitives/text.d.ts.map +1 -1
- package/lib/typescript/src/utils/chart.d.ts +96 -0
- package/lib/typescript/src/utils/chart.d.ts.map +1 -0
- package/lib/typescript/src/utils/date.d.ts +116 -0
- package/lib/typescript/src/utils/date.d.ts.map +1 -0
- package/package.json +9 -1
- package/src/components/accordion/index.tsx +8 -12
- package/src/components/alert/index.tsx +2 -2
- package/src/components/area-chart/index.tsx +1147 -0
- package/src/components/attachment/index.tsx +2 -2
- package/src/components/avatar/index.tsx +4 -4
- package/src/components/badge/index.tsx +6 -6
- package/src/components/bar-chart/index.tsx +1208 -0
- package/src/components/bottom-sheet/index.tsx +282 -21
- package/src/components/breadcrumb/index.tsx +363 -0
- package/src/components/button/index.tsx +12 -8
- package/src/components/calendar/index.tsx +703 -0
- package/src/components/carousel/index.tsx +788 -0
- package/src/components/chip/index.tsx +6 -8
- package/src/components/date-picker/index.tsx +237 -0
- package/src/components/dialog/index.tsx +7 -2
- package/src/components/direction/index.tsx +22 -22
- package/src/components/empty-state/index.tsx +2 -2
- package/src/components/field/index.tsx +3 -5
- package/src/components/flow/index.tsx +3 -3
- package/src/components/frame/index.tsx +22 -7
- package/src/components/heatmap-chart/index.tsx +58 -11
- package/src/components/input-group/index.tsx +5 -4
- package/src/components/item/index.tsx +9 -9
- package/src/components/label/index.tsx +2 -2
- package/src/components/line-chart/index.tsx +194 -223
- package/src/components/loader/index.tsx +812 -0
- package/src/components/map/basemap.ts +304 -0
- package/src/components/map/index.tsx +1116 -0
- package/src/components/map/maplibre.ts +201 -0
- package/src/components/marker/index.tsx +3 -3
- package/src/components/message/index.tsx +15 -14
- package/src/components/message-scroller/index.tsx +5 -4
- package/src/components/otp-input/index.tsx +2 -2
- package/src/components/popover/index.tsx +43 -7
- package/src/components/progress/index.tsx +10 -2
- package/src/components/radio-group/index.tsx +2 -2
- package/src/components/ring-chart/index.tsx +543 -0
- package/src/components/section-rail/index.tsx +6 -6
- package/src/components/select/index.tsx +10 -4
- package/src/components/separator/index.tsx +77 -19
- package/src/components/shimmer/index.tsx +10 -1
- package/src/components/slider/index.tsx +20 -5
- package/src/components/steps/index.tsx +5 -5
- package/src/components/switch/index.tsx +6 -1
- package/src/components/tabs/index.tsx +7 -9
- package/src/components/timeline/index.tsx +4 -4
- package/src/components/toast/index.tsx +2 -2
- package/src/components/toggle-button/index.tsx +5 -7
- package/src/components/tooltip/index.tsx +610 -0
- package/src/components/typography/index.tsx +7 -2
- package/src/hooks/index.ts +1 -0
- package/src/hooks/use-back-handler.ts +37 -0
- package/src/hooks/use-direction.ts +45 -0
- package/src/icons/index.tsx +111 -0
- package/src/index.ts +129 -0
- package/src/primitives/keyboard-avoider.tsx +2 -1
- package/src/primitives/text.tsx +53 -8
- package/src/utils/chart.ts +355 -0
- package/src/utils/date.ts +446 -0
|
@@ -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
|
+
}
|