@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
@@ -0,0 +1,136 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { act, cleanup, render, screen } from "@testing-library/react";
3
+ import { afterEach, describe, expect, it, vi } from "vite-plus/test";
4
+ import { MonthView } from "./month-view";
5
+ import type { MonthViewProps } from "./month-view";
6
+ import { useMonthViewStable, useMonthViewState } from "./month-view-context";
7
+ import { useViewContext } from "./view-context";
8
+
9
+ const T = Temporal;
10
+
11
+ let go: { next: () => void; prev: () => void };
12
+
13
+ function Probe() {
14
+ const { goNextMonth, goPrevMonth } = useMonthViewStable();
15
+ const { currentMonth } = useMonthViewState();
16
+ const { focusedDate } = useViewContext();
17
+ go = { next: goNextMonth, prev: goPrevMonth };
18
+ return (
19
+ <>
20
+ <span data-testid="month">
21
+ {currentMonth.year}-{String(currentMonth.month).padStart(2, "0")}
22
+ </span>
23
+ <span data-testid="focus">{focusedDate.toString()}</span>
24
+ </>
25
+ );
26
+ }
27
+
28
+ const base = {
29
+ temporal: T,
30
+ timeZone: "UTC",
31
+ defaultValue: T.PlainDate.from("2026-03-15"),
32
+ } as const;
33
+
34
+ const month = () => screen.getByTestId("month").textContent;
35
+ const focus = () => screen.getByTestId("focus").textContent;
36
+
37
+ afterEach(cleanup);
38
+
39
+ describe("MonthView goNextMonth/goPrevMonth honor outOfRangeBehavior", () => {
40
+ it.each([
41
+ {
42
+ description: "stop: goPrevMonth into the min month moves",
43
+ props: {
44
+ min: T.PlainDate.from("2026-02-10"),
45
+ outOfRangeBehavior: "stop",
46
+ },
47
+ call: "prev" as const,
48
+ expected: { month: "2026-02", focus: "2026-02-01" },
49
+ },
50
+ {
51
+ description: "stop: goPrevMonth before the min month does nothing",
52
+ props: {
53
+ min: T.PlainDate.from("2026-03-10"),
54
+ outOfRangeBehavior: "stop",
55
+ },
56
+ call: "prev" as const,
57
+ expected: { month: "2026-03", focus: "2026-03-15" },
58
+ },
59
+ {
60
+ description: "stop: goNextMonth past the max month does nothing",
61
+ props: {
62
+ max: T.PlainDate.from("2026-03-20"),
63
+ outOfRangeBehavior: "stop",
64
+ },
65
+ call: "next" as const,
66
+ expected: { month: "2026-03", focus: "2026-03-15" },
67
+ },
68
+ {
69
+ description: "stop: goNextMonth with only min set still moves",
70
+ props: {
71
+ min: T.PlainDate.from("2026-03-10"),
72
+ outOfRangeBehavior: "stop",
73
+ },
74
+ call: "next" as const,
75
+ expected: { month: "2026-04", focus: "2026-04-01" },
76
+ },
77
+ {
78
+ description: "unbounded: goNextMonth moves past max",
79
+ props: { max: T.PlainDate.from("2026-03-20") },
80
+ call: "next" as const,
81
+ expected: { month: "2026-04", focus: "2026-04-01" },
82
+ },
83
+ ] satisfies {
84
+ description: string;
85
+ props: Partial<MonthViewProps>;
86
+ call: "next" | "prev";
87
+ expected: { month: string; focus: string };
88
+ }[])("uncontrolled, $description", ({ props, call, expected }) => {
89
+ render(
90
+ <MonthView {...base} {...props}>
91
+ <Probe />
92
+ </MonthView>,
93
+ );
94
+
95
+ act(() => go[call]());
96
+
97
+ expect({ month: month(), focus: focus() }).toEqual(expected);
98
+ });
99
+
100
+ it.each([
101
+ {
102
+ description: "stop: blocked step does not notify the parent",
103
+ props: {
104
+ max: T.PlainDate.from("2026-03-20"),
105
+ outOfRangeBehavior: "stop",
106
+ },
107
+ expected: { calls: 0 },
108
+ },
109
+ {
110
+ description: "unbounded: step notifies the parent once",
111
+ props: { max: T.PlainDate.from("2026-03-20") },
112
+ expected: { calls: 1 },
113
+ },
114
+ ] satisfies {
115
+ description: string;
116
+ props: Partial<MonthViewProps>;
117
+ expected: { calls: number };
118
+ }[])("controlled, $description", ({ props, expected }) => {
119
+ const onMonthChange = vi.fn();
120
+ render(
121
+ <MonthView
122
+ {...base}
123
+ {...props}
124
+ month={T.PlainYearMonth.from("2026-03")}
125
+ onMonthChange={onMonthChange}
126
+ >
127
+ <Probe />
128
+ </MonthView>,
129
+ );
130
+
131
+ act(() => go.next());
132
+
133
+ expect({ calls: onMonthChange.mock.calls.length }).toEqual(expected);
134
+ expect(month()).toBe("2026-03");
135
+ });
136
+ });
@@ -2,7 +2,7 @@ import { Temporal } from "@js-temporal/polyfill";
2
2
  import { render, act } from "@testing-library/react";
