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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/formats/date.cjs +1 -1
  2. package/dist/formats/date.d.cts +1 -1
  3. package/dist/formats/date.d.mts +1 -1
  4. package/dist/formats/date.mjs +1 -1
  5. package/dist/formats/object.cjs +1 -1
  6. package/dist/formats/object.d.cts +1 -1
  7. package/dist/formats/object.d.mts +1 -1
  8. package/dist/formats/object.mjs +1 -1
  9. package/dist/formats/plain-date-time.cjs +1 -1
  10. package/dist/formats/plain-date-time.d.cts +1 -1
  11. package/dist/formats/plain-date-time.d.mts +1 -1
  12. package/dist/formats/plain-date-time.mjs +1 -1
  13. package/dist/formats/plain-date.cjs +1 -1
  14. package/dist/formats/plain-date.d.cts +1 -1
  15. package/dist/formats/plain-date.d.mts +1 -1
  16. package/dist/formats/plain-date.mjs +1 -1
  17. package/dist/formats/plain-month-day.cjs +1 -1
  18. package/dist/formats/plain-month-day.d.cts +1 -1
  19. package/dist/formats/plain-month-day.d.mts +1 -1
  20. package/dist/formats/plain-month-day.mjs +1 -1
  21. package/dist/formats/plain-year-month.cjs +1 -1
  22. package/dist/formats/plain-year-month.d.cts +1 -1
  23. package/dist/formats/plain-year-month.d.mts +1 -1
  24. package/dist/formats/plain-year-month.mjs +1 -1
  25. package/dist/formats/zoned-date-time.cjs +1 -1
  26. package/dist/formats/zoned-date-time.d.cts +1 -1
  27. package/dist/formats/zoned-date-time.d.mts +1 -1
  28. package/dist/formats/zoned-date-time.mjs +1 -1
  29. package/dist/index.cjs +1 -1
  30. package/dist/index.d.cts +1 -1
  31. package/dist/index.d.mts +1 -1
  32. package/dist/index.mjs +1 -1
  33. package/dist/{weeks-navigation-elCpdfQ3.mjs → weeks-navigation-CEV8w_69.mjs} +210 -196
  34. package/dist/{weeks-navigation-DV538__x.cjs → weeks-navigation-q3beZn2n.cjs} +210 -196
  35. package/dist/{weeks-view-context-CUCy8njs.d.cts → weeks-view-context-BLZZm1fq.d.mts} +31 -2
  36. package/dist/{weeks-view-context-E1NwyNgQ.d.mts → weeks-view-context-CkTcdZpj.d.cts} +31 -2
  37. package/package.json +1 -1
  38. package/src/month-view-navigation.ts +180 -0
  39. package/src/month-view-types.ts +3 -0
  40. package/src/month-view.bounds.test.tsx +136 -0
  41. package/src/month-view.tsx +37 -205
  42. package/src/month-window.test.ts +207 -0
  43. package/src/month-window.ts +93 -0
  44. package/src/navigation.tsx +15 -74
@@ -1307,6 +1307,177 @@ function useMonthViewState() {
1307
1307
  return ctx;
1308
1308
  }
1309
1309
  //#endregion
