@klinking/colander 3.1.0-alpha.3 → 3.1.0-alpha.5

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 (51) hide show
  1. package/dist/formats/date.cjs +1 -1
  2. package/dist/formats/date.d.cts +1 -1
  3. package/dist/formats/date.d.mts +1 -1
  4. package/dist/formats/date.mjs +1 -1
  5. package/dist/formats/object.cjs +1 -1
  6. package/dist/formats/object.d.cts +1 -1
  7. package/dist/formats/object.d.mts +1 -1
  8. package/dist/formats/object.mjs +1 -1
  9. package/dist/formats/plain-date-time.cjs +1 -1
  10. package/dist/formats/plain-date-time.d.cts +1 -1
  11. package/dist/formats/plain-date-time.d.mts +1 -1
  12. package/dist/formats/plain-date-time.mjs +1 -1
  13. package/dist/formats/plain-date.cjs +1 -1
  14. package/dist/formats/plain-date.d.cts +1 -1
  15. package/dist/formats/plain-date.d.mts +1 -1
  16. package/dist/formats/plain-date.mjs +1 -1
  17. package/dist/formats/plain-month-day.cjs +1 -1
  18. package/dist/formats/plain-month-day.d.cts +1 -1
  19. package/dist/formats/plain-month-day.d.mts +1 -1
  20. package/dist/formats/plain-month-day.mjs +1 -1
  21. package/dist/formats/plain-year-month.cjs +1 -1
  22. package/dist/formats/plain-year-month.d.cts +1 -1
  23. package/dist/formats/plain-year-month.d.mts +1 -1
  24. package/dist/formats/plain-year-month.mjs +1 -1
  25. package/dist/formats/zoned-date-time.cjs +1 -1
  26. package/dist/formats/zoned-date-time.d.cts +1 -1
  27. package/dist/formats/zoned-date-time.d.mts +1 -1
  28. package/dist/formats/zoned-date-time.mjs +1 -1
  29. package/dist/index.cjs +1 -1
  30. package/dist/index.d.cts +1 -1
  31. package/dist/index.d.mts +1 -1
  32. package/dist/index.mjs +1 -1
  33. package/dist/{weeks-navigation-DEaASUAh.mjs → weeks-navigation-CEV8w_69.mjs} +239 -221
  34. package/dist/{weeks-navigation-DvryOSWF.cjs → weeks-navigation-q3beZn2n.cjs} +239 -221
  35. package/dist/{weeks-view-context-CNAq2Hiu.d.cts → weeks-view-context-BLZZm1fq.d.mts} +33 -2
  36. package/dist/{weeks-view-context-CsP3PBiu.d.mts → weeks-view-context-CkTcdZpj.d.cts} +33 -2
  37. package/package.json +1 -1
  38. package/src/calendar-provider.tsx +6 -0
  39. package/src/calendar-types.ts +2 -0
  40. package/src/day-cell.tsx +8 -1
  41. package/src/month-view-navigation.ts +180 -0
  42. package/src/month-view-types.ts +3 -0
  43. package/src/month-view.bounds.test.tsx +136 -0
  44. package/src/month-view.tsx +41 -217
  45. package/src/month-window.test.ts +207 -0
  46. package/src/month-window.ts +93 -0
  47. package/src/navigation.tsx +15 -74
  48. package/src/today.test.tsx +98 -0
  49. package/src/utils.test.ts +25 -0
  50. package/src/utils.ts +17 -0
  51. package/src/weeks-view-state.ts +8 -15
@@ -1,5 +1,5 @@
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";
@@ -7,20 +7,19 @@ import {
7
7
  MonthViewStableContext,
8
8
  MonthViewStateContext,
9
9
  } from "./month-view-context";
10
+ import { useMonthNavigation } from "./month-view-navigation";
10
11
  import type {
11
12
  MonthViewRootProps,
12
13
  MonthViewStableContextValue,
13
14
  MonthViewStateContextValue,
14
15
  } from "./month-view-types";