3
3
  import { useState } from "react";
4
4
  // @vitest-environment jsdom
5
- import { describe, it, expect, vi } from "vitest";
5
+ import { describe, it, expect, vi } from "vite-plus/test";
6
6
  import { useCalendarStable } from "./calendar-context";
7
7
  import { Grid } from "./grid";
8
8
  import { MonthView } from "./month-view";
@@ -2092,3 +2092,91 @@ describe("outsideDays", () => {
2092
2092
  });
2093
2093
  });
2094
2094
  });
2095
+
2096
+ describe("opening month", () => {
2097
+ // 2020 dates keep the real today out of every window under test.
2098
+ const ym = (s: string) => Temporal.PlainYearMonth.from(s);
2099
+ const day = (s: string) => Temporal.PlainDate.from(s);
2100
+
2101
+ it.each([
2102
+ {
2103
+ description: "defaultMonth opens on that month, not today's",
2104
+ props: { defaultMonth: ym("2020-03") },
2105
+ keys: [],
2106
+ expected: { months: ["2020-03"], focused: "2020-03-01", monthChanges: 0 },
2107
+ },
2108
+ {
2109
+ description: "defaultMonth opens on that month in range mode",
2110
+ props: { selectionMode: "range", defaultMonth: ym("2020-03") },
2111
+ keys: [],
2112
+ expected: { months: ["2020-03"], focused: "2020-03-01", monthChanges: 0 },
2113
+ },
2114
+ {
2115
+ description: "defaultMonth wins over a defaultValue outside it",
2116
+ props: { defaultMonth: ym("2020-03"), defaultValue: day("2020-05-15") },
2117
+ keys: [],
2118
+ expected: { months: ["2020-03"], focused: "2020-03-01", monthChanges: 0 },
2119
+ },
2120
+ {
2121
+ description: "a defaultValue inside a multi-month window keeps focus",
2122
+ props: {
2123
+ defaultMonth: ym("2020-03"),
2124
+ defaultValue: day("2020-04-15"),
2125
+ numberOfMonths: 2,
2126
+ },
2127
+ keys: [],
2128
+ expected: {
2129
+ months: ["2020-03", "2020-04"],
2130
+ focused: "2020-04-15",
2131
+ monthChanges: 0,
2132
+ },
2133
+ },
2134
+ {
2135
+ description: "the first arrow key after defaultMonth moves within it",
2136
+ props: { defaultMonth: ym("2020-03") },
2137
+ keys: ["ArrowRight"],
2138
+ expected: { months: ["2020-03"], focused: "2020-03-02", monthChanges: 0 },
2139
+ },
2140
+ {
2141
+ description: "the first arrow key in a controlled month moves within it",
2142
+ props: { month: ym("2020-03") },
2143
+ keys: ["ArrowRight"],
2144
+ expected: { months: ["2020-03"], focused: "2020-03-02", monthChanges: 0 },
2145
+ },
2146
+ ])("$description", ({ props, keys, expected }) => {
2147
+ const onMonthChange = vi.fn();
2148
+ let captured: { months: string[]; focused: string } | undefined;
2149
+ const { container } = render(
2150
+ <MonthView
2151
+ {...defaultProps}
2152
+ {...(props as object)}
2153
+ onMonthChange={onMonthChange}
2154
+ >
2155
+ <Grid />
2156
+ <MonthDataCapture
2157
+ onCapture={(d) => {
2158
+ captured = {
2159
+ months: d.allMonths.map((m) =>
2160
+ Temporal.PlainYearMonth.from(m).toString(),
2161
+ ),
2162
+ focused: d.focusedDate,
2163
+ };
2164
+ }}
2165
+ />
2166
+ </MonthView>,
2167
+ );
2168
+ const grid = container.querySelector('[role="grid"]')!;
2169
+ for (const key of keys) {
2170
+ act(() => {
2171
+ grid.dispatchEvent(
2172
+ new KeyboardEvent("keydown", { key, bubbles: true }),
2173
+ );
2174
+ });
2175
+ }
2176
+
2177
+ expect({
2178
+ ...captured,
2179
+ monthChanges: onMonthChange.mock.calls.length,
2180
+ }).toEqual(expected);
2181
+ });
2182
+ });
@@ -1,28 +1,35 @@
1
1
  import type { Temporal } from "@js-temporal/polyfill";
