@klinking/colander 3.0.1-alpha.1 → 3.1.0-alpha.10

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 (98) hide show
  1. package/dist/formats/date.cjs +2 -3
  2. package/dist/formats/date.d.cts +10 -14
  3. package/dist/formats/date.d.mts +10 -14
  4. package/dist/formats/date.mjs +2 -3
  5. package/dist/formats/object.cjs +2 -3
  6. package/dist/formats/object.d.cts +10 -14
  7. package/dist/formats/object.d.mts +10 -14
  8. package/dist/formats/object.mjs +2 -3
  9. package/dist/formats/plain-date-time.cjs +2 -3
  10. package/dist/formats/plain-date-time.d.cts +10 -14
  11. package/dist/formats/plain-date-time.d.mts +10 -14
  12. package/dist/formats/plain-date-time.mjs +2 -3
  13. package/dist/formats/plain-date.cjs +2 -3
  14. package/dist/formats/plain-date.d.cts +10 -14
  15. package/dist/formats/plain-date.d.mts +10 -14
  16. package/dist/formats/plain-date.mjs +2 -3
  17. package/dist/formats/plain-month-day.cjs +2 -3
  18. package/dist/formats/plain-month-day.d.cts +10 -14
  19. package/dist/formats/plain-month-day.d.mts +10 -14
  20. package/dist/formats/plain-month-day.mjs +2 -3
  21. package/dist/formats/plain-year-month.cjs +2 -3
  22. package/dist/formats/plain-year-month.d.cts +10 -14
  23. package/dist/formats/plain-year-month.d.mts +10 -14
  24. package/dist/formats/plain-year-month.mjs +2 -3
  25. package/dist/formats/zoned-date-time.cjs +2 -3
  26. package/dist/formats/zoned-date-time.d.cts +10 -14
  27. package/dist/formats/zoned-date-time.d.mts +10 -14
  28. package/dist/formats/zoned-date-time.mjs +2 -3
  29. package/dist/{weeks-view-context-CNAq2Hiu.d.cts → index-K1_zOIpa.d.mts} +198 -114
  30. package/dist/{weeks-view-context-CsP3PBiu.d.mts → index-bRoYX03d.d.cts} +198 -114
  31. package/dist/index.cjs +1 -1
  32. package/dist/index.d.cts +2 -11
  33. package/dist/index.d.mts +2 -11
  34. package/dist/index.mjs +1 -2
  35. package/dist/{weeks-navigation-CJzhxbgj.cjs → weeks-navigation-3UTRiTUP.cjs} +1102 -952
  36. package/dist/{weeks-navigation-Bg0nZLgh.mjs → weeks-navigation-BoHqK5fq.mjs} +1102 -951
  37. package/package.json +21 -18
  38. package/src/calendar-provider.test.tsx +88 -1
  39. package/src/calendar-provider.tsx +89 -73
  40. package/src/calendar-types.ts +2 -0
  41. package/src/compute-weeks-in-window.test.ts +1 -1
  42. package/src/day-cell-state.test.ts +214 -0
  43. package/src/day-cell-state.ts +244 -0
  44. package/src/day-cell-store.test.tsx +273 -0
  45. package/src/day-cell-store.tsx +204 -0
  46. package/src/day-cell.test.tsx +73 -90
  47. package/src/day-cell.tsx +106 -444
  48. package/src/drag-handle.tsx +2 -2
  49. package/src/formats/date.ts +2 -0
  50. package/src/formats/formats.test.ts +1 -1
  51. package/src/formats/object.ts +2 -0
  52. package/src/formats/plain-date-time.ts +2 -0
  53. package/src/formats/plain-date.ts +2 -0
  54. package/src/formats/plain-month-day.ts +2 -0
  55. package/src/formats/plain-year-month.ts +2 -0
  56. package/src/formats/zoned-date-time.ts +2 -0
  57. package/src/grid-header.tsx +3 -3
  58. package/src/grid.event-perf.test.tsx +7 -1
  59. package/src/grid.perf.test.tsx +7 -1
  60. package/src/grid.tsx +38 -77
  61. package/src/index.ts +2 -0
  62. package/src/keyboard.test.ts +1 -1
  63. package/src/month-separator.test.tsx +1 -1
  64. package/src/month-view-navigation.ts +180 -0
  65. package/src/month-view-types.ts +3 -0
  66. package/src/month-view.bounds.test.tsx +136 -0
  67. package/src/month-view.test.tsx +89 -1
  68. package/src/month-view.tsx +74 -217
  69. package/src/month-window.test.ts +207 -0
  70. package/src/month-window.ts +93 -0
  71. package/src/navigation.tsx +18 -76
  72. package/src/out-of-range.test.ts +34 -54
  73. package/src/out-of-range.ts +47 -123
  74. package/src/range-preview.test.ts +120 -0
  75. package/src/range-preview.tsx +3 -5
  76. package/src/resolve-first-week.test.ts +1 -1
  77. package/src/root-selection.ts +99 -97
  78. package/src/selected-range.tsx +3 -5
  79. package/src/store.ts +72 -0
  80. package/src/today.test.tsx +105 -0
  81. package/src/types.ts +24 -1
  82. package/src/utils.bench.ts +158 -106
  83. package/src/utils.test.ts +26 -1
  84. package/src/utils.ts +40 -9
  85. package/src/view-context.test.tsx +126 -0
  86. package/src/view-context.ts +45 -0
  87. package/src/week-number.test.tsx +76 -0
  88. package/src/week-number.tsx +48 -19
  89. package/src/weeks-grid.tsx +20 -10
  90. package/src/weeks-keyboard.test.ts +1 -1
  91. package/src/weeks-navigation.tsx +15 -31
  92. package/src/weeks-view-state.ts +67 -101
  93. package/src/weeks-view-types.ts +4 -1
  94. package/src/weeks-view.bounds.test.tsx +246 -0
  95. package/src/weeks-view.test.tsx +116 -14
  96. package/src/weeks-view.tsx +24 -52
  97. package/src/weeks-window.test.ts +131 -0
  98. package/src/weeks-window.ts +102 -0