16
+ import { createMonthWindowPolicy } from "./month-window";
15
17
  import type { MonthData, RootState, ValueFormat } from "./types";
16
18
  import {
17
- computeAdjacentMonth,
18
- focusedDateForMonth,
19
19
  getMonthWeeks,
20
20
  resolveFocusTarget,
21
+ resolveInitialAnchor,
21
22
  selectedToZdt,
22
- shiftWindowToMonth,
23
- toZonedDateTime,
24
23
  } from "./utils";
25
24
  import {
26
25
  ViewContext,
@@ -57,42 +56,47 @@ function MonthViewRoot(props: MonthViewRootProps) {
57
56
  const calState = useCalendarState();
58
57
 
59
58
  const T = calStable.temporal;
60
- const { timeZone, weekStartDay, isDateDisabled } = calStable;
61
-
62
- // --- Month navigation state (controlled/uncontrolled) ---
63
- const isMonthControlled = monthProp !== undefined;
64
-
65
- const [internalMonth, setInternalMonth] = useState<{
66
- year: number;
67
- month: number;
68
- }>(() => {
69
- if (monthProp) return { year: monthProp.year, month: monthProp.month };
70
- if (defaultMonthProp)
71
- return { year: defaultMonthProp.year, month: defaultMonthProp.month };
72
- // Derive from selection or today
73
- if (calState.selected) {
74
- const zdt = toZonedDateTime(calState.selected, timeZone, T);
75
- return { year: zdt.year, month: zdt.month };
76
- }
77
- const now = T.Now.zonedDateTimeISO(timeZone);
78
- return { year: now.year, month: now.month };
79
- });
80
-
81
- const currentMonth = isMonthControlled
82
- ? { year: monthProp.year, month: monthProp.month }
83
- : internalMonth;
59
+ const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
84
60
 
85
61
  // --- Focus state ---
86
- const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() => {
87
- if (calState.selected) {
88
- return toZonedDateTime(calState.selected, timeZone, T).toPlainDate();
89
- }
90
- return T.Now.plainDateISO();
91
- });
62
+ const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() =>
63
+ resolveInitialAnchor(calState.selected, today, timeZone, T),
64
+ );
92
65
 
93
66
  const [gridHasFocus, setGridHasFocus] = useState(false);
94
67
  const gridFocusedRef = useRef(false);
95
68
 
69
+ // --- Month window and navigation ---
70
+ const { minValue, maxValue } = calStable;
71
+ const windowPolicy = useMemo(
72
+ () =>
73
+ createMonthWindowPolicy({
74
+ numberOfMonths,
75
+ behavior: outOfRangeBehavior,
76
+ min: minValue,
77
+ max: maxValue,
78
+ T,
79
+ }),
80
+ [numberOfMonths, outOfRangeBehavior, minValue, maxValue, T],
81
+ );
82
+
83
+ const initialAnchor = useCallback(
84
+ () => resolveInitialAnchor(calState.selected, today, timeZone, T),
85
+ [calState.selected, today, timeZone, T],
86
+ );
87
+
88
+ const { currentMonth, viewingYearMonth, goNextMonth, goPrevMonth } =
89
+ useMonthNavigation({
90
+ month: monthProp,
91
+ defaultMonth: defaultMonthProp,
92
+ initialAnchor,
93
+ onMonthChange,
94
+ policy: windowPolicy,
95
+ focusedDate,
96
+ setFocusedDate,
97
+ T,
98
+ });
99
+
96
100
  // --- Grid label IDs ---
97
101
  const [gridLabelIds, setGridLabelIds] = useState<Record<number, string>>({});
98
102
  const setGridLabelId = useCallback(
@@ -111,162 +115,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
111
115
  [],
112
116
  );
113
117
 