2
- import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
+ import { useCallback, useMemo, useRef, useState } from "react";
3
3
  import { useCalendarStable, useCalendarState } from "./calendar-context";
4
4
  import { CalendarProvider } from "./calendar-provider";
5
5
  import type { CalendarProviderProps } from "./calendar-types";
6
+ import { DayCellStoreProvider } from "./day-cell-store";
6
7
  import {
7
8
  MonthViewStableContext,
8
9
  MonthViewStateContext,
9
10
  } from "./month-view-context";
11
+ import { useMonthNavigation } from "./month-view-navigation";
10
12
  import type {
11
13
  MonthViewRootProps,
12
14
  MonthViewStableContextValue,
13
15
  MonthViewStateContextValue,
14
16
  } from "./month-view-types";
17
+ import { createMonthWindowPolicy } from "./month-window";
15
18
  import type { MonthData, RootState, ValueFormat } from "./types";
16
19
  import {
17
- computeAdjacentMonth,
18
- focusedDateForMonth,
19
20
  getMonthWeeks,
20
21
  resolveFocusTarget,
22
+ resolveInitialAnchor,
21
23
  selectedToZdt,
22
- shiftWindowToMonth,
23
- toZonedDateTime,
24
24
  } from "./utils";
25
- import { ViewContext, type ViewContextValue } from "./view-context";
25
+ import {
26
+ ViewContext,
27
+ ViewStableContext,
28
+ ViewStateContext,
29
+ type ViewContextValue,
30
+ type ViewStableContextValue,
31
+ type ViewStateContextValue,
32
+ } from "./view-context";
26
33
 
27
34
  // ---------------------------------------------------------------------------
28
35
  // MonthView.Root — placed inside CalendarProvider