1310
+ //#region src/month-view-navigation.ts
1311
+ /**
1312
+ * Owns which month the view shows, in both controlled and uncontrolled mode:
1313
+ * the month state, the Prev/Next steps, and syncing the window with focus.
1314
+ *
1315
+ * `onMonthChange` fires exactly once per move: from the effect below when
1316
+ * uncontrolled, and from the step or the focus-sync effect when controlled.
1317
+ */
1318
+ function useMonthNavigation(input) {
1319
+ const { month, defaultMonth, initialAnchor, onMonthChange, policy, focusedDate, setFocusedDate, T } = input;
1320
+ const isControlled = month !== void 0;
1321
+ const [internalMonth, setInternalMonth] = useState(() => {
1322
+ if (month) return {
1323
+ year: month.year,
1324
+ month: month.month
1325
+ };
1326
+ if (defaultMonth) return {
1327
+ year: defaultMonth.year,
1328
+ month: defaultMonth.month
1329
+ };
1330
+ const anchor = initialAnchor();
1331
+ return {
1332
+ year: anchor.year,
1333
+ month: anchor.month
1334
+ };
1335
+ });
1336
+ const year = isControlled ? month.year : internalMonth.year;
1337
+ const monthNumber = isControlled ? month.month : internalMonth.month;
1338
+ const currentMonth = useMemo(() => ({
1339
+ year,
1340
+ month: monthNumber
1341
+ }), [year, monthNumber]);
1342
+ const viewingYearMonth = useMemo(() => T.PlainYearMonth.from({
1343
+ year,
1344
+ month: monthNumber
1345
+ }), [
1346
+ year,
1347
+ monthNumber,
1348
+ T
1349
+ ]);
1350
+ const onMonthChangeRef = useRef(onMonthChange);
1351
+ onMonthChangeRef.current = onMonthChange;
1352
+ const notifyMonth = useCallback((target) => {
1353
+ onMonthChangeRef.current?.(T.PlainYearMonth.from(target));
1354
+ }, [T]);
1355
+ const skipFocusSyncForRef = useRef(null);
1356
+ const prevFocusedRef = useRef(focusedDate);
1357
+ useEffect(() => {
1358
+ skipFocusSyncForRef.current = null;
1359
+ }, [month?.year, month?.month]);
1360
+ useEffect(() => {
1361
+ const prevFocused = prevFocusedRef.current;
1362
+ prevFocusedRef.current = focusedDate;
1363
+ const target = {
1364
+ year: focusedDate.year,
1365
+ month: focusedDate.month
1366
+ };
1367
+ if (isControlled) {
1368
+ if (skipFocusSyncForRef.current === focusedDate) {
1369
+ skipFocusSyncForRef.current = null;
1370
+ return;
1371
+ }
1372
+ if (prevFocused === focusedDate) return;
1373
+ if (policy.isVisible(currentMonth, target)) return;
1374
+ notifyMonth(policy.reveal(currentMonth, target));
1375
+ return;
1376
+ }
1377
+ setInternalMonth((prev) => {
1378
+ const next = policy.reveal(prev, target);
1379
+ if (next.year === prev.year && next.month === prev.month) return prev;
1380
+ return next;
1381
+ });
1382
+ }, [
1383
+ focusedDate,
1384
+ isControlled,
1385
+ policy,
1386
+ currentMonth,
1387
+ notifyMonth
1388
+ ]);
1389
+ const mountedRef = useRef(false);
1390
+ useEffect(() => {
1391
+ if (!mountedRef.current) {
1392
+ mountedRef.current = true;
1393
+ return;
1394
+ }
1395
+ if (isControlled) return;
1396
+ onMonthChangeRef.current?.(viewingYearMonth);
1397
+ }, [viewingYearMonth, isControlled]);
1398
+ const go = useCallback((direction) => {
1399
+ if (isControlled) {
1400
+ const step = policy.step(month, direction);
1401
+ if (step.blocked) return;
1402
+ const nextFocus = focusedDateForMonth(focusedDate, step.start, step.firstDay);
1403
+ skipFocusSyncForRef.current = nextFocus;
1404
+ notifyMonth(step.start);
1405
+ setFocusedDate(nextFocus);
1406
+ return;
1407
+ }
1408
+ setInternalMonth((m) => {
1409
+ const step = policy.step(m, direction);
1410
+ if (step.blocked) return m;
1411
+ setFocusedDate((prev) => focusedDateForMonth(prev, step.start, step.firstDay));
1412
+ return step.start;
1413
+ });
1414
+ }, [
1415
+ isControlled,
1416
+ month,
1417
+ policy,
1418
+ notifyMonth,
1419
+ focusedDate,
1420
+ setFocusedDate
1421
+ ]);
1422
+ return {
1423
+ currentMonth,
1424
+ viewingYearMonth,
1425
+ goNextMonth: useCallback(() => go("next"), [go]),
1426
+ goPrevMonth: useCallback(() => go("prev"), [go])
1427
+ };
1428
+ }
1429
+ //#endregion
1430
+ //#region src/month-window.ts
1431
+ const monthIndex = (m) => m.year * 12 + (m.month - 1);
1432
+ /**
1433
+ * Builds the policy for a MonthView window of `numberOfMonths` months.
1434
+ *
1435
+ * `unbounded` never blocks a step: `min`/`max` only gate which days are
1436
+ * selectable, not which months can be viewed. Any other behavior is `stop`,
1437
+ * which blocks a step whose entering month lies beyond the bound's month.
1438
+ */
1439
+ function createMonthWindowPolicy(input) {
1440
+ const { numberOfMonths, behavior, min, max, T } = input;
1441
+ return {
1442
+ step(start, direction) {
1443
+ const adjacent = computeAdjacentMonth(start, direction, T);
1444
+ const newStart = {
1445
+ year: adjacent.year,
1446
+ month: adjacent.month
1447
+ };
1448
+ const entering = direction === "prev" ? newStart : monthAt(newStart, numberOfMonths - 1);
1449
+ let blocked = false;
1450
+ if (behavior !== "unbounded") {
1451
+ const bound = direction === "prev" ? min : max;
1452
+ if (bound) {
1453
+ const diff = monthIndex(entering) - monthIndex(bound);
1454
+ blocked = direction === "prev" ? diff < 0 : diff > 0;
1455
+ }
1456
+ }
1457
+ return {
1458
+ start: newStart,
1459
+ entering,
1460
+ firstDay: adjacent.firstDay,
1461
+ blocked
1462
+ };
1463
+ },
1464
+ isVisible(start, target) {
1465
+ const offset = monthIndex(target) - monthIndex(start);
1466
+ return offset >= 0 && offset < numberOfMonths;
1467
+ },
1468
+ reveal(start, target) {
1469
+ return shiftWindowToMonth(start, target, numberOfMonths);
1470
+ }
1471
+ };
1472
+ }
1473
+ function monthAt(start, offset) {
1474
+ const index = monthIndex(start) + offset;
1475
+ return {
1476
+ year: Math.floor(index / 12),
1477
+ month: index % 12 + 1
1478
+ };
1479
+ }
1480
+ //#endregion
1310
1481
  //#region src/view-context.ts
