@klinking/colander 3.1.0-alpha.4 → 3.1.0-alpha.6

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 (55) 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/{weeks-view-context-CUCy8njs.d.cts → index-DnFCKyyw.d.cts} +71 -8
  30. package/dist/{weeks-view-context-E1NwyNgQ.d.mts → index-q3cQKJdA.d.mts} +71 -8
  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 -1
  35. package/dist/{weeks-navigation-elCpdfQ3.mjs → weeks-navigation-Dn3UtG76.mjs} +578 -589
  36. package/dist/{weeks-navigation-DV538__x.cjs → weeks-navigation-SuKJZJCM.cjs} +578 -589
  37. package/package.json +1 -1
  38. package/src/calendar-provider.tsx +10 -55
  39. package/src/day-cell-state.test.ts +214 -0
  40. package/src/day-cell-state.ts +314 -0
  41. package/src/day-cell.tsx +29 -383
  42. package/src/month-view-navigation.ts +180 -0
  43. package/src/month-view-types.ts +3 -0
  44. package/src/month-view.bounds.test.tsx +136 -0
  45. package/src/month-view.tsx +37 -205
  46. package/src/month-window.test.ts +207 -0
  47. package/src/month-window.ts +93 -0
  48. package/src/navigation.tsx +15 -74
  49. package/src/range-preview.test.ts +120 -0
  50. package/src/root-selection.ts +99 -97
  51. package/src/weeks-navigation.tsx +7 -32
  52. package/src/weeks-view-state.ts +24 -81
  53. package/src/weeks-view-types.ts +4 -1
  54. package/src/weeks-window.test.ts +43 -0
  55. package/src/weeks-window.ts +64 -0
package/src/day-cell.tsx CHANGED
@@ -2,209 +2,19 @@ import { mergeProps } from "@base-ui/react/merge-props";
2
2
  import { useRender } from "@base-ui/react/use-render";
3
3
  import type { Temporal as TemporalPoly } from "@js-temporal/polyfill";
4
4
  import { useContext, useEffect, useRef, memo, forwardRef } from "react";
5
- import { useCalendarStable, useCalendarState } from "./calendar-context";
6
- import { WeekDataContext, DayCellDataContext, GridContext } from "./context";
7
- import type { GridOrientation } from "./context";
5
+ import { useCalendarStable } from "./calendar-context";
6
+ import { WeekDataContext, DayCellDataContext } from "./context";
7
+ import { dayCellStateEqual, useDayCellDeriver } from "./day-cell-state";
8
+ import type { DayCellState } from "./day-cell-state";
8
9
  import { MonthViewStateContext } from "./month-view-context";
9
10
  import type { StateAttributesMapping } from "./types";
10
11
  import type {
11
12
  DayCellTemplateProps,
12
13
  DayButtonProps,
13
14
  DayCellTemplateState,
14
- TemporalNamespace,
15
- OutsideDays,
16
15
  } from "./types";