@@ -50,41 +57,55 @@ function MonthViewRoot(props: MonthViewRootProps) {
50
57
  const calState = useCalendarState();
51
58
 
52
59
  const T = calStable.temporal;
53
- const { timeZone, weekStartDay, isDateDisabled } = calStable;
54
-
55
- // --- Month navigation state (controlled/uncontrolled) ---
56
- const isMonthControlled = monthProp !== undefined;
60
+ const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
57
61
 
58
- const [internalMonth, setInternalMonth] = useState<{
59
- year: number;
60
- month: number;
61
- }>(() => {
62
- if (monthProp) return { year: monthProp.year, month: monthProp.month };
63
- if (defaultMonthProp)
64
- return { year: defaultMonthProp.year, month: defaultMonthProp.month };
65
- // Derive from selection or today
66
- if (calState.selected) {
67
- const zdt = toZonedDateTime(calState.selected, timeZone, T);
68
- return { year: zdt.year, month: zdt.month };
69
- }
70
- const now = T.Now.zonedDateTimeISO(timeZone);
71
- return { year: now.year, month: now.month };
72
- });
62
+ const [gridHasFocus, setGridHasFocus] = useState(false);
63
+ const gridFocusedRef = useRef(false);
73
64
 
74
- const currentMonth = isMonthControlled
75
- ? { year: monthProp.year, month: monthProp.month }
76
- : internalMonth;
65
+ // --- Month window and navigation ---
66
+ const { minValue, maxValue } = calStable;
67
+ const windowPolicy = useMemo(
68
+ () =>
69
+ createMonthWindowPolicy({
70
+ numberOfMonths,
71
+ behavior: outOfRangeBehavior,
72
+ min: minValue,
73
+ max: maxValue,
74
+ T,
75
+ }),
76
+ [numberOfMonths, outOfRangeBehavior, minValue, maxValue, T],
77
+ );
77
78
 
78
79
  // --- Focus state ---
80
+ // Focus starts inside the window the view opens on. Arrow keys move from
81
+ // the focused date, so focus outside the window would pull the view to it.
79
82
  const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() => {
80
- if (calState.selected) {
81
- return toZonedDateTime(calState.selected, timeZone, T).toPlainDate();
82
- }
83
- return T.Now.plainDateISO();
83
+ const anchor = resolveInitialAnchor(calState.selected, today, timeZone, T);
84
+ const opening = monthProp ?? defaultMonthProp;
85
+ if (!opening || windowPolicy.isVisible(opening, anchor)) return anchor;
86
+ return T.PlainDate.from({
87
+ year: opening.year,
88
+ month: opening.month,
89
+ day: 1,
90
+ });
84
91
  });
85
92
 
86
- const [gridHasFocus, setGridHasFocus] = useState(false);
87
- const gridFocusedRef = useRef(false);
93
+ const initialAnchor = useCallback(
94
+ () => resolveInitialAnchor(calState.selected, today, timeZone, T),
95
+ [calState.selected, today, timeZone, T],
96
+ );
97
+
98
+ const { currentMonth, viewingYearMonth, goNextMonth, goPrevMonth } =
99
+ useMonthNavigation({
100
+ month: monthProp,
101
+ defaultMonth: defaultMonthProp,
102
+ initialAnchor,
103
+ onMonthChange,
104
+ policy: windowPolicy,
105
+ focusedDate,
106
+ setFocusedDate,
107
+ T,
108
+ });
88
109
 
89
110
  // --- Grid label IDs ---
90
111
  const [gridLabelIds, setGridLabelIds] = useState<Record<number, string>>({});
@@ -104,162 +125,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
104
125
  [],
105
126
  );
106
127
 
