@klinking/colander 0.0.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 (93) hide show
  1. package/dist/formats/date.cjs +49 -0
  2. package/dist/formats/date.d.cts +45 -0
  3. package/dist/formats/date.d.mts +45 -0
  4. package/dist/formats/date.mjs +2 -0
  5. package/dist/formats/object.cjs +49 -0
  6. package/dist/formats/object.d.cts +45 -0
  7. package/dist/formats/object.d.mts +45 -0
  8. package/dist/formats/object.mjs +2 -0
  9. package/dist/formats/plain-date-time.cjs +49 -0
  10. package/dist/formats/plain-date-time.d.cts +45 -0
  11. package/dist/formats/plain-date-time.d.mts +45 -0
  12. package/dist/formats/plain-date-time.mjs +2 -0
  13. package/dist/formats/plain-date.cjs +49 -0
  14. package/dist/formats/plain-date.d.cts +45 -0
  15. package/dist/formats/plain-date.d.mts +45 -0
  16. package/dist/formats/plain-date.mjs +2 -0
  17. package/dist/formats/plain-month-day.cjs +49 -0
  18. package/dist/formats/plain-month-day.d.cts +45 -0
  19. package/dist/formats/plain-month-day.d.mts +45 -0
  20. package/dist/formats/plain-month-day.mjs +2 -0
  21. package/dist/formats/plain-year-month.cjs +49 -0
  22. package/dist/formats/plain-year-month.d.cts +45 -0
  23. package/dist/formats/plain-year-month.d.mts +45 -0
  24. package/dist/formats/plain-year-month.mjs +2 -0
  25. package/dist/formats/zoned-date-time.cjs +49 -0
  26. package/dist/formats/zoned-date-time.d.cts +45 -0
  27. package/dist/formats/zoned-date-time.d.mts +45 -0
  28. package/dist/formats/zoned-date-time.mjs +2 -0
  29. package/dist/index.cjs +49 -0
  30. package/dist/index.d.cts +2 -0
  31. package/dist/index.d.mts +2 -0
  32. package/dist/index.mjs +2 -0
  33. package/dist/temporal-polyfill-DTx0VIX5.mjs +4556 -0
  34. package/dist/temporal-polyfill-DU8VfleL.cjs +4860 -0
  35. package/dist/weeks-view-context-BN6qR-83.d.mts +2022 -0
  36. package/dist/weeks-view-context-aHGgYkpt.d.cts +2022 -0
  37. package/package.json +89 -0
  38. package/src/calendar-context.ts +22 -0
  39. package/src/calendar-provider.test.tsx +123 -0
  40. package/src/calendar-provider.tsx +538 -0
  41. package/src/calendar-types.ts +212 -0
  42. package/src/compute-weeks-in-window.test.ts +83 -0
  43. package/src/compute-weeks-in-window.ts +40 -0
  44. package/src/context.ts +34 -0
  45. package/src/day-cell.tsx +635 -0
  46. package/src/drag-handle.tsx +137 -0
  47. package/src/formats/date.ts +110 -0
  48. package/src/formats/formats.test.ts +18 -0
  49. package/src/formats/object.ts +110 -0
  50. package/src/formats/plain-date-time.ts +110 -0
  51. package/src/formats/plain-date.ts +110 -0
  52. package/src/formats/plain-month-day.ts +110 -0
  53. package/src/formats/plain-year-month.ts +110 -0
  54. package/src/formats/zoned-date-time.ts +110 -0
  55. package/src/grid-header.tsx +141 -0
  56. package/src/grid.event-perf.test.tsx +282 -0
  57. package/src/grid.perf.test.tsx +467 -0
  58. package/src/grid.tsx +608 -0
  59. package/src/index.ts +116 -0
  60. package/src/keyboard.test.ts +498 -0
  61. package/src/keyboard.ts +142 -0
  62. package/src/month-separator.test.tsx +183 -0
  63. package/src/month-separator.tsx +238 -0
  64. package/src/month-view-context.ts +26 -0
  65. package/src/month-view-types.ts +89 -0
  66. package/src/month-view.test.tsx +1433 -0
  67. package/src/month-view.tsx +373 -0
  68. package/src/navigation.tsx +350 -0
  69. package/src/overflow.test.ts +124 -0
  70. package/src/overflow.ts +233 -0
  71. package/src/range-preview.tsx +90 -0
  72. package/src/resolve-first-week.test.ts +132 -0
  73. package/src/resolve-first-week.ts +145 -0
  74. package/src/root-selection.ts +621 -0
  75. package/src/selected-range.tsx +163 -0
  76. package/src/temporal-polyfill.test.ts +317 -0
  77. package/src/temporal-polyfill.ts +566 -0
  78. package/src/test-temporal.ts +17 -0
  79. package/src/types.ts +814 -0
  80. package/src/utils.bench.ts +183 -0
  81. package/src/utils.test.ts +983 -0
  82. package/src/utils.ts +493 -0
  83. package/src/view-context.ts +28 -0
  84. package/src/week-number.tsx +118 -0
  85. package/src/weeks-grid.tsx +265 -0
  86. package/src/weeks-keyboard.test.ts +222 -0
  87. package/src/weeks-keyboard.ts +156 -0
  88. package/src/weeks-navigation.tsx +209 -0
  89. package/src/weeks-view-context.ts +26 -0
  90. package/src/weeks-view-state.ts +421 -0
  91. package/src/weeks-view-types.ts +102 -0
  92. package/src/weeks-view.test.tsx +479 -0
  93. package/src/weeks-view.tsx +127 -0