17
- import { shouldMoveDomFocus, isInRange as isInRangeUtil } from "./utils";
18
- import { useViewContext, useViewStable, useViewState } from "./view-context";
19
-
20
- /** Computes derived day cell state from context values. Pure function, no hooks. */
21
- export function computeDayCellState(
22
- date: TemporalPoly.PlainDate,
23
- orientation: GridOrientation,
24
- columnIndex: number,
25
- rootState: DayCellTemplateState["root"],
26
- selectedDates: TemporalPoly.PlainDate[],
27
- currentDateTime: { year: number; month: number },
28
- disabled: boolean,
29
- isDateDisabled: ((date: TemporalPoly.PlainDate) => boolean) | undefined,
30
- focusedDate: TemporalPoly.PlainDate,
31
- rangeStart: TemporalPoly.PlainDate | undefined,
32
- rangeEnd: TemporalPoly.PlainDate | undefined,
33
- tabTargetDate: TemporalPoly.PlainDate,
34
- today: TemporalPoly.PlainDate,
35
- T: TemporalNamespace,
36
- selectionMode: "single" | "range" | "multiple",
37
- outsideDays: OutsideDays,
38
- previewStart?: TemporalPoly.PlainDate,
39
- previewEnd?: TemporalPoly.PlainDate,
40
- ): DayCellTemplateState & { isTabTarget: boolean } {
41
- const isCurrentMonth =
42
- date.year === currentDateTime.year && date.month === currentDateTime.month;
43
- const hidden = !isCurrentMonth && outsideDays === "hidden";
44
-
45
- if (hidden) {
46
- return {
47
- root: rootState,
48
- date,
49
- columnIndex,
50
- orientation,
51
- outsideMonth: true,
52
- hidden: true,
53
- selected: false,
54
- today: false,
55
- disabled: true,
56
- focused: false,
57
- rangeStart: false,
58
- rangeEnd: false,
59
- rangeBoundary: false,
60
- inRange: false,
61
- rangeIndex: false,
62
- rangeLength: false,
63
- rangeHasStart: false,
64
- rangeHasEnd: false,
65
- rangePreviewStart: false,
66
- rangePreviewEnd: false,
67
- rangePreviewBoundary: false,
68
- rangePreviewInRange: false,
69
- rangePreviewIndex: false,
70
- rangePreviewLength: false,
71
- rangePreviewHasStart: false,
72
- rangePreviewHasEnd: false,
73
- isTabTarget: false,
74
- };
75
- }
76
-
77
- const isToday = T.PlainDate.compare(date, today) === 0;
78
- const isRange = selectionMode === "range";
79
-
80
- let isRangeStart: boolean = false;
81
- let isRangeEnd: boolean = false;
82
- let isInRangeDay: boolean = false;
83
- let rangeIdx: number | false = false;
84
- let rangeLen: number | false = false;
85
- let isPreviewRangeStart: boolean = false;
86
- let isPreviewRangeEnd: boolean = false;
87
- let isInPreviewRange: boolean = false;
88
- let previewIdx: number | false = false;
89
- let previewLen: number | false = false;
90
-
91
- if (isRange) {
92
- isRangeStart = rangeStart
93
- ? T.PlainDate.compare(date, rangeStart) === 0
94
- : false;
95
- isRangeEnd = rangeEnd ? T.PlainDate.compare(date, rangeEnd) === 0 : false;
96
- isInRangeDay = isInRangeUtil(date, rangeStart, rangeEnd, T) !== false;
97
- const effectiveStart = rangeStart ?? rangeEnd;
98
- const effectiveEnd = rangeEnd ?? rangeStart;
99
- rangeIdx =
100
- isInRangeDay && effectiveStart ? date.since(effectiveStart).days : false;
101
- rangeLen =
102
- isInRangeDay && effectiveStart && effectiveEnd
103
- ? effectiveEnd.since(effectiveStart).days + 1
104
- : false;
105
-
106
- isPreviewRangeStart = previewStart
107
- ? T.PlainDate.compare(date, previewStart) === 0
108
- : false;
109
- isPreviewRangeEnd = previewEnd
110
- ? T.PlainDate.compare(date, previewEnd) === 0
111
- : false;
112
- isInPreviewRange =
113
- previewStart && previewEnd
114
- ? isInRangeUtil(date, previewStart, previewEnd, T) !== false
115
- : false;
116
- previewIdx =
117
- isInPreviewRange && previewStart ? date.since(previewStart).days : false;
118
- previewLen =
119
- isInPreviewRange && previewStart && previewEnd
120
- ? previewEnd.since(previewStart).days + 1
121
- : false;
122
- }
123
-
124
- const outsideNonInteractive = !isCurrentMonth && outsideDays !== "enabled";
125
- const suppressRange =
126
- !isCurrentMonth && (outsideDays === "disabled" || outsideDays === "hidden");
127
-
128
- if (outsideNonInteractive) {
129
- return {
130
- root: rootState,
131
- date,
132
- columnIndex,
133
- orientation,
134
- outsideMonth: true,
135
- hidden: false,
136
- selected: false,
137
- today: isToday,
138
- disabled: true,
139
- focused: false,
140
- rangeStart: suppressRange ? false : isRangeStart,
141
- rangeEnd: suppressRange ? false : isRangeEnd,
142
- rangeBoundary: suppressRange ? false : isRangeStart || isRangeEnd,
143
- inRange: suppressRange ? false : isInRangeDay,
144
- rangeIndex: suppressRange ? false : rangeIdx,
145
- rangeLength: suppressRange ? false : rangeLen,
146
- rangeHasStart: suppressRange
147
- ? false
148
- : isInRangeDay && rangeStart !== undefined,
149
- rangeHasEnd: suppressRange
150
- ? false
151
- : isInRangeDay && rangeEnd !== undefined,
152
- rangePreviewStart: suppressRange ? false : isPreviewRangeStart,
153
- rangePreviewEnd: suppressRange ? false : isPreviewRangeEnd,
154
- rangePreviewBoundary: suppressRange
155
- ? false
156
- : isPreviewRangeStart || isPreviewRangeEnd,
157
- rangePreviewInRange: suppressRange ? false : isInPreviewRange,
158
- rangePreviewIndex: suppressRange ? false : previewIdx,
159
- rangePreviewLength: suppressRange ? false : previewLen,
160
- rangePreviewHasStart: suppressRange
161
- ? false
162
- : isInPreviewRange && previewStart !== undefined,
163
- rangePreviewHasEnd: suppressRange
164
- ? false
165
- : isInPreviewRange && previewEnd !== undefined,
166
- isTabTarget: false,
167
- };
168
- }
169
-
170
- const isSelected =
171
- selectionMode !== "range"
172
- ? selectedDates.some((d) => T.PlainDate.compare(d, date) === 0)
173
- : false;
174
- const isDisabled = disabled || (isDateDisabled?.(date) ?? false);
175
- const isFocused = T.PlainDate.compare(date, focusedDate) === 0;
176
- const isTabTarget = T.PlainDate.compare(date, tabTargetDate) === 0;
177
-
178
- return {
179
- root: rootState,
180
- date,
181
- columnIndex,
182
- orientation,
183
- selected: isSelected,
184
- today: isToday,
185
- disabled: isDisabled,
186
- outsideMonth: !isCurrentMonth,
187
- hidden: false,
188
- focused: isFocused,
189
- rangeStart: isRangeStart,
190
- rangeEnd: isRangeEnd,
191
- rangeBoundary: isRangeStart || isRangeEnd,
192
- inRange: isInRangeDay,
193
- rangeIndex: rangeIdx,
194
- rangeLength: rangeLen,
195
- rangeHasStart: isInRangeDay && rangeStart !== undefined,
196
- rangeHasEnd: isInRangeDay && rangeEnd !== undefined,
197
- rangePreviewStart: isPreviewRangeStart,
198
- rangePreviewEnd: isPreviewRangeEnd,
199
- rangePreviewBoundary: isPreviewRangeStart || isPreviewRangeEnd,
200
- rangePreviewInRange: isInPreviewRange,
201
- rangePreviewIndex: previewIdx,
202
- rangePreviewLength: previewLen,
203
- rangePreviewHasStart: isInPreviewRange && previewStart !== undefined,
204
- rangePreviewHasEnd: isInPreviewRange && previewEnd !== undefined,
205
- isTabTarget,
206
- };
207
- }
16
+ import { shouldMoveDomFocus } from "./utils";
17
+ import { useViewContext, useViewStable } from "./view-context";
208
18
 