@@ -12,7 +12,8 @@ import type { TemporalNamespace, WeekStartDay } from "./types";
12
12
  * - `"unbounded"` — Navigation is never restricted by `min`/`max`; any range of
13
13
  * weeks can be viewed. Out-of-range days simply render disabled. (Default.)
14
14
  * - `"stop"` — Navigation halts at the boundary: the Prev/Next button is
15
- * disabled once the next step would show a window containing no in-range day.
15
+ * disabled, and `goNext`/`goPrev`/`scrollToWeek` and keyboard paging do
16
+ * nothing, once the next step would show a window containing no in-range day.
16
17
  * - `"stop-shrink"` — Like `"stop"`, but near the edge the window **shrinks** to
17
18
  * fewer week rows instead of showing rows that are entirely out of range.
18
19
  * - `"snap"` — A jump that would overshoot **snaps** the window so its edge
@@ -20,6 +21,8 @@ import type { TemporalNamespace, WeekStartDay } from "./types";
20
21
  * - `"snap-shrink"` — Snap to the boundary, then **shrink** the window to just
21
22
  * the in-range weeks.
22
23
  *
24
+ * A single bound (`min` only or `max` only) is honored; the missing side is unlimited.
25
+ *
23
26
  * `"snap"` and `"snap-shrink"` are identical **unless the selectable range spans
24
27
  * fewer weeks than `weekCount`** — i.e. the window is taller than the range.
25
28
  * Snapping can only pin *one* edge to a bound; if the range is narrower than the
@@ -63,19 +66,33 @@ export type MonthOutOfRangeBehavior = "unbounded" | "stop";
63
66
  /** Returns true if any day in the 7-day span starting at weekStart falls within [min, max]. */
64
67
  function weekHasValidDay(
65
68
  weekStart: Temporal.PlainDate,
66
- min: Temporal.PlainDate,
67
- max: Temporal.PlainDate,
69
+ min: Temporal.PlainDate | undefined,
70
+ max: Temporal.PlainDate | undefined,
68
71
  T: TemporalNamespace,
69
72
  ): boolean {
70
73
  // Week spans [weekStart, weekStart+6]. Overlaps with [min, max] iff
71
- // weekStart <= max AND weekStart+6 >= min.
74
+ // weekStart <= max AND weekStart+6 >= min. A missing bound is unlimited.
72
75
  const weekEnd = weekStart.add({ days: 6 });
73
76
  return (
74
- T.PlainDate.compare(weekStart, max) <= 0 &&
75
- T.PlainDate.compare(weekEnd, min) >= 0
77
+ (!max || T.PlainDate.compare(weekStart, max) <= 0) &&
78
+ (!min || T.PlainDate.compare(weekEnd, min) >= 0)
76
79
  );
77
80
  }