1311
1482
  /** @internal React context for the active view (MonthView or WeeksView). */
1312
1483
  const ViewContext = createContext(null);
@@ -1344,29 +1515,38 @@ function MonthViewRoot(props) {
1344
1515
  const calState = useCalendarState();
1345
1516
  const T = calStable.temporal;
1346
1517
  const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
1347
- const isMonthControlled = monthProp !== void 0;
1348
- const [internalMonth, setInternalMonth] = useState(() => {
1349
- if (monthProp) return {
1350
- year: monthProp.year,
1351
- month: monthProp.month
1352
- };
1353
- if (defaultMonthProp) return {
1354
- year: defaultMonthProp.year,
1355
- month: defaultMonthProp.month
1356
- };
1357
- const anchor = resolveInitialAnchor(calState.selected, today, timeZone, T);
1358
- return {
1359
- year: anchor.year,
1360
- month: anchor.month
1361
- };
1362
- });
1363
- const currentMonth = isMonthControlled ? {
1364
- year: monthProp.year,
1365
- month: monthProp.month
1366
- } : internalMonth;
1367
1518
  const [focusedDate, setFocusedDate] = useState(() => resolveInitialAnchor(calState.selected, today, timeZone, T));
1368
1519
  const [gridHasFocus, setGridHasFocus] = useState(false);
1369
1520
  const gridFocusedRef = useRef(false);
1521
+ const { minValue, maxValue } = calStable;
1522
+ const windowPolicy = useMemo(() => createMonthWindowPolicy({
1523
+ numberOfMonths,
1524
+ behavior: outOfRangeBehavior,
1525
+ min: minValue,
1526
+ max: maxValue,
1527
+ T
1528
+ }), [
1529
+ numberOfMonths,
1530
+ outOfRangeBehavior,
1531
+ minValue,
1532
+ maxValue,
1533
+ T
1534
+ ]);
1535
+ const { currentMonth, viewingYearMonth, goNextMonth, goPrevMonth } = useMonthNavigation({
1536
+ month: monthProp,
1537
+ defaultMonth: defaultMonthProp,
1538
+ initialAnchor: useCallback(() => resolveInitialAnchor(calState.selected, today, timeZone, T), [
1539
+ calState.selected,
1540
+ today,
1541
+ timeZone,
1542
+ T
1543
+ ]),
1544
+ onMonthChange,
1545
+ policy: windowPolicy,
1546
+ focusedDate,
1547
+ setFocusedDate,
1548
+ T
1549
+ });
1370
1550
  const [gridLabelIds, setGridLabelIds] = useState({});
1371
1551
  const setGridLabelId = useCallback((monthIndex, id) => {
1372
1552
  setGridLabelIds((prev) => {
@@ -1383,140 +1563,6 @@ function MonthViewRoot(props) {
1383
1563
  };
1384
1564
  });
1385
1565
  }, []);
1386
- const onMonthChangeRef = useRef(onMonthChange);
1387
- onMonthChangeRef.current = onMonthChange;
1388
- const notifyMonth = useCallback((year, month) => {
1389
- onMonthChangeRef.current?.(T.PlainYearMonth.from({
1390
- year,
1391
- month
1392
- }));
1393
- }, [T]);
1394
- const isMonthVisible = useCallback((targetYear, targetMonth) => {
1395
- for (let i = 0; i < numberOfMonths; i++) {
1396
- const totalMonths = currentMonth.year * 12 + (currentMonth.month - 1) + i;
1397
- const y = Math.floor(totalMonths / 12);
1398
- const m = totalMonths % 12 + 1;
1399
- if (targetYear === y && targetMonth === m) return true;
1400
- }
1401
- return false;
1402
- }, [
1403
- currentMonth.year,
1404
- currentMonth.month,
1405
- numberOfMonths
1406
- ]);
1407
- const navigateToMonth = useCallback((targetYear, targetMonth) => {
1408
- if (isMonthControlled) return;
1409
- setInternalMonth((prev) => {
1410
- const next = shiftWindowToMonth(prev, {
1411
- year: targetYear,
1412
- month: targetMonth
1413
- }, numberOfMonths);
1414
- if (next.year === prev.year && next.month === prev.month) return prev;
1415
- return next;
1416
- });
1417
- }, [isMonthControlled, numberOfMonths]);
1418
- const skipFocusSyncForRef = useRef(null);
1419
- const prevFocusedRef = useRef(focusedDate);
1420
- useEffect(() => {
1421
- skipFocusSyncForRef.current = null;
1422
- }, [monthProp?.year, monthProp?.month]);
1423
- useEffect(() => {
1424
- const prevFocused = prevFocusedRef.current;
1425
- prevFocusedRef.current = focusedDate;
1426
- if (isMonthControlled) {
1427
- if (skipFocusSyncForRef.current === focusedDate) {
1428
- skipFocusSyncForRef.current = null;
1429
- return;
1430
- }
1431
- if (prevFocused === focusedDate) return;
1432
- if (isMonthVisible(focusedDate.year, focusedDate.month)) return;
1433
- const next = shiftWindowToMonth({
1434
- year: currentMonth.year,
1435
- month: currentMonth.month
1436
- }, {
1437
- year: focusedDate.year,
1438
- month: focusedDate.month
1439
- }, numberOfMonths);
1440
- notifyMonth(next.year, next.month);
1441
- return;
1442
- }
1443
- navigateToMonth(focusedDate.year, focusedDate.month);
1444
- }, [
1445
- focusedDate,
1446
- navigateToMonth,
1447
- isMonthControlled,
1448
- isMonthVisible,
1449
- notifyMonth,
1450
- currentMonth.year,
1451
- currentMonth.month,
1452
- numberOfMonths
1453
- ]);
1454
- const goNextMonth = useCallback(() => {
1455
- if (isMonthControlled) {
1456
- const { year, month, firstDay } = computeAdjacentMonth({
1457
- year: monthProp.year,
1458
- month: monthProp.month
1459
- }, "next", T);
1460
- const nextFocus = focusedDateForMonth(focusedDate, {
1461
- year,
1462
- month
1463
- }, firstDay);
1464
- skipFocusSyncForRef.current = nextFocus;
1465
- notifyMonth(year, month);
1466
- setFocusedDate(nextFocus);
1467
- return;
1468
- }
1469
- setInternalMonth((m) => {
1470
- const { year, month, firstDay } = computeAdjacentMonth(m, "next", T);
1471
- setFocusedDate((prev) => focusedDateForMonth(prev, {
1472
- year,
1473
- month
1474
- }, firstDay));
1475
- return {
1476
- year,
1477
- month
1478
- };
1479
- });
1480
- }, [
1481
- T,
1482
- isMonthControlled,
1483
- monthProp,
1484
- notifyMonth,
1485
- focusedDate
1486
- ]);
1487
- const goPrevMonth = useCallback(() => {
1488
- if (isMonthControlled) {
1489
- const { year, month, firstDay } = computeAdjacentMonth({
1490
- year: monthProp.year,
1491
- month: monthProp.month
1492
- }, "prev", T);
1493
- const nextFocus = focusedDateForMonth(focusedDate, {
1494
- year,
1495
- month
1496
- }, firstDay);
1497
- skipFocusSyncForRef.current = nextFocus;
1498
- notifyMonth(year, month);
1499
- setFocusedDate(nextFocus);
1500
- return;
1501
- }
1502
- setInternalMonth((m) => {
1503
- const { year, month, firstDay } = computeAdjacentMonth(m, "prev", T);
1504
- setFocusedDate((prev) => focusedDateForMonth(prev, {
1505
- year,
1506
- month
1507
- }, firstDay));
1508
- return {
1509
- year,
1510
- month
1511
- };
1512
- });
1513
- }, [
1514
- T,
1515
- isMonthControlled,
1516
- monthProp,
1517
- notifyMonth,
1518
- focusedDate
1519
- ]);
1520
1566
  const allMonths = useMemo(() => {
1521
1567
  const opts = {
1522
1568
  weekStartDay,
@@ -1576,19 +1622,6 @@ function MonthViewRoot(props) {
1576
1622
  T,
1577
1623
  gridHasFocus
1578
1624
  ]);
1579
- const viewingYearMonth = useMemo(() => T.PlainYearMonth.from({
1580
- year: currentMonth.year,
1581
- month: currentMonth.month
1582
- }), [currentMonth, T]);
1583
- const mountedRef = useRef(false);
1584
- useEffect(() => {
1585
- if (!mountedRef.current) {
1586
- mountedRef.current = true;
1587
- return;
1588
- }
1589
- if (isMonthControlled) return;
1590
- onMonthChangeRef.current?.(viewingYearMonth);
1591
- }, [viewingYearMonth, isMonthControlled]);
1592
1625
  const rootState = useMemo(() => ({
1593
1626
  ...calState.baseRootState,
1594
1627
  focused: focusedDate,
@@ -1603,6 +1636,7 @@ function MonthViewRoot(props) {
1603
1636
  fixedWeeks,
1604
1637
  outsideDays,
1605
1638
  outOfRangeBehavior,
1639
+ windowPolicy,
1606
1640
  goNextMonth,
1607
1641
  goPrevMonth,
1608
1642
  setGridLabelId,
@@ -1612,6 +1646,7 @@ function MonthViewRoot(props) {
1612
1646
  fixedWeeks,
1613
1647
  outsideDays,
1614
1648
  outOfRangeBehavior,
1649
+ windowPolicy,
1615
1650
  goNextMonth,
1616
1651
  goPrevMonth,
1617
1652
  setGridLabelId
@@ -3171,37 +3206,16 @@ function MonthYearStringFn(props, ref) {
3171
3206
  */
3172
3207
  const MonthYearString = forwardRef(MonthYearStringFn);
3173
3208
  function useNavButton(direction) {
3174
- const { disabled: globalDisabled, minValue, maxValue, temporal: T } = useCalendarStable();
3175
- const monthViewStable = useMonthViewStable();
3176
- const monthViewState = useMonthViewState();
3177
- const { goNextMonth: goToNextMonth, goPrevMonth: goToPrevMonth, numberOfMonths, outOfRangeBehavior } = monthViewStable;
3178
- const { currentMonth: currentDateTime, allMonths, rootState } = monthViewState;
3179
- const refMonth = direction === "next" ? allMonths[numberOfMonths - 1] ?? currentDateTime : currentDateTime;
3180
- const destMonth = direction === "prev" ? refMonth.month === 1 ? 12 : refMonth.month - 1 : refMonth.month === 12 ? 1 : refMonth.month + 1;
3181
- const destYear = direction === "prev" ? refMonth.month === 1 ? refMonth.year - 1 : refMonth.year : refMonth.month === 12 ? refMonth.year + 1 : refMonth.year;
3182
- const boundValue = direction === "prev" ? minValue : maxValue;
3183
- const isDisabled = useMemo(() => {
3184
- if (globalDisabled) return true;
3185
- if (outOfRangeBehavior === "unbounded") return false;
3186
- if (!boundValue) return false;
3187
- if (direction === "prev") return destYear < boundValue.year || destYear === boundValue.year && destMonth < boundValue.month;
3188
- return destYear > boundValue.year || destYear === boundValue.year && destMonth > boundValue.month;
3189
- }, [
3190
- globalDisabled,
3191
- outOfRangeBehavior,
3192
- destYear,
3193
- destMonth,
3194
- boundValue,
3209
+ const { disabled: globalDisabled, temporal: T } = useCalendarStable();
3210
+ const { goNextMonth: goToNextMonth, goPrevMonth: goToPrevMonth, windowPolicy } = useMonthViewStable();
3211
+ const { currentMonth, rootState } = useMonthViewState();
3212
+ const step = useMemo(() => windowPolicy.step(currentMonth, direction), [
3213
+ windowPolicy,
3214
+ currentMonth,
3195
3215
  direction
3196
3216
  ]);
3197
- const target = useMemo(() => T.PlainYearMonth.from({
3198
- year: destYear,
3199
- month: destMonth
3200
- }), [
3201
- destYear,
3202
- destMonth,
3203
- T
3204
- ]);
3217
+ const isDisabled = globalDisabled || step.blocked;
3218
+ const target = useMemo(() => T.PlainYearMonth.from(step.entering), [step.entering, T]);
3205
3219
  return {
3206
3220
  state: useMemo(() => ({
3207
3221
  root: rootState,