209
19
  export const dayStateAttributesMapping = {
210
20
  root: () => null,
@@ -247,7 +57,7 @@ interface DayCellInstanceProps {
247
57
  date: TemporalPoly.PlainDate;
248
58
  columnIndex?: number | undefined;
249
59
  children?: React.ReactNode;
250
- _derivedState: DayCellTemplateState & { isTabTarget: boolean };
60
+ _derivedState: DayCellState;
251
61
  [key: string]: unknown;
252
62
  }
253
63
 
@@ -338,31 +148,8 @@ export function dayCellInstancePropsAreEqual(
338
148
  prev.columnIndex !== next.columnIndex
339
149
  )
340
150
  return false;
341
- const a = prev._derivedState;
342
- const b = next._derivedState;
343
151
  return (
344
- a.selected === b.selected &&
345
- a.today === b.today &&
346
- a.disabled === b.disabled &&
347
- a.outsideMonth === b.outsideMonth &&
348
- a.hidden === b.hidden &&
349
- a.focused === b.focused &&
350
- a.rangeStart === b.rangeStart &&
351
- a.rangeEnd === b.rangeEnd &&
352
- a.inRange === b.inRange &&
353
- a.rangeIndex === b.rangeIndex &&
354
- a.rangeLength === b.rangeLength &&
355
- a.rangeHasStart === b.rangeHasStart &&
356
- a.rangeHasEnd === b.rangeHasEnd &&
357
- a.rangePreviewStart === b.rangePreviewStart &&
358
- a.rangePreviewEnd === b.rangePreviewEnd &&
359
- a.rangePreviewBoundary === b.rangePreviewBoundary &&
360
- a.rangePreviewInRange === b.rangePreviewInRange &&
361
- a.rangePreviewIndex === b.rangePreviewIndex &&
362
- a.rangePreviewLength === b.rangePreviewLength &&
363
- a.rangePreviewHasStart === b.rangePreviewHasStart &&
364
- a.rangePreviewHasEnd === b.rangePreviewHasEnd &&
365
- a.isTabTarget === b.isTabTarget &&
152
+ dayCellStateEqual(prev._derivedState, next._derivedState) &&
366
153
  passThroughPropsEqual(prev, next, DAY_CELL_HANDLED_KEYS)
367
154
  );
368
155
  }
