@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,621 @@
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
+ OutsideDays,
12
+ } from "./types";
13
+ import { fromZonedDateTime } from "./utils";
14
+
15
+ export interface UseRootStateParamsBase<F extends ValueFormat> {
16
+ format: F;
17
+ min?: RawValueForFormat<F>;
18
+ max?: RawValueForFormat<F>;
19
+ disabled: boolean;
20
+ readOnly: boolean;
21
+ isDateDisabled?: (date: Temporal.PlainDate) => boolean;
22
+ timeZone: string;
23
+ locale: string;
24
+ temporal: TemporalNamespace;
25
+ weekStartDay: WeekStartDay;
26
+ fixedWeeks: boolean;
27
+ numberOfMonths: number;
28
+ outsideDays: OutsideDays;
29
+ onMonthChange?: (month: Temporal.PlainYearMonth) => void;
30
+ }
31
+
32
+ export type UseRootStateParams<F extends ValueFormat> = UseRootStateParamsBase<F> &
33
+ (
34
+ | {
35
+ selectionMode: "single";
36
+ value?: RawValueForFormat<F> | null;
37
+ defaultValue?: RawValueForFormat<F>;
38
+ onValueChange?: (
39
+ value: RawValueForFormat<F> | null,
40
+ meta: ValueChangeMeta<RawValueForFormat<F> | null>,
41
+ ) => void;
42
+ }
43
+ | {
44
+ selectionMode: "range";
45
+ value?: DateRange<F> | null;
46
+ defaultValue?: DateRange<F>;
47
+ onValueChange?: (
48
+ value: DateRange<F> | null,
49
+ meta: ValueChangeMeta<DateRange<F> | null>,
50
+ ) => void;
51
+ rangeMode?: RangeMode;
52
+ preventRangeReversal?: boolean;
53
+ previewRange?: DateRange<F> | null;
54
+ onHoveredDateChange?: (date: Temporal.PlainDate | undefined) => void;
55
+ }
56
+ | {
57
+ selectionMode: "multiple";
58
+ value?: RawValueForFormat<F>[] | null;
59
+ defaultValue?: RawValueForFormat<F>[];
60
+ onValueChange?: (
61
+ value: RawValueForFormat<F>[],
62
+ meta: ValueChangeMeta<RawValueForFormat<F>[]>,
63
+ ) => void;
64
+ }
65
+ );
66
+
67
+ /** Construct a DateRange (workaround for TS generic conditional type widening). */
68
+ function mkRange<F extends ValueFormat>(start: unknown, end: unknown): DateRange<F> {
69
+ return { start, end } as DateRange<F>;
70
+ }
71
+
72
+ export function isDateRange<F extends ValueFormat>(
73
+ v: RawValueForFormat<F> | DateRange<F>,
74
+ ): v is DateRange<F> {
75
+ return v != null && typeof v === "object" && "start" in v && "end" in v;
76
+ }
77
+
78
+ export function tagRaw<F extends ValueFormat>(
79
+ raw: RawValueForFormat<F> | undefined,
80
+ format: ValueFormat,
81
+ ): DateValueObject | undefined {
82
+ if (raw == null) return undefined;
83
+ return { format, value: raw } as DateValueObject;
84
+ }
85
+
86
+ /** Resolve a `RangeMode` to a concrete "start" | "end" | "reset" action for inside-range clicks. */
87
+ export function resolveInsideAction(
88
+ mode: RangeMode,
89
+ date: Temporal.PlainDate,
90
+ start: Temporal.PlainDate,
91
+ end: Temporal.PlainDate,
92
+ ): "start" | "end" | "reset" {
93
+ if (mode === "adjust-start") return "start";
94
+ if (mode === "adjust-end") return "end";
95
+ if (mode === "reset") return "reset";
96
+
97
+ // nearest-start or nearest-end: compare distance to each boundary
98
+ const daysFromStart = Math.abs(date.since(start).days);
99
+ const daysFromEnd = Math.abs(date.since(end).days);
100
+
101
+ if (daysFromStart < daysFromEnd) return "start";
102
+ if (daysFromEnd < daysFromStart) return "end";
103
+ // Tie: use the suffix
104
+ return mode === "nearest-start" ? "start" : "end";
105
+ }
106
+
107
+ // --- Selection update types ---
108
+
109
+ export type SelectionResult<F extends ValueFormat> =
110
+ | { skip: true }
111
+ | {
112
+ skip?: false;
113
+ newDates: (Temporal.PlainDate | null)[];
114
+ fireCallback: (
115
+ onValueChange: unknown,
116
+ plainToFormatValue: (d: Temporal.PlainDate) => RawValueForFormat<F>,
117
+ ) => void;
118
+ };
119
+
120
+ /** Compute what `onSelect(date)` should do, without touching React state. */
121
+ export function computeSelectionUpdate<F extends ValueFormat>(opts: {
122
+ date: Temporal.PlainDate;
123
+ readOnly: boolean;
124
+ isDateDisabled: (date: Temporal.PlainDate) => boolean;
125
+ isMultiple: boolean;
126
+ isRange: boolean;
127
+ committedDates: (Temporal.PlainDate | null)[];
128
+ committedStart: Temporal.PlainDate | undefined;
129
+ committedEnd: Temporal.PlainDate | undefined;
130
+ selectedZdt: Temporal.ZonedDateTime | undefined;
131
+ rangeMode: RangeMode;
132
+ allowRangeReversal: boolean;
133
+ sortDates: (dates: Temporal.PlainDate[]) => Temporal.PlainDate[];
134
+ currentSingleFormatted: () => RawValueForFormat<F> | null;
135
+ currentRangeFormatted: () => DateRange<F> | null;
136
+ currentMultipleFormatted: () => RawValueForFormat<F>[];
137
+ resolvedFormat: ValueFormat;
138
+ timeZone: string;
139
+ T: TemporalNamespace;
140
+ }): SelectionResult<F> {
141
+ const {
142
+ date,
143
+ readOnly,
144
+ isDateDisabled,
145
+ isMultiple,
146
+ isRange,
147
+ committedDates,
148
+ committedStart,
149
+ committedEnd,
150
+ selectedZdt,
151
+ rangeMode,
152
+ allowRangeReversal,
153
+ sortDates,
154
+ currentSingleFormatted,
155
+ currentRangeFormatted,
156
+ currentMultipleFormatted,
157
+ resolvedFormat,
158
+ timeZone,
159
+ T,
160
+ } = opts;
161
+
162
+ if (readOnly) return { skip: true };
163
+ if (isDateDisabled(date)) return { skip: true };
164
+
165
+ if (isMultiple) {
166
+ const dates = committedDates.filter((d): d is Temporal.PlainDate => d != null);
167
+ const prevArr = currentMultipleFormatted();
168
+ const idx = dates.findIndex((d) => T.PlainDate.compare(d, date) === 0);
169
+ let newDates: (Temporal.PlainDate | null)[];
170
+ if (idx >= 0) {
171
+ newDates = [...dates.slice(0, idx), ...dates.slice(idx + 1)];
172
+ } else {
173
+ newDates = sortDates([...dates, date]);
174
+ }
175
+ return {
176
+ newDates,
177
+ fireCallback: (onValueChange, plainToFormatValue) => {
178
+ (
179
+ onValueChange as
180
+ | ((v: RawValueForFormat<F>[], m: ValueChangeMeta<RawValueForFormat<F>[]>) => void)
181
+ | undefined
182
+ )?.((newDates as Temporal.PlainDate[]).map(plainToFormatValue), {
183
+ date,
184
+ previous: prevArr,
185
+ });
186
+ },
187
+ };
188
+ }
189
+
190
+ // --- start-end range mode ---
191
+ if (isRange && rangeMode === "start-end") {
192
+ const prevRange = currentRangeFormatted();
193
+ const isPending =
194
+ committedDates.length === 2 && (committedDates[0] === null || committedDates[1] === null);
195
+ const isComplete =
196
+ committedDates.length === 2 && committedDates[0] != null && committedDates[1] != null;
197
+
198
+ type RangeCb =
199
+ | ((v: DateRange<F> | null, m: ValueChangeMeta<DateRange<F> | null>) => void)
200
+ | undefined;
201
+
202
+ // No selection → first click: fire partial range {start, end: null}
203
+ if (committedDates.length === 0) {
204
+ return {
205
+ newDates: [date, null],
206
+ fireCallback: (onValueChange, plainToFormatValue) => {
207
+ (onValueChange as RangeCb)?.(mkRange<F>(plainToFormatValue(date), null), {
208
+ date,
209
+ previous: prevRange,
210
+ });
211
+ },
212
+ };
213
+ }
214
+
215
+ // Pending start [start, null]
216
+ if (isPending && committedDates[0] != null && committedDates[1] === null) {
217
+ const start = committedDates[0];
218
+
219
+ // Clicking same date as start → finalize single-day range
220
+ if (T.PlainDate.compare(date, start) === 0) {
221
+ return {
222
+ newDates: [date, date],
223
+ fireCallback: (onValueChange, plainToFormatValue) => {
224
+ (onValueChange as RangeCb)?.(
225
+ mkRange<F>(plainToFormatValue(date), plainToFormatValue(date)),
226
+ { date, previous: prevRange },
227
+ );
228
+ },
229
+ };
230
+ }
231
+
232
+ // date >= start: complete range
233
+ if (T.PlainDate.compare(date, start) >= 0) {
234
+ return {
235
+ newDates: [start, date],
236
+ fireCallback: (onValueChange, plainToFormatValue) => {
237
+ (onValueChange as RangeCb)?.(
238
+ mkRange<F>(plainToFormatValue(start), plainToFormatValue(date)),
239
+ { date, previous: prevRange },
240
+ );
241
+ },
242
+ };
243
+ }
244
+
245
+ // date < start
246
+ if (allowRangeReversal) {
247
+ return {
248
+ newDates: [date, start],
249
+ fireCallback: (onValueChange, plainToFormatValue) => {
250
+ (onValueChange as RangeCb)?.(
251
+ mkRange<F>(plainToFormatValue(date), plainToFormatValue(start)),
252
+ { date, previous: prevRange },
253
+ );
254
+ },
255
+ };
256
+ }
257
+ // Collapse to single-day
258
+ return {
259
+ newDates: [start, start],
260
+ fireCallback: (onValueChange, plainToFormatValue) => {
261
+ (onValueChange as RangeCb)?.(
262
+ mkRange<F>(plainToFormatValue(start), plainToFormatValue(start)),
263
+ { date, previous: prevRange },
264
+ );
265
+ },
266
+ };
267
+ }
268
+
269
+ // Complete range [date1, date2]
270
+ if (isComplete) {
271
+ const start = committedDates[0]!;
272
+ const end = committedDates[1]!;
273
+ const isOnStart = T.PlainDate.compare(date, start) === 0;
274
+ const isOnEnd = T.PlainDate.compare(date, end) === 0;
275
+
276
+ // Click boundary → deselect
277
+ if (isOnStart || isOnEnd) {
278
+ return {
279
+ newDates: [],
280
+ fireCallback: (onValueChange) => {
281
+ (onValueChange as RangeCb)?.(null, {
282
+ date,
283
+ previous: prevRange,
284
+ });
285
+ },
286
+ };
287
+ }
288
+
289
+ // Non-boundary → new pending start
290
+ return {
291
+ newDates: [date, null],
292
+ fireCallback: (onValueChange, plainToFormatValue) => {
293
+ (onValueChange as RangeCb)?.(mkRange<F>(plainToFormatValue(date), null), {
294
+ date,
295
+ previous: prevRange,
296
+ });
297
+ },
298
+ };
299
+ }
300
+
301
+ return { skip: true };
302
+ }
303
+
304
+ // --- Non-start-end range modes with complete range ---
305
+ if (isRange && committedStart && committedEnd) {
306
+ const prevRange = currentRangeFormatted();
307
+ const isSingleDay = T.PlainDate.compare(committedStart, committedEnd) === 0;
308
+
309
+ // Single-day range: clicking same date → deselect
310
+ if (isSingleDay && T.PlainDate.compare(date, committedStart) === 0) {
311
+ return {
312
+ newDates: [],
313
+ fireCallback: (onValueChange) => {
314
+ (
315
+ onValueChange as
316
+ | ((v: DateRange<F> | null, m: ValueChangeMeta<DateRange<F> | null>) => void)
317
+ | undefined
318
+ )?.(null, { date, previous: prevRange });
319
+ },
320
+ };
321
+ }
322
+
323
+ // Multi-day range: clicking on start or end boundary → collapse to single-day
324
+ if (!isSingleDay) {
325
+ const isOnStart = T.PlainDate.compare(date, committedStart) === 0;
326
+ const isOnEnd = T.PlainDate.compare(date, committedEnd) === 0;
327
+ if (isOnStart || isOnEnd) {
328
+ const newDates = [date, date];
329
+ return {
330
+ newDates,
331
+ fireCallback: (onValueChange, plainToFormatValue) => {
332
+ (
333
+ onValueChange as
334
+ | ((v: DateRange<F> | null, m: ValueChangeMeta<DateRange<F> | null>) => void)
335
+ | undefined
336
+ )?.(mkRange<F>(plainToFormatValue(date), plainToFormatValue(date)), {
337
+ date,
338
+ previous: prevRange,
339
+ });
340
+ },
341
+ };
342
+ }
343
+ }
344
+
345
+ const beforeStart = T.PlainDate.compare(date, committedStart) < 0;
346
+ const afterEnd = T.PlainDate.compare(date, committedEnd) > 0;
347
+
348
+ let newDates: (Temporal.PlainDate | null)[];
349
+ if (beforeStart) {
350
+ newDates = [date, committedEnd];
351
+ } else if (afterEnd) {
352
+ newDates = [committedStart, date];
353
+ } else {
354
+ const action = resolveInsideAction(rangeMode, date, committedStart, committedEnd);
355
+ if (action === "start") {
356
+ newDates = [date, committedEnd];
357
+ } else if (action === "end") {
358
+ newDates = [committedStart, date];
359
+ } else {
360
+ newDates = [date, date];
361
+ }
362
+ }
363
+
364
+ return {
365
+ newDates,
366
+ fireCallback: (onValueChange, plainToFormatValue) => {
367
+ (
368
+ onValueChange as
369
+ | ((v: DateRange<F> | null, m: ValueChangeMeta<DateRange<F> | null>) => void)
370
+ | undefined
371
+ )?.(mkRange<F>(plainToFormatValue(newDates[0]!), plainToFormatValue(newDates[1]!)), {
372
+ date,
373
+ previous: prevRange,
374
+ });
375
+ },
376
+ };
377
+ }
378
+
379
+ if (isRange) {
380
+ const prevRange = currentRangeFormatted();
381
+ const newDates = [date, date];
382
+ return {
383
+ newDates,
384
+ fireCallback: (onValueChange, plainToFormatValue) => {
385
+ (
386
+ onValueChange as
387
+ | ((v: DateRange<F> | null, m: ValueChangeMeta<DateRange<F> | null>) => void)
388
+ | undefined
389
+ )?.(mkRange<F>(plainToFormatValue(newDates[0]), plainToFormatValue(newDates[1])), {
390
+ date,
391
+ previous: prevRange,
392
+ });
393
+ },
394
+ };
395
+ }
396
+
397
+ // Single mode
398
+ const prevSingle = currentSingleFormatted();
399
+ const alreadySelected =
400
+ committedDates.length === 1 &&
401
+ committedDates[0] != null &&
402
+ T.PlainDate.compare(committedDates[0], date) === 0;
403
+
404
+ if (alreadySelected) {
405
+ return {
406
+ newDates: [],
407
+ fireCallback: (onValueChange) => {
408
+ (
409
+ onValueChange as
410
+ | ((
411
+ v: RawValueForFormat<F> | null,
412
+ m: ValueChangeMeta<RawValueForFormat<F> | null>,
413
+ ) => void)
414
+ | undefined
415
+ )?.(null, { date, previous: prevSingle });
416
+ },
417
+ };
418
+ }
419
+
420
+ const newDates = [date];
421
+ return {
422
+ newDates,
423
+ fireCallback: (onValueChange) => {
424
+ const prevTime = selectedZdt
425
+ ? {
426
+ hour: selectedZdt.hour,
427
+ minute: selectedZdt.minute,
428
+ second: selectedZdt.second,
429
+ }
430
+ : { hour: 0, minute: 0, second: 0 };
431
+ const newZdt = date.toPlainDateTime(prevTime).toZonedDateTime(timeZone);
432
+ const newTagged = fromZonedDateTime(newZdt, resolvedFormat, T);
433
+ (
434
+ onValueChange as
435
+ | ((v: DateValueObject["value"], m: ValueChangeMeta<RawValueForFormat<F> | null>) => void)
436
+ | undefined
437
+ )?.(newTagged.value, { date, previous: prevSingle });
438
+ },
439
+ };
440
+ }
441
+
442
+ /** Compute what `setRange(start, end)` should do, without touching React state. */
443
+ export function computeSetRangeUpdate<F extends ValueFormat>(opts: {
444
+ start: Temporal.PlainDate;
445
+ end: Temporal.PlainDate;
446
+ readOnly: boolean;
447
+ isMultiple: boolean;
448
+ isRange: boolean;
449
+ currentRangeFormatted: () => DateRange<F> | null;
450
+ currentSingleFormatted: () => RawValueForFormat<F> | null;
451
+ resolvedFormat: ValueFormat;
452
+ timeZone: string;
453
+ T: TemporalNamespace;
454
+ }): SelectionResult<F> {
455
+ const {
456
+ start,
457
+ end,
458
+ readOnly,
459
+ isMultiple,
460
+ isRange,
461
+ currentRangeFormatted,
462
+ currentSingleFormatted,
463
+ resolvedFormat,
464
+ timeZone,
465
+ T,
466
+ } = opts;
467
+
468
+ if (readOnly || isMultiple) return { skip: true };
469
+
470
+ const [lo, hi] = T.PlainDate.compare(start, end) <= 0 ? [start, end] : [end, start];
471
+ const effectiveEnd = isRange ? hi : lo;
472
+ const newDates = isRange ? [lo, effectiveEnd] : [lo];
473
+
474
+ return {
475
+ newDates,
476
+ fireCallback: (onValueChange, plainToFormatValue) => {
477
+ if (isRange) {
478
+ const prevRange = currentRangeFormatted();
479
+ (
480
+ onValueChange as
481
+ | ((v: DateRange<F> | null, m: ValueChangeMeta<DateRange<F> | null>) => void)
482
+ | undefined
483
+ )?.(mkRange<F>(plainToFormatValue(lo), plainToFormatValue(effectiveEnd)), {
484
+ date: undefined,
485
+ previous: prevRange,
486
+ });
487
+ } else {
488
+ const prevSingle = currentSingleFormatted();
489
+ const zdt = lo.toPlainDateTime({ hour: 0, minute: 0, second: 0 }).toZonedDateTime(timeZone);
490
+ const tagged = fromZonedDateTime(zdt, resolvedFormat, T);
491
+ (
492
+ onValueChange as
493
+ | ((
494
+ v: DateValueObject["value"],
495
+ m: ValueChangeMeta<RawValueForFormat<F> | null>,
496
+ ) => void)
497
+ | undefined
498
+ )?.(tagged.value, { date: undefined, previous: prevSingle });
499
+ }
500
+ },
501
+ };
502
+ }
503
+
504
+ /** Truncate dates when selection mode changes. Returns new dates + fires callback. */
505
+ export function truncateDatesForMode<F extends ValueFormat>(opts: {
506
+ prev: (Temporal.PlainDate | null)[];
507
+ maxDatesForMode: number;
508
+ selectionMode: "single" | "range" | "multiple";
509
+ plainToFormatValue: (d: Temporal.PlainDate) => RawValueForFormat<F>;
510
+ onValueChange: unknown;
511
+ }): (Temporal.PlainDate | null)[] {
512
+ const { prev, maxDatesForMode, selectionMode, plainToFormatValue, onValueChange } = opts;
513
+
514
+ const clamped = prev.slice(0, maxDatesForMode);
515
+ if (clamped.length === prev.length) return prev;
516
+
517
+ const noDate = { date: undefined };
518
+ const fmt = (d: Temporal.PlainDate | null) => (d != null ? plainToFormatValue(d) : null);
519
+
520
+ if (selectionMode === "single") {
521
+ const prevVal = prev.length > 0 && prev[0] != null ? plainToFormatValue(prev[0]) : null;
522
+ const newVal = clamped.length > 0 && clamped[0] != null ? plainToFormatValue(clamped[0]) : null;
523
+ (
524
+ onValueChange as
525
+ | ((
526
+ v: RawValueForFormat<F> | null,
527
+ m: ValueChangeMeta<RawValueForFormat<F> | null>,
528
+ ) => void)
529
+ | undefined
530
+ )?.(newVal, { ...noDate, previous: prevVal });
531
+ } else if (selectionMode === "range") {
532
+ const prevRange: DateRange<F> | null =
533
+ prev.length >= 2 ? mkRange<F>(fmt(prev[0]), fmt(prev[1])) : null;
534
+ let newRange: DateRange<F> | null = null;
535
+ if (clamped.length >= 2) {
536
+ newRange = mkRange<F>(fmt(clamped[0]), fmt(clamped[1]));
537
+ } else if (clamped.length === 1) {
538
+ const v = fmt(clamped[0]);
539
+ newRange = mkRange<F>(v, v);
540
+ }
541
+ (
542
+ onValueChange as
543
+ | ((v: DateRange<F> | null, m: ValueChangeMeta<DateRange<F> | null>) => void)
544
+ | undefined
545
+ )?.(newRange, { ...noDate, previous: prevRange });
546
+ } else {
547
+ const nonNull = prev.filter((d): d is Temporal.PlainDate => d != null);
548
+ const prevArr = nonNull.map(plainToFormatValue);
549
+ const clampedNonNull = clamped.filter((d): d is Temporal.PlainDate => d != null);
550
+ (
551
+ onValueChange as
552
+ | ((v: RawValueForFormat<F>[], m: ValueChangeMeta<RawValueForFormat<F>[]>) => void)
553
+ | undefined
554
+ )?.(clampedNonNull.map(plainToFormatValue), {
555
+ ...noDate,
556
+ previous: prevArr,
557
+ });
558
+ }
559
+ return clamped;
560
+ }
561
+
562
+ /**
563
+ * Pure utility that computes the preview range for a hovered date, using the
564
+ * same logic as the internal hook. Useful for deriving the preview in an
565
+ * `onHoveredDateChange` handler without needing a controlled `previewRange`.
566
+ */
567
+ export function computePreviewRange(
568
+ hoveredDate: Temporal.PlainDate,
569
+ currentRange: DateRange | null,
570
+ rangeMode: RangeMode,
571
+ preventRangeReversal = false,
572
+ T?: TemporalNamespace,
573
+ ): DateRange<"PlainDate"> | null {
574
+ const Temporal: TemporalNamespace = T ?? (globalThis as any).Temporal;
575
+ if (!Temporal) {
576
+ throw new Error(
577
+ "computePreviewRange requires a Temporal namespace. Pass it as the 5th argument or ensure globalThis.Temporal is available.",
578
+ );
579
+ }
580
+
581
+ const rawStart =
582
+ currentRange?.start != null ? Temporal.PlainDate.from(currentRange.start as any) : null;
583
+ const rawEnd =
584
+ currentRange?.end != null ? Temporal.PlainDate.from(currentRange.end as any) : null;
585
+ // Normalize {start: null, end} → [end, null] (pending start)
586
+ const committedDates: (Temporal.PlainDate | null)[] = currentRange
587
+ ? rawStart === null && rawEnd !== null
588
+ ? [rawEnd, null]
589
+ : [rawStart, rawEnd]
590
+ : [];
591
+ const committedStart = (committedDates[0] ?? undefined) as Temporal.PlainDate | undefined;
592
+ const committedEnd = (committedDates[1] ?? undefined) as Temporal.PlainDate | undefined;
593
+
594
+ const noop = () => [] as any;
595
+ const result = computeSelectionUpdate<"PlainDate">({
596
+ date: hoveredDate,
597
+ readOnly: false,
598
+ isDateDisabled: () => false,
599
+ isMultiple: false,
600
+ isRange: true,
601
+ committedDates,
602
+ committedStart,
603
+ committedEnd,
604
+ selectedZdt: undefined,
605
+ rangeMode,
606
+ allowRangeReversal: !preventRangeReversal,
607
+ sortDates: (d) => [...d].sort((a, b) => Temporal.PlainDate.compare(a, b)),
608
+ currentSingleFormatted: noop,
609
+ currentRangeFormatted: () => currentRange as DateRange<"PlainDate"> | null,
610
+ currentMultipleFormatted: noop,
611
+ resolvedFormat: "PlainDate",
612
+ timeZone: "UTC",
613
+ T: Temporal,
614
+ });
615
+
616
+ if (result.skip || result.newDates.length === 0) return null;
617
+ const start = result.newDates[0];
618
+ const end = result.newDates.length > 1 ? result.newDates[1] : start;
619
+ if (start == null || end == null) return null;
620
+ return mkRange<"PlainDate">(start, end);
621
+ }