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

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 (57) 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 +11 -2
  31. package/dist/index.d.mts +11 -2
  32. package/dist/index.mjs +1 -1
  33. package/dist/{weeks-navigation-BRxzsy0z.cjs → weeks-navigation-DV538__x.cjs} +115 -135
  34. package/dist/{weeks-navigation-CXRaVP02.mjs → weeks-navigation-elCpdfQ3.mjs} +115 -135
  35. package/dist/{index-CJgr0MEp.d.cts → weeks-view-context-CUCy8njs.d.cts} +3 -8
  36. package/dist/{index-DB6V_Y6x.d.mts → weeks-view-context-E1NwyNgQ.d.mts} +3 -8
  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 +17 -16
  41. package/src/drag-handle.tsx +2 -2
  42. package/src/grid-header.tsx +3 -3
  43. package/src/grid.tsx +20 -61
  44. package/src/month-view.tsx +32 -17
  45. package/src/navigation.tsx +3 -2
  46. package/src/range-preview.tsx +3 -5
  47. package/src/selected-range.tsx +3 -5
  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/view-context.test.tsx +126 -0
  52. package/src/view-context.ts +43 -0
  53. package/src/week-number.tsx +3 -3
  54. package/src/weeks-navigation.tsx +3 -3
  55. package/src/weeks-view-state.ts +31 -17
  56. package/src/weeks-view.test.tsx +115 -13
  57. package/src/weeks-view.tsx +22 -51
@@ -188,6 +188,8 @@ export interface CalendarStableContextValue {
188
188
  maxValue?: Temporal.PlainDate;
189
189
  /** Resolved IANA time zone. */
190
190
  timeZone: string;
191
+ /** Today's date in the resolved {@link timeZone}. Reads the clock on every call. */
192
+ today: () => Temporal.PlainDate;
191
193
  /** Resolved BCP 47 locale. */
192
194
  locale: string;
193
195
  /** Resolved Temporal namespace. */
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(
@@ -32,6 +31,7 @@ export function computeDayCellState(
32
31
  rangeStart: TemporalPoly.PlainDate | undefined,
33
32
  rangeEnd: TemporalPoly.PlainDate | undefined,
34
33
  tabTargetDate: TemporalPoly.PlainDate,
34
+ today: TemporalPoly.PlainDate,
35
35
  T: TemporalNamespace,
36
36
  selectionMode: "single" | "range" | "multiple",
37
37
  outsideDays: OutsideDays,
@@ -74,7 +74,6 @@ export function computeDayCellState(
74
74
  };
75
75
  }
76
76
 
77
- const today = T.Now.plainDateISO();
78
77
  const isToday = T.PlainDate.compare(date, today) === 0;
79
78
  const isRange = selectionMode === "range";
80
79
 
@@ -390,19 +389,19 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
390
389
  isDateDisabled,
391
390
  selectionMode,
392
391
  temporal: T,
392
+ today,
393
393
  weekStartDay,
394
394
  } = useCalendarStable();
395
+ const todayDate = today();
395
396
 
396
397
  const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } =
397
398
  useCalendarState();
398
399
 
399
400
  const viewCtx = useViewContext();
400
401
  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";
402
+ const { rootState, currentDateTime } = useViewState();
403
+ const { outsideDays } = useViewStable();
404
+ const monthState = useContext(MonthViewStateContext);
406
405
 
407
406
  // Use the grid-specific month for outsideMonth checks, falling back to currentMonth.
408
407
  // In multi-month mode, gridMonth is set by WeekTemplate from GridMonthContext.
@@ -432,6 +431,7 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
432
431
  rangeStart,
433
432
  rangeEnd,
434
433
  tabTargetDate,
434
+ todayDate,
435
435
  T,
436
436
  selectionMode,
437
437
  outsideDays,
@@ -443,7 +443,7 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
443
443
  );
444
444
  }
445
445
 
