@klinking/colander 3.1.0-alpha.7 → 3.1.0-alpha.9

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 (72) hide show
  1. package/dist/formats/date.cjs +2 -3
  2. package/dist/formats/date.d.cts +10 -14
  3. package/dist/formats/date.d.mts +10 -14
  4. package/dist/formats/date.mjs +2 -3
  5. package/dist/formats/object.cjs +2 -3
  6. package/dist/formats/object.d.cts +10 -14
  7. package/dist/formats/object.d.mts +10 -14
  8. package/dist/formats/object.mjs +2 -3
  9. package/dist/formats/plain-date-time.cjs +2 -3
  10. package/dist/formats/plain-date-time.d.cts +10 -14
  11. package/dist/formats/plain-date-time.d.mts +10 -14
  12. package/dist/formats/plain-date-time.mjs +2 -3
  13. package/dist/formats/plain-date.cjs +2 -3
  14. package/dist/formats/plain-date.d.cts +10 -14
  15. package/dist/formats/plain-date.d.mts +10 -14
  16. package/dist/formats/plain-date.mjs +2 -3
  17. package/dist/formats/plain-month-day.cjs +2 -3
  18. package/dist/formats/plain-month-day.d.cts +10 -14
  19. package/dist/formats/plain-month-day.d.mts +10 -14
  20. package/dist/formats/plain-month-day.mjs +2 -3
  21. package/dist/formats/plain-year-month.cjs +2 -3
  22. package/dist/formats/plain-year-month.d.cts +10 -14
  23. package/dist/formats/plain-year-month.d.mts +10 -14
  24. package/dist/formats/plain-year-month.mjs +2 -3
  25. package/dist/formats/zoned-date-time.cjs +2 -3
  26. package/dist/formats/zoned-date-time.d.cts +10 -14
  27. package/dist/formats/zoned-date-time.d.mts +10 -14
  28. package/dist/formats/zoned-date-time.mjs +2 -3
  29. package/dist/{weeks-view-context-BTojXkys.d.cts → index-B5VJ_WF2.d.mts} +91 -114
  30. package/dist/{weeks-view-context-CFXV8orh.d.mts → index-BMsxaq1s.d.cts} +91 -114
  31. package/dist/index.cjs +1 -1
  32. package/dist/index.d.cts +1 -10
  33. package/dist/index.d.mts +1 -10
  34. package/dist/index.mjs +1 -2
  35. package/dist/{weeks-navigation-CPevz3Xg.cjs → weeks-navigation-CUQApLMk.cjs} +531 -400
  36. package/dist/{weeks-navigation-BSKwFvdP.mjs → weeks-navigation-CWNpF_6M.mjs} +531 -399
  37. package/package.json +21 -18
  38. package/src/calendar-provider.test.tsx +1 -1
  39. package/src/calendar-provider.tsx +8 -2
  40. package/src/compute-weeks-in-window.test.ts +1 -1
  41. package/src/day-cell-state.test.ts +1 -1
  42. package/src/day-cell-state.ts +0 -70
  43. package/src/day-cell-store.test.tsx +273 -0
  44. package/src/day-cell-store.tsx +204 -0
  45. package/src/day-cell.test.tsx +73 -90
  46. package/src/day-cell.tsx +93 -78
  47. package/src/formats/formats.test.ts +1 -1
  48. package/src/grid.event-perf.test.tsx +7 -1
  49. package/src/grid.perf.test.tsx +7 -1
  50. package/src/grid.tsx +17 -15
  51. package/src/keyboard.test.ts +1 -1
  52. package/src/month-separator.test.tsx +1 -1
  53. package/src/month-view.bounds.test.tsx +1 -1
  54. package/src/month-view.test.tsx +89 -1
  55. package/src/month-view.tsx +17 -7
  56. package/src/month-window.test.ts +1 -1
  57. package/src/out-of-range.test.ts +1 -1
  58. package/src/range-preview.test.ts +1 -1
  59. package/src/resolve-first-week.test.ts +1 -1
  60. package/src/store.ts +72 -0
  61. package/src/today.test.tsx +8 -1
  62. package/src/utils.bench.ts +158 -106
  63. package/src/utils.test.ts +1 -1
  64. package/src/view-context.test.tsx +1 -1
  65. package/src/view-context.ts +2 -0
  66. package/src/weeks-grid.tsx +20 -10
  67. package/src/weeks-keyboard.test.ts +1 -1
  68. package/src/weeks-view-state.ts +1 -1
  69. package/src/weeks-view.bounds.test.tsx +1 -1
  70. package/src/weeks-view.test.tsx +1 -1
  71. package/src/weeks-view.tsx +2 -1
  72. package/src/weeks-window.test.ts +1 -1
@@ -1,4 +1,4 @@
1
- //#region \0rolldown/runtime.js
1
+ // \0rolldown/runtime.js
2
2
  var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -15,17 +15,17 @@ var __copyProps = (to, from, except, desc) => {
15
15
  }
16
16
  return to;
17
17
  };
18
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
19
19
  value: mod,
20
20
  enumerable: true
21
21
  }) : target, mod));
22
- //#endregion
23
22
  let react = require("react");
24
23
  react = __toESM(react, 1);
24
+ let use_sync_external_store_shim_with_selector = require("use-sync-external-store/shim/with-selector");
25
25
  let react_jsx_runtime = require("react/jsx-runtime");
26
26
  let _base_ui_react_merge_props = require("@base-ui/react/merge-props");
27
27
  let _base_ui_react_use_render = require("@base-ui/react/use-render");
28
- //#region src/calendar-context.ts
28
+ // src/calendar-context.ts
29
29
  /** @internal React context carrying stable CalendarProvider values (callbacks, config). */
30
30
  const CalendarStableContext = (0, react.createContext)(null);
31
31
  /** @internal React context carrying volatile CalendarProvider state. */
@@ -42,8 +42,7 @@ function useCalendarState() {
42
42
  if (!ctx) throw new Error("useCalendarState must be used within CalendarProvider");
43
43
  return ctx;
44
44
  }
45
- //#endregion
46
- //#region src/utils.ts
45
+ // src/utils.ts
47
46
  /**
48
47
  * Resolves a {@link TemporalNamespace} instance.
49
48
  *
@@ -489,8 +488,7 @@ function getISOWeekNumber(date, T) {
489
488
  const daysDiff = thursday.since(jan4Thursday, { largestUnit: "days" }).days;
490
489
  return Math.round(daysDiff / 7) + 1;
491
490
  }
492
- //#endregion
493
- //#region src/root-selection.ts
491
+ // src/root-selection.ts
494
492
  /** Construct a DateRange (workaround for TS generic conditional type widening). */
495
493
  function mkRange(start, end) {
496
494
  return {
@@ -855,8 +853,46 @@ function computePreviewRange(hoveredDate, currentRange, rangeMode, preventRangeR
855
853
  });
856
854
  return preview ? mkRange(preview[0], preview[1]) : null;
857
855
  }
