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

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 (36) hide show
  1. package/dist/formats/date.cjs +1 -1
  2. package/dist/formats/date.mjs +1 -1
  3. package/dist/formats/object.cjs +1 -1
  4. package/dist/formats/object.mjs +1 -1
  5. package/dist/formats/plain-date-time.cjs +1 -1
  6. package/dist/formats/plain-date-time.mjs +1 -1
  7. package/dist/formats/plain-date.cjs +1 -1
  8. package/dist/formats/plain-date.mjs +1 -1
  9. package/dist/formats/plain-month-day.cjs +1 -1
  10. package/dist/formats/plain-month-day.mjs +1 -1
  11. package/dist/formats/plain-year-month.cjs +1 -1
  12. package/dist/formats/plain-year-month.mjs +1 -1
  13. package/dist/formats/zoned-date-time.cjs +1 -1
  14. package/dist/formats/zoned-date-time.mjs +1 -1
  15. package/dist/index.cjs +1 -1
  16. package/dist/index.mjs +1 -1
  17. package/dist/{weeks-navigation-Bg0nZLgh.mjs → weeks-navigation-DEaASUAh.mjs} +125 -120
  18. package/dist/{weeks-navigation-CJzhxbgj.cjs → weeks-navigation-DvryOSWF.cjs} +125 -120
  19. package/package.json +1 -1
  20. package/src/calendar-provider.test.tsx +87 -0
  21. package/src/calendar-provider.tsx +65 -16
  22. package/src/day-cell.tsx +9 -15
  23. package/src/drag-handle.tsx +2 -2
  24. package/src/grid-header.tsx +3 -3
  25. package/src/grid.tsx +20 -61
  26. package/src/month-view.tsx +25 -2
  27. package/src/navigation.tsx +3 -2
  28. package/src/range-preview.tsx +3 -5
  29. package/src/selected-range.tsx +3 -5
  30. package/src/view-context.test.tsx +126 -0
  31. package/src/view-context.ts +43 -0
  32. package/src/week-number.tsx +3 -3
  33. package/src/weeks-navigation.tsx +3 -3
  34. package/src/weeks-view-state.ts +23 -2
  35. package/src/weeks-view.test.tsx +115 -13
  36. package/src/weeks-view.tsx +22 -51
package/src/day-cell.tsx CHANGED
@@ -5,8 +5,7 @@ import { useContext, useEffect, useRef, memo, forwardRef } from "react";
5
5
  import { useCalendarStable, useCalendarState } from "./calendar-context";
6
6
  import { WeekDataContext, DayCellDataContext, GridContext } from "./context";
7
7
  import type { GridOrientation } from "./context";
8
- import { MonthViewStableContext } from "./month-view-context";
9
- import { useMonthViewState } from "./month-view-context";
8
+ import { MonthViewStateContext } from "./month-view-context";
10
9
  import type { StateAttributesMapping } from "./types";
11
10
  import type {
12
11
  DayCellTemplateProps,
@@ -16,7 +15,7 @@ import type {
16
15
  OutsideDays,
17
16
  } from "./types";
18
17
  import { shouldMoveDomFocus, isInRange as isInRangeUtil } from "./utils";
19
- import { useViewContext } from "./view-context";
18
+ import { useViewContext, useViewStable, useViewState } from "./view-context";
20
19
 
21
20
  /** Computes derived day cell state from context values. Pure function, no hooks. */
22
21
  export function computeDayCellState(
@@ -398,11 +397,9 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
398
397
 
399
398
  const viewCtx = useViewContext();
400
399
  const { focusedDate, tabTargetDate } = viewCtx;
401
-
402
- const monthStable = useContext(MonthViewStableContext);
403
- const monthState = useMonthViewState();
404
- const { rootState, weeks, currentMonth: currentDateTime } = monthState;
405
- const outsideDays = monthStable?.outsideDays ?? "enabled";
400
+ const { rootState, currentDateTime } = useViewState();
401
+ const { outsideDays } = useViewStable();
402
+ const monthState = useContext(MonthViewStateContext);
406
403
 
407
404
  // Use the grid-specific month for outsideMonth checks, falling back to currentMonth.
408
405
  // In multi-month mode, gridMonth is set by WeekTemplate from GridMonthContext.
@@ -443,7 +440,7 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
443
440
  );
444
441
  }