114
- // Always-current ref so callbacks/effects don't re-subscribe on every
115
- // onMonthChange identity change.
116
- const onMonthChangeRef = useRef(onMonthChange);
117
- onMonthChangeRef.current = onMonthChange;
118
-
119
- // Request the parent move the view to `{year, month}` (controlled mode).
120
- // Built in ISO — `year`/`month` are ISO numbers, and the locale calendar
121
- // only affects display (see MonthYearString). Passing `calendar` here would
122
- // reinterpret the ISO numbers as locale-calendar fields (e.g. Buddhist),
123
- // shifting the value by centuries.
124
- const notifyMonth = useCallback(
125
- (year: number, month: number) => {
126
- onMonthChangeRef.current?.(T.PlainYearMonth.from({ year, month }));
127
- },
128
- [T],
129
- );
130
-
131
- // Whether `{year, month}` falls within the currently displayed window.
132
- const isMonthVisible = useCallback(
133
- (targetYear: number, targetMonth: number) => {
134
- for (let i = 0; i < numberOfMonths; i++) {
135
- const totalMonths =
136
- currentMonth.year * 12 + (currentMonth.month - 1) + i;
137
- const y = Math.floor(totalMonths / 12);
138
- const m = (totalMonths % 12) + 1;
139
- if (targetYear === y && targetMonth === m) return true;
140
- }
141
- return false;
142
- },
143
- [currentMonth.year, currentMonth.month, numberOfMonths],
144
- );
145
-
146
- // --- navigateToMonth: shift the window by the minimum needed to reveal the
147
- // target month (keeps keyboard crossing symmetric with the Prev/Next
148
- // buttons, which step one month at a time). ---
149
- const navigateToMonth = useCallback(
150
- (targetYear: number, targetMonth: number) => {
151
- if (isMonthControlled) return; // controlled mode: parent manages
152
- setInternalMonth((prev) => {
153
- const next = shiftWindowToMonth(
154
- prev,
155
- { year: targetYear, month: targetMonth },
156
- numberOfMonths,
157
- );
158
- if (next.year === prev.year && next.month === prev.month) return prev;
159
- return next;
160
- });
161
- },
162
- [isMonthControlled, numberOfMonths],
163
- );
164
-
165
- // Button navigation in controlled mode notifies the parent directly, then
166
- // moves focus into the target month. This ref holds that focus value so the
167
- // focus-sync effect skips re-notifying for it. Keyed by the value (not a
168
- // one-shot flag) so it can't strand and suppress a later keyboard crossing
169
- // if the button's focus move is a no-op or the parent ignores the change.
170
- const skipFocusSyncForRef = useRef<Temporal.PlainDate | null>(null);
171
- const prevFocusedRef = useRef(focusedDate);
172
-
173
- // Clear a stale skip target whenever the parent actually moves the view.
174
- useEffect(() => {
175
- skipFocusSyncForRef.current = null;
176
- }, [monthProp?.year, monthProp?.month]);
177
-
178
- // Sync month when the focused date crosses a month boundary. Uncontrolled:
179
- // shift internal state. Controlled: ask the parent to move the view, but
180
- // only when focus actually moved out of the visible window (keyboard nav) —
181
- // never as an echo of an external `month` update or a button click.
182
- useEffect(() => {
183
- const prevFocused = prevFocusedRef.current;
184
- prevFocusedRef.current = focusedDate;
185
- if (isMonthControlled) {
186
- if (skipFocusSyncForRef.current === focusedDate) {
187
- skipFocusSyncForRef.current = null;
188
- return;
189
- }
190
- if (prevFocused === focusedDate) return;
191
- if (isMonthVisible(focusedDate.year, focusedDate.month)) return;
192
- // Emit the minimally-shifted first-visible month (what the parent binds
193
- // to `month`), not the focused month — so a controlled window scrolls by
194
- // the minimum, matching uncontrolled mode and the nav buttons.
195
- const next = shiftWindowToMonth(
196
- { year: currentMonth.year, month: currentMonth.month },
197
- { year: focusedDate.year, month: focusedDate.month },
198
- numberOfMonths,
199
- );
200
- notifyMonth(next.year, next.month);
201
- return;
202
- }
203
- navigateToMonth(focusedDate.year, focusedDate.month);
204
- }, [
205
- focusedDate,
206
- navigateToMonth,
207
- isMonthControlled,
208
- isMonthVisible,
209
- notifyMonth,
210
- currentMonth.year,
211
- currentMonth.month,
212
- numberOfMonths,
213
- ]);
214
-
215
- // --- Month navigation callbacks ---
216
- const goNextMonth = useCallback(() => {
217
- if (isMonthControlled) {
218
- const { year, month, firstDay } = computeAdjacentMonth(
219
- { year: monthProp!.year, month: monthProp!.month },
220
- "next",
221
- T,
222
- );
223
- // Notify the parent directly; the follow-on focus move is suppressed in
224
- // the focus-sync effect (keyed on the value) so onMonthChange fires once.
225
- const nextFocus = focusedDateForMonth(
226
- focusedDate,
227
- { year, month },
228
- firstDay,
229
- );
230
- skipFocusSyncForRef.current = nextFocus;
231
- notifyMonth(year, month);
232
- setFocusedDate(nextFocus);
233
- return;
234
- }
235
- setInternalMonth((m) => {
236
- const { year, month, firstDay } = computeAdjacentMonth(m, "next", T);
237
- setFocusedDate((prev) =>
238
- focusedDateForMonth(prev, { year, month }, firstDay),
239
- );
240
- return { year, month };
241
- });
242
- }, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
243
-
244
- const goPrevMonth = useCallback(() => {
245
- if (isMonthControlled) {
246
- const { year, month, firstDay } = computeAdjacentMonth(
247
- { year: monthProp!.year, month: monthProp!.month },
248
- "prev",
249
- T,
250
- );
251
- const nextFocus = focusedDateForMonth(
252
- focusedDate,
253
- { year, month },
254
- firstDay,
255
- );
256
- skipFocusSyncForRef.current = nextFocus;
257
- notifyMonth(year, month);
258
- setFocusedDate(nextFocus);
259
- return;
260
- }
261
- setInternalMonth((m) => {
262
- const { year, month, firstDay } = computeAdjacentMonth(m, "prev", T);
263
- setFocusedDate((prev) =>
264
- focusedDateForMonth(prev, { year, month }, firstDay),
265
- );
266
- return { year, month };
267
- });
268
- }, [T, isMonthControlled, monthProp, notifyMonth, focusedDate]);
269
-
270
118
  // --- Grid computation ---
271
119
  const allMonths = useMemo<MonthData[]>(() => {
272
120
  const opts = { weekStartDay, fixedWeeks };
@@ -348,32 +196,6 @@ function MonthViewRoot(props: MonthViewRootProps) {
348
196
  ],
349
197
  );
350
198
 
351
- // --- viewingYearMonth (for the render-prop rootState) ---
352
- // ISO — the locale calendar only affects display, not this value.
353
- const viewingYearMonth = useMemo(
354
- () =>
355
- T.PlainYearMonth.from({
356
- year: currentMonth.year,
357
- month: currentMonth.month,
358
- }),
359
- [currentMonth, T],
360
- );
361
-
362
- // --- Fire onMonthChange when uncontrolled navigation moves the view (not on
363
- // mount). In controlled mode the parent owns `month`, so notification flows
364
- // from button clicks (goNext/goPrevMonth) and the focus-sync effect instead;
365
- // firing here would just echo the parent's own update back to it. ---
366
- const mountedRef = useRef(false);
367
-
368
- useEffect(() => {
369
- if (!mountedRef.current) {
370
- mountedRef.current = true;
371
- return;
372
- }
373
- if (isMonthControlled) return;
374
- onMonthChangeRef.current?.(viewingYearMonth);
375
- }, [viewingYearMonth, isMonthControlled]);
376
-
377
199
  // --- rootState (for render functions) ---
378
200
  const rootState = useMemo<RootState>(
379
201
  () => ({
@@ -391,6 +213,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
391
213
  fixedWeeks,
392
214
  outsideDays,
393
215
  outOfRangeBehavior,
216
+ windowPolicy,
394
217
  goNextMonth,
395
218
  goPrevMonth,
396
219
  setGridLabelId,
@@ -401,6 +224,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
401
224
  fixedWeeks,
402
225
  outsideDays,
403
226
  outOfRangeBehavior,
227
+ windowPolicy,
404
228
  goNextMonth,
405
229
  goPrevMonth,
406
230
  setGridLabelId,
@@ -0,0 +1,207 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { describe, expect, it } from "vitest";
3
+ import { createMonthWindowPolicy } from "./month-window";
4
+ import type { MonthWindowPolicyInput } from "./month-window";
5
+
6
+ const T = Temporal;
7
+
8
+ const policy = (overrides: Partial<MonthWindowPolicyInput> = {}) =>
9
+ createMonthWindowPolicy({
10
+ numberOfMonths: 1,
11
+ behavior: "stop",
12
+ min: undefined,
13
+ max: undefined,
14
+ T,
15
+ ...overrides,
16
+ });
17
+
18
+ const date = (iso: string) => T.PlainDate.from(iso);
19
+
20
+ describe("MonthWindowPolicy.step", () => {
21
+ it.each([
22
+ {
23
+ description: "next across a year boundary",
24
+ start: { year: 2026, month: 12 },
25
+ direction: "next" as const,
26
+ expected: {
27
+ start: { year: 2027, month: 1 },
28
+ entering: { year: 2027, month: 1 },
29
+ firstDay: "2027-01-01",
30
+ blocked: false,
31
+ },
32
+ },
33
+ {
34
+ description: "prev across a year boundary",
35
+ start: { year: 2026, month: 1 },
36
+ direction: "prev" as const,
37
+ expected: {
38
+ start: { year: 2025, month: 12 },
39
+ entering: { year: 2025, month: 12 },
40
+ firstDay: "2025-12-01",
41
+ blocked: false,
42
+ },
43
+ },
44
+ {
45
+ description:
46
+ "next with two months visible enters the month after the window",
47
+ start: { year: 2026, month: 3 },
48
+ direction: "next" as const,
49
+ numberOfMonths: 2,
50
+ expected: {
51
+ start: { year: 2026, month: 4 },
52
+ entering: { year: 2026, month: 5 },
53
+ firstDay: "2026-04-01",
54
+ blocked: false,
55
+ },
56
+ },
57
+ ])("$description", ({ start, direction, numberOfMonths, expected }) => {
58
+ const step = policy({ numberOfMonths: numberOfMonths ?? 1 }).step(
59
+ start,
60
+ direction,
61
+ );
62
+ expect({ ...step, firstDay: step.firstDay.toString() }).toEqual(expected);
63
+ });
64
+
65
+ it.each([
66
+ {
67
+ description: "stop: prev into the min month is allowed (min mid-month)",
68
+ direction: "prev" as const,
69
+ start: { year: 2026, month: 4 },
70
+ min: "2026-03-20",
71
+ expected: false,
72
+ },
73
+ {
74
+ description: "stop: prev before the min month is blocked",
75
+ direction: "prev" as const,
76
+ start: { year: 2026, month: 3 },
77
+ min: "2026-03-20",
78
+ expected: true,
79
+ },
80
+ {
81
+ description: "stop: next into the max month is allowed",
82
+ direction: "next" as const,
83
+ start: { year: 2026, month: 2 },
84
+ max: "2026-03-05",
85
+ expected: false,
86
+ },
87
+ {
88
+ description: "stop: next past the max month is blocked",
89
+ direction: "next" as const,
90
+ start: { year: 2026, month: 3 },
91
+ max: "2026-03-05",
92
+ expected: true,
93
+ },
94
+ {
95
+ description: "stop: the window's last month is what must stay within max",
96
+ direction: "next" as const,
97
+ start: { year: 2026, month: 2 },
98
+ numberOfMonths: 2,
99
+ max: "2026-03-31",
100
+ expected: true,
101
+ },
102
+ {
103
+ description: "stop: min only never blocks next",
104
+ direction: "next" as const,
105
+ start: { year: 2030, month: 1 },
106
+ min: "2026-03-20",
107
+ expected: false,
108
+ },
109
+ {
110
+ description: "stop: max only never blocks prev",
111
+ direction: "prev" as const,
112
+ start: { year: 2020, month: 1 },
113
+ max: "2026-03-20",
114
+ expected: false,
115
+ },
116
+ {
117
+ description: "unbounded: never blocks past min",
118
+ direction: "prev" as const,
119
+ behavior: "unbounded" as const,
120
+ start: { year: 2026, month: 3 },
121
+ min: "2026-03-20",
122
+ expected: false,
123
+ },
124
+ {
125
+ description: "unbounded: never blocks past max",
126
+ direction: "next" as const,
127
+ behavior: "unbounded" as const,
128
+ start: { year: 2026, month: 3 },
129
+ max: "2026-03-05",
130
+ expected: false,
131
+ },
132
+ ])(
133
+ "$description",
134
+ ({ direction, start, min, max, numberOfMonths, behavior, expected }) => {
135
+ const blocked = policy({
136
+ numberOfMonths: numberOfMonths ?? 1,
137
+ behavior: behavior ?? "stop",
138
+ min: min ? date(min) : undefined,
139
+ max: max ? date(max) : undefined,
140
+ }).step(start, direction).blocked;
141
+ expect(blocked).toBe(expected);
142
+ },
143
+ );
144
+ });
145
+
146
+ describe("MonthWindowPolicy.isVisible", () => {
147
+ it.each([
148
+ {
149
+ description: "the first month",
150
+ target: { year: 2026, month: 3 },
151
+ expected: true,
152
+ },
153
+ {
154
+ description: "the last month",
155
+ target: { year: 2026, month: 5 },
156
+ expected: true,
157
+ },
158
+ {
159
+ description: "the month before the window",
160
+ target: { year: 2026, month: 2 },
161
+ expected: false,
162
+ },
163
+ {
164
+ description: "the month after the window",
165
+ target: { year: 2026, month: 6 },
166
+ expected: false,
167
+ },
168
+ {
169
+ description: "the same month number in another year",
170
+ target: { year: 2027, month: 3 },
171
+ expected: false,
172
+ },
173
+ ])("$description", ({ target, expected }) => {
174
+ expect(
175
+ policy({ numberOfMonths: 3 }).isVisible({ year: 2026, month: 3 }, target),
176
+ ).toBe(expected);
177
+ });
178
+ });
179
+
180
+ describe("MonthWindowPolicy.reveal", () => {
181
+ it.each([
182
+ {
183
+ description: "keeps the window when the target is visible",
184
+ target: { year: 2026, month: 4 },
185
+ expected: { year: 2026, month: 3 },
186
+ },
187
+ {
188
+ description: "target before the window becomes the first month",
189
+ target: { year: 2026, month: 1 },
190
+ expected: { year: 2026, month: 1 },
191
+ },
192
+ {
193
+ description: "target after the window becomes the last month",
194
+ target: { year: 2026, month: 8 },
195
+ expected: { year: 2026, month: 7 },
196
+ },
197
+ {
198
+ description: "target after the window across a year boundary",
199
+ target: { year: 2027, month: 2 },
200
+ expected: { year: 2027, month: 1 },
201
+ },
202
+ ])("$description", ({ target, expected }) => {
203
+ expect(
204
+ policy({ numberOfMonths: 2 }).reveal({ year: 2026, month: 3 }, target),
205
+ ).toEqual(expected);
206
+ });
207
+ });
@@ -0,0 +1,93 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import type { MonthOutOfRangeBehavior } from "./out-of-range";
3
+ import type { TemporalNamespace } from "./types";
4
+ import { computeAdjacentMonth, shiftWindowToMonth } from "./utils";
5
+
6
+ /** An ISO year + month. */
7
+ export interface MonthRef {
8
+ year: number;
9
+ month: number;
10
+ }
11
+
12
+ /** One step of the visible month window. */
13
+ export interface MonthStep {
14
+ /** The window's first month after the step. */
15
+ start: MonthRef;
16
+ /** The month that enters view (before the window for `prev`, after it for `next`). */
17
+ entering: MonthRef;
18
+ /** First day of `start`, the focus target when focus isn't already in `start`. */
19
+ firstDay: Temporal.PlainDate;
20
+ /** Whether `stop` forbids the step because `entering` lies beyond `min`/`max`. */
21
+ blocked: boolean;
22
+ }
23
+
24
+ /** The one seam for moving the MonthView window. */
25
+ export interface MonthWindowPolicy {
26
+ /** Steps the window one month towards `direction`. */
27
+ step(start: MonthRef, direction: "prev" | "next"): MonthStep;
28
+ /** Whether `target` is inside the window starting at `start`. */
29
+ isVisible(start: MonthRef, target: MonthRef): boolean;
30
+ /** The window start that reveals `target` with the smallest shift. */
31
+ reveal(start: MonthRef, target: MonthRef): MonthRef;
32
+ }
33
+
34
+ export interface MonthWindowPolicyInput {
35
+ numberOfMonths: number;
36
+ behavior: MonthOutOfRangeBehavior;
37
+ min: Temporal.PlainDate | undefined;
38
+ max: Temporal.PlainDate | undefined;
39
+ T: TemporalNamespace;
40
+ }
41
+
42
+ const monthIndex = (m: MonthRef): number => m.year * 12 + (m.month - 1);
43
+
44
+ /**
45
+ * Builds the policy for a MonthView window of `numberOfMonths` months.
46
+ *
47
+ * `unbounded` never blocks a step: `min`/`max` only gate which days are
48
+ * selectable, not which months can be viewed. Any other behavior is `stop`,
49
+ * which blocks a step whose entering month lies beyond the bound's month.
50
+ */
51
+ export function createMonthWindowPolicy(
52
+ input: MonthWindowPolicyInput,
53
+ ): MonthWindowPolicy {
54
+ const { numberOfMonths, behavior, min, max, T } = input;
55
+
56
+ return {
57
+ step(start, direction) {
58
+ const adjacent = computeAdjacentMonth(start, direction, T);
59
+ const newStart = { year: adjacent.year, month: adjacent.month };
60
+ const entering =
61
+ direction === "prev" ? newStart : monthAt(newStart, numberOfMonths - 1);
62
+
63
+ let blocked = false;
64
+ if (behavior !== "unbounded") {
65
+ const bound = direction === "prev" ? min : max;
66
+ if (bound) {
67
+ const diff = monthIndex(entering) - monthIndex(bound);
68
+ blocked = direction === "prev" ? diff < 0 : diff > 0;
69
+ }
70
+ }
71
+ return {
72
+ start: newStart,
73
+ entering,
74
+ firstDay: adjacent.firstDay,
75
+ blocked,
76
+ };
77
+ },
78
+
79
+ isVisible(start, target) {
80
+ const offset = monthIndex(target) - monthIndex(start);
81
+ return offset >= 0 && offset < numberOfMonths;
82
+ },
83
+
84
+ reveal(start, target) {
85
+ return shiftWindowToMonth(start, target, numberOfMonths);
86
+ },
87
+ };
88
+ }
89
+
90
+ function monthAt(start: MonthRef, offset: number): MonthRef {
91
+ const index = monthIndex(start) + offset;
92
+ return { year: Math.floor(index / 12), month: (index % 12) + 1 };
93
+ }