858
- //#endregion
859
- //#region src/calendar-provider.tsx
856
+ // src/store.ts
857
+ function createStore(initial) {
858
+ let state = initial;
859
+ const listeners = /* @__PURE__ */ new Set();
860
+ return {
861
+ get: () => state,
862
+ set: (next) => {
863
+ if (Object.is(state, next)) return;
864
+ state = next;
865
+ for (const listener of listeners) listener();
866
+ },
867
+ subscribe: (listener) => {
868
+ listeners.add(listener);
869
+ return () => listeners.delete(listener);
870
+ }
871
+ };
872
+ }
873
+ /** `useLayoutEffect` in the browser, `useEffect` on the server (no warning). */
874
+ const useIsoLayoutEffect = typeof window === "undefined" ? react.useEffect : react.useLayoutEffect;
875
+ /**
876
+ * Subscribes to the slice of `store` that `select` picks. The component
877
+ * re-renders only when `isEqual` says the slice changed; otherwise the
878
+ * previously returned slice is handed back, keeping its identity.
879
+ */
880
+ function useStoreSelector(store, select, isEqual) {
881
+ return (0, use_sync_external_store_shim_with_selector.useSyncExternalStoreWithSelector)(store.subscribe, store.get, store.get, select, isEqual);
882
+ }
883
+ /**
884
+ * Returns a function with a stable identity that always calls the latest
885
+ * `fn`, so contexts holding it don't change when its closure does. Call it
886
+ * from event handlers and effects, not during render.
887
+ */
888
+ function useStableCallback(fn) {
889
+ const latest = (0, react.useRef)(fn);
890
+ (0, react.useInsertionEffect)(() => {
891
+ latest.current = fn;
892
+ });
893
+ return (0, react.useCallback)((...args) => latest.current(...args), []);
894
+ }
895
+ // src/calendar-provider.tsx
860
896
  /**
861
897
  * Provides shared calendar state (selection, config, range preview) to all
862
898
  * descendant view components. Does NOT manage focus, navigation, or grid data.
@@ -974,7 +1010,7 @@ function CalendarProvider(props) {
974
1010
  isRange,
975
1011
  isMultiple
976
1012
  ]);
977
- const [internalDates, setInternalDates] = (0, react.useState)((0, react.useMemo)(() => {
1013
+ const defaultDates = (0, react.useMemo)(() => {
978
1014
  if (isMultiple) {
979
1015
  if (multipleDefault) return sortDates(multipleDefault.map(rawToPlain));
980
1016
  return [];
@@ -989,7 +1025,8 @@ function CalendarProvider(props) {
989
1025
  singleDefault,
990
1026
  rawToPlain,
991
1027
  sortDates
992
- ]));
1028
+ ]);
1029
+ const [internalDates, setInternalDates] = (0, react.useState)(defaultDates);
993
1030
  const isControlled = controlledDates !== void 0;
994
1031
  const committedDates = isControlled ? controlledDates : internalDates;
995
1032
  const rangeStart = !isMultiple && committedDates.length > 0 ? committedDates[0] ?? void 0 : void 0;
@@ -1148,7 +1185,7 @@ function CalendarProvider(props) {
1148
1185
  isMultiple,
1149
1186
  isControlled
1150
1187
  ]);
1151
- const onSelect = (0, react.useCallback)((date) => {
1188
+ const onSelectLatest = (0, react.useCallback)((date) => {
1152
1189
  const result = computeSelectionUpdate({
1153
1190
  date,
1154
1191
  readOnly,
@@ -1192,7 +1229,7 @@ function CalendarProvider(props) {
1192
1229
  currentRangeFormatted,
1193
1230
  currentMultipleFormatted
1194
1231
  ]);
1195
- const setRange = (0, react.useCallback)((start, end) => {
1232
+ const setRangeLatest = (0, react.useCallback)((start, end) => {
1196
1233
  const result = computeSetRangeUpdate({
1197
1234
  start,
1198
1235
  end,
@@ -1218,6 +1255,8 @@ function CalendarProvider(props) {
1218
1255
  resolvedFormat,
1219
1256
  T
1220
1257
  ]);
1258
+ const onSelect = useStableCallback(onSelectLatest);
1259
+ const setRange = useStableCallback(setRangeLatest);
1221
1260
  const nonNullDates = (0, react.useMemo)(() => committedDates.filter((d) => d != null), [committedDates]);
1222
1261
  const stableCtx = (0, react.useMemo)(() => ({
1223
1262
  onSelect,
@@ -1300,8 +1339,293 @@ function CalendarProvider(props) {
1300
1339
  })
1301
1340
  });
1302
1341
  }
1303
- //#endregion
1304
- //#region src/month-view-context.ts
1342
+ // src/context.ts
1343
+ /** @internal Provides the days and index of the current week row to child components. */
1344
+ const WeekDataContext = (0, react.createContext)(null);
1345
+ /** @internal Provides the resolved grid orientation to descendant cells. */
1346
+ const GridContext = (0, react.createContext)({ orientation: "horizontal" });
1347
+ /** @internal Provides the date and column index from a `DayCellTemplate` to its child `DayButton`. */
1348
+ const DayCellDataContext = (0, react.createContext)(null);
1349
+ /** @internal Provides per-grid month data to WeekTemplate and DayCellTemplate. */
1350
+ const GridMonthContext = (0, react.createContext)(null);
1351
+ // src/day-cell-state.ts
1352
+ const NO_RANGE = {
1353
+ rangeStart: false,
1354
+ rangeEnd: false,
1355
+ rangeBoundary: false,
1356
+ inRange: false,
1357
+ rangeIndex: false,
1358
+ rangeLength: false,
1359
+ rangeHasStart: false,
1360
+ rangeHasEnd: false,
1361
+ rangePreviewStart: false,
1362
+ rangePreviewEnd: false,
1363
+ rangePreviewBoundary: false,
1364
+ rangePreviewInRange: false,
1365
+ rangePreviewIndex: false,
1366
+ rangePreviewLength: false,
1367
+ rangePreviewHasStart: false,
1368
+ rangePreviewHasEnd: false
1369
+ };
1370
+ function computeRangeState(date, inputs) {
1371
+ const { T, selectionMode, rangeStart, rangeEnd, previewStart, previewEnd } = inputs;
1372
+ if (selectionMode !== "range") return NO_RANGE;
1373
+ const isRangeStart = rangeStart ? T.PlainDate.compare(date, rangeStart) === 0 : false;
1374
+ const isRangeEnd = rangeEnd ? T.PlainDate.compare(date, rangeEnd) === 0 : false;
1375
+ const inRange = isInRange(date, rangeStart, rangeEnd, T) !== false;
1376
+ const effectiveStart = rangeStart ?? rangeEnd;
1377
+ const effectiveEnd = rangeEnd ?? rangeStart;
1378
+ const isPreviewStart = previewStart ? T.PlainDate.compare(date, previewStart) === 0 : false;
1379
+ const isPreviewEnd = previewEnd ? T.PlainDate.compare(date, previewEnd) === 0 : false;
1380
+ const inPreview = previewStart && previewEnd ? isInRange(date, previewStart, previewEnd, T) !== false : false;
1381
+ return {
1382
+ rangeStart: isRangeStart,
1383
+ rangeEnd: isRangeEnd,
1384
+ rangeBoundary: isRangeStart || isRangeEnd,
1385
+ inRange,
1386
+ rangeIndex: inRange && effectiveStart ? date.since(effectiveStart).days : false,
1387
+ rangeLength: inRange && effectiveStart && effectiveEnd ? effectiveEnd.since(effectiveStart).days + 1 : false,
1388
+ rangeHasStart: inRange && rangeStart !== void 0,
1389
+ rangeHasEnd: inRange && rangeEnd !== void 0,
1390
+ rangePreviewStart: isPreviewStart,
1391
+ rangePreviewEnd: isPreviewEnd,
1392
+ rangePreviewBoundary: isPreviewStart || isPreviewEnd,
1393
+ rangePreviewInRange: inPreview,
1394
+ rangePreviewIndex: inPreview && previewStart ? date.since(previewStart).days : false,
1395
+ rangePreviewLength: inPreview && previewStart && previewEnd ? previewEnd.since(previewStart).days + 1 : false,
1396
+ rangePreviewHasStart: inPreview && previewStart !== void 0,
1397
+ rangePreviewHasEnd: inPreview && previewEnd !== void 0
1398
+ };
1399
+ }
1400
+ /** Derives the state of one day cell. Pure function, no hooks. */
1401
+ function computeDayCellState(date, position, inputs) {
1402
+ const { T, outsideDays } = inputs;
1403
+ const { columnIndex, month } = position;
1404
+ const isCurrentMonth = date.year === month.year && date.month === month.month;
1405
+ const base = {
1406
+ root: inputs.rootState,
1407
+ date,
1408
+ columnIndex,
1409
+ orientation: inputs.orientation
1410
+ };
1411
+ if (!isCurrentMonth && outsideDays === "hidden") return {
1412
+ ...base,
1413
+ ...NO_RANGE,
1414
+ outsideMonth: true,
1415
+ hidden: true,
1416
+ selected: false,
1417
+ today: false,
1418
+ disabled: true,
1419
+ focused: false,
1420
+ isTabTarget: false
1421
+ };
1422
+ const isToday = T.PlainDate.compare(date, inputs.today) === 0;
1423
+ if (!isCurrentMonth && outsideDays !== "enabled") {
1424
+ const suppressRange = outsideDays === "disabled";
1425
+ return {
1426
+ ...base,
1427
+ ...suppressRange ? NO_RANGE : computeRangeState(date, inputs),
1428
+ outsideMonth: true,
1429
+ hidden: false,
1430
+ selected: false,
1431
+ today: isToday,
1432
+ disabled: true,
1433
+ focused: false,
1434
+ isTabTarget: false
1435
+ };
1436
+ }
1437
+ return {
1438
+ ...base,
1439
+ ...computeRangeState(date, inputs),
1440
+ outsideMonth: !isCurrentMonth,
1441
+ hidden: false,
1442
+ selected: inputs.selectionMode !== "range" ? inputs.selectedDates.some((d) => T.PlainDate.compare(d, date) === 0) : false,
1443
+ today: isToday,
1444
+ disabled: inputs.disabled || (inputs.isDateDisabled?.(date) ?? false),
1445
+ focused: T.PlainDate.compare(date, inputs.focusedDate) === 0,
1446
+ isTabTarget: T.PlainDate.compare(date, inputs.tabTargetDate) === 0
1447
+ };
1448
+ }
1449
+ const COMPARED_KEYS = Object.keys({
1450
+ outsideMonth: true,
1451
+ hidden: true,
1452
+ selected: true,
1453
+ today: true,
1454
+ disabled: true,
1455
+ focused: true,
1456
+ rangeStart: true,
1457
+ rangeEnd: true,
1458
+ rangeBoundary: true,
1459
+ inRange: true,
1460
+ rangeIndex: true,
1461
+ rangeLength: true,
1462
+ rangeHasStart: true,
1463
+ rangeHasEnd: true,
1464
+ rangePreviewStart: true,
1465
+ rangePreviewEnd: true,
1466
+ rangePreviewBoundary: true,
1467
+ rangePreviewInRange: true,
1468
+ rangePreviewIndex: true,
1469
+ rangePreviewLength: true,
1470
+ rangePreviewHasStart: true,
1471
+ rangePreviewHasEnd: true,
1472
+ isTabTarget: true
1473
+ });
1474
+ /** Whether two cell states render the same cell. */
1475
+ function dayCellStateEqual(a, b) {
1476
+ for (const key of COMPARED_KEYS) if (a[key] !== b[key]) return false;
1477
+ return true;
1478
+ }
1479
+ // src/view-context.ts
1480
+ /** @internal React context for the active view (MonthView or WeeksView). */
1481
+ const ViewContext = (0, react.createContext)(null);
1482
+ /** Returns the nearest view context (MonthView or WeeksView). */
1483
+ function useViewContext() {
1484
+ const ctx = (0, react.useContext)(ViewContext);
1485
+ if (!ctx) throw new Error("useViewContext must be used within MonthView or WeeksView");
1486
+ return ctx;
1487
+ }
1488
+ /** @internal Context for {@link ViewStableContextValue}. */
1489
+ const ViewStableContext = (0, react.createContext)(null);
1490
+ /** @internal Context for {@link ViewStateContextValue}. */
1491
+ const ViewStateContext = (0, react.createContext)(null);
1492
+ /** Returns the nearest view's stable values. */
1493
+ function useViewStable() {
1494
+ const ctx = (0, react.useContext)(ViewStableContext);
1495
+ if (!ctx) throw new Error("useViewStable must be used within MonthView or WeeksView");
1496
+ return ctx;
1497
+ }
1498
+ /** Returns the nearest view's volatile state. */
1499
+ function useViewState() {
1500
+ const ctx = (0, react.useContext)(ViewStateContext);
1501
+ if (!ctx) throw new Error("useViewState must be used within MonthView or WeeksView");
1502
+ return ctx;
1503
+ }
1504
+ // src/day-cell-store.tsx
1505
+ /** @internal The view's day-cell store. Its identity never changes. */
1506
+ const DayCellStoreContext = (0, react.createContext)(null);
1507
+ /**
1508
+ * Publishes the calendar and view state that day cells derive from. Cells
1509
+ * subscribe per date, so a change re-renders only the cells it affects instead
1510
+ * of every cell under a context.
1511
+ *
1512
+ * The store is seeded on the first render and updated only after a change
1513
+ * commits, never during render: a render React discards (a suspended
1514
+ * transition) must not leak into cells that re-render meanwhile. Cells that
1515
+ * render in the same pass as a change read the previous state first, then
1516
+ * re-render from the layout effect before the browser paints.
1517
+ */
1518
+ function DayCellStoreProvider(props) {
1519
+ const { disabled, isDateDisabled, selectionMode, temporal: T, today, weekStartDay } = useCalendarStable();
1520
+ const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
1521
+ const { viewType, focusedDate, tabTargetDate } = useViewContext();
1522
+ const { rootState, currentDateTime } = useViewState();
1523
+ const { outsideDays } = useViewStable();
1524
+ const todayKey = today().toString();
1525
+ const { year, month } = currentDateTime;
1526
+ const snapshot = (0, react.useMemo)(() => ({
1527
+ inputs: {
1528
+ T,
1529
+ today: T.PlainDate.from(todayKey),
1530
+ rootState,
1531
+ selectionMode,
1532
+ selectedDates,
1533
+ rangeStart,
1534
+ rangeEnd,
1535
+ previewStart,
1536
+ previewEnd,
1537
+ disabled,
1538
+ isDateDisabled,
1539
+ focusedDate,
1540
+ tabTargetDate,
1541
+ outsideDays
1542
+ },
1543
+ viewType,
1544
+ viewMonth: {
1545
+ year,
1546
+ month
1547
+ },
1548
+ weekStartDay
1549
+ }), [
1550
+ T,
1551
+ todayKey,
1552
+ rootState,
1553
+ selectionMode,
1554
+ selectedDates,
1555
+ rangeStart,
1556
+ rangeEnd,
1557
+ previewStart,
1558
+ previewEnd,
1559
+ disabled,
1560
+ isDateDisabled,
1561
+ focusedDate,
1562
+ tabTargetDate,
1563
+ outsideDays,
1564
+ viewType,
1565
+ year,
1566
+ month,
1567
+ weekStartDay
1568
+ ]);
1569
+ const [store] = (0, react.useState)(() => createStore(snapshot));
1570
+ useIsoLayoutEffect(() => store.set(snapshot), [store, snapshot]);
1571
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellStoreContext.Provider, {
1572
+ value: store,
1573
+ children: props.children
1574
+ });
1575
+ }
1576
+ /**
1577
+ * Hands `state` to render functions with `root` as a non-enumerable getter
1578
+ * that calls `onReadRoot`. Base UI's data-attribute pass only walks
1579
+ * enumerable keys, so `root` counts as read only when user code reads it.
1580
+ */
1581
+ function withLazyRoot(state, onReadRoot) {
1582
+ const { root, ...rest } = state;
1583
+ return Object.defineProperty(rest, "root", {
1584
+ get() {
1585
+ onReadRoot();
1586
+ return root;
1587
+ },
1588
+ enumerable: false
1589
+ });
1590
+ }
1591
+ /**
1592
+ * Subscribes to one day cell's state. The cell re-renders only when a field
1593
+ * it renders changes; `root` joins that comparison once a render function has
1594
+ * read `state.root`, so cells that never read it ignore focus and selection
1595
+ * changes elsewhere in the grid.
1596
+ */
1597
+ function useDayCellState(date, columnIndex, gridMonth) {
1598
+ const store = (0, react.useContext)(DayCellStoreContext);
1599
+ if (!store) throw new Error("Day cells must be used inside MonthView or WeeksView.");
1600
+ const { orientation } = (0, react.useContext)(GridContext);
1601
+ const readsRoot = (0, react.useRef)(false);
1602
+ const state = useStoreSelector(store, (0, react.useCallback)((s) => {
1603
+ const daysInWeek = date.daysInWeek;
1604
+ return computeDayCellState(date, {
1605
+ columnIndex: columnIndex ?? (date.dayOfWeek % daysInWeek - s.weekStartDay + daysInWeek) % daysInWeek,
1606
+ month: s.viewType === "weeks" ? {
1607
+ year: date.year,
1608
+ month: date.month
1609
+ } : gridMonth ?? s.viewMonth
1610
+ }, {
1611
+ ...s.inputs,
1612
+ orientation
1613
+ });
1614
+ }, [
1615
+ date,
1616
+ columnIndex,
1617
+ gridMonth,
1618
+ orientation
1619
+ ]), (0, react.useCallback)((a, b) => a.date === b.date && a.columnIndex === b.columnIndex && a.orientation === b.orientation && dayCellStateEqual(a, b) && (!readsRoot.current || a.root === b.root), []));
1620
+ return (0, react.useMemo)(() => withLazyRoot(state, () => {
1621
+ readsRoot.current = true;
1622
+ }), [state]);
1623
+ }
1624
+ /** Like {@link useDayCellState}, taking the grid month from the enclosing week. */
1625
+ function useStandaloneDayCellState(date) {
1626
+ return useDayCellState(date, void 0, (0, react.useContext)(WeekDataContext)?.gridMonth);
1627
+ }
1628
+ // src/month-view-context.ts
1305
1629
  /** @internal React context for MonthView stable values (config + callbacks). */