@@ -383,63 +170,16 @@ const DayCellInstance = memo(
383
170
  export function DayCellTemplate(props: DayCellTemplateProps) {
384
171
  const { date: dateProp, ...restProps } = props;
385
172
  const weekData = useContext(WeekDataContext);
386
-
387
- const {
388
- disabled,
389
- isDateDisabled,
390
- selectionMode,
391
- temporal: T,
392
- today,
393
- weekStartDay,
394
- } = useCalendarStable();
395
- const todayDate = today();
396
-
397
- const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } =
398
- useCalendarState();
399
-
400
- const viewCtx = useViewContext();
401
- const { focusedDate, tabTargetDate } = viewCtx;
402
- const { rootState, currentDateTime } = useViewState();
403
- const { outsideDays } = useViewStable();
404
173
  const monthState = useContext(MonthViewStateContext);
405
-
406
- // Use the grid-specific month for outsideMonth checks, falling back to currentMonth.
407
- // In multi-month mode, gridMonth is set by WeekTemplate from GridMonthContext.
408
- // In weeks view, cellMonth is computed per-day (see below) so outsideMonth is always false.
409
- const isWeeksView = viewCtx.viewType === "weeks";
410
- const cellMonth = isWeeksView
411
- ? null // per-day below
412
- : (weekData?.gridMonth ?? currentDateTime);
413
-
414
- const { orientation } = useContext(GridContext);
174
+ const derive = useDayCellDeriver();
415
175
 
416
176
  if (dateProp) {
417
- const daysInWeek = dateProp.daysInWeek;
418
- const colIdx =
419
- ((dateProp.dayOfWeek % daysInWeek) - weekStartDay + daysInWeek) %
420
- daysInWeek;
421
- const derived = computeDayCellState(
422
- dateProp,
423
- orientation,
424
- colIdx,
425
- rootState,
426
- selectedDates,
427
- isWeeksView ? { year: dateProp.year, month: dateProp.month } : cellMonth!,
428
- disabled,
429
- isDateDisabled,
430
- focusedDate,
431
- rangeStart,
432
- rangeEnd,
433
- tabTargetDate,
434
- todayDate,
435
- T,
436
- selectionMode,
437
- outsideDays,
438
- previewStart,
439
- previewEnd,
440
- );
441
177
  return (
442
- <DayCellInstance {...restProps} date={dateProp} _derivedState={derived} />
178
+ <DayCellInstance
179
+ {...restProps}
180
+ date={dateProp}
181
+ _derivedState={derive(dateProp)}
182
+ />
443
183
  );
444
184
  }
445
185
 
@@ -448,42 +188,15 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
448
188
 
449
189
  return (
450
190
  <>
451
- {days.map((day, i) => {
452
- const daysInWeek = day.daysInWeek;
453
- const colIdx = perWeek
454
- ? i
455
- : ((day.dayOfWeek % daysInWeek) - weekStartDay + daysInWeek) %
456
- daysInWeek;
457
- const derived = computeDayCellState(
458
- day,
459
- orientation,
460
- colIdx,
461
- rootState,
462
- selectedDates,
463
- isWeeksView ? { year: day.year, month: day.month } : cellMonth!,
464
- disabled,
465
- isDateDisabled,
466
- focusedDate,
467
- rangeStart,
468
- rangeEnd,
469
- tabTargetDate,
470
- todayDate,
471
- T,
472
- selectionMode,
473
- outsideDays,
474
- previewStart,
475
- previewEnd,
476
- );
477
- return (
478
- <DayCellInstance
479
- key={day.toString()}
480
- {...restProps}
481
- date={day}
482
- columnIndex={perWeek ? i : undefined}
483
- _derivedState={derived}
484
- />
485
- );
486
- })}
191
+ {days.map((day, i) => (
192
+ <DayCellInstance
193
+ key={day.toString()}
194
+ {...restProps}
195
+ date={day}
196
+ columnIndex={perWeek ? i : undefined}
197
+ _derivedState={derive(day, perWeek ? i : undefined)}
198
+ />
199
+ ))}
487
200
  </>
488
201
  );
489
202
  }