107
- // Always-current ref so callbacks/effects don't re-subscribe on every
108
- // onMonthChange identity change.
109
- const onMonthChangeRef = useRef(onMonthChange);
110
- onMonthChangeRef.current = onMonthChange;
111
-
112
- // Request the parent move the view to `{year, month}` (controlled mode).
113
- // Built in ISO — `year`/`month` are ISO numbers, and the locale calendar
114
- // only affects display (see MonthYearString). Passing `calendar` here would
115
- // reinterpret the ISO numbers as locale-calendar fields (e.g. Buddhist),
116
- // shifting the value by centuries.
117
- const notifyMonth = useCallback(
118
- (year: number, month: number) => {
119
- onMonthChangeRef.current?.(T.PlainYearMonth.from({ year, month }));
120
- },
121
- [T],
122
- );
123
-
124
- // Whether `{year, month}` falls within the currently displayed window.
125
- const isMonthVisible = useCallback(
126
- (targetYear: number, targetMonth: number) => {
127
- for (let i = 0; i < numberOfMonths; i++) {
128
- const totalMonths =
129
- currentMonth.year * 12 + (currentMonth.month - 1) + i;
130
- const y = Math.floor(totalMonths / 12);
131
- const m = (totalMonths % 12) + 1;
132
- if (targetYear === y && targetMonth === m) return true;
133
- }
134
- return false;
135
- },
136
- [currentMonth.year, currentMonth.month, numberOfMonths],
137
- );
138
-
139
- // --- navigateToMonth: shift the window by the minimum needed to reveal the
140
- // target month (keeps keyboard crossing symmetric with the Prev/Next
141
- // buttons, which step one month at a time). ---
142
- const navigateToMonth = useCallback(
143
- (targetYear: number, targetMonth: number) => {
144
- if (isMonthControlled) return; // controlled mode: parent manages
145
- setInternalMonth((prev) => {
146
- const next = shiftWindowToMonth(
147
- prev,
148
- { year: targetYear, month: targetMonth },
149
- numberOfMonths,
150
- );
151
- if (next.year === prev.year && next.month === prev.month) return prev;
152
- return next;
153
- });
154
- },
155
- [isMonthControlled, numberOfMonths],
156
- );
157
-
158
- // Button navigation in controlled mode notifies the parent directly, then
159
- // moves focus into the target month. This ref holds that focus value so the
160
- // focus-sync effect skips re-notifying for it. Keyed by the value (not a
161
- // one-shot flag) so it can't strand and suppress a later keyboard crossing
162
- // if the button's focus move is a no-op or the parent ignores the change.
163
- const skipFocusSyncForRef = useRef<Temporal.PlainDate | null>(null);
164
- const prevFocusedRef = useRef(focusedDate);
165
-
166
- // Clear a stale skip target whenever the parent actually moves the view.
167
- useEffect(() => {
168
- skipFocusSyncForRef.current = null;
169
- }, [monthProp?.year, monthProp?.month]);
170
-
171
- // Sync month when the focused date crosses a month boundary. Uncontrolled:
172
- // shift internal state. Controlled: ask the parent to move the view, but
173
- // only when focus actually moved out of the visible window (keyboard nav) —
174
- // never as an echo of an external `month` update or a button click.
175
- useEffect(() => {
176
- const prevFocused = prevFocusedRef.current;
177
- prevFocusedRef.current = focusedDate;
178
- if (isMonthControlled) {
179
- if (skipFocusSyncForRef.current === focusedDate) {
180
- skipFocusSyncForRef.current = null;
181
- return;
182
- }
183
- if (prevFocused === focusedDate) return;
184
- if (isMonthVisible(focusedDate.year, focusedDate.month)) return;
185
- // Emit the minimally-shifted first-visible month (what the parent binds
186
- // to `month`), not the focused month — so a controlled window scrolls by
187
- // the minimum, matching uncontrolled mode and the nav buttons.
188
- const next = shiftWindowToMonth(
189
- { year: currentMonth.year, month: currentMonth.month },
190
- { year: focusedDate.year, month: focusedDate.month },
191
- numberOfMonths,
192
- );
193
- notifyMonth(next.year, next.month);
194
- return;
195
- }
196
- navigateToMonth(focusedDate.year, focusedDate.month);
197
- }, [
198
- focusedDate,
199
- navigateToMonth,
200
- isMonthControlled,
201
- isMonthVisible,
202
- notifyMonth,
203
- currentMonth.year,
204
- currentMonth.month,
205
- numberOfMonths,
206
- ]);
207
-
208
- // --- Month navigation callbacks ---
209
- const goNextMonth = useCallback(() => {
210
- if (isMonthControlled) {
211
- const { year, month, firstDay } = computeAdjacentMonth(
212
- { year: monthProp!.year, month: monthProp!.month },
213
- "next",
214
- T,
215
- );
216
- // Notify the parent directly; the follow-on focus move is suppressed in
217
- // the focus-sync effect (keyed on the value) so onMonthChange fires once.
218
- const nextFocus = focusedDateForMonth(
219
- focusedDate,
220
- { year, month },
221
- firstDay,
222
- );
223
- skipFocusSyncForRef.current = nextFocus;
224
- notifyMonth(year, month);
225
- setFocusedDate(nextFocus);
226
- return;
227
- }
228
- setInternalMonth((m) => {
229
- const { year, month, firstDay } = computeAdjacentMonth(m, "next", T);
230
- setFocusedDate((prev) =>
231
- focusedDateForMonth(prev, { year, month }, firstDay),
232
- );
233
- return { year, month };
234
- });
235
- }, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
236
-
237
- const goPrevMonth = useCallback(() => {
238
- if (isMonthControlled) {
239
- const { year, month, firstDay } = computeAdjacentMonth(
240
- { year: monthProp!.year, month: monthProp!.month },
241
- "prev",
242
- T,
243
- );
244
- const nextFocus = focusedDateForMonth(
245
- focusedDate,
246
- { year, month },
247
- firstDay,
248
- );
249
- skipFocusSyncForRef.current = nextFocus;
250
- notifyMonth(year, month);
251
- setFocusedDate(nextFocus);
252
- return;
253
- }
254
- setInternalMonth((m) => {
255
- const { year, month, firstDay } = computeAdjacentMonth(m, "prev", T);
256
- setFocusedDate((prev) =>
257
- focusedDateForMonth(prev, { year, month }, firstDay),
258
- );
259
- return { year, month };
260
- });
261
- }, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
262
-
263
128
  // --- Grid computation ---
264
129
  const allMonths = useMemo<MonthData[]>(() => {
265
130
  const opts = { weekStartDay, fixedWeeks };
@@ -341,32 +206,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
341
206
  ],
342
207
  );
343
208
 
344
- // --- viewingYearMonth (for the render-prop rootState) ---
345
- // ISO — the locale calendar only affects display, not this value.
346
- const viewingYearMonth = useMemo(
347
- () =>
348
- T.PlainYearMonth.from({
349
- year: currentMonth.year,
350
- month: currentMonth.month,
351
- }),
352
- [currentMonth, T],
353
- );
354
-
355
- // --- Fire onMonthChange when uncontrolled navigation moves the view (not on
356
- // mount). In controlled mode the parent owns `month`, so notification flows
357
- // from button clicks (goNext/goPrevMonth) and the focus-sync effect instead;
358
- // firing here would just echo the parent's own update back to it. ---
359
- const mountedRef = useRef(false);
360
-
361
- useEffect(() => {
362
- if (!mountedRef.current) {
363
- mountedRef.current = true;
364
- return;
365
- }
366
- if (isMonthControlled) return;
367
- onMonthChangeRef.current?.(viewingYearMonth);
368
- }, [viewingYearMonth, isMonthControlled]);
369
-
370
209
  // --- rootState (for render functions) ---
371
210
  const rootState = useMemo<RootState>(
372
211
  () => ({
@@ -384,6 +223,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
384
223
  fixedWeeks,
385
224
  outsideDays,
386
225
  outOfRangeBehavior,
226
+ windowPolicy,
387
227
  goNextMonth,
388
228
  goPrevMonth,
389
229
  setGridLabelId,
@@ -394,6 +234,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
394
234
  fixedWeeks,
395
235
  outsideDays,
396
236
  outOfRangeBehavior,
237
+ windowPolicy,
397
238
  goNextMonth,
398
239
  goPrevMonth,
399
240
  setGridLabelId,
@@ -424,10 +265,26 @@ function MonthViewRoot(props: MonthViewRootProps) {
424
265
  [focusedDate, tabTargetDate, gridHasFocus],
425
266
  );
426
267
 
268
+ const viewStableCtx = useMemo<ViewStableContextValue>(
269
+ () => ({ gridFocusedRef, outsideDays, setFocusedDate }),
270
+ [outsideDays],
271
+ );
272
+
273
+ const viewStateCtx = useMemo<ViewStateContextValue>(
274
+ () => ({ currentDateTime, rootState }),
275
+ [currentDateTime, rootState],
276
+ );
277
+
427
278
  return (
428
279
  <MonthViewStableContext.Provider value={stableCtx}>
429
280
  <MonthViewStateContext.Provider value={stateCtx}>
430
- <ViewContext.Provider value={viewCtx}>{children}</ViewContext.Provider>
281
+ <ViewStableContext.Provider value={viewStableCtx}>
282
+ <ViewStateContext.Provider value={viewStateCtx}>
283
+ <ViewContext.Provider value={viewCtx}>
284
+ <DayCellStoreProvider>{children}</DayCellStoreProvider>
285
+ </ViewContext.Provider>
286
+ </ViewStateContext.Provider>
287
+ </ViewStableContext.Provider>
431
288
  </MonthViewStateContext.Provider>
432
289
  </MonthViewStableContext.Provider>
433
290
  );