1306
1630
  const MonthViewStableContext = (0, react.createContext)(null);
1307
1631
  /** @internal React context for MonthView volatile state. */
@@ -1318,8 +1642,7 @@ function useMonthViewState() {
1318
1642
  if (!ctx) throw new Error("useMonthViewState must be used within MonthView.Root");
1319
1643
  return ctx;
1320
1644
  }
1321
- //#endregion
1322
- //#region src/month-view-navigation.ts
1645
+ // src/month-view-navigation.ts
1323
1646
  /**
1324
1647
  * Owns which month the view shows, in both controlled and uncontrolled mode:
1325
1648
  * the month state, the Prev/Next steps, and syncing the window with focus.
@@ -1438,8 +1761,7 @@ function useMonthNavigation(input) {
1438
1761
  goPrevMonth: (0, react.useCallback)(() => go("prev"), [go])
1439
1762
  };
1440
1763
  }
1441
- //#endregion
1442
- //#region src/month-window.ts
1764
+ // src/month-window.ts
1443
1765
  const monthIndex = (m) => m.year * 12 + (m.month - 1);
1444
1766
  /**
1445
1767
  * Builds the policy for a MonthView window of `numberOfMonths` months.
@@ -1489,34 +1811,7 @@ function monthAt(start, offset) {
1489
1811
  month: index % 12 + 1
1490
1812
  };
1491
1813
  }
1492
- //#endregion
1493
- //#region src/view-context.ts
1494
- /** @internal React context for the active view (MonthView or WeeksView). */
1495
- const ViewContext = (0, react.createContext)(null);
1496
- /** Returns the nearest view context (MonthView or WeeksView). */
1497
- function useViewContext() {
1498
- const ctx = (0, react.useContext)(ViewContext);
1499
- if (!ctx) throw new Error("useViewContext must be used within MonthView or WeeksView");
1500
- return ctx;
1501
- }
1502
- /** @internal Context for {@link ViewStableContextValue}. */
1503
- const ViewStableContext = (0, react.createContext)(null);
1504
- /** @internal Context for {@link ViewStateContextValue}. */
1505
- const ViewStateContext = (0, react.createContext)(null);
1506
- /** Returns the nearest view's stable values. */
1507
- function useViewStable() {
1508
- const ctx = (0, react.useContext)(ViewStableContext);
1509
- if (!ctx) throw new Error("useViewStable must be used within MonthView or WeeksView");
1510
- return ctx;
1511
- }
1512
- /** Returns the nearest view's volatile state. */
1513
- function useViewState() {
1514
- const ctx = (0, react.useContext)(ViewStateContext);
1515
- if (!ctx) throw new Error("useViewState must be used within MonthView or WeeksView");
1516
- return ctx;
1517
- }
1518
- //#endregion
1519
- //#region src/month-view.tsx
1814
+ // src/month-view.tsx
1520
1815
  function MonthViewRoot(props) {
1521
1816
  const { numberOfMonths: numberOfMonthsProp, fixedWeeks: fixedWeeksProp, outsideDays: outsideDaysProp, outOfRangeBehavior: outOfRangeBehaviorProp, month: monthProp, defaultMonth: defaultMonthProp, onMonthChange, children } = props;
1522
1817
  const numberOfMonths = Math.max(1, Math.min(numberOfMonthsProp ?? 1, 12));
@@ -1527,7 +1822,6 @@ function MonthViewRoot(props) {
1527
1822
  const calState = useCalendarState();
1528
1823
  const T = calStable.temporal;
1529
1824
  const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
1530
- const [focusedDate, setFocusedDate] = (0, react.useState)(() => resolveInitialAnchor(calState.selected, today, timeZone, T));
1531
1825
  const [gridHasFocus, setGridHasFocus] = (0, react.useState)(false);
1532
1826
  const gridFocusedRef = (0, react.useRef)(false);
1533
1827
  const { minValue, maxValue } = calStable;
@@ -1544,6 +1838,16 @@ function MonthViewRoot(props) {
1544
1838
  maxValue,
1545
1839
  T
1546
1840
  ]);
1841
+ const [focusedDate, setFocusedDate] = (0, react.useState)(() => {
1842
+ const anchor = resolveInitialAnchor(calState.selected, today, timeZone, T);
1843
+ const opening = monthProp ?? defaultMonthProp;
1844
+ if (!opening || windowPolicy.isVisible(opening, anchor)) return anchor;
1845
+ return T.PlainDate.from({
1846
+ year: opening.year,
1847
+ month: opening.month,
1848
+ day: 1
1849
+ });
1850
+ });
1547
1851
  const { currentMonth, viewingYearMonth, goNextMonth, goPrevMonth } = useMonthNavigation({
1548
1852
  month: monthProp,
1549
1853
  defaultMonth: defaultMonthProp,
@@ -1692,7 +1996,8 @@ function MonthViewRoot(props) {
1692
1996
  ]);
1693
1997
  const viewStableCtx = (0, react.useMemo)(() => ({
1694
1998
  gridFocusedRef,
1695
- outsideDays
1999
+ outsideDays,
2000
+ setFocusedDate
1696
2001
  }), [outsideDays]);
1697
2002
  const viewStateCtx = (0, react.useMemo)(() => ({
1698
2003
  currentDateTime,
@@ -1708,7 +2013,7 @@ function MonthViewRoot(props) {
1708
2013
  value: viewStateCtx,
1709
2014
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewContext.Provider, {
1710
2015
  value: viewCtx,
1711
- children
2016
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellStoreProvider, { children })
1712
2017
  })
1713
2018
  })
1714
2019
  })