78
81
 
82
+ /** Returns true if any of the `weekCount` weeks starting at firstWeek has a day in [min, max]. */
83
+ export function windowHasValidWeek(
84
+ firstWeek: Temporal.PlainDate,
85
+ weekCount: number,
86
+ min: Temporal.PlainDate | undefined,
87
+ max: Temporal.PlainDate | undefined,
88
+ T: TemporalNamespace,
89
+ ): boolean {
90
+ for (let i = 0; i < weekCount; i++) {
91
+ if (weekHasValidDay(firstWeek.add({ weeks: i }), min, max, T)) return true;
92
+ }
93
+ return false;
94
+ }
95
+
79
96
  /**
80
97
  * Returns the start of the week (per weekStartDay) that contains the given date.
81
98
  * Uses dayOfWeek % 7 → Sun=0…Sat=6 convention.
@@ -83,31 +100,12 @@ function weekHasValidDay(
83
100
  function weekStartContaining(
84
101
  date: Temporal.PlainDate,
85
102
  weekStartDay: WeekStartDay,
86
- _T: TemporalNamespace,
87
103
  ): Temporal.PlainDate {
88
104
  const dow = date.dayOfWeek % 7; // Sun=0…Sat=6
89
105
  const daysFromStart = (dow - weekStartDay + 7) % 7;
90
106
  return date.subtract({ days: daysFromStart });
91
107
  }
92
108
 
93
- /** Find the week start (per weekStartDay) whose 7-day span contains min. */
94
- function findFirstValidWeekStart(
95
- min: Temporal.PlainDate,
96
- weekStartDay: WeekStartDay,
97
- T: TemporalNamespace,
98
- ): Temporal.PlainDate {
99
- return weekStartContaining(min, weekStartDay, T);
100
- }
101
-
102
- /** Find the week start (per weekStartDay) whose 7-day span contains max. */
103
- function findLastValidWeekStart(
104
- max: Temporal.PlainDate,
105
- weekStartDay: WeekStartDay,
106
- T: TemporalNamespace,
107
- ): Temporal.PlainDate {
108
- return weekStartContaining(max, weekStartDay, T);
109
- }
110
-
111
109
  /**
112
110
  * Trim leading and trailing fully-disabled weeks from a window.
113
111
  * Returns new { firstWeek, weekCount }.
@@ -115,8 +113,8 @@ function findLastValidWeekStart(
115
113
  function shrinkWindow(
116
114
  firstWeek: Temporal.PlainDate,
117
115
  weekCount: number,
118
- min: Temporal.PlainDate,
119
- max: Temporal.PlainDate,
116
+ min: Temporal.PlainDate | undefined,
117
+ max: Temporal.PlainDate | undefined,
120
118
  T: TemporalNamespace,
121
119
  ): { firstWeek: Temporal.PlainDate; weekCount: number } {
122
120
  let start = 0;
@@ -174,13 +172,14 @@ export function applyOutOfRange({
174
172
  weekStartDay,
175
173
  T,
176
174
  }: ApplyOutOfRangeInput): { firstWeek: Temporal.PlainDate; weekCount: number } {
177
- // Without bounds, all modes behave as unbounded
178
- if (!min || !max || behavior === "unbounded") {
175
+ // Without bounds, all modes behave as unbounded. A single bound is honored;
176
+ // the missing side is unlimited.
177
+ if ((!min && !max) || behavior === "unbounded") {
179
178
  return { firstWeek: targetFirstWeek, weekCount };
180
179
  }
181
180
 
182
181
  if (behavior === "stop") {
183
- // Caller checks canShift; here just return the target as-is
182
+ // Whether the move is allowed is decided by the caller (see WeekWindowPolicy)
184
183
  return { firstWeek: targetFirstWeek, weekCount };
185
184
  }
186
185
 
@@ -188,100 +187,25 @@ export function applyOutOfRange({
188
187
  return shrinkWindow(targetFirstWeek, weekCount, min, max, T);
189
188
  }
190
189
 
191
- if (behavior === "snap" || behavior === "snap-shrink") {
192
- // Compute the snapped position: window ends at lastValidWeekStart
193
- const lastValid = findLastValidWeekStart(max, weekStartDay, T);
194
-
195
- // The snapped firstWeek positions the window so it ends at lastValid
196
- // i.e. firstWeek = lastValid - (weekCount - 1) * 7
197
- const snappedFirst = lastValid.subtract({ days: (weekCount - 1) * 7 });
198
-
199
- // Determine which firstWeek to use: if target is already within bounds
200
- // (doesn't overshoot max), use the target; otherwise snap.
201
- // "Overshoots max" means the target window extends past the last valid week.
202
- // The last week of the target window is targetFirstWeek + (weekCount-1)*7.
203
- const targetLastWeek = targetFirstWeek.add({ days: (weekCount - 1) * 7 });
204
-
205
- let resolvedFirst: Temporal.PlainDate;
206
- if (T.PlainDate.compare(targetLastWeek, lastValid) <= 0) {
207
- // Target window fits within max — also check if it goes before min
208
- const firstValid = findFirstValidWeekStart(min, weekStartDay, T);
209
- if (T.PlainDate.compare(targetFirstWeek, firstValid) >= 0) {
210
- resolvedFirst = targetFirstWeek;
211
- } else {
212
- // Overshoots min: snap forward so window starts at firstValid
213
- resolvedFirst = firstValid;
214
- }
215
- } else {
216
- // Overshoots max: snap back
217
- resolvedFirst = snappedFirst;
218
- }
219
-
220
- if (behavior === "snap-shrink") {
221
- return shrinkWindow(resolvedFirst, weekCount, min, max, T);
222
- }
223
- return { firstWeek: resolvedFirst, weekCount };
224
- }
225
-
226
- return { firstWeek: targetFirstWeek, weekCount };
227
- }
228
-
229
- export interface CanShiftInput {
230
- currentFirstWeek: Temporal.PlainDate;
231
- weekCount: number;
232
- direction: 1 | -1;
233
- shiftBy?: number;
234
- behavior: OutOfRangeBehavior;
235
- min?: Temporal.PlainDate;
236
- max?: Temporal.PlainDate;
237
- weekStartDay: WeekStartDay;
238
- T: TemporalNamespace;
239
- }
240
-
241
- /**
242
- * Returns whether the navigation button for the given direction should be enabled.
243
- */
244
- export function canShift({
245
- currentFirstWeek,
246
- weekCount,
247
- direction,
248
- shiftBy,
249
- behavior,
250
- min,
251
- max,
252
- weekStartDay,
253
- T,
254
- }: CanShiftInput): boolean {
255
- if (behavior === "unbounded" || !min || !max) {
256
- return true;
190
+ // snap | snap-shrink
191
+ const targetLastWeek = targetFirstWeek.add({ weeks: weekCount - 1 });
192
+ const lastValid = max && weekStartContaining(max, weekStartDay);
193
+ const firstValid = min && weekStartContaining(min, weekStartDay);
194
+
195
+ let resolvedFirst = targetFirstWeek;
196
+ if (lastValid && T.PlainDate.compare(targetLastWeek, lastValid) > 0) {
197
+ // Overshoots max: snap back so the window ends at the last in-range week
198
+ resolvedFirst = lastValid.subtract({ weeks: weekCount - 1 });
199
+ } else if (
200
+ firstValid &&
201
+ T.PlainDate.compare(targetFirstWeek, firstValid) < 0
202
+ ) {
203
+ // Overshoots min: snap forward so the window starts at the first in-range week
204
+ resolvedFirst = firstValid;
257
205
  }
258
206
 
259
- const shift = shiftBy ?? weekCount;
260
- const targetFirstWeek = currentFirstWeek.add({ days: direction * shift * 7 });
261
-
262
- if (behavior === "stop" || behavior === "stop-shrink") {
263
- // Check if any week in the target window has valid days
264
- for (let i = 0; i < weekCount; i++) {
265
- const ws = targetFirstWeek.add({ days: i * 7 });
266
- if (weekHasValidDay(ws, min, max, T)) return true;
267
- }
268
- return false;
207
+ if (behavior === "snap-shrink") {
208
+ return shrinkWindow(resolvedFirst, weekCount, min, max, T);
269
209
  }
270
-
271
- if (behavior === "snap" || behavior === "snap-shrink") {
272
- // Disabled when the current position is already the snapped position
273
- // (i.e. applying the out-of-range behavior to the target produces the same firstWeek)
274
- const applied = applyOutOfRange({
275
- targetFirstWeek,
276
- weekCount,
277
- behavior,
278
- min,
279
- max,
280
- weekStartDay,
281
- T,
282
- });
283
- return T.PlainDate.compare(applied.firstWeek, currentFirstWeek) !== 0;
284
- }
285
-
286
- return true;
210
+ return { firstWeek: resolvedFirst, weekCount };
287
211
  }
@@ -0,0 +1,120 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, expect, it } from "vite-plus/test";
3
+ import {
4
+ computePreviewRange,
5
+ computeRangePreview,
6
+ decodeRange,
7
+ } from "./root-selection";
8
+
9
+ const T = Temporal;
10
+ const d = (iso: string) => T.PlainDate.from(iso);
11
+ const iso = (r: ReturnType<typeof computeRangePreview>) =>
12
+ r && [r[0].toString(), r[1].toString()];
13
+
14
+ describe("decodeRange", () => {
15
+ const toPlain = (raw: string) => d(raw);
16
+
17
+ it.each([
18
+ {
19
+ description: "both ends",
20
+ range: { start: "2026-03-10", end: "2026-03-12" },
21
+ expected: ["2026-03-10", "2026-03-12"],
22
+ },
23
+ {
24
+ description: "start only keeps a null end",
25
+ range: { start: "2026-03-10", end: null },
26
+ expected: ["2026-03-10", null],
27
+ },
28
+ {
29
+ description: "end only becomes [end, null] (pending start)",
30
+ range: { start: null, end: "2026-03-12" },
31
+ expected: ["2026-03-12", null],
32
+ },
33
+ {
34
+ description: "neither end is empty",
35
+ range: { start: null, end: null },
36
+ expected: [],
37
+ },
38
+ ])("$description", ({ range, expected }) => {
39
+ const decoded = decodeRange(range as never, toPlain as never);
40
+ expect(decoded.map((x) => x?.toString() ?? null)).toEqual(expected);
41
+ });
42
+ });
43
+
44
+ describe("computeRangePreview", () => {
45
+ const base = {
46
+ hoveredDate: d("2026-03-15"),
47
+ committedDates: [d("2026-03-10"), null],
48
+ rangeMode: "start-end" as const,
49
+ allowRangeReversal: true,
50
+ readOnly: false,
51
+ isDateDisabled: () => false,
52
+ T,
53
+ };
54
+
55
+ it.each([
56
+ {
57
+ description: "pending end previews start..hovered",
58
+ input: {},
59
+ expected: ["2026-03-10", "2026-03-15"],
60
+ },
61
+ {
62
+ description: "hovering before the start reverses when allowed",
63
+ input: { hoveredDate: d("2026-03-05") },
64
+ expected: ["2026-03-05", "2026-03-10"],
65
+ },
66
+ {
67
+ description: "read-only has no preview",
68
+ input: { readOnly: true },
69
+ expected: null,
70
+ },
71
+ {
72
+ description: "a disabled hovered date has no preview",
73
+ input: { isDateDisabled: (x: Temporal.PlainDate) => x.day === 15 },
74
+ expected: null,
75
+ },
76
+ {
77
+ description:
78
+ "no committed dates: the first click only anchors, so no preview",
79
+ input: { committedDates: [] },
80
+ expected: null,
81
+ },
82
+ ])("$description", ({ input, expected }) => {
83
+ expect(iso(computeRangePreview({ ...base, ...input }))).toEqual(expected);
84
+ });
85
+ });
86
+
87
+ describe("computePreviewRange", () => {
88
+ const range = { start: d("2026-03-10"), end: null } as never;
89
+ const run = (options?: Parameters<typeof computePreviewRange>[5]) => {
90
+ const r = computePreviewRange(
91
+ d("2026-03-15"),
92
+ range,
93
+ "start-end",
94
+ false,
95
+ T,
96
+ options,
97
+ );
98
+ return r && [String(r.start), String(r.end)];
99
+ };
100
+
101
+ it.each([
102
+ {
103
+ description: "defaults to every date selectable",
104
+ options: undefined,
105
+ expected: ["2026-03-10", "2026-03-15"],
106
+ },
107
+ {
108
+ description: "honors isDateDisabled",
109
+ options: { isDateDisabled: (x: Temporal.PlainDate) => x.day === 15 },
110
+ expected: null,
111
+ },
112
+ {
113
+ description: "honors readOnly",
114
+ options: { readOnly: true },
115
+ expected: null,
116
+ },
117
+ ])("$description", ({ options, expected }) => {
118
+ expect(run(options)).toEqual(expected);
119
+ });
120
+ });
@@ -3,13 +3,12 @@ import { useRender } from "@base-ui/react/use-render";
3
3
  import { forwardRef, useContext, useMemo } from "react";
4
4
  import { useCalendarStable, useCalendarState } from "./calendar-context";
5
5
  import { WeekDataContext, GridContext } from "./context";
6
- import { useMonthViewState } from "./month-view-context";
7
- import { MonthViewStableContext } from "./month-view-context";
8
6
  import {
9
7
  computeClippedRangeInfo,
10
8
  rangeOverlayStateAttributesMapping,
11
9
  } from "./selected-range";
12
10
  import type { RangePreviewState, RangePreviewProps } from "./types";
11
+ import { useViewStable, useViewState } from "./view-context";
13
12
 
14
13
  function RangePreviewFn(
15
14
  props: RangePreviewProps,
@@ -41,9 +40,8 @@ function RangePreviewInnerFn(
41
40
  const { orientation } = useContext(GridContext);
42
41
  const { temporal: T } = useCalendarStable();
43
42
  const { previewStart, previewEnd } = useCalendarState();
44
- const monthStable = useContext(MonthViewStableContext);
45
- const { rootState } = useMonthViewState();
46
- const outsideDays = monthStable?.outsideDays ?? "enabled";
43
+ const { rootState } = useViewState();
44
+ const { outsideDays } = useViewStable();
47
45
 
48
46
  const days = weekData?.days ?? [];
49
47
  const gridMonth = weekData?.gridMonth;
@@ -1,5 +1,5 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
- import { describe, it, expect } from "vitest";
2
+ import { describe, it, expect } from "vite-plus/test";
3
3
  import { resolveFirstWeekSpec, resolveFirstWeek } from "./resolve-first-week";
4
4
 
5
5
  const T = Temporal;
@@ -7,64 +7,9 @@ import type {
7
7
  TemporalNamespace,
8
8
  ValueChangeMeta,
9
9
  ValueFormat,
10
- WeekStartDay,
11
- OutsideDays,
12
10
  } from "./types";
13
11
  import { fromZonedDateTime } from "./utils";
14
12
 
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> =
33
- UseRootStateParamsBase<F> &
34
- (
35
- | {
36
- selectionMode: "single";
37
- value?: RawValueForFormat<F> | null;
38
- defaultValue?: RawValueForFormat<F>;
39
- onValueChange?: (
40
- value: RawValueForFormat<F> | null,
41
- meta: ValueChangeMeta<RawValueForFormat<F> | null>,
42
- ) => void;
43
- }
44
- | {
45
- selectionMode: "range";
46
- value?: DateRange<F> | null;
47
- defaultValue?: DateRange<F>;
48
- onValueChange?: (
49
- value: DateRange<F> | null,
50
- meta: ValueChangeMeta<DateRange<F> | null>,
51
- ) => void;
52
- rangeMode?: RangeMode;
53
- preventRangeReversal?: boolean;
54
- previewRange?: DateRange<F> | null;
55
- onHoveredDateChange?: (date: Temporal.PlainDate | undefined) => void;
56
- }
57
- | {
58
- selectionMode: "multiple";
59
- value?: RawValueForFormat<F>[] | null;
60
- defaultValue?: RawValueForFormat<F>[];
61
- onValueChange?: (
62
- value: RawValueForFormat<F>[],
63
- meta: ValueChangeMeta<RawValueForFormat<F>[]>,
64
- ) => void;
65
- }
66
- );
67
-
68
13
  /** Construct a DateRange (workaround for TS generic conditional type widening). */
69
14
  function mkRange<F extends ValueFormat>(
70
15
  start: unknown,
@@ -87,6 +32,22 @@ export function tagRaw<F extends ValueFormat>(
87
32
  return { format, value: raw } as DateValueObject;
88
33
  }
89
34
 
35
+ /**
36
+ * Decodes a range value into committed dates. An empty range is `[]`; a range
37
+ * with only an end is `[end, null]` (pending start, so the end is still the
38
+ * anchor of the next click).
39
+ */
40
+ export function decodeRange<F extends ValueFormat>(
41
+ range: DateRange<F>,
42
+ toPlain: (raw: RawValueForFormat<F>) => Temporal.PlainDate,
43
+ ): (Temporal.PlainDate | null)[] {
44
+ const start = range.start != null ? toPlain(range.start) : null;
45
+ const end = range.end != null ? toPlain(range.end) : null;
46
+ if (start === null && end === null) return [];
47
+ if (start === null && end !== null) return [end, null];
48
+ return [start, end];
49
+ }
50
+
90
51
  /** Resolve a `RangeMode` to a concrete "start" | "end" | "reset" action for inside-range clicks. */
91
52
  export function resolveInsideAction(
92
53
  mode: RangeMode,
@@ -639,47 +600,48 @@ export function truncateDatesForMode<F extends ValueFormat>(opts: {
639
600
  return clamped;
640
601
  }
641
602
 
603
+ /** Inputs shared by the provider's hover preview and the public {@link computePreviewRange}. */
604
+ export interface RangePreviewInput {
605
+ hoveredDate: Temporal.PlainDate;
606
+ committedDates: readonly (Temporal.PlainDate | null)[];
607
+ rangeMode: RangeMode;
608
+ allowRangeReversal: boolean;
609
+ readOnly: boolean;
610
+ isDateDisabled: (date: Temporal.PlainDate) => boolean;
611
+ T: TemporalNamespace;
612
+ }
613
+
642
614
  /**
643
- * Pure utility that computes the preview range for a hovered date, using the
644
- * same logic as the internal hook. Useful for deriving the preview in an
645
- * `onHoveredDateChange` handler without needing a controlled `previewRange`.
615
+ * The range a click on `hoveredDate` would produce, or `null` when the click
616
+ * would do nothing (read-only, disabled date). One path for both the
617
+ * provider's own preview and the public {@link computePreviewRange}.
646
618
  */
647
- export function computePreviewRange(
648
- hoveredDate: Temporal.PlainDate,
649
- currentRange: DateRange | null,
650
- rangeMode: RangeMode,
651
- preventRangeReversal = false,
652
- T?: TemporalNamespace,
653
- ): DateRange<"PlainDate"> | null {
654
- const Temporal: TemporalNamespace = T ?? (globalThis as any).Temporal;
655
- if (!Temporal) {
656
- throw new Error(
657
- "computePreviewRange requires a Temporal namespace. Pass it as the 5th argument or ensure globalThis.Temporal is available.",
658
- );
659
- }
619
+ export function computeRangePreview(
620
+ input: RangePreviewInput,
621
+ ): [Temporal.PlainDate, Temporal.PlainDate] | null {
622
+ const {
623
+ hoveredDate,
624
+ committedDates,
625
+ rangeMode,
626
+ allowRangeReversal,
627
+ readOnly,
628
+ isDateDisabled,
629
+ T,
630
+ } = input;
660
631
 
661
- const rawStart =
662
- currentRange?.start != null
663
- ? Temporal.PlainDate.from(currentRange.start as any)
664
- : null;
665
- const rawEnd =
666
- currentRange?.end != null
667
- ? Temporal.PlainDate.from(currentRange.end as any)
668
- : null;
669
- // Normalize {start: null, end} → [end, null] (pending start)
670
- const committedDates = currentRange
671
- ? rawStart === null && rawEnd !== null
672
- ? ([rawEnd, null] as const)
673
- : ([rawStart, rawEnd] as const)
674
- : ([] as const);
675
632
  const committedStart = committedDates[0] ?? undefined;
676
- const committedEnd = committedDates[1] ?? undefined;
677
-
678
- const noop = () => [] as any;
633
+ const committedEnd =
634
+ committedDates.length > 1
635
+ ? (committedDates[committedDates.length - 1] ?? undefined)
636
+ : committedStart;
637
+
638
+ // The preview never emits a value, so the formatting and time-zone inputs
639
+ // of a real selection are unused.
640
+ const unused = () => [] as never;
679
641
  const result = computeSelectionUpdate<"PlainDate">({
680
642
  date: hoveredDate,
681
- readOnly: false,
682
- isDateDisabled: () => false,
643
+ readOnly,
644
+ isDateDisabled,
683
645
  isMultiple: false,
684
646
  isRange: true,
685
647
  committedDates,
@@ -687,19 +649,59 @@ export function computePreviewRange(
687
649
  committedEnd,
688
650
  selectedZdt: undefined,
689
651
  rangeMode,
690
- allowRangeReversal: !preventRangeReversal,
691
- sortDates: (d) => [...d].sort((a, b) => Temporal.PlainDate.compare(a, b)),
692
- currentSingleFormatted: noop,
693
- currentRangeFormatted: () => currentRange as DateRange<"PlainDate"> | null,
694
- currentMultipleFormatted: noop,
652
+ allowRangeReversal,
653
+ sortDates: (d) => [...d].sort((a, b) => T.PlainDate.compare(a, b)),
654
+ currentSingleFormatted: unused,
655
+ currentRangeFormatted: unused,
656
+ currentMultipleFormatted: unused,
695
657
  resolvedFormat: "PlainDate",
696
658
  timeZone: "UTC",
697
- T: Temporal,
659
+ T,
698
660
  });
699
661
 
700
662
  if (result.skip || result.newDates.length === 0) return null;
701
663
  const start = result.newDates[0];
702
664
  const end = result.newDates.length > 1 ? result.newDates[1] : start;
703
665
  if (start == null || end == null) return null;
704
- return mkRange<"PlainDate">(start, end);
666
+ return [start, end];
667
+ }
668
+
669
+ /**
670
+ * Pure utility that computes the preview range for a hovered date, using the
671
+ * same logic as the built-in hover preview. Useful for deriving the preview in
672
+ * an `onHoveredDateChange` handler without needing a controlled `previewRange`.
673
+ *
674
+ * Pass `options.isDateDisabled` and `options.readOnly` to match a calendar that
675
+ * disables dates or is read-only; by default every date is selectable.
676
+ */
677
+ export function computePreviewRange(
678
+ hoveredDate: Temporal.PlainDate,
679
+ currentRange: DateRange | null,
680
+ rangeMode: RangeMode,
681
+ preventRangeReversal = false,
682
+ T?: TemporalNamespace,
683
+ options: {
684
+ isDateDisabled?: (date: Temporal.PlainDate) => boolean;
685
+ readOnly?: boolean;
686
+ } = {},
687
+ ): DateRange<"PlainDate"> | null {
688
+ const Temporal: TemporalNamespace = T ?? (globalThis as any).Temporal;
689
+ if (!Temporal) {
690
+ throw new Error(
691
+ "computePreviewRange requires a Temporal namespace. Pass it as the 5th argument or ensure globalThis.Temporal is available.",
692
+ );
693
+ }
694
+
695
+ const preview = computeRangePreview({
696
+ hoveredDate,
697
+ committedDates: currentRange
698
+ ? decodeRange(currentRange, (raw) => Temporal.PlainDate.from(raw as any))
699
+ : [],
700
+ rangeMode,
701
+ allowRangeReversal: !preventRangeReversal,
702
+ readOnly: options.readOnly ?? false,
703
+ isDateDisabled: options.isDateDisabled ?? (() => false),
704
+ T: Temporal,
705
+ });
706
+ return preview ? mkRange<"PlainDate">(preview[0], preview[1]) : null;
705
707
  }
@@ -4,8 +4,6 @@ import type { Temporal } from "@js-temporal/polyfill";
4
4
  import { forwardRef, useContext, useMemo } from "react";
5
5
  import { useCalendarStable, useCalendarState } from "./calendar-context";
6
6
  import { WeekDataContext, GridContext } from "./context";
7
- import { useMonthViewState } from "./month-view-context";
8
- import { MonthViewStableContext } from "./month-view-context";
9
7
  import type { StateAttributesMapping } from "./types";
10
8
  import type {
11
9
  RangeSelectedState,
@@ -14,6 +12,7 @@ import type {
14
12
  TemporalNamespace,
15
13
  } from "./types";
16
14
  import { computeWeekRangeInfo } from "./utils";
15
+ import { useViewStable, useViewState } from "./view-context";
17
16
 
18
17
  export const rangeOverlayStateAttributesMapping = {
19
18
  root: () => null,
@@ -114,9 +113,8 @@ function RangeSelectedInnerFn(
114
113
  const { orientation } = useContext(GridContext);
115
114
  const { temporal: T } = useCalendarStable();
116
115
  const { rangeStart, rangeEnd } = useCalendarState();
117
- const monthStable = useContext(MonthViewStableContext);
118
- const { rootState } = useMonthViewState();
119
- const outsideDays = monthStable?.outsideDays ?? "enabled";
116
+ const { rootState } = useViewState();
117
+ const { outsideDays } = useViewStable();
120
118
 
121
119
  const days = weekData?.days ?? [];
122
120
  const gridMonth = weekData?.gridMonth;