@@ -492,7 +205,7 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
492
205
  interface DayButtonInnerProps {
493
206
  render?: DayButtonProps["render"];
494
207
  date: TemporalPoly.PlainDate;
495
- _derivedState?: DayCellTemplateState & { isTabTarget: boolean };
208
+ _derivedState?: DayCellState;
496
209
  [key: string]: unknown;
497
210
  }
498
211
 
@@ -570,30 +283,7 @@ export function dayButtonInnerPropsAreEqual(
570
283
  const a = prev._derivedState;
571
284
  const b = next._derivedState;
572
285
  if (!a || !b) return a === b;
573
- return (
574
- a.selected === b.selected &&
575
- a.today === b.today &&
576
- a.disabled === b.disabled &&
577
- a.outsideMonth === b.outsideMonth &&
578
- a.hidden === b.hidden &&
579
- a.focused === b.focused &&
580
- a.rangeStart === b.rangeStart &&
581
- a.rangeEnd === b.rangeEnd &&
582
- a.inRange === b.inRange &&
583
- a.rangeIndex === b.rangeIndex &&
584
- a.rangeLength === b.rangeLength &&
585
- a.rangeHasStart === b.rangeHasStart &&
586
- a.rangeHasEnd === b.rangeHasEnd &&
587
- a.rangePreviewStart === b.rangePreviewStart &&
588
- a.rangePreviewEnd === b.rangePreviewEnd &&
589
- a.rangePreviewBoundary === b.rangePreviewBoundary &&
590
- a.rangePreviewInRange === b.rangePreviewInRange &&
591
- a.rangePreviewIndex === b.rangePreviewIndex &&
592
- a.rangePreviewLength === b.rangePreviewLength &&
593
- a.rangePreviewHasStart === b.rangePreviewHasStart &&
594
- a.rangePreviewHasEnd === b.rangePreviewHasEnd &&
595
- a.isTabTarget === b.isTabTarget
596
- );
286
+ return dayCellStateEqual(a, b);
597
287
  }
598
288
 
599
289
  // The `as any` casts are needed because DayButtonInnerProps uses an index