@@ -1732,18 +2037,7 @@ function MonthView(props) {
1732
2037
  });
1733
2038
  }
1734
2039
  MonthView.Root = MonthViewRoot;
1735
- //#endregion
1736
- //#region src/context.ts
1737
- /** @internal Provides the days and index of the current week row to child components. */
1738
- const WeekDataContext = (0, react.createContext)(null);
1739
- /** @internal Provides the resolved grid orientation to descendant cells. */
1740
- const GridContext = (0, react.createContext)({ orientation: "horizontal" });
1741
- /** @internal Provides the date and column index from a `DayCellTemplate` to its child `DayButton`. */
1742
- const DayCellDataContext = (0, react.createContext)(null);
1743
- /** @internal Provides per-grid month data to WeekTemplate and DayCellTemplate. */
1744
- const GridMonthContext = (0, react.createContext)(null);
1745
- //#endregion
1746
- //#region src/grid-header.tsx
2040
+ // src/grid-header.tsx
1747
2041
  function useGridHeaderCellState(index) {
1748
2042
  const { locale, temporal: T, weekStartDay } = useCalendarStable();
1749
2043
  const { rootState } = useViewState();
@@ -1835,8 +2129,7 @@ function GridHeaderFn(props, ref) {
1835
2129
  }
1836
2130
  /** Table header section (`<thead>`) wrapping a row of {@link GridHeaderCell}s. */
1837
2131
  const GridHeader = (0, react.forwardRef)(GridHeaderFn);
1838
- //#endregion
1839
- //#region src/keyboard.ts
2132
+ // src/keyboard.ts
1840
2133
  /**
1841
2134
  * Computes the target date when jumping by `months` from `focusedDate`.
1842
2135
  *
@@ -1909,8 +2202,7 @@ function computeNextFocusDate(input) {
1909
2202
  if (nextDate) return clampToBounds(nextDate, focusedDate, minValue, maxValue, T);
1910
2203
  return { action: "none" };
1911
2204
  }
1912
- //#endregion
1913
- //#region src/month-separator.tsx
2205
+ // src/month-separator.tsx
1914
2206
  /** Context providing MonthSeparator data to child components. */
1915
2207
  const MonthSeparatorDataContext = (0, react.createContext)(null);
1916
2208
  function useMonthSeparatorData() {
@@ -2040,8 +2332,7 @@ function MonthSeparatorFn(props, ref) {
2040
2332
  * Default children: `<Month /> <Year />`.
2041
2333
  */
2042
2334
  const MonthSeparator = (0, react.forwardRef)(MonthSeparatorFn);
2043
- //#endregion
2044
- //#region src/weeks-view-context.ts
2335
+ // src/weeks-view-context.ts
2045
2336
  /** @internal React context for WeeksView stable values (config + callbacks). */
2046
2337
  const WeeksViewStableContext = (0, react.createContext)(null);
2047
2338
  /** @internal React context for WeeksView volatile state. */
@@ -2058,8 +2349,7 @@ function useWeeksViewState() {
2058
2349
  if (!ctx) throw new Error("useWeeksViewState must be used within WeeksView.Root");
2059
2350
  return ctx;
2060
2351
  }
2061
- //#endregion
2062
- //#region src/weeks-grid.tsx
2352
+ // src/weeks-grid.tsx
2063
2353
  /**
2064
2354
  * @internal WeeksGrid — renders continuous week rows for WeeksView.
2065
2355
  * Not exported from the public API.
@@ -2193,28 +2483,35 @@ function WeeksGrid(props) {
2193
2483
  }
2194
2484
  return map;
2195
2485
  }, [monthBoundaries]);
2486
+ const rowValues = (0, react.useMemo)(() => weeks.map((week, i) => {
2487
+ const days = weekDaysArrays[i] ?? [];
2488
+ return {
2489
+ weekData: {
2490
+ days,
2491
+ weekIndex: week.weekIndex,
2492
+ gridRowIndex: i + 2
2493
+ },
2494
+ gridMonth: {
2495
+ weeks: [days],
2496
+ year: week.year,
2497
+ month: week.month
2498
+ }
2499
+ };
2500
+ }), [weeks, weekDaysArrays]);
2196
2501
  const rows = [];
2197
2502
  for (const [i, week] of weeks.entries()) {
2198
- const days = weekDaysArrays[i];
2199
- if (days === void 0) continue;
2503
+ if (weekDaysArrays[i] === void 0) continue;
2200
2504
  const separators = boundariesAtIndex.get(i);
2201
2505
  if (separators && separatorTemplate.length > 0) for (const separatorData of separators) rows.push(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthSeparatorDataContext.Provider, {
2202
2506
  value: separatorData,
2203
2507
  children: separatorTemplate
2204
2508
  }, `sep-${separatorData.year}-${separatorData.month}`));
2205
- const weekGridRow = i + 2;
2509
+ const rowValue = rowValues[i];
2510
+ if (rowValue === void 0) continue;
2206
2511
  rows.push(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeekDataContext.Provider, {
2207
- value: {
2208
- days,
2209
- weekIndex: week.weekIndex,
2210
- gridRowIndex: weekGridRow
2211
- },
2512
+ value: rowValue.weekData,
2212
2513
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GridMonthContext.Provider, {
2213
- value: {
2214
- weeks: [days],
2215
- year: week.year,
2216
- month: week.month
2217
- },
2514
+ value: rowValue.gridMonth,
2218
2515
  children: weekRowChildren
2219
2516
  })
2220
2517
  }, week.startDate.toString()));
@@ -2225,8 +2522,7 @@ function WeeksGrid(props) {
2225
2522
  children: rows
2226
2523
  });
2227
2524
  }
2228
- //#endregion
2229
- //#region src/weeks-keyboard.ts
2525
+ // src/weeks-keyboard.ts
2230
2526
  const NONE = {
2231
2527
  action: "none",
2232
2528
  windowShift: 0,
@@ -2300,181 +2596,7 @@ function computeWeeksKeyNav(input) {
2300
2596
  default: return NONE;
2301
2597
  }
2302
2598
  }
2303
- //#endregion
2304
- //#region src/day-cell-state.ts
2305
- const NO_RANGE = {
2306
- rangeStart: false,
2307
- rangeEnd: false,
2308
- rangeBoundary: false,
2309
- inRange: false,
2310
- rangeIndex: false,
2311
- rangeLength: false,
2312
- rangeHasStart: false,
2313
- rangeHasEnd: false,
2314
- rangePreviewStart: false,
2315
- rangePreviewEnd: false,
2316
- rangePreviewBoundary: false,
2317
- rangePreviewInRange: false,
2318
- rangePreviewIndex: false,
2319
- rangePreviewLength: false,
2320
- rangePreviewHasStart: false,
2321
- rangePreviewHasEnd: false
2322
- };
2323
- function computeRangeState(date, inputs) {
2324
- const { T, selectionMode, rangeStart, rangeEnd, previewStart, previewEnd } = inputs;
2325
- if (selectionMode !== "range") return NO_RANGE;
2326
- const isRangeStart = rangeStart ? T.PlainDate.compare(date, rangeStart) === 0 : false;
2327
- const isRangeEnd = rangeEnd ? T.PlainDate.compare(date, rangeEnd) === 0 : false;
2328
- const inRange = isInRange(date, rangeStart, rangeEnd, T) !== false;
2329
- const effectiveStart = rangeStart ?? rangeEnd;
2330
- const effectiveEnd = rangeEnd ?? rangeStart;
2331
- const isPreviewStart = previewStart ? T.PlainDate.compare(date, previewStart) === 0 : false;
2332
- const isPreviewEnd = previewEnd ? T.PlainDate.compare(date, previewEnd) === 0 : false;
2333
- const inPreview = previewStart && previewEnd ? isInRange(date, previewStart, previewEnd, T) !== false : false;
2334
- return {
2335
- rangeStart: isRangeStart,
2336
- rangeEnd: isRangeEnd,
2337
- rangeBoundary: isRangeStart || isRangeEnd,
2338
- inRange,
2339
- rangeIndex: inRange && effectiveStart ? date.since(effectiveStart).days : false,
2340
- rangeLength: inRange && effectiveStart && effectiveEnd ? effectiveEnd.since(effectiveStart).days + 1 : false,
2341
- rangeHasStart: inRange && rangeStart !== void 0,
2342
- rangeHasEnd: inRange && rangeEnd !== void 0,
2343
- rangePreviewStart: isPreviewStart,
2344
- rangePreviewEnd: isPreviewEnd,
2345
- rangePreviewBoundary: isPreviewStart || isPreviewEnd,
2346
- rangePreviewInRange: inPreview,
2347
- rangePreviewIndex: inPreview && previewStart ? date.since(previewStart).days : false,
2348
- rangePreviewLength: inPreview && previewStart && previewEnd ? previewEnd.since(previewStart).days + 1 : false,
2349
- rangePreviewHasStart: inPreview && previewStart !== void 0,
2350
- rangePreviewHasEnd: inPreview && previewEnd !== void 0
2351
- };
2352
- }
2353
- /** Derives the state of one day cell. Pure function, no hooks. */
2354
- function computeDayCellState(date, position, inputs) {
2355
- const { T, outsideDays } = inputs;
2356
- const { columnIndex, month } = position;
2357
- const isCurrentMonth = date.year === month.year && date.month === month.month;
2358
- const base = {
2359
- root: inputs.rootState,
2360
- date,
2361
- columnIndex,
2362
- orientation: inputs.orientation
2363
- };
2364
- if (!isCurrentMonth && outsideDays === "hidden") return {
2365
- ...base,
2366
- ...NO_RANGE,
2367
- outsideMonth: true,
2368
- hidden: true,
2369
- selected: false,
2370
- today: false,
2371
- disabled: true,
2372
- focused: false,
2373
- isTabTarget: false
2374
- };
2375
- const isToday = T.PlainDate.compare(date, inputs.today) === 0;
2376
- if (!isCurrentMonth && outsideDays !== "enabled") {
2377
- const suppressRange = outsideDays === "disabled";
2378
- return {
2379
- ...base,
2380
- ...suppressRange ? NO_RANGE : computeRangeState(date, inputs),
2381
- outsideMonth: true,
2382
- hidden: false,
2383
- selected: false,
2384
- today: isToday,
2385
- disabled: true,
2386
- focused: false,
2387
- isTabTarget: false
2388
- };
2389
- }
2390
- return {
2391
- ...base,
2392
- ...computeRangeState(date, inputs),
2393
- outsideMonth: !isCurrentMonth,
2394
- hidden: false,
2395
- selected: inputs.selectionMode !== "range" ? inputs.selectedDates.some((d) => T.PlainDate.compare(d, date) === 0) : false,
2396
- today: isToday,
2397
- disabled: inputs.disabled || (inputs.isDateDisabled?.(date) ?? false),
2398
- focused: T.PlainDate.compare(date, inputs.focusedDate) === 0,
2399
- isTabTarget: T.PlainDate.compare(date, inputs.tabTargetDate) === 0
2400
- };
2401
- }
2402
- const COMPARED_KEYS = Object.keys({
2403
- outsideMonth: true,
2404
- hidden: true,
2405
- selected: true,
2406
- today: true,
2407
- disabled: true,
2408
- focused: true,
2409
- rangeStart: true,
2410
- rangeEnd: true,
2411
- rangeBoundary: true,
2412
- inRange: true,
2413
- rangeIndex: true,
2414
- rangeLength: true,
2415
- rangeHasStart: true,
2416
- rangeHasEnd: true,
2417
- rangePreviewStart: true,
2418
- rangePreviewEnd: true,
2419
- rangePreviewBoundary: true,
2420
- rangePreviewInRange: true,
2421
- rangePreviewIndex: true,
2422
- rangePreviewLength: true,
2423
- rangePreviewHasStart: true,
2424
- rangePreviewHasEnd: true,
2425
- isTabTarget: true
2426
- });
2427
- /** Whether two cell states render the same cell. */
2428
- function dayCellStateEqual(a, b) {
2429
- for (const key of COMPARED_KEYS) if (a[key] !== b[key]) return false;
2430
- return true;
2431
- }
2432
- /**
2433
- * Reads the contexts a day cell depends on and returns a function that derives
2434
- * the state of any cell. `columnIndex` defaults to the date's column in a
2435
- * week starting on the calendar's `weekStartDay`.
2436
- */
2437
- function useDayCellDeriver() {
2438
- const { orientation } = (0, react.useContext)(GridContext);
2439
- const weekData = (0, react.useContext)(WeekDataContext);
2440
- const { disabled, isDateDisabled, selectionMode, temporal: T, today, weekStartDay } = useCalendarStable();
2441
- const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
2442
- const viewCtx = useViewContext();
2443
- const { focusedDate, tabTargetDate } = viewCtx;
2444
- const { rootState, currentDateTime } = useViewState();
2445
- const { outsideDays } = useViewStable();
2446
- const inputs = {
2447
- T,
2448
- today: today(),
2449
- orientation,
2450
- rootState,
2451
- selectionMode,
2452
- selectedDates,
2453
- rangeStart,
2454
- rangeEnd,
2455
- previewStart,
2456
- previewEnd,
2457
- disabled,
2458
- isDateDisabled,
2459
- focusedDate,
2460
- tabTargetDate,
2461
- outsideDays
2462
- };
2463
- const isWeeksView = viewCtx.viewType === "weeks";
2464
- const gridMonth = weekData?.gridMonth ?? currentDateTime;
2465
- return (date, columnIndex) => {
2466
- const daysInWeek = date.daysInWeek;
2467
- return computeDayCellState(date, {
2468
- columnIndex: columnIndex ?? (date.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek,
2469
- month: isWeeksView ? {
2470
- year: date.year,
2471
- month: date.month
2472
- } : gridMonth
2473
- }, inputs);
2474
- };
2475
- }
2476
- //#endregion
2477
- //#region src/day-cell.tsx
2599
+ // src/day-cell.tsx
2478
2600
  const dayStateAttributesMapping = {
2479
2601
  root: () => null,
2480
2602
  date: (v) => v ? { "data-date": v.toString() } : null,
@@ -2503,34 +2625,46 @@ const dayStateAttributesMapping = {
2503
2625
  rangePreviewHasStart: (v) => v ? { "data-range-preview-has-start": "" } : null,
2504
2626
  rangePreviewHasEnd: (v) => v ? { "data-range-preview-has-end": "" } : null
2505
2627
  };
2628
+ /** @internal The enclosing day cell's state, read by its `DayButton`. */
2629
+ const DayCellStateContext = (0, react.createContext)(null);
2506
2630
  function DayCellInstanceFn(props) {
2507
- const { render, date, columnIndex, children, _derivedState, ...otherProps } = props;
2508
- const state = _derivedState;
2631
+ const { render, date, columnIndex, gridMonth, children, ...otherProps } = props;
2632
+ const derived = useDayCellState(date, columnIndex, gridMonth);
2633
+ const state = derived;
2634
+ const defaultProps = state.hidden ? {
2635
+ role: "gridcell",
2636
+ "aria-hidden": true,
2637
+ children: null
2638
+ } : {
2639
+ role: "gridcell",
2640
+ "aria-selected": state.selected || void 0,
2641
+ "aria-disabled": state.disabled || void 0,
2642
+ children: children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayButton, {})
2643
+ };
2509
2644
  const cell = (0, _base_ui_react_use_render.useRender)({
2510
2645
  defaultTagName: "td",
2511
2646
  render,
2512
2647
  ref: [],
2513
2648
  state,
2514
2649
  stateAttributesMapping: dayStateAttributesMapping,
2515
- props: (0, _base_ui_react_merge_props.mergeProps)(state.hidden ? {
2516
- role: "gridcell",
2517
- "aria-hidden": true,
2518
- children: null
2519
- } : {
2520
- role: "gridcell",
2521
- "aria-selected": state.selected || void 0,
2522
- "aria-disabled": state.disabled || void 0,
2523
- children: children ?? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayButton, { _derivedState })
2524
- }, otherProps)
2650
+ props: (0, _base_ui_react_merge_props.mergeProps)(defaultProps, otherProps)
2525
2651
  });
2526
2652
  const outsideDisabled = state.hidden || state.outsideMonth && state.disabled;
2653
+ const cellData = (0, react.useMemo)(() => ({
2654
+ date,
2655
+ columnIndex,
2656
+ outsideDisabled
2657
+ }), [
2658
+ date,
2659
+ columnIndex,
2660
+ outsideDisabled
2661
+ ]);
2527
2662
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellDataContext.Provider, {
2528
- value: {
2529
- date,
2530
- columnIndex,
2531
- outsideDisabled
2532
- },
2533
- children: cell
2663
+ value: cellData,
2664
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellStateContext.Provider, {
2665
+ value: derived,
2666
+ children: cell
2667
+ })
2534
2668
  });
2535
2669
  }
2536
2670
  /**
@@ -2546,24 +2680,35 @@ function passThroughPropsEqual(prev, next, handledKeys) {
2546
2680
  for (const key of prevKeys) if (prev[key] !== next[key]) return false;
2547
2681
  return true;
2548
2682
  }
2549
- const DAY_CELL_HANDLED_KEYS = new Set([
2683
+ const DAY_CELL_HANDLED_KEYS = /* @__PURE__ */ new Set([
2550
2684
  "render",
2551
2685
  "date",
2552
2686
  "columnIndex",
2553
- "children",
2554
- "_derivedState"
2687
+ "gridMonth",
2688
+ "children"
2555
2689
  ]);
2556
- const DAY_BUTTON_HANDLED_KEYS = new Set([
2690
+ const DAY_BUTTON_HANDLED_KEYS = /* @__PURE__ */ new Set([
2557
2691
  "render",
2558
2692
  "date",
2559
2693
  "children",
2560
- "_derivedState"
2694
+ "_state"
2561
2695
  ]);
2696
+ /**
2697
+ * Whether a day cell can skip a parent re-render. Its own state comes from
2698
+ * the store, so only props are compared here.
2699
+ */
2562
2700
  function dayCellInstancePropsAreEqual(prev, next) {
2563
- if (prev.date !== next.date || prev.render !== next.render || prev.columnIndex !== next.columnIndex) return false;
2564
- return dayCellStateEqual(prev._derivedState, next._derivedState) && passThroughPropsEqual(prev, next, DAY_CELL_HANDLED_KEYS);
2701
+ return prev.date === next.date && prev.render === next.render && prev.columnIndex === next.columnIndex && prev.gridMonth === next.gridMonth && passThroughPropsEqual(prev, next, DAY_CELL_HANDLED_KEYS);
2565
2702
  }
2566
2703
  const DayCellInstance = (0, react.memo)(DayCellInstanceFn, dayCellInstancePropsAreEqual);
2704
+ /** The cells of a whole month, for a `DayCellTemplate` outside any week row. */
2705
+ function MonthDayCells(props) {
2706
+ const days = (0, react.useContext)(MonthViewStateContext)?.weeks.flat() ?? [];
2707
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: days.map((day) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellInstance, {
2708
+ ...props,
2709
+ date: day
2710
+ }, day.toString())) });
2711
+ }
2567
2712
  /**
2568
2713
  * Renders one `<td role="gridcell">` per day. Exposes data-attributes for
2569
2714
  * `selected`, `today`, `disabled`, `outside-month`, `focused`,
@@ -2575,36 +2720,32 @@ const DayCellInstance = (0, react.memo)(DayCellInstanceFn, dayCellInstancePropsA
2575
2720
  function DayCellTemplate(props) {
2576
2721
  const { date: dateProp, ...restProps } = props;
2577
2722
  const weekData = (0, react.useContext)(WeekDataContext);
2578
- const monthState = (0, react.useContext)(MonthViewStateContext);
2579
- const derive = useDayCellDeriver();
2580
2723
  if (dateProp) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellInstance, {
2581
2724
  ...restProps,
2582
2725
  date: dateProp,
2583
- _derivedState: derive(dateProp)
2726
+ gridMonth: weekData?.gridMonth
2584
2727
  });
2585
- const days = weekData ? weekData.days : monthState?.weeks.flat() ?? [];
2586
- const perWeek = weekData != null;
2587
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: days.map((day, i) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellInstance, {
2728
+ if (!weekData) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MonthDayCells, { ...restProps });
2729
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: weekData.days.map((day, i) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellInstance, {
2588
2730
  ...restProps,
2589
2731
  date: day,
2590
- columnIndex: perWeek ? i : void 0,
2591
- _derivedState: derive(day, perWeek ? i : void 0)
2732
+ columnIndex: i,
2733
+ gridMonth: weekData.gridMonth
2592
2734
  }, day.toString())) });
2593
2735
  }
2594
2736
  function DayButtonInnerFn(props, ref) {
2595
- const { render, date, _derivedState, ...otherProps } = props;
2737
+ const { render, date, _state, ...otherProps } = props;
2596
2738
  const { onSelect, locale, readOnly, setHoveredDate } = useCalendarStable();
2597
- const { setFocusedDate } = useViewContext();
2598
- const { gridFocusedRef } = useViewStable();
2739
+ const { gridFocusedRef, setFocusedDate } = useViewStable();
2599
2740
  const internalRef = (0, react.useRef)(null);
2600
- const isHidden = _derivedState?.hidden ?? false;
2601
- const isFocused = _derivedState?.focused ?? false;
2602
- const isTabTarget = _derivedState?.isTabTarget ?? false;
2603
- const isDisabled = _derivedState?.disabled ?? false;
2741
+ const isHidden = _state.hidden;
2742
+ const isFocused = _state.focused;
2743
+ const isTabTarget = _state.isTabTarget;
2744
+ const isDisabled = _state.disabled;
2604
2745
  (0, react.useEffect)(() => {
2605
2746
  if (shouldMoveDomFocus(isFocused, gridFocusedRef.current) && internalRef.current) internalRef.current.focus();
2606
2747
  }, [isFocused, gridFocusedRef]);
2607
- const state = _derivedState;
2748
+ const state = _state;
2608
2749
  const defaultProps = isHidden ? {} : {
2609
2750
  type: "button",
2610
2751
  tabIndex: isTabTarget ? 0 : -1,
@@ -2636,12 +2777,7 @@ function DayButtonInnerFn(props, ref) {
2636
2777
  });
2637
2778
  }
2638
2779
  function dayButtonInnerPropsAreEqual(prev, next) {
2639
- if (prev.date !== next.date || prev.render !== next.render) return false;
2640
- if (!passThroughPropsEqual(prev, next, DAY_BUTTON_HANDLED_KEYS)) return false;
2641
- const a = prev._derivedState;
2642
- const b = next._derivedState;
2643
- if (!a || !b) return a === b;
2644
- return dayCellStateEqual(a, b);
2780
+ return prev.date === next.date && prev.render === next.render && prev._state === next._state && passThroughPropsEqual(prev, next, DAY_BUTTON_HANDLED_KEYS);
2645
2781
  }
2646
2782
  const DayButtonInner = (0, react.memo)((0, react.forwardRef)(DayButtonInnerFn), dayButtonInnerPropsAreEqual);
2647
2783
  /**
@@ -2651,39 +2787,37 @@ const DayButtonInner = (0, react.memo)((0, react.forwardRef)(DayButtonInnerFn),
2651
2787
  * Must be used inside a {@link DayCellTemplate} or given an explicit `date` prop.
2652
2788
  */