446
- const days = weekData ? weekData.days : weeks.flat();
446
+ const days = weekData ? weekData.days : (monthState?.weeks.flat() ?? []);
447
447
  const perWeek = weekData != null;
448
448
 
449
449
  return (
@@ -467,6 +467,7 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
467
467
  rangeStart,
468
468
  rangeEnd,
469
469
  tabTargetDate,
470
+ todayDate,
470
471
  T,
471
472
  selectionMode,
472
473
  outsideDays,
@@ -502,8 +503,7 @@ function DayButtonInnerFn(
502
503
  const { render, date, _derivedState, ...otherProps } = props;
503
504
  const { onSelect, locale, readOnly, setHoveredDate } = useCalendarStable();
504
505
  const { setFocusedDate } = useViewContext();
505
- const monthStable = useContext(MonthViewStableContext);
506
- const gridFocusedRef = monthStable!.gridFocusedRef;
506
+ const { gridFocusedRef } = useViewStable();
507
507
 
508
508
  const internalRef = useRef<HTMLButtonElement>(null);
509
509
 
@@ -663,16 +663,16 @@ function DayButtonFallbackFn(
663
663
  isDateDisabled,
664
664
  selectionMode,
665
665
  temporal: T,
666
+ today,
666
667
  weekStartDay,
667
668
  } = useCalendarStable();
669
+ const todayDate = today();
668
670
  const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } =
669
671
  useCalendarState();
670
672
  const viewCtx = useViewContext();
671
673
  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";
674
+ const { rootState, currentDateTime } = useViewState();
675
+ const { outsideDays } = useViewStable();
676
676
 
677
677
  const isWeeksView = viewCtx.viewType === "weeks";
678
678
  const cellMonth = isWeeksView
@@ -694,6 +694,7 @@ function DayButtonFallbackFn(
694
694
  rangeStart,
695
695
  rangeEnd,
696
696
  tabTargetDate,
697
+ todayDate,
697
698
  T,
698
699
  selectionMode,
699
700
  outsideDays,
@@ -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
@@ -20,9 +20,16 @@ import {
20
20
  resolveFocusTarget,
21
21
  selectedToZdt,
22
22
  shiftWindowToMonth,
23
- toZonedDateTime,
23
+ resolveInitialAnchor,
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,7 +57,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
50
57
  const calState = useCalendarState();
51
58
 
52
59
  const T = calStable.temporal;
53
- const { timeZone, weekStartDay, isDateDisabled } = calStable;
60
+ const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
54
61
 
55
62
  // --- Month navigation state (controlled/uncontrolled) ---
56
63
  const isMonthControlled = monthProp !== undefined;
@@ -62,13 +69,8 @@ function MonthViewRoot(props: MonthViewRootProps) {
62
69
  if (monthProp) return { year: monthProp.year, month: monthProp.month };
63
70
  if (defaultMonthProp)
64
71
  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
+ const anchor = resolveInitialAnchor(calState.selected, today, timeZone, T);
73
+ return { year: anchor.year, month: anchor.month };
72
74
  });
73
75
 
74
76
  const currentMonth = isMonthControlled
@@ -76,12 +78,9 @@ function MonthViewRoot(props: MonthViewRootProps) {
76
78
  : internalMonth;
77
79
 
78
80
  // --- Focus state ---
79
- const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() => {
80
- if (calState.selected) {
81
- return toZonedDateTime(calState.selected, timeZone, T).toPlainDate();
82
- }
83
- return T.Now.plainDateISO();
84
- });
81
+ const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() =>
82
+ resolveInitialAnchor(calState.selected, today, timeZone, T),
83
+ );
85
84
 
86
85
  const [gridHasFocus, setGridHasFocus] = useState(false);
87
86
  const gridFocusedRef = useRef(false);
@@ -424,10 +423,26 @@ function MonthViewRoot(props: MonthViewRootProps) {
424
423
  [focusedDate, tabTargetDate, gridHasFocus],
425
424
  );
426
425
 
426
+ const viewStableCtx = useMemo<ViewStableContextValue>(
427
+ () => ({ gridFocusedRef, outsideDays }),
428
+ [outsideDays],
429
+ );
430
+
431
+ const viewStateCtx = useMemo<ViewStateContextValue>(
432
+ () => ({ currentDateTime, rootState }),
433
+ [currentDateTime, rootState],
434
+ );
435
+
427
436
  return (
428
437
  <MonthViewStableContext.Provider value={stableCtx}>
429
438
  <MonthViewStateContext.Provider value={stateCtx}>
430
- <ViewContext.Provider value={viewCtx}>{children}</ViewContext.Provider>
439
+ <ViewStableContext.Provider value={viewStableCtx}>
440
+ <ViewStateContext.Provider value={viewStateCtx}>
441
+ <ViewContext.Provider value={viewCtx}>
442
+ {children}
443
+ </ViewContext.Provider>
444
+ </ViewStateContext.Provider>
445
+ </ViewStableContext.Provider>
431
446
  </MonthViewStateContext.Provider>
432
447
  </MonthViewStableContext.Provider>
433
448
  );
@@ -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,98 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import { render, screen, cleanup } from "@testing-library/react";
3
+ import { describe, it, expect, vi, afterEach, beforeEach } from "vitest";
4
+ import {
5
+ Grid,
6
+ GridBody,
7
+ WeekTemplate,
8
+ DayCellTemplate,
9
+ DayButton,
10
+ } from "./grid";
11
+ import { MonthView } from "./month-view";
12
+ import { useViewContext } from "./view-context";
13
+ import { WeeksView } from "./weeks-view";
14
+
15
+ const T = Temporal;
16
+
17
+ function FocusDisplay() {
18
+ const view = useViewContext();
19
+ return <span data-testid="focus">{view.focusedDate.toString()}</span>;
20
+ }
21
+
22
+ const days = (
23
+ <Grid>
24
+ <GridBody>
25
+ <WeekTemplate>
26
+ <DayCellTemplate>
27
+ <DayButton />
28
+ </DayCellTemplate>
29
+ </WeekTemplate>
30
+ </GridBody>
31
+ </Grid>
32
+ );
33
+
34
+ // 2026-03-31T20:00Z is 2026-04-01 in Auckland (NZDT, +13) and 2026-03-31 in Pago Pago (-11).
35
+ // 2026-04-04T20:00Z is Sunday 2026-04-05 in Auckland (NZST, +12) and Saturday 2026-04-04 in Pago Pago.
36
+ describe("today follows the provider time zone", () => {
37
+ beforeEach(() => {
38
+ vi.useFakeTimers({ toFake: ["Date"] });
39
+ });
40
+ afterEach(() => {
41
+ cleanup();
42
+ vi.useRealTimers();
43
+ });
44
+
45
+ it.each([
46
+ {
47
+ description: "ahead of the clock's UTC date",
48
+ timeZone: "Pacific/Auckland",
49
+ expected: { today: "2026-04-01", todayCell: "1" },
50
+ },
51
+ {
52
+ description: "behind the clock's UTC date",
53
+ timeZone: "Pacific/Pago_Pago",
54
+ expected: { today: "2026-03-31", todayCell: "31" },
55
+ },
56
+ ])("MonthView: $description", ({ timeZone, expected }) => {
57
+ vi.setSystemTime(new Date("2026-03-31T20:00:00Z"));
58
+ const { container } = render(
59
+ <MonthView temporal={T} timeZone={timeZone}>
60
+ <FocusDisplay />
61
+ {days}
62
+ </MonthView>,
63
+ );
64
+
65
+ expect(screen.getByTestId("focus").textContent).toBe(expected.today);
66
+ const marked = container.querySelectorAll("td[data-today]");
67
+ expect(marked.length).toBe(1);
68
+ expect(marked[0]?.textContent).toBe(expected.todayCell);
69
+ expect(marked[0]?.hasAttribute("data-outside-month")).toBe(false);
70
+ });
71
+
72
+ it.each([
73
+ {
74
+ description: "ahead of the clock's UTC date",
75
+ timeZone: "Pacific/Auckland",
76
+ expected: { firstCell: "5", todayCell: "5" },
77
+ },
78
+ {
79
+ description: "behind the clock's UTC date",
80
+ timeZone: "Pacific/Pago_Pago",
81
+ expected: { firstCell: "29", todayCell: "4" },
82
+ },
83
+ ])("WeeksView: $description", ({ timeZone, expected }) => {
84
+ vi.setSystemTime(new Date("2026-04-04T20:00:00Z"));
85
+ const { container } = render(
86
+ <WeeksView temporal={T} timeZone={timeZone} weekCount={1}>
87
+ {days}
88
+ </WeeksView>,
89
+ );
90
+
91
+ expect(container.querySelector("td[role='gridcell']")?.textContent).toBe(
92
+ expected.firstCell,
93
+ );
94
+ const marked = container.querySelectorAll("td[data-today]");
95
+ expect(marked.length).toBe(1);
96
+ expect(marked[0]?.textContent).toBe(expected.todayCell);
97
+ });
98
+ });
package/src/utils.test.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  toZonedDateTime,
9
9
  fromZonedDateTime,
10
10
  selectedToZdt,
11
+ resolveInitialAnchor,
11
12
  getMonthWeeks,
12
13
  zdtToNativeDate,
13
14
  sameCalendarDay,
@@ -249,6 +250,30 @@ describe("selectedToZdt", () => {
249
250
  });
250
251
  });