@@ -614,7 +304,7 @@ const DayButtonInner = memo(
614
304
  */
615
305
  function DayButtonFn(
616
306
  props: DayButtonProps & {
617
- _derivedState?: DayCellTemplateState & { isTabTarget: boolean };
307
+ _derivedState?: DayCellState;
618
308
  },
619
309
  ref: React.ForwardedRef<HTMLButtonElement>,
620
310
  ) {
@@ -646,7 +336,7 @@ function DayButtonFn(
646
336
 
647
337
  export const DayButton = forwardRef(DayButtonFn) as (
648
338
  props: DayButtonProps & {
649
- _derivedState?: DayCellTemplateState & { isTabTarget: boolean };
339
+ _derivedState?: DayCellState;
650
340
  } & React.RefAttributes<HTMLButtonElement>,
651
341
  ) => React.ReactElement | null;
652
342
 
@@ -656,58 +346,14 @@ function DayButtonFallbackFn(
656
346
  ref: React.ForwardedRef<HTMLButtonElement>,
657
347
  ) {
658
348
  const { date, ...restProps } = props;
659
- const { orientation } = useContext(GridContext);
660
- const weekData = useContext(WeekDataContext);
661
- const {
662
- disabled,
663
- isDateDisabled,
664
- selectionMode,
665
- temporal: T,
666
- today,
667
- weekStartDay,
668
- } = useCalendarStable();
669
- const todayDate = today();
670
- const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } =
671
- useCalendarState();
672
- const viewCtx = useViewContext();
673
- const { focusedDate, tabTargetDate } = viewCtx;
674
- const { rootState, currentDateTime } = useViewState();
675
- const { outsideDays } = useViewStable();
676
-
677
- const isWeeksView = viewCtx.viewType === "weeks";
678
- const cellMonth = isWeeksView
679
- ? { year: date.year, month: date.month }
680
- : (weekData?.gridMonth ?? currentDateTime);
681
- const daysInWeek = date.daysInWeek;
682
- const colIdx =
683
- ((date.dayOfWeek % daysInWeek) - weekStartDay + daysInWeek) % daysInWeek;
684
- const derived = computeDayCellState(
685
- date,
686
- orientation,
687
- colIdx,
688
- rootState,
689
- selectedDates,
690
- cellMonth,
691
- disabled,
692
- isDateDisabled,
693
- focusedDate,
694
- rangeStart,
695
- rangeEnd,
696
- tabTargetDate,
697
- todayDate,
698
- T,
699
- selectionMode,
700
- outsideDays,
701
- previewStart,
702
- previewEnd,
703
- );
349
+ const derive = useDayCellDeriver();
704
350
 
705
351
  return (
706
352
  <DayButtonInner
707
353
  {...restProps}
708
354
  ref={ref}
709
355
  date={date}
710
- _derivedState={derived}
356
+ _derivedState={derive(date)}
711
357
  />
712
358
  );
713
359
  }
@@ -0,0 +1,180 @@
1
+ import type { Temporal } from "@js-temporal/polyfill";
2
+ import type { Dispatch, SetStateAction } from "react";
3
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
4
+ import type { MonthRef, MonthWindowPolicy } from "./month-window";
5
+ import type { TemporalNamespace } from "./types";
6
+ import { focusedDateForMonth } from "./utils";
7
+
8
+ export interface UseMonthNavigationInput {
9
+ /** Controlled first visible month. */
10
+ month: MonthRef | undefined;
11
+ /** Initial first visible month when uncontrolled. */
12
+ defaultMonth: MonthRef | undefined;
13
+ /** Date the view opens on when neither `month` nor `defaultMonth` is given. */
14
+ initialAnchor: () => Temporal.PlainDate;
15
+ onMonthChange: ((month: Temporal.PlainYearMonth) => void) | undefined;
16
+ policy: MonthWindowPolicy;
17
+ focusedDate: Temporal.PlainDate;
18
+ setFocusedDate: Dispatch<SetStateAction<Temporal.PlainDate>>;
19
+ T: TemporalNamespace;
20
+ }
21
+
22
+ export interface MonthNavigation {
23
+ /** The first visible month. */
24
+ currentMonth: MonthRef;
25
+ /** The first visible month as an ISO `PlainYearMonth`. */
26
+ viewingYearMonth: Temporal.PlainYearMonth;
27
+ goNextMonth: () => void;
28
+ goPrevMonth: () => void;
29
+ }
30
+
31
+ /**
32
+ * Owns which month the view shows, in both controlled and uncontrolled mode:
33
+ * the month state, the Prev/Next steps, and syncing the window with focus.
34
+ *
35
+ * `onMonthChange` fires exactly once per move: from the effect below when
36
+ * uncontrolled, and from the step or the focus-sync effect when controlled.
37
+ */
38
+ export function useMonthNavigation(
39
+ input: UseMonthNavigationInput,
40
+ ): MonthNavigation {
41
+ const {
42
+ month,
43
+ defaultMonth,
44
+ initialAnchor,
45
+ onMonthChange,
46
+ policy,
47
+ focusedDate,
48
+ setFocusedDate,
49
+ T,
50
+ } = input;
51
+
52
+ const isControlled = month !== undefined;
53
+
54
+ const [internalMonth, setInternalMonth] = useState<MonthRef>(() => {
55
+ if (month) return { year: month.year, month: month.month };
56
+ if (defaultMonth)
57
+ return { year: defaultMonth.year, month: defaultMonth.month };
58
+ const anchor = initialAnchor();
59
+ return { year: anchor.year, month: anchor.month };
60
+ });
61
+
62
+ const year = isControlled ? month.year : internalMonth.year;
63
+ const monthNumber = isControlled ? month.month : internalMonth.month;
64
+ const currentMonth = useMemo<MonthRef>(
65
+ () => ({ year, month: monthNumber }),
66
+ [year, monthNumber],
67
+ );
68
+
69
+ // ISO — the locale calendar only affects display, not this value.
70
+ const viewingYearMonth = useMemo(
71
+ () => T.PlainYearMonth.from({ year, month: monthNumber }),
72
+ [year, monthNumber, T],
73
+ );
74
+
75
+ // Always-current ref so callbacks/effects don't re-subscribe on every
76
+ // onMonthChange identity change.
77
+ const onMonthChangeRef = useRef(onMonthChange);
78
+ onMonthChangeRef.current = onMonthChange;
79
+
80
+ // Request the parent move the view to `{year, month}` (controlled mode).
81
+ // Built in ISO — `year`/`month` are ISO numbers, and the locale calendar
82
+ // only affects display (see MonthYearString). Passing `calendar` here would
83
+ // reinterpret the ISO numbers as locale-calendar fields (e.g. Buddhist),
84
+ // shifting the value by centuries.
85
+ const notifyMonth = useCallback(
86
+ (target: MonthRef) => {
87
+ onMonthChangeRef.current?.(T.PlainYearMonth.from(target));
88
+ },
89
+ [T],
90
+ );
91
+
92
+ // Button navigation in controlled mode notifies the parent directly, then
93
+ // moves focus into the target month. This ref holds that focus value so the
94
+ // focus-sync effect skips re-notifying for it. Keyed by the value (not a
95
+ // one-shot flag) so it can't strand and suppress a later keyboard crossing
96
+ // if the button's focus move is a no-op or the parent ignores the change.
97
+ const skipFocusSyncForRef = useRef<Temporal.PlainDate | null>(null);
98
+ const prevFocusedRef = useRef(focusedDate);
99
+
100
+ // Clear a stale skip target whenever the parent actually moves the view.
101
+ useEffect(() => {
102
+ skipFocusSyncForRef.current = null;
103
+ }, [month?.year, month?.month]);
104
+
105
+ // Sync month when the focused date crosses a month boundary. Uncontrolled:
106
+ // shift internal state. Controlled: ask the parent to move the view, but
107
+ // only when focus actually moved out of the visible window (keyboard nav) —
108
+ // never as an echo of an external `month` update or a button click.
109
+ useEffect(() => {
110
+ const prevFocused = prevFocusedRef.current;
111
+ prevFocusedRef.current = focusedDate;
112
+ const target = { year: focusedDate.year, month: focusedDate.month };
113
+ if (isControlled) {
114
+ if (skipFocusSyncForRef.current === focusedDate) {
115
+ skipFocusSyncForRef.current = null;
116
+ return;
117
+ }
118
+ if (prevFocused === focusedDate) return;
119
+ if (policy.isVisible(currentMonth, target)) return;
120
+ // Emit the minimally-shifted first-visible month (what the parent binds
121
+ // to `month`), not the focused month — so a controlled window scrolls by
122
+ // the minimum, matching uncontrolled mode and the nav buttons.
123
+ notifyMonth(policy.reveal(currentMonth, target));
124
+ return;
125
+ }
126
+ setInternalMonth((prev) => {
127
+ const next = policy.reveal(prev, target);
128
+ if (next.year === prev.year && next.month === prev.month) return prev;
129
+ return next;
130
+ });
131
+ }, [focusedDate, isControlled, policy, currentMonth, notifyMonth]);
132
+
133
+ // Fire onMonthChange when uncontrolled navigation moves the view (not on
134
+ // mount). In controlled mode the parent owns `month`, so notification flows
135
+ // from the steps and the focus-sync effect instead; firing here would just
136
+ // echo the parent's own update back to it.
137
+ const mountedRef = useRef(false);
138
+ useEffect(() => {
139
+ if (!mountedRef.current) {
140
+ mountedRef.current = true;
141
+ return;
142
+ }
143
+ if (isControlled) return;
144
+ onMonthChangeRef.current?.(viewingYearMonth);
145
+ }, [viewingYearMonth, isControlled]);
146
+
147
+ const go = useCallback(
148
+ (direction: "prev" | "next") => {
149
+ if (isControlled) {
150
+ const step = policy.step(month, direction);
151
+ if (step.blocked) return;
152
+ // Notify the parent directly; the follow-on focus move is suppressed in
153
+ // the focus-sync effect (keyed on the value) so onMonthChange fires once.
154
+ const nextFocus = focusedDateForMonth(
155
+ focusedDate,
156
+ step.start,
157
+ step.firstDay,
158
+ );
159
+ skipFocusSyncForRef.current = nextFocus;
160
+ notifyMonth(step.start);
161
+ setFocusedDate(nextFocus);
162
+ return;
163
+ }
164
+ setInternalMonth((m) => {
165
+ const step = policy.step(m, direction);
166
+ if (step.blocked) return m;
167
+ setFocusedDate((prev) =>
168
+ focusedDateForMonth(prev, step.start, step.firstDay),
169
+ );
170
+ return step.start;
171
+ });
172
+ },
173
+ [isControlled, month, policy, notifyMonth, focusedDate, setFocusedDate],
174
+ );
175
+
176
+ const goNextMonth = useCallback(() => go("next"), [go]);
177
+ const goPrevMonth = useCallback(() => go("prev"), [go]);
178
+
179
+ return { currentMonth, viewingYearMonth, goNextMonth, goPrevMonth };
180
+ }
@@ -1,4 +1,5 @@
1
1
  import type { Temporal } from "@js-temporal/polyfill";
2
+ import type { MonthWindowPolicy } from "./month-window";
2
3
  import type { MonthOutOfRangeBehavior } from "./out-of-range";
3
4
  import type { MonthData, OutsideDays, RootState } from "./types";
4
5
 
@@ -71,6 +72,8 @@ export interface MonthViewStableContextValue {
71
72
  outsideDays: OutsideDays;
72
73
  /** How month navigation behaves at bounds. */
73
74
  outOfRangeBehavior: MonthOutOfRangeBehavior;
75
+ /** Steps and reveals the visible month window. */
76
+ windowPolicy: MonthWindowPolicy;
74
77
  /** Navigate to the next month(s). */
75
78
  goNextMonth: () => void;
76
79
  /** Navigate to the previous month(s). */