2653
2789
  function DayButtonFn(props, ref) {
2654
- const { date: dateProp, _derivedState, ...restProps } = props;
2790
+ const { date: dateProp, ...restProps } = props;
2791
+ const cellState = (0, react.useContext)(DayCellStateContext);
2655
2792
  const cellData = (0, react.useContext)(DayCellDataContext);
2793
+ if (cellState && (!dateProp || dateProp.equals(cellState.date))) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayButtonInner, {
2794
+ ...restProps,
2795
+ ref,
2796
+ date: cellState.date,
2797
+ _state: cellState
2798
+ });
2656
2799
  const resolvedDate = dateProp ?? cellData?.date;
2657
- if (resolvedDate) {
2658
- if (_derivedState) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayButtonInner, {
2659
- ...restProps,
2660
- ref,
2661
- date: resolvedDate,
2662
- _derivedState
2663
- });
2664
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayButtonFallback, {
2665
- ref,
2666
- ...restProps,
2667
- date: resolvedDate
2668
- });
2669
- }
2800
+ if (resolvedDate) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(StandaloneDayButton, {
2801
+ ...restProps,
2802
+ ref,
2803
+ date: resolvedDate
2804
+ });
2670
2805
  throw new Error("DayButton must be used inside DayCellTemplate or receive an explicit date prop.");
