@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,538 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
+ import { CalendarStableContext, CalendarStateContext } from "./calendar-context";
4
+ import type {
5
+ CalendarProviderProps,
6
+ CalendarStableContextValue,
7
+ CalendarStateContextValue,
8
+ } from "./calendar-types";
9
+ import {
10
+ isDateRange,
11
+ tagRaw,
12
+ computeSelectionUpdate,
13
+ computeSetRangeUpdate,
14
+ truncateDatesForMode,
15
+ type SelectionResult,
16
+ } from "./root-selection";
17
+ import type {
18
+ DateRange,
19
+ DateValueObject,
20
+ RangeMode,
21
+ RawValueForFormat,
22
+ ValueChangeMeta,
23
+ ValueFormat,
24
+ } from "./types";
25
+ import { fromZonedDateTime, toZonedDateTime, selectedToZdt } from "./utils";
26
+ import { getSystemTimeZone, resolveTemporal } from "./utils";
27
+
28
+ /**
29
+ * Provides shared calendar state (selection, config, range preview) to all
30
+ * descendant view components. Does NOT manage focus, navigation, or grid data.
31
+ */
32
+ function CalendarProvider<F extends ValueFormat = "PlainDate">(props: CalendarProviderProps<F>) {
33
+ const {
34
+ format: formatProp,
35
+ selectionMode: selectionModeProp,
36
+ min,
37
+ max,
38
+ disabled: disabledProp,
39
+ readOnly: readOnlyProp,
40
+ isDateDisabled: isDateDisabledProp,
41
+ timeZone: timeZoneProp,
42
+ locale: localeProp,
43
+ temporal: temporalProp,
44
+ weekStartDay: weekStartDayProp,
45
+ children,
46
+ } = props;
47
+
48
+ // --- Resolve defaults ---
49
+ const T = resolveTemporal(temporalProp);
50
+ const locale = localeProp ?? "en-US";
51
+ const timeZone = timeZoneProp ?? getSystemTimeZone(T);
52
+ const weekStartDay = weekStartDayProp ?? 0;
53
+ const resolvedFormat = (formatProp ?? "PlainDate") as F;
54
+ const selectionMode = selectionModeProp ?? "single";
55
+ const disabled = disabledProp ?? false;
56
+ const readOnly = readOnlyProp ?? false;
57
+
58
+ // --- Extract mode-specific props ---
59
+ const rangeParams =
60
+ selectionMode === "range"
61
+ ? (props as Extract<CalendarProviderProps<F>, { selectionMode: "range" }>)
62
+ : undefined;
63
+
64
+ const rangeMode: RangeMode = rangeParams?.rangeMode ?? "nearest-end";
65
+ const allowRangeReversal = !(rangeParams?.preventRangeReversal ?? false);
66
+ const previewRangeProp = rangeParams?.previewRange;
67
+ const onHoveredDateChange = rangeParams?.onHoveredDateChange;
68
+
69
+ const isRange = selectionMode === "range";
70
+ const isMultiple = selectionMode === "multiple";
71
+
72
+ // --- Value/defaultValue extraction ---
73
+ const valueProp = (props as any).value;
74
+ const defaultValueProp = (props as any).defaultValue;
75
+ const onValueChange = (props as any).onValueChange;
76
+
77
+ const singleValue: RawValueForFormat<F> | null | undefined =
78
+ !isRange && !isMultiple ? (valueProp as RawValueForFormat<F> | null | undefined) : undefined;
79
+ const singleDefault: RawValueForFormat<F> | undefined =
80
+ !isRange && !isMultiple ? (defaultValueProp as RawValueForFormat<F> | undefined) : undefined;
81
+
82
+ const rangeValue: DateRange<F> | null | undefined = isRange
83
+ ? valueProp === null
84
+ ? null
85
+ : valueProp != null && isDateRange<F>(valueProp as any)
86
+ ? (valueProp as DateRange<F>)
87
+ : undefined
88
+ : undefined;
89
+ const rangeDefault: DateRange<F> | undefined =
90
+ isRange && defaultValueProp != null && isDateRange<F>(defaultValueProp as any)
91
+ ? (defaultValueProp as DateRange<F>)
92
+ : undefined;
93
+
94
+ const multipleValue: RawValueForFormat<F>[] | null | undefined = isMultiple
95
+ ? valueProp === null
96
+ ? null
97
+ : Array.isArray(valueProp)
98
+ ? (valueProp as RawValueForFormat<F>[])
99
+ : undefined
100
+ : undefined;
101
+ const multipleDefault: RawValueForFormat<F>[] | undefined =
102
+ isMultiple && Array.isArray(defaultValueProp)
103
+ ? (defaultValueProp as RawValueForFormat<F>[])
104
+ : undefined;
105
+
106
+ // --- Tagged values ---
107
+ const taggedValue = useMemo(
108
+ () => tagRaw(singleValue ?? undefined, resolvedFormat),
109
+ [singleValue, resolvedFormat],
110
+ );
111
+
112
+ // --- Min/Max resolution ---
113
+ const minValue: Temporal.PlainDate | undefined = useMemo(() => {
114
+ if (min == null) return undefined;
115
+ const tagged = { format: resolvedFormat, value: min } as DateValueObject;
116
+ return toZonedDateTime(tagged, timeZone, T).toPlainDate();
117
+ }, [min, resolvedFormat, timeZone, T]);
118
+
119
+ const maxValue: Temporal.PlainDate | undefined = useMemo(() => {
120
+ if (max == null) return undefined;
121
+ const tagged = { format: resolvedFormat, value: max } as DateValueObject;
122
+ return toZonedDateTime(tagged, timeZone, T).toPlainDate();
123
+ }, [max, resolvedFormat, timeZone, T]);
124
+
125
+ // --- isDateDisabled ---
126
+ const isDateDisabled = useCallback(
127
+ (date: Temporal.PlainDate): boolean => {
128
+ if (disabled) return true;
129
+ if (minValue && T.PlainDate.compare(date, minValue) < 0) return true;
130
+ if (maxValue && T.PlainDate.compare(date, maxValue) > 0) return true;
131
+ return isDateDisabledProp?.(date) ?? false;
132
+ },
133
+ [disabled, minValue, maxValue, isDateDisabledProp, T],
134
+ );
135
+
136
+ // --- Helpers ---
137
+ const rawToPlain = useCallback(
138
+ (raw: RawValueForFormat<F>): Temporal.PlainDate => {
139
+ const tagged = { format: resolvedFormat, value: raw } as DateValueObject;
140
+ return toZonedDateTime(tagged, timeZone, T).toPlainDate();
141
+ },
142
+ [resolvedFormat, timeZone, T],
143
+ );
144
+
145
+ const sortDates = useCallback(
146
+ (dates: Temporal.PlainDate[]) => [...dates].sort((a, b) => T.PlainDate.compare(a, b)),
147
+ [T],
148
+ );
149
+
150
+ const maxDatesForMode = isMultiple ? Number.POSITIVE_INFINITY : isRange ? 2 : 1;
151
+
152
+ // --- Controlled/Uncontrolled dates ---
153
+ const controlledDates = useMemo<(Temporal.PlainDate | null)[] | undefined>(() => {
154
+ if (isMultiple) {
155
+ if (multipleValue === null) return [];
156
+ if (multipleValue) return sortDates(multipleValue.map(rawToPlain));
157
+ return undefined;
158
+ }
159
+ if (isRange) {
160
+ if (rangeValue === null) return [];
161
+ if (rangeValue) {
162
+ const start = rangeValue.start != null ? rawToPlain(rangeValue.start) : null;
163
+ const end = rangeValue.end != null ? rawToPlain(rangeValue.end) : null;
164
+ if (start === null && end === null) return [];
165
+ if (start === null && end !== null) return [end, null];
166
+ return [start, end];
167
+ }
168
+ return undefined;
169
+ }
170
+ if (singleValue === null) return [];
171
+ if (singleValue !== undefined) return [rawToPlain(singleValue)];
172
+ return undefined;
173
+ }, [isMultiple, isRange, multipleValue, rangeValue, singleValue, rawToPlain, sortDates]);
174
+
175
+ const defaultDates = useMemo<(Temporal.PlainDate | null)[]>(() => {
176
+ if (isMultiple) {
177
+ if (multipleDefault) return sortDates(multipleDefault.map(rawToPlain));
178
+ return [];
179
+ }
180
+ if (rangeDefault) {
181
+ const start = rangeDefault.start != null ? rawToPlain(rangeDefault.start) : null;
182
+ const end = rangeDefault.end != null ? rawToPlain(rangeDefault.end) : null;
183
+ if (start === null && end === null) return [];
184
+ if (start === null && end !== null) return [end, null];
185
+ return [start, end];
186
+ }
187
+ if (singleDefault != null) return [rawToPlain(singleDefault)];
188
+ return [];
189
+ }, [isMultiple, multipleDefault, rangeDefault, singleDefault, rawToPlain, sortDates]);
190
+
191
+ const [internalDates, setInternalDates] = useState<(Temporal.PlainDate | null)[]>(defaultDates);
192
+
193
+ const isControlled = controlledDates !== undefined;
194
+ const committedDates = isControlled ? controlledDates : internalDates;
195
+
196
+ const rangeStart =
197
+ !isMultiple && committedDates.length > 0 ? (committedDates[0] ?? undefined) : undefined;
198
+ const rangeEnd =
199
+ !isMultiple && committedDates.length >= 2 ? (committedDates[1] ?? undefined) : undefined;
200
+ const committedStart = (committedDates[0] ?? undefined) as Temporal.PlainDate | undefined;
201
+ const committedEnd =
202
+ committedDates.length > 1
203
+ ? ((committedDates[committedDates.length - 1] ?? undefined) as Temporal.PlainDate | undefined)
204
+ : committedStart;
205
+
206
+ // --- Hover state for range preview ---
207
+ const [hoveredDate, setHoveredDateRaw] = useState<Temporal.PlainDate | undefined>(undefined);
208
+
209
+ const setHoveredDate = useCallback(
210
+ (date: Temporal.PlainDate | undefined) => {
211
+ onHoveredDateChange?.(date);
212
+ setHoveredDateRaw(date);
213
+ },
214
+ [onHoveredDateChange],
215
+ );
216
+
217
+ const plainToFormatValue = useCallback(
218
+ (plain: Temporal.PlainDate): RawValueForFormat<F> => {
219
+ const zdt = plain
220
+ .toPlainDateTime({ hour: 0, minute: 0, second: 0 })
221
+ .toZonedDateTime(timeZone);
222
+ return fromZonedDateTime(zdt, resolvedFormat, T).value as RawValueForFormat<F>;
223
+ },
224
+ [resolvedFormat, timeZone, T],
225
+ );
226
+
227
+ // --- Commit selection ---
228
+ const commitSelection = useCallback(
229
+ (result: SelectionResult<F>) => {
230
+ if (result.skip) return;
231
+ if (isControlled) {
232
+ setInternalDates((prev) => (prev.length === 0 ? prev : []));
233
+ } else {
234
+ setInternalDates(result.newDates);
235
+ }
236
+ result.fireCallback(onValueChange, plainToFormatValue);
237
+ },
238
+ [isControlled, onValueChange, plainToFormatValue],
239
+ );
240
+
241
+ // --- Current formatted values (for "previous" in meta) ---
242
+ const currentSingleFormatted = useCallback(() => {
243
+ if (committedDates.length === 0) return null;
244
+ const first = committedDates[0];
245
+ if (first == null) return null;
246
+ return plainToFormatValue(first);
247
+ }, [committedDates, plainToFormatValue]);
248
+
249
+ const currentRangeFormatted = useCallback((): DateRange<F> | null => {
250
+ if (!committedStart && !committedEnd) return null;
251
+ return {
252
+ start: committedStart ? plainToFormatValue(committedStart) : null,
253
+ end: committedEnd ? plainToFormatValue(committedEnd) : null,
254
+ } as DateRange<F>;
255
+ }, [committedStart, committedEnd, plainToFormatValue]);
256
+
257
+ const currentMultipleFormatted = useCallback(
258
+ (): RawValueForFormat<F>[] =>
259
+ committedDates.filter((d): d is Temporal.PlainDate => d != null).map(plainToFormatValue),
260
+ [committedDates, plainToFormatValue],
261
+ );
262
+
263
+ // --- Selected value (tagged) ---
264
+ const selected: DateValueObject | undefined = useMemo(() => {
265
+ if (taggedValue) return taggedValue;
266
+ if (committedDates.length === 0) return undefined;
267
+ const first = committedDates[0];
268
+ if (!first) return undefined;
269
+ const zdt = first.toPlainDateTime({ hour: 0, minute: 0, second: 0 }).toZonedDateTime(timeZone);
270
+ return fromZonedDateTime(zdt, resolvedFormat, T);
271
+ }, [taggedValue, committedDates, timeZone, resolvedFormat, T]);
272
+
273
+ const selectedZdt = useMemo(() => selectedToZdt(selected, timeZone, T), [selected, timeZone, T]);
274
+
275
+ // --- Compute preview range ---
276
+ const [previewStart, previewEnd] = useMemo<
277
+ [Temporal.PlainDate | undefined, Temporal.PlainDate | undefined]
278
+ >(() => {
279
+ if (previewRangeProp !== undefined) {
280
+ if (!previewRangeProp) return [undefined, undefined];
281
+ const rawToPlainLocal = (raw: RawValueForFormat<F>): Temporal.PlainDate => {
282
+ const tagged = {
283
+ format: resolvedFormat,
284
+ value: raw,
285
+ } as DateValueObject;
286
+ return toZonedDateTime(tagged, timeZone, T).toPlainDate();
287
+ };
288
+ const s =
289
+ previewRangeProp.start != null ? rawToPlainLocal(previewRangeProp.start) : undefined;
290
+ const e = previewRangeProp.end != null ? rawToPlainLocal(previewRangeProp.end) : undefined;
291
+ return [s, e];
292
+ }
293
+ if (!hoveredDate || !isRange) return [undefined, undefined];
294
+ const result = computeSelectionUpdate<F>({
295
+ date: hoveredDate,
296
+ readOnly,
297
+ isDateDisabled,
298
+ isMultiple: false,
299
+ isRange: true,
300
+ committedDates,
301
+ committedStart,
302
+ committedEnd,
303
+ selectedZdt: undefined,
304
+ rangeMode,
305
+ allowRangeReversal,
306
+ sortDates,
307
+ currentSingleFormatted,
308
+ currentRangeFormatted,
309
+ currentMultipleFormatted,
310
+ resolvedFormat,
311
+ timeZone,
312
+ T,
313
+ });
314
+ if (result.skip || result.newDates.length === 0) return [undefined, undefined];
315
+ const ps = result.newDates[0];
316
+ const pe = result.newDates.length > 1 ? result.newDates[1] : ps;
317
+ if (ps == null || pe == null) return [undefined, undefined];
318
+ return [ps, pe];
319
+ }, [
320
+ previewRangeProp,
321
+ hoveredDate,
322
+ isRange,
323
+ readOnly,
324
+ isDateDisabled,
325
+ committedDates,
326
+ committedStart,
327
+ committedEnd,
328
+ rangeMode,
329
+ allowRangeReversal,
330
+ sortDates,
331
+ currentSingleFormatted,
332
+ currentRangeFormatted,
333
+ currentMultipleFormatted,
334
+ resolvedFormat,
335
+ timeZone,
336
+ T,
337
+ ]);
338
+
339
+ // --- Auto-truncate internal dates on mode change ---
340
+ const prevModeRef = useRef(selectionMode);
341
+ useEffect(() => {
342
+ if (prevModeRef.current === selectionMode) return;
343
+ prevModeRef.current = selectionMode;
344
+ if (isControlled) return;
345
+ setInternalDates((prev) =>
346
+ truncateDatesForMode<F>({
347
+ prev,
348
+ maxDatesForMode,
349
+ selectionMode,
350
+ plainToFormatValue,
351
+ onValueChange,
352
+ }),
353
+ );
354
+ }, [selectionMode, maxDatesForMode, onValueChange, plainToFormatValue, isControlled]);
355
+
356
+ // --- Out-of-bounds cleanup for single mode ---
357
+ useEffect(() => {
358
+ if (isRange || isMultiple) return;
359
+ const start = committedStart;
360
+ if (!start) return;
361
+ const outOfBounds =
362
+ (minValue && T.PlainDate.compare(start, minValue) < 0) ||
363
+ (maxValue && T.PlainDate.compare(start, maxValue) > 0);
364
+ if (outOfBounds) {
365
+ const prev = currentSingleFormatted();
366
+ commitSelection({
367
+ newDates: [],
368
+ fireCallback: (onValueChange) => {
369
+ (
370
+ onValueChange as
371
+ | ((
372
+ v: RawValueForFormat<F> | null,
373
+ m: ValueChangeMeta<RawValueForFormat<F> | null>,
374
+ ) => void)
375
+ | undefined
376
+ )?.(null, { date: undefined, previous: prev });
377
+ },
378
+ });
379
+ }
380
+ }, [
381
+ minValue,
382
+ maxValue,
383
+ committedStart,
384
+ commitSelection,
385
+ currentSingleFormatted,
386
+ T,
387
+ isRange,
388
+ isMultiple,
389
+ ]);
390
+
391
+ // --- onSelect ---
392
+ const onSelect = useCallback(
393
+ (date: Temporal.PlainDate) => {
394
+ const result = computeSelectionUpdate<F>({
395
+ date,
396
+ readOnly,
397
+ isDateDisabled,
398
+ isMultiple,
399
+ isRange,
400
+ committedDates,
401
+ committedStart,
402
+ committedEnd,
403
+ selectedZdt,
404
+ rangeMode,
405
+ allowRangeReversal,
406
+ sortDates,
407
+ currentSingleFormatted,
408
+ currentRangeFormatted,
409
+ currentMultipleFormatted,
410
+ resolvedFormat,
411
+ timeZone,
412
+ T,
413
+ });
414
+ if (result.skip) return;
415
+ commitSelection(result);
416
+ if (isRange) setHoveredDateRaw(undefined);
417
+ },
418
+ [
419
+ readOnly,
420
+ isMultiple,
421
+ committedDates,
422
+ selectedZdt,
423
+ commitSelection,
424
+ resolvedFormat,
425
+ isDateDisabled,
426
+ timeZone,
427
+ T,
428
+ isRange,
429
+ committedStart,
430
+ committedEnd,
431
+ sortDates,
432
+ rangeMode,
433
+ allowRangeReversal,
434
+ currentSingleFormatted,
435
+ currentRangeFormatted,
436
+ currentMultipleFormatted,
437
+ ],
438
+ );
439
+
440
+ // --- setRange ---
441
+ const setRange = useCallback(
442
+ (start: Temporal.PlainDate, end: Temporal.PlainDate) => {
443
+ const result = computeSetRangeUpdate<F>({
444
+ start,
445
+ end,
446
+ readOnly,
447
+ isMultiple,
448
+ isRange,
449
+ currentRangeFormatted,
450
+ currentSingleFormatted,
451
+ resolvedFormat,
452
+ timeZone,
453
+ T,
454
+ });
455
+ if (result.skip) return;
456
+ commitSelection(result);
457
+ },
458
+ [
459
+ readOnly,
460
+ isMultiple,
461
+ isRange,
462
+ commitSelection,
463
+ currentRangeFormatted,
464
+ currentSingleFormatted,
465
+ timeZone,
466
+ resolvedFormat,
467
+ T,
468
+ ],
469
+ );
470
+
471
+ // --- Selected dates (non-null) ---
472
+ const nonNullDates = useMemo(
473
+ () => committedDates.filter((d): d is Temporal.PlainDate => d != null),
474
+ [committedDates],
475
+ );
476
+
477
+ // --- Build context values ---
478
+ const stableCtx = useMemo<CalendarStableContextValue>(
479
+ () => ({
480
+ onSelect,
481
+ setRange,
482
+ selectionMode,
483
+ disabled,
484
+ readOnly,
485
+ isDateDisabled,
486
+ minValue,
487
+ maxValue,
488
+ timeZone,
489
+ locale,
490
+ temporal: T,
491
+ weekStartDay,
492
+ rangeMode,
493
+ preventRangeReversal: !allowRangeReversal,
494
+ valueFormat: resolvedFormat,
495
+ setHoveredDate,
496
+ }),
497
+ [
498
+ onSelect,
499
+ setRange,
500
+ selectionMode,
501
+ disabled,
502
+ readOnly,
503
+ isDateDisabled,
504
+ minValue,
505
+ maxValue,
506
+ timeZone,
507
+ locale,
508
+ T,
509
+ weekStartDay,
510
+ rangeMode,
511
+ allowRangeReversal,
512
+ resolvedFormat,
513
+ setHoveredDate,
514
+ ],
515
+ );
516
+
517
+ const stateCtx = useMemo<CalendarStateContextValue>(
518
+ () => ({
519
+ selected,
520
+ selectedDates: nonNullDates,
521
+ rangeStart,
522
+ rangeEnd,
523
+ hoveredDate,
524
+ previewStart,
525
+ previewEnd,
526
+ }),
527
+ [selected, nonNullDates, rangeStart, rangeEnd, hoveredDate, previewStart, previewEnd],
528
+ );
529
+
530
+ return (
531
+ <CalendarStableContext.Provider value={stableCtx}>
532
+ <CalendarStateContext.Provider value={stateCtx}>{children}</CalendarStateContext.Provider>
533
+ </CalendarStableContext.Provider>
534
+ );
535
+ }
536
+
537
+ export { CalendarProvider };
538
+ export type { CalendarProviderProps };