445
442
 
446
- const days = weekData ? weekData.days : weeks.flat();
443
+ const days = weekData ? weekData.days : (monthState?.weeks.flat() ?? []);
447
444
  const perWeek = weekData != null;
448
445
 
449
446
  return (
@@ -502,8 +499,7 @@ function DayButtonInnerFn(
502
499
  const { render, date, _derivedState, ...otherProps } = props;
503
500
  const { onSelect, locale, readOnly, setHoveredDate } = useCalendarStable();
504
501
  const { setFocusedDate } = useViewContext();
505
- const monthStable = useContext(MonthViewStableContext);
506
- const gridFocusedRef = monthStable!.gridFocusedRef;
502
+ const { gridFocusedRef } = useViewStable();
507
503
 
508
504
  const internalRef = useRef<HTMLButtonElement>(null);
509
505
 
@@ -669,10 +665,8 @@ function DayButtonFallbackFn(
669
665
  useCalendarState();
670
666
  const viewCtx = useViewContext();
671
667
  const { focusedDate, tabTargetDate } = viewCtx;
672
- const monthStable = useContext(MonthViewStableContext);
673
- const monthState = useMonthViewState();
674
- const { rootState, currentMonth: currentDateTime } = monthState;
675
- const outsideDays = monthStable?.outsideDays ?? "enabled";
668
+ const { rootState, currentDateTime } = useViewState();
669
+ const { outsideDays } = useViewStable();
676
670
 
677
671
  const isWeeksView = viewCtx.viewType === "weeks";
678
672
  const cellMonth = isWeeksView
@@ -3,7 +3,6 @@ import { useRender } from "@base-ui/react/use-render";
3
3
  import { forwardRef, useContext, useMemo, useRef } from "react";
4
4
  import { useCalendarStable, useCalendarState } from "./calendar-context";
5
5
  import { DayCellDataContext, GridContext } from "./context";
6
- import { useMonthViewState } from "./month-view-context";
7
6
  import type { StateAttributesMapping } from "./types";
8
7
  import type {
9
8
  DragHandleState,
@@ -11,6 +10,7 @@ import type {
11
10
  RangeEndDragHandleProps,
12
11
  RangeDragHandleProps,
13
12
  } from "./types";
13
+ import { useViewState } from "./view-context";
14
14
 
15
15
  const dragHandleStateAttributesMapping = {
16
16
  root: () => null,
@@ -31,7 +31,7 @@ function useDragHandle(
31
31
  disabled,
32
32
  } = useCalendarStable();
33
33
  const { rangeStart, rangeEnd } = useCalendarState();
34
- const { rootState } = useMonthViewState();
34
+ const { rootState } = useViewState();
35
35
  const cellData = useContext(DayCellDataContext);
36
36
  const { orientation } = useContext(GridContext);
37
37
  const date = cellData?.date;
@@ -2,7 +2,6 @@ import { mergeProps } from "@base-ui/react/merge-props";
2
2
  import { useRender } from "@base-ui/react/use-render";
3
3
  import { forwardRef, useMemo } from "react";
4
4
  import { useCalendarStable } from "./calendar-context";
5
- import { useMonthViewState } from "./month-view-context";
6
5
  import type { StateAttributesMapping } from "./types";
7
6
  import type {
8
7
  GridHeaderCellProps,
@@ -11,10 +10,11 @@ import type {
11
10
  GridHeaderCellState,
12
11
  } from "./types";
13
12
  import { getWeekdayNames, getReferenceWeekStart } from "./utils";
13
+ import { useViewState } from "./view-context";
14
14
 
15
15
  function useGridHeaderCellState(index: number) {
16
16
  const { locale, temporal: T, weekStartDay } = useCalendarStable();
17
- const { rootState } = useMonthViewState();
17
+ const { rootState } = useViewState();
18
18
 
19
19
  const weekdayNames = useMemo(
20
20
  () => getWeekdayNames(locale, T, weekStartDay),
@@ -120,7 +120,7 @@ function GridHeaderFn(
120
120
  ref: React.ForwardedRef<HTMLTableSectionElement>,
121
121
  ) {
122
122
  const { render, children, ...otherProps } = props;
123
- const { rootState } = useMonthViewState();
123
+ const { rootState } = useViewState();
124
124
 
125
125
  const state = useMemo<GridHeaderState>(
126
126
  () => ({ root: rootState as unknown as GridHeaderState["root"] }),
package/src/grid.tsx CHANGED
@@ -1,6 +1,5 @@
1
1
  import { mergeProps } from "@base-ui/react/merge-props";
2
2
  import { useRender } from "@base-ui/react/use-render";
3
- import type { Temporal } from "@js-temporal/polyfill";
4
3
  import {
5
4
  useContext,
6
5
  useMemo,
@@ -15,7 +14,6 @@ import { WeekDataContext, GridContext, GridMonthContext } from "./context";
15
14
  import { GridHeader, GridHeaderCell } from "./grid-header";
16
15
  import { computeNextFocusDate } from "./keyboard";
17
16
  import {
18
- MonthViewStableContext,
19
17
  MonthViewStateContext,
20
18
  useMonthViewStable,
21
19
  useMonthViewState,
@@ -29,14 +27,10 @@ import type {
29
27
  WeekTemplateState,
30
28
  WeekTemplateProps,
31
29
  } from "./types";
32
- import { useViewContext } from "./view-context";
30
+ import { ViewContext, useViewContext, useViewState } from "./view-context";
33
31
  import { WeeksGrid } from "./weeks-grid";
34
32
  import { computeWeeksKeyNav } from "./weeks-keyboard";
35
- import {
36
- WeeksViewStateContext,
37
- useWeeksViewState,
38
- useWeeksViewStable,
39
- } from "./weeks-view-context";
33
+ import { useWeeksViewState, useWeeksViewStable } from "./weeks-view-context";
40
34
  export { GridHeader, GridHeaderCell } from "./grid-header";
41
35
  export { DayCellTemplate, DayButton } from "./day-cell";
42
36
 
@@ -177,17 +171,19 @@ const gridStateAttributesMapping = {
177
171
  * delegates to the appropriate internal renderer.
178
172
  */
179
173
  function GridFn(props: GridProps, ref: React.ForwardedRef<HTMLTableElement>) {
180
- const monthState = useContext(MonthViewStableContext);
181
- const weeksState = useContext(WeeksViewStateContext);
174
+ const view = useContext(ViewContext);
182
175
 
183
- if (monthState) {
184
- return <MonthGrid ref={ref} {...props} />;
185
- }
186
- if (weeksState) {
187
- return <WeeksViewGrid ref={ref} {...props} />;
176
+ if (!view) {
177
+ throw new Error(
178
+ "Grid must be used inside MonthView.Root or WeeksView.Root.",
179
+ );
188
180
  }
189
181
 
190
- throw new Error("Grid must be used inside MonthView.Root or WeeksView.Root.");
182
+ return view.viewType === "weeks" ? (
183
+ <WeeksViewGrid ref={ref} {...props} />
184
+ ) : (
185
+ <MonthGrid ref={ref} {...props} />
186
+ );
191
187
  }
192
188
 
193
189
  export const Grid = forwardRef(GridFn) as (
@@ -361,7 +357,7 @@ function WeeksViewGridFn(
361
357
  const weeksStable = useWeeksViewStable();
362
358
  const weeksState = useWeeksViewState();
363
359
  const { gridFocusedRef } = weeksStable;
364
- const { gridLabelIds, weeks, currentDateTime } = weeksState;
360
+ const { gridLabelIds, weeks } = weeksState;
365
361
 
366
362
  const handleKeyDown = useWeeksGridKeyboard();
367
363
  const gridRef = useRef<HTMLTableElement>(null);
@@ -453,45 +449,8 @@ function WeeksViewGridFn(
453
449
  [resolvedOrientation],
454
450
  );
455
451
 
456
- // Provide a MonthViewStableContext shim so shared components
457
- // (DayCellTemplate, DayButton, etc.) that read it can function in WeeksView.
458
- const monthViewStableShim = useMemo(
459
- () => ({
460
- numberOfMonths: 1,
461
- fixedWeeks: false,
462
- outsideDays: "enabled" as const,
463
- outOfRangeBehavior: "unbounded" as const,
464
- goNextMonth: () => {},
465
- goPrevMonth: () => {},
466
- setGridLabelId: () => {},
467
- gridFocusedRef,
468
- }),
469
- [gridFocusedRef],
470
- );
471
-
472
- // Provide a MonthViewStateContext shim with reasonable defaults derived
473
- // from the WeeksView state so WeekTemplate/DayCellTemplate/DayButton work.
474
- const monthViewStateShim = useMemo(
475
- () => ({
476
- currentMonth: {
477
- year: currentDateTime.year,
478
- month: currentDateTime.month,
479
- },
480
- weeks: [] as Temporal.PlainDate[][],
481
- allMonths: [],
482
- currentDateTime,
483
- gridLabelIds,
484
- rootState: weeksState.rootState,
485
- }),
486
- [currentDateTime, gridLabelIds, weeksState.rootState],
487
- );
488
-
489
452
  return (
490
- <MonthViewStableContext.Provider value={monthViewStableShim}>
491
- <MonthViewStateContext.Provider value={monthViewStateShim}>
492
- <GridContext.Provider value={orientationCtx}>{el}</GridContext.Provider>
493
- </MonthViewStateContext.Provider>
494
- </MonthViewStableContext.Provider>
453
+ <GridContext.Provider value={orientationCtx}>{el}</GridContext.Provider>
495
454
  );
496
455
  }
497
456
 
@@ -508,11 +467,11 @@ function GridBodyFn(
508
467
  props: GridBodyProps,
509
468
  ref: React.ForwardedRef<HTMLTableSectionElement>,
510
469
  ) {
511
- const weeksState = useContext(WeeksViewStateContext);
470
+ const view = useContext(ViewContext);
512
471
 
513
472
  // In WeeksView, delegate to WeeksGrid which handles week iteration
514
473
  // and MonthSeparator context provision.
515
- if (weeksState) {
474
+ if (view?.viewType === "weeks") {
516
475
  const { children, className, style } = props as any;
517
476
  return (
518
477
  <WeeksGrid className={className} style={style}>
@@ -533,7 +492,7 @@ function MonthGridBodyFn(
533
492
  ref: React.ForwardedRef<HTMLTableSectionElement>,
534
493
  ) {
535
494
  const { render, ...otherProps } = props;
536
- const { rootState } = useMonthViewState();
495
+ const { rootState } = useViewState();
537
496
 
538
497
  const state = useMemo<GridBodyState>(
539
498
  () => ({ root: rootState as unknown as GridBodyState["root"] }),
@@ -575,7 +534,7 @@ const weekInstanceStateAttributesMapping = {
575
534
  function WeekInstance(props: WeekTemplateProps) {
576
535
  const { render, ...otherProps } = props;
577
536
  const weekData = useContext(WeekDataContext)!;
578
- const { rootState } = useMonthViewState();
537
+ const { rootState } = useViewState();
579
538
 
580
539
  const state = useMemo<WeekTemplateState>(
581
540
  () => ({
@@ -603,8 +562,8 @@ function WeekInstance(props: WeekTemplateProps) {
603
562
  export function WeekTemplate(props: WeekTemplateProps) {
604
563
  const outerWeekData = useContext(WeekDataContext);
605
564
  const gridMonthCtx = useContext(GridMonthContext);
606
- const { weeks: defaultWeeks } = useMonthViewState();
607
- const weeks = gridMonthCtx?.weeks ?? defaultWeeks;
565
+ const monthState = useContext(MonthViewStateContext);
566
+ const weeks = gridMonthCtx?.weeks ?? monthState?.weeks ?? [];
608
567
  const gridMonth = useMemo(
609
568
  () =>
610
569
  gridMonthCtx
@@ -22,7 +22,14 @@ import {
22
22
  shiftWindowToMonth,
23
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
@@ -424,10 +431,26 @@ function MonthViewRoot(props: MonthViewRootProps) {
424
431
  [focusedDate, tabTargetDate, gridHasFocus],
425
432
  );
426
433
 
434
+ const viewStableCtx = useMemo<ViewStableContextValue>(
435
+ () => ({ gridFocusedRef, outsideDays }),
436
+ [outsideDays],
437
+ );
438
+
439
+ const viewStateCtx = useMemo<ViewStateContextValue>(
440
+ () => ({ currentDateTime, rootState }),
441
+ [currentDateTime, rootState],
442
+ );
443
+
427
444
  return (
428
445
  <MonthViewStableContext.Provider value={stableCtx}>
429
446
  <MonthViewStateContext.Provider value={stateCtx}>
430
- <ViewContext.Provider value={viewCtx}>{children}</ViewContext.Provider>
447
+ <ViewStableContext.Provider value={viewStableCtx}>
448
+ <ViewStateContext.Provider value={viewStateCtx}>
449
+ <ViewContext.Provider value={viewCtx}>
450
+ {children}
451
+ </ViewContext.Provider>
452
+ </ViewStateContext.Provider>
453
+ </ViewStableContext.Provider>
431
454
  </MonthViewStateContext.Provider>
432
455
  </MonthViewStableContext.Provider>
433
456
  );
@@ -16,6 +16,7 @@ import type {
16
16
  NavButtonState,
17
17
  } from "./types";
18
18
  import { selectedToZdt, zdtToNativeDate } from "./utils";
19
+ import { useViewState } from "./view-context";
19
20
 
20
21
  const dateStringStateAttributesMapping = {
21
22
  root: () => null,
@@ -44,7 +45,7 @@ function DateStringFn(
44
45
  const { render, locales, options, ...otherProps } = props;
45
46
  const { locale, temporal: T, timeZone } = useCalendarStable();
46
47
  const { selected } = useCalendarState();
47
- const { currentDateTime, rootState } = useMonthViewState();
48
+ const { currentDateTime, rootState } = useViewState();
48
49
 
49
50
  const selectedZdt = selectedToZdt(selected, timeZone, T);
50
51
  const displayDate = selectedZdt
@@ -97,7 +98,7 @@ function TimeStringFn(
97
98
  const { render, locales, options, ...otherProps } = props;
98
99
  const { locale, temporal: T, timeZone } = useCalendarStable();
99
100
  const { selected } = useCalendarState();
100
- const { rootState } = useMonthViewState();
101
+ const { rootState } = useViewState();
101
102
 
102
103
  const selZdt = selectedToZdt(selected, timeZone, T);
103
104
  const displayDate = selZdt
@@ -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;
@@ -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;
@@ -0,0 +1,126 @@
1
+ // @vitest-environment jsdom
2
+ import { Temporal } from "@js-temporal/polyfill";
3
+ import { act, fireEvent, render, screen } from "@testing-library/react";
4
+ import type { ReactNode } from "react";
5
+ import { describe, expect, it } from "vitest";
6
+ import {
7
+ DayButton,
8
+ DayCellTemplate,
9
+ Grid,
10
+ GridBody,
11
+ WeekTemplate,
12
+ } from "./grid";
13
+ import { MonthView } from "./month-view";
14
+ import { useViewStable, useViewState } from "./view-context";
15
+ import { WeeksView } from "./weeks-view";
16
+
17
+ const T = Temporal;
18
+
19
+ const counts = { stable: 0, state: 0 };
20
+
21
+ function StableProbe() {
22
+ useViewStable();
23
+ counts.stable++;
24
+ return null;
25
+ }
26
+
27
+ function StateProbe() {
28
+ useViewState();
29
+ counts.state++;
30
+ return null;
31
+ }
32
+
33
+ const grid = (
34
+ <Grid>
35
+ <GridBody>
36
+ <WeekTemplate>
37
+ <DayCellTemplate>
38
+ <DayButton />
39
+ </DayCellTemplate>
40
+ </WeekTemplate>
41
+ </GridBody>
42
+ </Grid>
43
+ );
44
+
45
+ const views: {
46
+ description: string;
47
+ view: (children: ReactNode) => ReactNode;
48
+ }[] = [
49
+ {
50
+ description: "MonthView",
51
+ view: (children) => (
52
+ <MonthView
53
+ temporal={T}
54
+ defaultValue={T.PlainDate.from("2026-03-15")}
55
+ defaultMonth={T.PlainYearMonth.from("2026-03")}
56
+ >
57
+ {children}
58
+ </MonthView>
59
+ ),
60
+ },
61
+ {
62
+ description: "WeeksView",
63
+ view: (children) => (
64
+ <WeeksView
65
+ temporal={T}
66
+ weekCount={4}
67
+ defaultFirstWeek={T.PlainDate.from("2026-03-01")}
68
+ defaultValue={T.PlainDate.from("2026-03-15")}
69
+ >
70
+ {children}
71
+ </WeeksView>
72
+ ),
73
+ },
74
+ ];
75
+
76
+ describe.each(views)("$description stable/state view contexts", ({ view }) => {
77
+ function setup() {
78
+ render(
79
+ view(
80
+ <>
81
+ <StableProbe />
82
+ <StateProbe />
83
+ {grid}
84
+ </>,
85
+ ),
86
+ );
87
+ const day = (n: number) =>
88
+ screen.getAllByRole("button").find((b) => b.textContent === String(n))!;
89
+ const reset = () => {
90
+ counts.stable = 0;
91
+ counts.state = 0;
92
+ };
93
+ return { day, reset };
94
+ }
95
+
96
+ it.each([
97
+ {
98
+ description: "grid focus does not re-render either context's consumers",
99
+ act: (day: (n: number) => HTMLElement) => day(15).focus(),
100
+ expected: { stable: 0, state: 0 },
101
+ },
102
+ {
103
+ description: "hovering a day does not re-render either",
104
+ act: (day: (n: number) => HTMLElement) => {
105
+ fireEvent.pointerEnter(day(20));
106
+ fireEvent.mouseOver(day(20));
107
+ },
108
+ expected: { stable: 0, state: 0 },
109
+ },
110
+ {
111
+ description: "moving focus re-renders state consumers only",
112
+ act: (day: (n: number) => HTMLElement) => {
113
+ fireEvent.keyDown(day(15), { key: "ArrowRight" });
114
+ },
115
+ expected: { stable: 0, state: 1 },
116
+ },
117
+ ])("$description", ({ act: action, expected }) => {
118
+ const { day, reset } = setup();
119
+ act(() => day(15).focus());
120
+ reset();
121
+
122
+ act(() => action(day));
123
+
124
+ expect(counts).toEqual(expected);
125
+ });
126
+ });
@@ -1,5 +1,6 @@
1
1
  import type { Temporal } from "@js-temporal/polyfill";
2
2
  import { createContext, useContext } from "react";
3
+ import type { OutsideDays, RootState } from "./types";
3
4
 
4
5
  /** Shared context provided by both MonthView.Root and WeeksView.Root. */
5
6
  export interface ViewContextValue {
@@ -17,6 +18,22 @@ export interface ViewContextValue {
17
18
  setGridHasFocus: (v: boolean) => void;
18
19
  }
19
20
 
21
+ /** @internal Rarely-changing values shared by both views' parts. */
22
+ export interface ViewStableContextValue {
23
+ /** Ref mirroring whether the grid holds DOM focus, without triggering re-renders. */
24
+ gridFocusedRef: React.RefObject<boolean>;
25
+ /** How days outside the viewed month are displayed. Always `"enabled"` in WeeksView. */
26
+ outsideDays: OutsideDays;
27
+ }
28
+
29
+ /** @internal Volatile values shared by both views' parts. */
30
+ export interface ViewStateContextValue {
31
+ /** The viewed month with the selection's time, used for "today" highlighting and labels. */
32
+ currentDateTime: Temporal.PlainDateTime;
33
+ /** The root component's state object for render functions. */
34
+ rootState: RootState;
35
+ }
36
+
20
37
  /** @internal React context for the active view (MonthView or WeeksView). */
21
38
  export const ViewContext = createContext<ViewContextValue | null>(null);
22
39
 
@@ -29,3 +46,29 @@ export function useViewContext(): ViewContextValue {
29
46
  );
30
47
  return ctx;
31
48
  }
49
+
50
+ /** @internal Context for {@link ViewStableContextValue}. */
51
+ export const ViewStableContext = createContext<ViewStableContextValue | null>(
52
+ null,
53
+ );
54
+
55
+ /** @internal Context for {@link ViewStateContextValue}. */
56
+ export const ViewStateContext = createContext<ViewStateContextValue | null>(
57
+ null,
58
+ );
59
+
60
+ /** Returns the nearest view's stable values. */
61
+ export function useViewStable(): ViewStableContextValue {
62
+ const ctx = useContext(ViewStableContext);
63
+ if (!ctx)
64
+ throw new Error("useViewStable must be used within MonthView or WeeksView");
65
+ return ctx;
66
+ }
67
+
68
+ /** Returns the nearest view's volatile state. */
69
+ export function useViewState(): ViewStateContextValue {
70
+ const ctx = useContext(ViewStateContext);
71
+ if (!ctx)
72
+ throw new Error("useViewState must be used within MonthView or WeeksView");
73
+ return ctx;
74
+ }
@@ -3,7 +3,6 @@ import { useRender } from "@base-ui/react/use-render";
3
3
  import { forwardRef, useContext, useMemo } from "react";
4
4
  import { useCalendarStable } from "./calendar-context";
5
5
  import { WeekDataContext } from "./context";
6
- import { useMonthViewState } from "./month-view-context";
7
6
  import type { StateAttributesMapping } from "./types";
8
7
  import type {
9
8
  WeekNumberCellState,
@@ -12,6 +11,7 @@ import type {
12
11
  WeekNumberHeaderProps,
13
12
  } from "./types";
14
13
  import { getISOWeekNumber } from "./utils";
14
+ import { useViewState } from "./view-context";
15
15
 
16
16
  const weekNumberCellStateAttributesMapping = {
17
17
  root: () => null,
@@ -24,7 +24,7 @@ function WeekNumberCellFn(
24
24
  ) {
25
25
  const { render, ...otherProps } = props;
26
26
  const weekData = useContext(WeekDataContext);
27
- const { rootState } = useMonthViewState();
27
+ const { rootState } = useViewState();
28
28
  const { temporal: T } = useCalendarStable();
29
29
 
30
30
  const days = weekData?.days ?? [];
@@ -84,7 +84,7 @@ function WeekNumberHeaderFn(
84
84
  ref: React.ForwardedRef<HTMLTableCellElement>,
85
85
  ) {
86
86
  const { render, ...otherProps } = props;
87
- const { rootState } = useMonthViewState();
87
+ const { rootState } = useViewState();
88
88
 
89
89
  const state = useMemo<WeekNumberHeaderState>(
90
90
  () => ({ root: rootState as unknown as WeekNumberHeaderState["root"] }),
@@ -2,9 +2,9 @@ import { mergeProps } from "@base-ui/react/merge-props";
2
2
  import { useRender } from "@base-ui/react/use-render";
3
3
  import React, { forwardRef, useMemo } from "react";
4
4
  import { useCalendarStable } from "./calendar-context";
5
- import { useMonthViewState } from "./month-view-context";
6
5
  import { canShift } from "./out-of-range";
7
6
  import type { RootState, StateAttributesMapping } from "./types";
7
+ import { useViewState } from "./view-context";
8
8
  import { useWeeksViewStable, useWeeksViewState } from "./weeks-view-context";
9
9
 
10
10
  // ---------------------------------------------------------------------------
@@ -80,7 +80,7 @@ function useWeeksNavButton(
80
80
  const { weekCount, outOfRangeBehavior, goNext, goPrev } =
81
81
  useWeeksViewStable();
82
82
  const { windowInfo } = useWeeksViewState();
83
- const { rootState } = useMonthViewState();
83
+ const { rootState } = useViewState();
84
84
 
85
85
  const currentFirstWeek = windowInfo.windowStart;
86
86
 
@@ -193,7 +193,7 @@ function WeekCountFn(
193
193
  ) {
194
194
  const { render, ...otherProps } = props;
195
195
  const { windowInfo } = useWeeksViewState();
196
- const { rootState } = useMonthViewState();
196
+ const { rootState } = useViewState();
197
197
  const count = windowInfo.weekCount;
198
198
 
199
199
  const state = useMemo<WeekCountState>(