2671
2806
  }
2672
2807
  const DayButton = (0, react.forwardRef)(DayButtonFn);
2673
- /** Fallback for standalone DayButton usage that needs to read context. */
2674
- function DayButtonFallbackFn(props, ref) {
2808
+ /** A `DayButton` outside any day cell: subscribes to its own date's state. */
2809
+ function StandaloneDayButtonFn(props, ref) {
2675
2810
  const { date, ...restProps } = props;
2676
- const derive = useDayCellDeriver();
2811
+ const state = useStandaloneDayCellState(date);
2677
2812
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayButtonInner, {
2678
2813
  ...restProps,
2679
2814
  ref,
2680
2815
  date,
2681
- _derivedState: derive(date)
2816
+ _state: state
2682
2817
  });
2683
2818
  }
2684
- const DayButtonFallback = (0, react.forwardRef)(DayButtonFallbackFn);
2685
- //#endregion
2686
- //#region src/grid.tsx
2819
+ const StandaloneDayButton = (0, react.forwardRef)(StandaloneDayButtonFn);
2820
+ // src/grid.tsx
2687
2821
  function useGridKeyboard() {
2688
2822
  const { onSelect, disabled, readOnly, isDateDisabled, minValue, maxValue, temporal: T, weekStartDay } = useCalendarStable();
2689
2823
  const { focusedDate, setFocusedDate } = useViewContext();
@@ -3017,19 +3151,20 @@ function WeekInstance(props) {
3017
3151
  const { render, ...otherProps } = props;
3018
3152
  const weekData = (0, react.useContext)(WeekDataContext);
3019
3153
  const { rootState } = useViewState();
3154
+ const state = (0, react.useMemo)(() => ({
3155
+ root: rootState,
3156
+ weekIndex: weekData.weekIndex,
3157
+ gridRowIndex: weekData.gridRowIndex
3158
+ }), [
3159
+ rootState,
3160
+ weekData.weekIndex,
3161
+ weekData.gridRowIndex
3162
+ ]);
3020
3163
  return (0, _base_ui_react_use_render.useRender)({
3021
3164
  defaultTagName: "tr",
3022
3165
  render,
3023
3166
  ref: [],
3024
- state: (0, react.useMemo)(() => ({
3025
- root: rootState,
3026
- weekIndex: weekData.weekIndex,
3027
- gridRowIndex: weekData.gridRowIndex
3028
- }), [
3029
- rootState,
3030
- weekData.weekIndex,
3031
- weekData.gridRowIndex
3032
- ]),
3167
+ state,
3033
3168
  stateAttributesMapping: weekInstanceStateAttributesMapping,
3034
3169
  props: (0, _base_ui_react_merge_props.mergeProps)({}, otherProps)
3035
3170
  });
@@ -3047,23 +3182,27 @@ function WeekTemplate(props) {
3047
3182
  year: gridMonthCtx.year,
3048
3183
  month: gridMonthCtx.month
3049
3184
  } : void 0, [gridMonthCtx]);
3050
- const Instance = WeekInstance;
3051
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: weeks.map((weekDays, i) => {
3052
- const firstDay = weekDays[0];
3185
+ const gridRowIndex = outerWeekData?.gridRowIndex;
3186
+ const weekValues = (0, react.useMemo)(() => weeks.map((weekDays, i) => ({
3187
+ days: weekDays,
3188
+ weekIndex: i,
3189
+ ...gridMonth !== void 0 && { gridMonth },
3190
+ ...gridRowIndex !== void 0 && { gridRowIndex }
3191
+ })), [
3192
+ weeks,
3193
+ gridMonth,
3194
+ gridRowIndex
3195
+ ]);
3196
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: weekValues.map((value) => {
3197
+ const firstDay = value.days[0];
3053
3198
  if (firstDay === void 0) return null;
3054
3199
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeekDataContext.Provider, {
3055
- value: {
3056
- days: weekDays,
3057
- weekIndex: i,
3058
- ...gridMonth !== void 0 && { gridMonth },
3059
- ...outerWeekData?.gridRowIndex !== void 0 && { gridRowIndex: outerWeekData.gridRowIndex }
3060
- },
3061
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Instance, { ...props })
3200
+ value,
3201
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WeekInstance, { ...props })
3062
3202
  }, firstDay.toString());
3063
3203
  }) });