@@ -0,0 +1,212 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import type {
3
+ DateRange,
4
+ DateValueObject,
5
+ RangeMode,
6
+ RawValueForFormat,
7
+ TemporalNamespace,
8
+ ValueChangeMeta,
9
+ ValueFormat,
10
+ WeekStartDay,
11
+ } from "./types";
12
+
13
+ // ---------------------------------------------------------------------------
14
+ // CalendarProvider props (discriminated union mirroring Root's pattern)
15
+ // ---------------------------------------------------------------------------
16
+
17
+ interface CalendarProviderPropsBase<F extends ValueFormat = ValueFormat> {
18
+ /**
19
+ * The value format used for date serialization. Determines the type of
20
+ * `value`, `defaultValue`, `min`, `max`, and callback parameters.
21
+ * @default "PlainDate"
22
+ */
23
+ format?: F;
24
+ /** Earliest selectable date. Dates before this are disabled. */
25
+ min?: RawValueForFormat<F>;
26
+ /** Latest selectable date. Dates after this are disabled. */
27
+ max?: RawValueForFormat<F>;
28
+ /**
29
+ * When `true`, the entire calendar is disabled.
30
+ * @default false
31
+ */
32
+ disabled?: boolean;
33
+ /**
34
+ * When `true`, the calendar is read-only. Keyboard navigation still works
35
+ * but selection is prevented.
36
+ * @default false
37
+ */
38
+ readOnly?: boolean;
39
+ /**
40
+ * Callback to disable individual dates. Return `true` to disable a date.
41
+ * Called in addition to `min`/`max` bounds checking.
42
+ */
43
+ isDateDisabled?: (date: Temporal.PlainDate) => boolean;
44
+ /**
45
+ * IANA time zone identifier used for date/time conversions.
46
+ * @default The system's current time zone.
47
+ */
48
+ timeZone?: string;
49
+ /**
50
+ * BCP 47 locale string used for formatting month names, weekday labels,
51
+ * and other locale-sensitive output.
52
+ * @default "en-US"
53
+ */
54
+ locale?: string;
55
+ /**
56
+ * Custom Temporal namespace for environments without native Temporal support.
57
+ */
58
+ temporal?: TemporalNamespace;
59
+ /**
60
+ * Day of the week the calendar grid starts on.
61
+ * `0` = Sunday, `1` = Monday, ..., `6` = Saturday.
62
+ * @default 0
63
+ */
64
+ weekStartDay?: WeekStartDay;
65
+ /** React children. */
66
+ children?: React.ReactNode;
67
+ }
68
+
69
+ // --- Single selection ---
70
+
71
+ interface CalendarSingleControlledProps<F extends ValueFormat = ValueFormat> {
72
+ /** @default "single" */
73
+ selectionMode?: "single";
74
+ /** The controlled selected date. Pass `null` to clear. */
75
+ value: RawValueForFormat<F> | null;
76
+ defaultValue?: never;
77
+ onValueChange?: (
78
+ value: RawValueForFormat<F> | null,
79
+ meta: ValueChangeMeta<RawValueForFormat<F> | null>,
80
+ ) => void;
81
+ }
82
+
83
+ interface CalendarSingleUncontrolledProps<F extends ValueFormat = ValueFormat> {
84
+ /** @default "single" */
85
+ selectionMode?: "single";
86
+ value?: never;
87
+ defaultValue?: RawValueForFormat<F>;
88
+ onValueChange?: (
89
+ value: RawValueForFormat<F> | null,
90
+ meta: ValueChangeMeta<RawValueForFormat<F> | null>,
91
+ ) => void;
92
+ }
93
+
94
+ // --- Range selection ---
95
+
96
+ interface CalendarRangeControlledProps<F extends ValueFormat = ValueFormat> {
97
+ selectionMode: "range";
98
+ value: DateRange<F> | null;
99
+ defaultValue?: never;
100
+ onValueChange?: (value: DateRange<F> | null, meta: ValueChangeMeta<DateRange<F> | null>) => void;
101
+ /** @default "nearest-end" */
102
+ rangeMode?: RangeMode;
103
+ /** @default false */
104
+ preventRangeReversal?: boolean;
105
+ previewRange?: DateRange<F> | null;
106
+ onHoveredDateChange?: (date: Temporal.PlainDate | undefined) => void;
107
+ }
108
+
109
+ interface CalendarRangeUncontrolledProps<F extends ValueFormat = ValueFormat> {
110
+ selectionMode: "range";
111
+ value?: never;
112
+ defaultValue?: DateRange<F>;
113
+ onValueChange?: (value: DateRange<F> | null, meta: ValueChangeMeta<DateRange<F> | null>) => void;
114
+ /** @default "nearest-end" */
115
+ rangeMode?: RangeMode;
116
+ /** @default false */
117
+ preventRangeReversal?: boolean;
118
+ previewRange?: DateRange<F> | null;
119
+ onHoveredDateChange?: (date: Temporal.PlainDate | undefined) => void;
120
+ }
121
+
122
+ // --- Multiple selection ---
123
+
124
+ interface CalendarMultipleControlledProps<F extends ValueFormat = ValueFormat> {
125
+ selectionMode: "multiple";
126
+ value: RawValueForFormat<F>[];
127
+ defaultValue?: never;
128
+ onValueChange?: (
129
+ value: RawValueForFormat<F>[],
130
+ meta: ValueChangeMeta<RawValueForFormat<F>[]>,
131
+ ) => void;
132
+ }
133
+
134
+ interface CalendarMultipleUncontrolledProps<F extends ValueFormat = ValueFormat> {
135
+ selectionMode: "multiple";
136
+ value?: never;
137
+ defaultValue?: RawValueForFormat<F>[];
138
+ onValueChange?: (
139
+ value: RawValueForFormat<F>[],
140
+ meta: ValueChangeMeta<RawValueForFormat<F>[]>,
141
+ ) => void;
142
+ }
143
+
144
+ /** Props accepted by `CalendarProvider`. */
145
+ export type CalendarProviderProps<F extends ValueFormat = ValueFormat> =
146
+ CalendarProviderPropsBase<F> &
147
+ (
148
+ | CalendarSingleControlledProps<F>
149
+ | CalendarSingleUncontrolledProps<F>
150
+ | CalendarRangeControlledProps<F>
151
+ | CalendarRangeUncontrolledProps<F>
152
+ | CalendarMultipleControlledProps<F>
153
+ | CalendarMultipleUncontrolledProps<F>
154
+ );
155
+
156
+ // ---------------------------------------------------------------------------
157
+ // Context value types
158
+ // ---------------------------------------------------------------------------
159
+
160
+ /** Stable values (callbacks, config) provided by CalendarProvider — shared across all views. */
161
+ export interface CalendarStableContextValue {
162
+ /** Selects (or toggles) a date, respecting the current selection mode. */
163
+ onSelect: (date: Temporal.PlainDate) => void;
164
+ /** Programmatically sets the range boundaries (normalized so start <= end). */
165
+ setRange: (start: Temporal.PlainDate, end: Temporal.PlainDate) => void;
166
+ /** The active selection mode. */
167
+ selectionMode: "single" | "range" | "multiple";
168
+ /** Whether the entire calendar is disabled. */
169
+ disabled: boolean;
170
+ /** Whether the calendar is read-only. */
171
+ readOnly: boolean;
172
+ /** User-supplied predicate for individually disabled dates. */
173
+ isDateDisabled?: (date: Temporal.PlainDate) => boolean;
174
+ /** Earliest selectable date (resolved from the `min` prop). */
175
+ minValue?: Temporal.PlainDate;
176
+ /** Latest selectable date (resolved from the `max` prop). */
177
+ maxValue?: Temporal.PlainDate;
178
+ /** Resolved IANA time zone. */
179
+ timeZone: string;
180
+ /** Resolved BCP 47 locale. */
181
+ locale: string;
182
+ /** Resolved Temporal namespace. */
183
+ temporal: TemporalNamespace;
184
+ /** Day the calendar week starts on. */
185
+ weekStartDay: WeekStartDay;
186
+ /** Active range selection mode. */
187
+ rangeMode: RangeMode;
188
+ /** Whether reversed ranges are auto-sorted instead of collapsed. */
189
+ preventRangeReversal: boolean;
190
+ /** The value format discriminant. */
191
+ valueFormat: ValueFormat;
192
+ /** Sets the hovered date for range preview. */
193
+ setHoveredDate: (date: Temporal.PlainDate | undefined) => void;
194
+ }
195
+
196
+ /** Volatile state provided by CalendarProvider — shared across all views. */
197
+ export interface CalendarStateContextValue {
198
+ /** The currently selected value as a tagged {@link DateValueObject}. */
199
+ selected: DateValueObject | undefined;
200
+ /** Flat array of all selected dates (plain dates, sorted). */
201
+ selectedDates: Temporal.PlainDate[];
202
+ /** Start of the current range selection, or `undefined`. */
203
+ rangeStart: Temporal.PlainDate | undefined;
204
+ /** End of the current range selection, or `undefined`. */
205
+ rangeEnd: Temporal.PlainDate | undefined;
206
+ /** The currently hovered date (for range preview). */
207
+ hoveredDate: Temporal.PlainDate | undefined;
208
+ /** Start of the computed preview range. */
209
+ previewStart: Temporal.PlainDate | undefined;
210
+ /** End of the computed preview range. */
211
+ previewEnd: Temporal.PlainDate | undefined;
212
+ }
@@ -0,0 +1,83 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, it, expect } from "vitest";
3
+ import { computeWeeksInWindow } from "./compute-weeks-in-window";
4
+
5
+ const T = Temporal;
6
+
7
+ describe("computeWeeksInWindow", () => {
8
+ it.each([
9
+ {
10
+ description: "8 weeks starting from a Sunday",
11
+ firstWeek: T.PlainDate.from("2026-03-01"), // Sunday
12
+ weekCount: 8,
13
+ weekStartDay: 0 as const,
14
+ expected: {
15
+ length: 8,
16
+ firstStart: "2026-03-01",
17
+ firstEnd: "2026-03-07",
18
+ lastStart: "2026-04-19",
19
+ lastEnd: "2026-04-25",
20
+ },
21
+ },
22
+ {
23
+ description: "8 weeks starting mid-week, snaps back to week start",
24
+ firstWeek: T.PlainDate.from("2026-03-04"), // Wednesday
25
+ weekCount: 8,
26
+ weekStartDay: 0 as const,
27
+ expected: {
28
+ length: 8,
29
+ firstStart: "2026-03-01",
30
+ firstEnd: "2026-03-07",
31
+ lastStart: "2026-04-19",
32
+ lastEnd: "2026-04-25",
33
+ },
34
+ },
35
+ {
36
+ description: "Monday week start",
37
+ firstWeek: T.PlainDate.from("2026-03-04"), // Wednesday
38
+ weekCount: 4,
39
+ weekStartDay: 1 as const,
40
+ expected: {
41
+ length: 4,
42
+ firstStart: "2026-03-02",
43
+ firstEnd: "2026-03-08",
44
+ lastStart: "2026-03-23",
45
+ lastEnd: "2026-03-29",
46
+ },
47
+ },
48
+ {
49
+ description: "single week",
50
+ firstWeek: T.PlainDate.from("2026-01-15"),
51
+ weekCount: 1,
52
+ weekStartDay: 0 as const,
53
+ expected: {
54
+ length: 1,
55
+ firstStart: "2026-01-11",
56
+ firstEnd: "2026-01-17",
57
+ lastStart: "2026-01-11",
58
+ lastEnd: "2026-01-17",
59
+ },
60
+ },
61
+ ])("$description", ({ firstWeek, weekCount, weekStartDay, expected }) => {
62
+ const weeks = computeWeeksInWindow(firstWeek, weekCount, weekStartDay, T);
63
+ expect(weeks).toHaveLength(expected.length);
64
+ expect(weeks[0].startDate.toString()).toBe(expected.firstStart);
65
+ expect(weeks[0].endDate.toString()).toBe(expected.firstEnd);
66
+ expect(weeks[weeks.length - 1].startDate.toString()).toBe(expected.lastStart);
67
+ expect(weeks[weeks.length - 1].endDate.toString()).toBe(expected.lastEnd);
68
+ });
69
+
70
+ it("assigns correct month/year to each week based on start date", () => {
71
+ const weeks = computeWeeksInWindow(T.PlainDate.from("2026-03-29"), 1, 0, T);
72
+ expect(weeks[0].startDate.toString()).toBe("2026-03-29");
73
+ expect(weeks[0].month).toBe(3);
74
+ expect(weeks[0].year).toBe(2026);
75
+ });
76
+
77
+ it("populates weekIndex sequentially from 0", () => {
78
+ const weeks = computeWeeksInWindow(T.PlainDate.from("2026-03-01"), 5, 0, T);
79
+ weeks.forEach((w, i) => {
80
+ expect(w.weekIndex).toBe(i);
81
+ });
82
+ });
83
+ });
@@ -0,0 +1,40 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import type { TemporalNamespace, WeekStartDay } from "./types";
3
+
4
+ export interface WeekDescriptor {
5
+ startDate: Temporal.PlainDate;
6
+ endDate: Temporal.PlainDate;
7
+ weekIndex: number;
8
+ month: number;
9
+ year: number;
10
+ }
11
+
12
+ /**
13
+ * Computes an array of week descriptors for a continuous window.
14
+ * `firstWeek` is snapped back to the nearest `weekStartDay`.
15
+ */
16
+ export function computeWeeksInWindow(
17
+ firstWeek: Temporal.PlainDate,
18
+ weekCount: number,
19
+ weekStartDay: WeekStartDay,
20
+ _T: TemporalNamespace,
21
+ ): WeekDescriptor[] {
22
+ // Snap firstWeek back to the start of its week
23
+ const dow = firstWeek.dayOfWeek % 7; // 0=Sun
24
+ const offset = (dow - weekStartDay + 7) % 7;
25
+ let weekStart = firstWeek.subtract({ days: offset });
26
+
27
+ const weeks: WeekDescriptor[] = [];
28
+ for (let i = 0; i < weekCount; i++) {
29
+ const endDate = weekStart.add({ days: 6 });
30
+ weeks.push({
31
+ startDate: weekStart,
32
+ endDate,
33
+ weekIndex: i,
34
+ month: weekStart.month,
35
+ year: weekStart.year,
36
+ });
37
+ weekStart = weekStart.add({ weeks: 1 });
38
+ }
39
+ return weeks;
40
+ }
package/src/context.ts ADDED
@@ -0,0 +1,34 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import { createContext } from "react";
3
+
4
+ /** @internal Provides the days and index of the current week row to child components. */
5
+ export const WeekDataContext = createContext<{
6
+ days: Temporal.PlainDate[];
7
+ weekIndex: number;
8
+ /** The year/month this week belongs to (for outsideMonth checks in multi-month). */
9
+ gridMonth?: { year: number; month: number };
10
+ /** 1-based CSS grid row index for this week row (WeeksView only). */
11
+ gridRowIndex?: number;
12
+ } | null>(null);
13
+
14
+ /** Layout direction for the calendar grid. */
15
+ export type GridOrientation = "horizontal" | "vertical";
16
+
17
+ /** @internal Provides the resolved grid orientation to descendant cells. */
18
+ export const GridContext = createContext<{ orientation: GridOrientation }>({
19
+ orientation: "horizontal",
20
+ });
21
+
22
+ /** @internal Provides the date and column index from a `DayCellTemplate` to its child `DayButton`. */
23
+ export const DayCellDataContext = createContext<{
24
+ date: Temporal.PlainDate;
25
+ columnIndex?: number;
26
+ outsideDisabled?: boolean;
27
+ } | null>(null);
28
+
29
+ /** @internal Provides per-grid month data to WeekTemplate and DayCellTemplate. */
30
+ export const GridMonthContext = createContext<{
31
+ weeks: Temporal.PlainDate[][];
32
+ year: number;
33
+ month: number;
34
+ } | null>(null);