251
252
 
253
+ describe("resolveInitialAnchor", () => {
254
+ const today = () => Temporal.PlainDate.from("2026-04-01");
255
+
256
+ it.each([
257
+ {
258
+ description: "falls back to today when nothing is selected",
259
+ selected: undefined,
260
+ expected: "2026-04-01",
261
+ },
262
+ {
263
+ description: "uses the selected date over today",
264
+ selected: {
265
+ format: "PlainDate",
266
+ value: Temporal.PlainDate.from("2026-03-15"),
267
+ } as const,
268
+ expected: "2026-03-15",
269
+ },
270
+ ])("$description", ({ selected, expected }) => {
271
+ expect(resolveInitialAnchor(selected, today, "UTC", T).toString()).toBe(
272
+ expected,
273
+ );
274
+ });
275
+ });
276
+
252
277
  describe("zdtToNativeDate", () => {
253
278
  it("converts a ZonedDateTime to a native Date with the correct epoch", () => {
254
279
  const zdt = Temporal.PlainDateTime.from(
package/src/utils.ts CHANGED
@@ -39,6 +39,23 @@ export function getSystemTimeZone(T: TemporalNamespace): string {
39
39
  return T.Now.timeZoneId();
40
40
  }
41
41
 
42
+ /**
43
+ * The date a view opens on: the selected date, else today in the provider's time zone.
44
+ *
45
+ * @param selected - The currently selected value, or `undefined`.
46
+ * @param today - Reads today's date in the provider's time zone.
47
+ * @param timeZone - IANA time zone.
48
+ * @param T - Temporal namespace.
49
+ */
50
+ export function resolveInitialAnchor(
51
+ selected: DateValueObject | undefined,
52
+ today: () => Temporal.PlainDate,
53
+ timeZone: string,
54
+ T: TemporalNamespace,
55
+ ): Temporal.PlainDate {
56
+ return selectedToZdt(selected, timeZone, T)?.toPlainDate() ?? today();
57
+ }
58
+
42
59
  /**
43
60
  * Converts any {@link DateValueObject} into a `Temporal.ZonedDateTime`.
44
61
  *