3064
3204
  }
3065
- //#endregion
3066
- //#region src/navigation.tsx
3205
+ // src/navigation.tsx
3067
3206
  const dateStringStateAttributesMapping = {
3068
3207
  root: () => null,
3069
3208
  month: () => null,
@@ -3299,8 +3438,7 @@ function NextMonthButtonFn(props, ref) {
3299
3438
  * when the next month falls after `max`. Exposes `data-direction="next"`.
3300
3439
  */
3301
3440
  const NextMonthButton = (0, react.forwardRef)(NextMonthButtonFn);
3302
- //#endregion
3303
- //#region src/selected-range.tsx
3441
+ // src/selected-range.tsx
3304
3442
  const rangeOverlayStateAttributesMapping = {
3305
3443
  root: () => null,
3306
3444
  active: (v) => v ? { "data-active": "" } : null,
@@ -3420,8 +3558,7 @@ function RangeSelectedInnerFn(props, ref) {
3420
3558
  });
3421
3559
  }
3422
3560
  const RangeSelectedInner = (0, react.forwardRef)(RangeSelectedInnerFn);
3423
- //#endregion
3424
- //#region src/range-preview.tsx
3561
+ // src/range-preview.tsx
3425
3562
  function RangePreviewFn(props, ref) {
3426
3563
  const { selectionMode } = useCalendarStable();
3427
3564
  if (selectionMode !== "range") return null;
@@ -3494,8 +3631,7 @@ function RangePreviewInnerFn(props, ref) {
3494
3631
  });
3495
3632
  }
3496
3633
  const RangePreviewInner = (0, react.forwardRef)(RangePreviewInnerFn);
3497
- //#endregion
3498
- //#region src/drag-handle.tsx
3634
+ // src/drag-handle.tsx
3499
3635
  const dragHandleStateAttributesMapping = {
3500
3636
  root: () => null,
3501
3637
  active: (v) => v ? { "data-active": "" } : null,
@@ -3514,32 +3650,34 @@ function useDragHandle(edge, { dragging: draggingProp }) {
3514
3650
  const isActive = readOnly || disabled || outsideDisabled ? false : edge === "start" ? !!(date && rangeStart && T.PlainDate.compare(date, rangeStart) === 0) : !!(date && rangeEnd && T.PlainDate.compare(date, rangeEnd) === 0);
3515
3651
  const isDragging = !!draggingProp && isActive;
3516
3652
  const handleRef = (0, react.useRef)(null);
3653
+ const state = (0, react.useMemo)(() => ({
3654
+ root: rootState,
3655
+ active: isActive,
3656
+ dragging: isDragging,
3657
+ edge,
3658
+ orientation
3659
+ }), [
3660
+ rootState,
3661
+ isActive,
3662
+ isDragging,
3663
+ edge,
3664
+ orientation
3665
+ ]);
3666
+ const defaultProps = {
3667
+ role: "slider",
3668
+ "aria-roledescription": "drag handle",
3669
+ "aria-label": edge === "start" ? "Range start date" : "Range end date",
3670
+ "aria-valuetext": date?.toString(),
3671
+ "aria-grabbed": isActive ? isDragging : void 0,
3672
+ "aria-hidden": isActive ? void 0 : true,
3673
+ onPointerEnter: () => {
3674
+ if (date && !outsideDisabled) setHoveredDate(date);
3675
+ }
3676
+ };
3517
3677
  return {
3518
- state: (0, react.useMemo)(() => ({
3519
- root: rootState,
3520
- active: isActive,
3521
- dragging: isDragging,
3522
- edge,
3523
- orientation
3524
- }), [
3525
- rootState,
3526
- isActive,
3527
- isDragging,
3528
- edge,
3529
- orientation
3530
- ]),
3678
+ state,
3531
3679
  stateAttributesMapping: dragHandleStateAttributesMapping,
3532
- defaultProps: {
3533
- role: "slider",
3534
- "aria-roledescription": "drag handle",
3535
- "aria-label": edge === "start" ? "Range start date" : "Range end date",
3536
- "aria-valuetext": date?.toString(),
3537
- "aria-grabbed": isActive ? isDragging : void 0,
3538
- "aria-hidden": isActive ? void 0 : true,
3539
- onPointerEnter: () => {
3540
- if (date && !outsideDisabled) setHoveredDate(date);
3541
- }
3542
- },
3680
+ defaultProps,
3543
3681
  handleRef
3544
3682
  };
3545
3683
  }
@@ -3580,8 +3718,7 @@ function RangeEndDragHandleFn(props, ref) {
3580
3718
  }
3581
3719
  /** Convenience wrapper for {@link RangeDragHandle} with `edge="end"`. */
3582
3720
  const RangeEndDragHandle = (0, react.forwardRef)(RangeEndDragHandleFn);
3583
- //#endregion
3584
- //#region src/week-number.tsx
3721
+ // src/week-number.tsx
3585
3722
  const weekNumberCellStateAttributesMapping = {
3586
3723
  root: () => null,
3587
3724
  weekNumber: (v) => ({ "data-week-number": String(v) })
@@ -3646,8 +3783,7 @@ function WeekNumberHeaderFn(props, ref) {
3646
3783
  * Column header for the week number column. Renders a `<th scope="col">`.
3647
3784
  */
3648
3785
  const WeekNumberHeader = (0, react.forwardRef)(WeekNumberHeaderFn);
3649
- //#endregion
3650
- //#region src/compute-weeks-in-window.ts
3786
+ // src/compute-weeks-in-window.ts
3651
3787
  /**
3652
3788
  * Computes an array of week descriptors for a continuous window.
3653
3789
  * `firstWeek` is snapped back to the nearest `weekStartDay`.
@@ -3669,8 +3805,7 @@ function computeWeeksInWindow(firstWeek, weekCount, weekStartDay, _T) {
3669
3805
  }
3670
3806
  return weeks;
3671
3807
  }
3672
- //#endregion
3673
- //#region src/resolve-first-week.ts
3808
+ // src/resolve-first-week.ts
3674
3809
  /** Snaps `date` back to the start of its containing week (determined by `weekStartDay`). */
3675
3810
  function snapToWeekStart(date, weekStartDay) {
3676
3811
  const offset = (date.dayOfWeek % 7 - weekStartDay + 7) % 7;
@@ -3761,8 +3896,7 @@ function resolveFirstWeek(currentFirstWeek, weekCount, target, options) {
3761
3896
  }
3762
3897
  }
3763
3898
  }
3764
- //#endregion
3765
- //#region src/out-of-range.ts
3899
+ // src/out-of-range.ts
3766
3900
  /** Returns true if any day in the 7-day span starting at weekStart falls within [min, max]. */
3767
3901
  function weekHasValidDay(weekStart, min, max, T) {
3768
3902
  const weekEnd = weekStart.add({ days: 6 });
@@ -3831,8 +3965,7 @@ function applyOutOfRange({ targetFirstWeek, weekCount, behavior, min, max, weekS
3831
3965
  weekCount
3832
3966
  };
3833
3967
  }
3834
- //#endregion
3835
- //#region src/weeks-window.ts
3968
+ // src/weeks-window.ts
3836
3969
  function createWeekWindowPolicy({ weekCount, behavior, min, max, weekStartDay, T }) {
3837
3970
  const bounds = {
3838
3971
  weekCount,
@@ -3857,15 +3990,15 @@ function createWeekWindowPolicy({ weekCount, behavior, min, max, weekStartDay, T
3857
3990
  moveTo,
3858
3991
  shift: (current, direction, shiftBy) => {
3859
3992
  const by = shiftBy ?? current.weekCount;
3860
- const next = moveTo(current.firstWeek.add({ weeks: direction * by }));
3993
+ const target = current.firstWeek.add({ weeks: direction * by });
3994
+ const next = moveTo(target);
3861
3995
  if (next === null) return null;
3862
3996
  if ((behavior === "snap" || behavior === "snap-shrink") && isBounded && T.PlainDate.compare(next.firstWeek, current.firstWeek) === 0) return null;
3863
3997
  return next;
3864
3998
  }
3865
3999
  };
3866
4000
  }
3867
- //#endregion
3868
- //#region src/weeks-view-state.ts
4001
+ // src/weeks-view-state.ts
3869
4002
  /**
3870
4003
  * Core state management hook for WeeksView.Root.
3871
4004
  * Reads calendar-level context and manages weeks-specific state.
@@ -4190,7 +4323,8 @@ function useWeeksViewRootState(props) {
4190
4323
  ]),
4191
4324
  viewStableCtx: (0, react.useMemo)(() => ({
4192
4325
  gridFocusedRef,
4193
- outsideDays: "enabled"
4326
+ outsideDays: "enabled",
4327
+ setFocusedDate
4194
4328
  }), []),
4195
4329
  viewStateCtx: (0, react.useMemo)(() => ({
4196
4330
  currentDateTime,
@@ -4199,8 +4333,7 @@ function useWeeksViewRootState(props) {
4199
4333
  scrollToWeek
4200
4334
  };
4201
4335
  }
4202
- //#endregion
4203
- //#region src/weeks-view.tsx
4336
+ // src/weeks-view.tsx
4204
4337
  function WeeksViewRootFn(props, ref) {
4205
4338
  const { children } = props;
4206
4339
  const { stableCtx, stateCtx, viewCtx, viewStableCtx, viewStateCtx, scrollToWeek } = useWeeksViewRootState(props);
@@ -4215,7 +4348,7 @@ function WeeksViewRootFn(props, ref) {
4215
4348
  value: viewStateCtx,
4216
4349
  children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewContext.Provider, {
4217
4350
  value: viewCtx,
4218
- children
4351
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellStoreProvider, { children })
4219
4352
  })
4220
4353
  })
4221
4354
  })
@@ -4242,8 +4375,7 @@ function WeeksViewFn(props, ref) {
4242
4375
  }
4243
4376
  const WeeksView = (0, react.forwardRef)(WeeksViewFn);
4244
4377
  WeeksView.Root = WeeksViewRoot;
4245
- //#endregion
4246
- //#region src/weeks-navigation.tsx
4378
+ // src/weeks-navigation.tsx
4247
4379
  const weeksNavButtonStateAttributesMapping = {
4248
4380
  root: () => null,
4249
4381
  direction: (v) => ({ "data-direction": v }),
@@ -4352,7 +4484,6 @@ function WeekCountFn(props, ref) {
4352
4484
  * out-of-range modes where the actual count may be less than the `weekCount` prop.
4353
4485
  */
4354
4486
  const WeekCount = (0, react.forwardRef)(WeekCountFn);
4355
- //#endregion
4356
4487
  Object.defineProperty(exports, "CalendarProvider", {
4357
4488
  enumerable: true,
4358
4489
  get: function() {