@klinking/colander 3.1.0-alpha.3 → 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.
- package/dist/formats/date.cjs +1 -1
- package/dist/formats/date.d.cts +1 -1
- package/dist/formats/date.d.mts +1 -1
- package/dist/formats/date.mjs +1 -1
- package/dist/formats/object.cjs +1 -1
- package/dist/formats/object.d.cts +1 -1
- package/dist/formats/object.d.mts +1 -1
- package/dist/formats/object.mjs +1 -1
- package/dist/formats/plain-date-time.cjs +1 -1
- package/dist/formats/plain-date-time.d.cts +1 -1
- package/dist/formats/plain-date-time.d.mts +1 -1
- package/dist/formats/plain-date-time.mjs +1 -1
- package/dist/formats/plain-date.cjs +1 -1
- package/dist/formats/plain-date.d.cts +1 -1
- package/dist/formats/plain-date.d.mts +1 -1
- package/dist/formats/plain-date.mjs +1 -1
- package/dist/formats/plain-month-day.cjs +1 -1
- package/dist/formats/plain-month-day.d.cts +1 -1
- package/dist/formats/plain-month-day.d.mts +1 -1
- package/dist/formats/plain-month-day.mjs +1 -1
- package/dist/formats/plain-year-month.cjs +1 -1
- package/dist/formats/plain-year-month.d.cts +1 -1
- package/dist/formats/plain-year-month.d.mts +1 -1
- package/dist/formats/plain-year-month.mjs +1 -1
- package/dist/formats/zoned-date-time.cjs +1 -1
- package/dist/formats/zoned-date-time.d.cts +1 -1
- package/dist/formats/zoned-date-time.d.mts +1 -1
- package/dist/formats/zoned-date-time.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{weeks-navigation-DvryOSWF.cjs → weeks-navigation-DV538__x.cjs} +32 -28
- package/dist/{weeks-navigation-DEaASUAh.mjs → weeks-navigation-elCpdfQ3.mjs} +32 -28
- package/dist/{weeks-view-context-CNAq2Hiu.d.cts → weeks-view-context-CUCy8njs.d.cts} +2 -0
- package/dist/{weeks-view-context-CsP3PBiu.d.mts → weeks-view-context-E1NwyNgQ.d.mts} +2 -0
- package/package.json +1 -1
- package/src/calendar-provider.tsx +6 -0
- package/src/calendar-types.ts +2 -0
- package/src/day-cell.tsx +8 -1
- package/src/month-view.tsx +7 -15
- package/src/today.test.tsx +98 -0
- package/src/utils.test.ts +25 -0
- package/src/utils.ts +17 -0
- package/src/weeks-view-state.ts +8 -15
|
@@ -70,6 +70,17 @@ function getSystemTimeZone(T) {
|
|
|
70
70
|
return T.Now.timeZoneId();
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
|
+
* The date a view opens on: the selected date, else today in the provider's time zone.
|
|
74
|
+
*
|
|
75
|
+
* @param selected - The currently selected value, or `undefined`.
|
|
76
|
+
* @param today - Reads today's date in the provider's time zone.
|
|
77
|
+
* @param timeZone - IANA time zone.
|
|
78
|
+
* @param T - Temporal namespace.
|
|
79
|
+
*/
|
|
80
|
+
function resolveInitialAnchor(selected, today, timeZone, T) {
|
|
81
|
+
return selectedToZdt(selected, timeZone, T)?.toPlainDate() ?? today();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
73
84
|
* Converts any {@link DateValueObject} into a `Temporal.ZonedDateTime`.
|
|
74
85
|
*
|
|
75
86
|
* Handles all supported value formats (`PlainDate`, `PlainDateTime`,
|
|
@@ -826,6 +837,7 @@ function CalendarProvider(props) {
|
|
|
826
837
|
const locale = localeProp ?? "en-US";
|
|
827
838
|
const timeZone = timeZoneProp ?? getSystemTimeZone(T);
|
|
828
839
|
const weekStartDay = weekStartDayProp ?? 0;
|
|
840
|
+
const today = (0, react.useCallback)(() => T.Now.zonedDateTimeISO(timeZone).toPlainDate(), [T, timeZone]);
|
|
829
841
|
const resolvedFormat = formatProp ?? "PlainDate";
|
|
830
842
|
const selectionMode = selectionModeProp ?? "single";
|
|
831
843
|
const disabled = disabledProp ?? false;
|
|
@@ -1228,6 +1240,7 @@ function CalendarProvider(props) {
|
|
|
1228
1240
|
...minValue !== void 0 && { minValue },
|
|
1229
1241
|
...maxValue !== void 0 && { maxValue },
|
|
1230
1242
|
timeZone,
|
|
1243
|
+
today,
|
|
1231
1244
|
locale,
|
|
1232
1245
|
temporal: T,
|
|
1233
1246
|
weekStartDay,
|
|
@@ -1245,6 +1258,7 @@ function CalendarProvider(props) {
|
|
|
1245
1258
|
minValue,
|
|
1246
1259
|
maxValue,
|
|
1247
1260
|
timeZone,
|
|
1261
|
+
today,
|
|
1248
1262
|
locale,
|
|
1249
1263
|
T,
|
|
1250
1264
|
weekStartDay,
|
|
@@ -1352,7 +1366,7 @@ function MonthViewRoot(props) {
|
|
|
1352
1366
|
const calStable = useCalendarStable();
|
|
1353
1367
|
const calState = useCalendarState();
|
|
1354
1368
|
const T = calStable.temporal;
|
|
1355
|
-
const { timeZone, weekStartDay, isDateDisabled } = calStable;
|
|
1369
|
+
const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
|
|
1356
1370
|
const isMonthControlled = monthProp !== void 0;
|
|
1357
1371
|
const [internalMonth, setInternalMonth] = (0, react.useState)(() => {
|
|
1358
1372
|
if (monthProp) return {
|
|
@@ -1363,27 +1377,17 @@ function MonthViewRoot(props) {
|
|
|
1363
1377
|
year: defaultMonthProp.year,
|
|
1364
1378
|
month: defaultMonthProp.month
|
|
1365
1379
|
};
|
|
1366
|
-
|
|
1367
|
-
const zdt = toZonedDateTime(calState.selected, timeZone, T);
|
|
1368
|
-
return {
|
|
1369
|
-
year: zdt.year,
|
|
1370
|
-
month: zdt.month
|
|
1371
|
-
};
|
|
1372
|
-
}
|
|
1373
|
-
const now = T.Now.zonedDateTimeISO(timeZone);
|
|
1380
|
+
const anchor = resolveInitialAnchor(calState.selected, today, timeZone, T);
|
|
1374
1381
|
return {
|
|
1375
|
-
year:
|
|
1376
|
-
month:
|
|
1382
|
+
year: anchor.year,
|
|
1383
|
+
month: anchor.month
|
|
1377
1384
|
};
|
|
1378
1385
|
});
|
|
1379
1386
|
const currentMonth = isMonthControlled ? {
|
|
1380
1387
|
year: monthProp.year,
|
|
1381
1388
|
month: monthProp.month
|
|
1382
1389
|
} : internalMonth;
|
|
1383
|
-
const [focusedDate, setFocusedDate] = (0, react.useState)(() =>
|
|
1384
|
-
if (calState.selected) return toZonedDateTime(calState.selected, timeZone, T).toPlainDate();
|
|
1385
|
-
return T.Now.plainDateISO();
|
|
1386
|
-
});
|
|
1390
|
+
const [focusedDate, setFocusedDate] = (0, react.useState)(() => resolveInitialAnchor(calState.selected, today, timeZone, T));
|
|
1387
1391
|
const [gridHasFocus, setGridHasFocus] = (0, react.useState)(false);
|
|
1388
1392
|
const gridFocusedRef = (0, react.useRef)(false);
|
|
1389
1393
|
const [gridLabelIds, setGridLabelIds] = (0, react.useState)({});
|
|
@@ -2275,7 +2279,7 @@ function computeWeeksKeyNav(input) {
|
|
|
2275
2279
|
//#endregion
|
|
2276
2280
|
//#region src/day-cell.tsx
|
|
2277
2281
|
/** Computes derived day cell state from context values. Pure function, no hooks. */
|
|
2278
|
-
function computeDayCellState(date, orientation, columnIndex, rootState, selectedDates, currentDateTime, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd) {
|
|
2282
|
+
function computeDayCellState(date, orientation, columnIndex, rootState, selectedDates, currentDateTime, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, today, T, selectionMode, outsideDays, previewStart, previewEnd) {
|
|
2279
2283
|
const isCurrentMonth = date.year === currentDateTime.year && date.month === currentDateTime.month;
|
|
2280
2284
|
if (!isCurrentMonth && outsideDays === "hidden") return {
|
|
2281
2285
|
root: rootState,
|
|
@@ -2306,7 +2310,6 @@ function computeDayCellState(date, orientation, columnIndex, rootState, selected
|
|
|
2306
2310
|
rangePreviewHasEnd: false,
|
|
2307
2311
|
isTabTarget: false
|
|
2308
2312
|
};
|
|
2309
|
-
const today = T.Now.plainDateISO();
|
|
2310
2313
|
const isToday = T.PlainDate.compare(date, today) === 0;
|
|
2311
2314
|
const isRange = selectionMode === "range";
|
|
2312
2315
|
let isRangeStart = false;
|
|
@@ -2500,7 +2503,8 @@ const DayCellInstance = (0, react.memo)(DayCellInstanceFn, dayCellInstancePropsA
|
|
|
2500
2503
|
function DayCellTemplate(props) {
|
|
2501
2504
|
const { date: dateProp, ...restProps } = props;
|
|
2502
2505
|
const weekData = (0, react.useContext)(WeekDataContext);
|
|
2503
|
-
const { disabled, isDateDisabled, selectionMode, temporal: T, weekStartDay } = useCalendarStable();
|
|
2506
|
+
const { disabled, isDateDisabled, selectionMode, temporal: T, today, weekStartDay } = useCalendarStable();
|
|
2507
|
+
const todayDate = today();
|
|
2504
2508
|
const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
|
|
2505
2509
|
const viewCtx = useViewContext();
|
|
2506
2510
|
const { focusedDate, tabTargetDate } = viewCtx;
|
|
@@ -2515,7 +2519,7 @@ function DayCellTemplate(props) {
|
|
|
2515
2519
|
const derived = computeDayCellState(dateProp, orientation, (dateProp.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, isWeeksView ? {
|
|
2516
2520
|
year: dateProp.year,
|
|
2517
2521
|
month: dateProp.month
|
|
2518
|
-
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2522
|
+
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, todayDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2519
2523
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellInstance, {
|
|
2520
2524
|
...restProps,
|
|
2521
2525
|
date: dateProp,
|
|
@@ -2529,7 +2533,7 @@ function DayCellTemplate(props) {
|
|
|
2529
2533
|
const derived = computeDayCellState(day, orientation, perWeek ? i : (day.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, isWeeksView ? {
|
|
2530
2534
|
year: day.year,
|
|
2531
2535
|
month: day.month
|
|
2532
|
-
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2536
|
+
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, todayDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2533
2537
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayCellInstance, {
|
|
2534
2538
|
...restProps,
|
|
2535
2539
|
date: day,
|
|
@@ -2622,7 +2626,8 @@ function DayButtonFallbackFn(props, ref) {
|
|
|
2622
2626
|
const { date, ...restProps } = props;
|
|
2623
2627
|
const { orientation } = (0, react.useContext)(GridContext);
|
|
2624
2628
|
const weekData = (0, react.useContext)(WeekDataContext);
|
|
2625
|
-
const { disabled, isDateDisabled, selectionMode, temporal: T, weekStartDay } = useCalendarStable();
|
|
2629
|
+
const { disabled, isDateDisabled, selectionMode, temporal: T, today, weekStartDay } = useCalendarStable();
|
|
2630
|
+
const todayDate = today();
|
|
2626
2631
|
const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
|
|
2627
2632
|
const viewCtx = useViewContext();
|
|
2628
2633
|
const { focusedDate, tabTargetDate } = viewCtx;
|
|
@@ -2633,7 +2638,7 @@ function DayButtonFallbackFn(props, ref) {
|
|
|
2633
2638
|
month: date.month
|
|
2634
2639
|
} : weekData?.gridMonth ?? currentDateTime;
|
|
2635
2640
|
const daysInWeek = date.daysInWeek;
|
|
2636
|
-
const derived = computeDayCellState(date, orientation, (date.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2641
|
+
const derived = computeDayCellState(date, orientation, (date.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, todayDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2637
2642
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DayButtonInner, {
|
|
2638
2643
|
...restProps,
|
|
2639
2644
|
ref,
|
|
@@ -3862,14 +3867,13 @@ function useWeeksViewRootState(props) {
|
|
|
3862
3867
|
const calStable = useCalendarStable();
|
|
3863
3868
|
const calState = useCalendarState();
|
|
3864
3869
|
const T = calStable.temporal;
|
|
3865
|
-
const { weekStartDay, isDateDisabled, minValue, maxValue, timeZone } = calStable;
|
|
3870
|
+
const { weekStartDay, isDateDisabled, minValue, maxValue, timeZone, today } = calStable;
|
|
3866
3871
|
const isControlled = firstWeekProp !== void 0;
|
|
3867
3872
|
const resolveSpec = (0, react.useCallback)((spec) => resolveFirstWeekSpec(spec, weekStartDay, T), [weekStartDay, T]);
|
|
3868
3873
|
const [internalFirstWeek, setInternalFirstWeek] = (0, react.useState)(() => {
|
|
3869
3874
|
if (firstWeekProp !== void 0) return resolveSpec(firstWeekProp);
|
|
3870
3875
|
if (defaultFirstWeekProp !== void 0) return resolveSpec(defaultFirstWeekProp);
|
|
3871
|
-
|
|
3872
|
-
return resolveSpec(T.Now.plainDateISO());
|
|
3876
|
+
return resolveSpec(resolveInitialAnchor(calState.selected, today, timeZone, T));
|
|
3873
3877
|
});
|
|
3874
3878
|
const resolvedFirstWeek = isControlled ? resolveSpec(firstWeekProp) : internalFirstWeek;
|
|
3875
3879
|
const rawWeeks = (0, react.useMemo)(() => computeWeeksInWindow(resolvedFirstWeek, weekCount, weekStartDay, T), [
|
|
@@ -3924,10 +3928,10 @@ function useWeeksViewRootState(props) {
|
|
|
3924
3928
|
const firstWeek = weeks[0];
|
|
3925
3929
|
const lastWeek = weeks[weeks.length - 1];
|
|
3926
3930
|
if (firstWeek === void 0 || lastWeek === void 0) {
|
|
3927
|
-
const
|
|
3931
|
+
const now = today();
|
|
3928
3932
|
return {
|
|
3929
|
-
windowStart:
|
|
3930
|
-
windowEnd:
|
|
3933
|
+
windowStart: now,
|
|
3934
|
+
windowEnd: now,
|
|
3931
3935
|
weekCount: 0,
|
|
3932
3936
|
dayCount: 0,
|
|
3933
3937
|
enabledWeekCount: 0,
|
|
@@ -47,6 +47,17 @@ function getSystemTimeZone(T) {
|
|
|
47
47
|
return T.Now.timeZoneId();
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
+
* The date a view opens on: the selected date, else today in the provider's time zone.
|
|
51
|
+
*
|
|
52
|
+
* @param selected - The currently selected value, or `undefined`.
|
|
53
|
+
* @param today - Reads today's date in the provider's time zone.
|
|
54
|
+
* @param timeZone - IANA time zone.
|
|
55
|
+
* @param T - Temporal namespace.
|
|
56
|
+
*/
|
|
57
|
+
function resolveInitialAnchor(selected, today, timeZone, T) {
|
|
58
|
+
return selectedToZdt(selected, timeZone, T)?.toPlainDate() ?? today();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
50
61
|
* Converts any {@link DateValueObject} into a `Temporal.ZonedDateTime`.
|
|
51
62
|
*
|
|
52
63
|
* Handles all supported value formats (`PlainDate`, `PlainDateTime`,
|
|
@@ -803,6 +814,7 @@ function CalendarProvider(props) {
|
|
|
803
814
|
const locale = localeProp ?? "en-US";
|
|
804
815
|
const timeZone = timeZoneProp ?? getSystemTimeZone(T);
|
|
805
816
|
const weekStartDay = weekStartDayProp ?? 0;
|
|
817
|
+
const today = useCallback(() => T.Now.zonedDateTimeISO(timeZone).toPlainDate(), [T, timeZone]);
|
|
806
818
|
const resolvedFormat = formatProp ?? "PlainDate";
|
|
807
819
|
const selectionMode = selectionModeProp ?? "single";
|
|
808
820
|
const disabled = disabledProp ?? false;
|
|
@@ -1205,6 +1217,7 @@ function CalendarProvider(props) {
|
|
|
1205
1217
|
...minValue !== void 0 && { minValue },
|
|
1206
1218
|
...maxValue !== void 0 && { maxValue },
|
|
1207
1219
|
timeZone,
|
|
1220
|
+
today,
|
|
1208
1221
|
locale,
|
|
1209
1222
|
temporal: T,
|
|
1210
1223
|
weekStartDay,
|
|
@@ -1222,6 +1235,7 @@ function CalendarProvider(props) {
|
|
|
1222
1235
|
minValue,
|
|
1223
1236
|
maxValue,
|
|
1224
1237
|
timeZone,
|
|
1238
|
+
today,
|
|
1225
1239
|
locale,
|
|
1226
1240
|
T,
|
|
1227
1241
|
weekStartDay,
|
|
@@ -1329,7 +1343,7 @@ function MonthViewRoot(props) {
|
|
|
1329
1343
|
const calStable = useCalendarStable();
|
|
1330
1344
|
const calState = useCalendarState();
|
|
1331
1345
|
const T = calStable.temporal;
|
|
1332
|
-
const { timeZone, weekStartDay, isDateDisabled } = calStable;
|
|
1346
|
+
const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
|
|
1333
1347
|
const isMonthControlled = monthProp !== void 0;
|
|
1334
1348
|
const [internalMonth, setInternalMonth] = useState(() => {
|
|
1335
1349
|
if (monthProp) return {
|
|
@@ -1340,27 +1354,17 @@ function MonthViewRoot(props) {
|
|
|
1340
1354
|
year: defaultMonthProp.year,
|
|
1341
1355
|
month: defaultMonthProp.month
|
|
1342
1356
|
};
|
|
1343
|
-
|
|
1344
|
-
const zdt = toZonedDateTime(calState.selected, timeZone, T);
|
|
1345
|
-
return {
|
|
1346
|
-
year: zdt.year,
|
|
1347
|
-
month: zdt.month
|
|
1348
|
-
};
|
|
1349
|
-
}
|
|
1350
|
-
const now = T.Now.zonedDateTimeISO(timeZone);
|
|
1357
|
+
const anchor = resolveInitialAnchor(calState.selected, today, timeZone, T);
|
|
1351
1358
|
return {
|
|
1352
|
-
year:
|
|
1353
|
-
month:
|
|
1359
|
+
year: anchor.year,
|
|
1360
|
+
month: anchor.month
|
|
1354
1361
|
};
|
|
1355
1362
|
});
|
|
1356
1363
|
const currentMonth = isMonthControlled ? {
|
|
1357
1364
|
year: monthProp.year,
|
|
1358
1365
|
month: monthProp.month
|
|
1359
1366
|
} : internalMonth;
|
|
1360
|
-
const [focusedDate, setFocusedDate] = useState(() =>
|
|
1361
|
-
if (calState.selected) return toZonedDateTime(calState.selected, timeZone, T).toPlainDate();
|
|
1362
|
-
return T.Now.plainDateISO();
|
|
1363
|
-
});
|
|
1367
|
+
const [focusedDate, setFocusedDate] = useState(() => resolveInitialAnchor(calState.selected, today, timeZone, T));
|
|
1364
1368
|
const [gridHasFocus, setGridHasFocus] = useState(false);
|
|
1365
1369
|
const gridFocusedRef = useRef(false);
|
|
1366
1370
|
const [gridLabelIds, setGridLabelIds] = useState({});
|
|
@@ -2252,7 +2256,7 @@ function computeWeeksKeyNav(input) {
|
|
|
2252
2256
|
//#endregion
|
|
2253
2257
|
//#region src/day-cell.tsx
|
|
2254
2258
|
/** Computes derived day cell state from context values. Pure function, no hooks. */
|
|
2255
|
-
function computeDayCellState(date, orientation, columnIndex, rootState, selectedDates, currentDateTime, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd) {
|
|
2259
|
+
function computeDayCellState(date, orientation, columnIndex, rootState, selectedDates, currentDateTime, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, today, T, selectionMode, outsideDays, previewStart, previewEnd) {
|
|
2256
2260
|
const isCurrentMonth = date.year === currentDateTime.year && date.month === currentDateTime.month;
|
|
2257
2261
|
if (!isCurrentMonth && outsideDays === "hidden") return {
|
|
2258
2262
|
root: rootState,
|
|
@@ -2283,7 +2287,6 @@ function computeDayCellState(date, orientation, columnIndex, rootState, selected
|
|
|
2283
2287
|
rangePreviewHasEnd: false,
|
|
2284
2288
|
isTabTarget: false
|
|
2285
2289
|
};
|
|
2286
|
-
const today = T.Now.plainDateISO();
|
|
2287
2290
|
const isToday = T.PlainDate.compare(date, today) === 0;
|
|
2288
2291
|
const isRange = selectionMode === "range";
|
|
2289
2292
|
let isRangeStart = false;
|
|
@@ -2477,7 +2480,8 @@ const DayCellInstance = memo(DayCellInstanceFn, dayCellInstancePropsAreEqual);
|
|
|
2477
2480
|
function DayCellTemplate(props) {
|
|
2478
2481
|
const { date: dateProp, ...restProps } = props;
|
|
2479
2482
|
const weekData = useContext(WeekDataContext);
|
|
2480
|
-
const { disabled, isDateDisabled, selectionMode, temporal: T, weekStartDay } = useCalendarStable();
|
|
2483
|
+
const { disabled, isDateDisabled, selectionMode, temporal: T, today, weekStartDay } = useCalendarStable();
|
|
2484
|
+
const todayDate = today();
|
|
2481
2485
|
const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
|
|
2482
2486
|
const viewCtx = useViewContext();
|
|
2483
2487
|
const { focusedDate, tabTargetDate } = viewCtx;
|
|
@@ -2492,7 +2496,7 @@ function DayCellTemplate(props) {
|
|
|
2492
2496
|
const derived = computeDayCellState(dateProp, orientation, (dateProp.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, isWeeksView ? {
|
|
2493
2497
|
year: dateProp.year,
|
|
2494
2498
|
month: dateProp.month
|
|
2495
|
-
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2499
|
+
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, todayDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2496
2500
|
return /* @__PURE__ */ jsx(DayCellInstance, {
|
|
2497
2501
|
...restProps,
|
|
2498
2502
|
date: dateProp,
|
|
@@ -2506,7 +2510,7 @@ function DayCellTemplate(props) {
|
|
|
2506
2510
|
const derived = computeDayCellState(day, orientation, perWeek ? i : (day.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, isWeeksView ? {
|
|
2507
2511
|
year: day.year,
|
|
2508
2512
|
month: day.month
|
|
2509
|
-
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2513
|
+
} : cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, todayDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2510
2514
|
return /* @__PURE__ */ jsx(DayCellInstance, {
|
|
2511
2515
|
...restProps,
|
|
2512
2516
|
date: day,
|
|
@@ -2599,7 +2603,8 @@ function DayButtonFallbackFn(props, ref) {
|
|
|
2599
2603
|
const { date, ...restProps } = props;
|
|
2600
2604
|
const { orientation } = useContext(GridContext);
|
|
2601
2605
|
const weekData = useContext(WeekDataContext);
|
|
2602
|
-
const { disabled, isDateDisabled, selectionMode, temporal: T, weekStartDay } = useCalendarStable();
|
|
2606
|
+
const { disabled, isDateDisabled, selectionMode, temporal: T, today, weekStartDay } = useCalendarStable();
|
|
2607
|
+
const todayDate = today();
|
|
2603
2608
|
const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } = useCalendarState();
|
|
2604
2609
|
const viewCtx = useViewContext();
|
|
2605
2610
|
const { focusedDate, tabTargetDate } = viewCtx;
|
|
@@ -2610,7 +2615,7 @@ function DayButtonFallbackFn(props, ref) {
|
|
|
2610
2615
|
month: date.month
|
|
2611
2616
|
} : weekData?.gridMonth ?? currentDateTime;
|
|
2612
2617
|
const daysInWeek = date.daysInWeek;
|
|
2613
|
-
const derived = computeDayCellState(date, orientation, (date.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2618
|
+
const derived = computeDayCellState(date, orientation, (date.dayOfWeek % daysInWeek - weekStartDay + daysInWeek) % daysInWeek, rootState, selectedDates, cellMonth, disabled, isDateDisabled, focusedDate, rangeStart, rangeEnd, tabTargetDate, todayDate, T, selectionMode, outsideDays, previewStart, previewEnd);
|
|
2614
2619
|
return /* @__PURE__ */ jsx(DayButtonInner, {
|
|
2615
2620
|
...restProps,
|
|
2616
2621
|
ref,
|
|
@@ -3839,14 +3844,13 @@ function useWeeksViewRootState(props) {
|
|
|
3839
3844
|
const calStable = useCalendarStable();
|
|
3840
3845
|
const calState = useCalendarState();
|
|
3841
3846
|
const T = calStable.temporal;
|
|
3842
|
-
const { weekStartDay, isDateDisabled, minValue, maxValue, timeZone } = calStable;
|
|
3847
|
+
const { weekStartDay, isDateDisabled, minValue, maxValue, timeZone, today } = calStable;
|
|
3843
3848
|
const isControlled = firstWeekProp !== void 0;
|
|
3844
3849
|
const resolveSpec = useCallback((spec) => resolveFirstWeekSpec(spec, weekStartDay, T), [weekStartDay, T]);
|
|
3845
3850
|
const [internalFirstWeek, setInternalFirstWeek] = useState(() => {
|
|
3846
3851
|
if (firstWeekProp !== void 0) return resolveSpec(firstWeekProp);
|
|
3847
3852
|
if (defaultFirstWeekProp !== void 0) return resolveSpec(defaultFirstWeekProp);
|
|
3848
|
-
|
|
3849
|
-
return resolveSpec(T.Now.plainDateISO());
|
|
3853
|
+
return resolveSpec(resolveInitialAnchor(calState.selected, today, timeZone, T));
|
|
3850
3854
|
});
|
|
3851
3855
|
const resolvedFirstWeek = isControlled ? resolveSpec(firstWeekProp) : internalFirstWeek;
|
|
3852
3856
|
const rawWeeks = useMemo(() => computeWeeksInWindow(resolvedFirstWeek, weekCount, weekStartDay, T), [
|
|
@@ -3901,10 +3905,10 @@ function useWeeksViewRootState(props) {
|
|
|
3901
3905
|
const firstWeek = weeks[0];
|
|
3902
3906
|
const lastWeek = weeks[weeks.length - 1];
|
|
3903
3907
|
if (firstWeek === void 0 || lastWeek === void 0) {
|
|
3904
|
-
const
|
|
3908
|
+
const now = today();
|
|
3905
3909
|
return {
|
|
3906
|
-
windowStart:
|
|
3907
|
-
windowEnd:
|
|
3910
|
+
windowStart: now,
|
|
3911
|
+
windowEnd: now,
|
|
3908
3912
|
weekCount: 0,
|
|
3909
3913
|
dayCount: 0,
|
|
3910
3914
|
enabledWeekCount: 0,
|
|
@@ -1505,6 +1505,8 @@ interface CalendarStableContextValue {
|
|
|
1505
1505
|
maxValue?: Temporal.PlainDate;
|
|
1506
1506
|
/** Resolved IANA time zone. */
|
|
1507
1507
|
timeZone: string;
|
|
1508
|
+
/** Today's date in the resolved {@link timeZone}. Reads the clock on every call. */
|
|
1509
|
+
today: () => Temporal.PlainDate;
|
|
1508
1510
|
/** Resolved BCP 47 locale. */
|
|
1509
1511
|
locale: string;
|
|
1510
1512
|
/** Resolved Temporal namespace. */
|
|
@@ -1505,6 +1505,8 @@ interface CalendarStableContextValue {
|
|
|
1505
1505
|
maxValue?: Temporal.PlainDate;
|
|
1506
1506
|
/** Resolved IANA time zone. */
|
|
1507
1507
|
timeZone: string;
|
|
1508
|
+
/** Today's date in the resolved {@link timeZone}. Reads the clock on every call. */
|
|
1509
|
+
today: () => Temporal.PlainDate;
|
|
1508
1510
|
/** Resolved BCP 47 locale. */
|
|
1509
1511
|
locale: string;
|
|
1510
1512
|
/** Resolved Temporal namespace. */
|
package/package.json
CHANGED
|
@@ -55,6 +55,10 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
55
55
|
const locale = localeProp ?? "en-US";
|
|
56
56
|
const timeZone = timeZoneProp ?? getSystemTimeZone(T);
|
|
57
57
|
const weekStartDay = weekStartDayProp ?? 0;
|
|
58
|
+
const today = useCallback(
|
|
59
|
+
() => T.Now.zonedDateTimeISO(timeZone).toPlainDate(),
|
|
60
|
+
[T, timeZone],
|
|
61
|
+
);
|
|
58
62
|
const resolvedFormat = (formatProp ?? "PlainDate") as F;
|
|
59
63
|
const selectionMode = selectionModeProp ?? "single";
|
|
60
64
|
const disabled = disabledProp ?? false;
|
|
@@ -604,6 +608,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
604
608
|
...(minValue !== undefined && { minValue }),
|
|
605
609
|
...(maxValue !== undefined && { maxValue }),
|
|
606
610
|
timeZone,
|
|
611
|
+
today,
|
|
607
612
|
locale,
|
|
608
613
|
temporal: T,
|
|
609
614
|
weekStartDay,
|
|
@@ -622,6 +627,7 @@ function CalendarProvider<F extends ValueFormat = "PlainDate">(
|
|
|
622
627
|
minValue,
|
|
623
628
|
maxValue,
|
|
624
629
|
timeZone,
|
|
630
|
+
today,
|
|
625
631
|
locale,
|
|
626
632
|
T,
|
|
627
633
|
weekStartDay,
|
package/src/calendar-types.ts
CHANGED
|
@@ -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
|
@@ -31,6 +31,7 @@ export function computeDayCellState(
|
|
|
31
31
|
rangeStart: TemporalPoly.PlainDate | undefined,
|
|
32
32
|
rangeEnd: TemporalPoly.PlainDate | undefined,
|
|
33
33
|
tabTargetDate: TemporalPoly.PlainDate,
|
|
34
|
+
today: TemporalPoly.PlainDate,
|
|
34
35
|
T: TemporalNamespace,
|
|
35
36
|
selectionMode: "single" | "range" | "multiple",
|
|
36
37
|
outsideDays: OutsideDays,
|
|
@@ -73,7 +74,6 @@ export function computeDayCellState(
|
|
|
73
74
|
};
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
const today = T.Now.plainDateISO();
|
|
77
77
|
const isToday = T.PlainDate.compare(date, today) === 0;
|
|
78
78
|
const isRange = selectionMode === "range";
|
|
79
79
|
|
|
@@ -389,8 +389,10 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
|
|
|
389
389
|
isDateDisabled,
|
|
390
390
|
selectionMode,
|
|
391
391
|
temporal: T,
|
|
392
|
+
today,
|
|
392
393
|
weekStartDay,
|
|
393
394
|
} = useCalendarStable();
|
|
395
|
+
const todayDate = today();
|
|
394
396
|
|
|
395
397
|
const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } =
|
|
396
398
|
useCalendarState();
|
|
@@ -429,6 +431,7 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
|
|
|
429
431
|
rangeStart,
|
|
430
432
|
rangeEnd,
|
|
431
433
|
tabTargetDate,
|
|
434
|
+
todayDate,
|
|
432
435
|
T,
|
|
433
436
|
selectionMode,
|
|
434
437
|
outsideDays,
|
|
@@ -464,6 +467,7 @@ export function DayCellTemplate(props: DayCellTemplateProps) {
|
|
|
464
467
|
rangeStart,
|
|
465
468
|
rangeEnd,
|
|
466
469
|
tabTargetDate,
|
|
470
|
+
todayDate,
|
|
467
471
|
T,
|
|
468
472
|
selectionMode,
|
|
469
473
|
outsideDays,
|
|
@@ -659,8 +663,10 @@ function DayButtonFallbackFn(
|
|
|
659
663
|
isDateDisabled,
|
|
660
664
|
selectionMode,
|
|
661
665
|
temporal: T,
|
|
666
|
+
today,
|
|
662
667
|
weekStartDay,
|
|
663
668
|
} = useCalendarStable();
|
|
669
|
+
const todayDate = today();
|
|
664
670
|
const { selectedDates, rangeStart, rangeEnd, previewStart, previewEnd } =
|
|
665
671
|
useCalendarState();
|
|
666
672
|
const viewCtx = useViewContext();
|
|
@@ -688,6 +694,7 @@ function DayButtonFallbackFn(
|
|
|
688
694
|
rangeStart,
|
|
689
695
|
rangeEnd,
|
|
690
696
|
tabTargetDate,
|
|
697
|
+
todayDate,
|
|
691
698
|
T,
|
|
692
699
|
selectionMode,
|
|
693
700
|
outsideDays,
|
package/src/month-view.tsx
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
resolveFocusTarget,
|
|
21
21
|
selectedToZdt,
|
|
22
22
|
shiftWindowToMonth,
|
|
23
|
-
|
|
23
|
+
resolveInitialAnchor,
|
|
24
24
|
} from "./utils";
|
|
25
25
|
import {
|
|
26
26
|
ViewContext,
|
|
@@ -57,7 +57,7 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
57
57
|
const calState = useCalendarState();
|
|
58
58
|
|
|
59
59
|
const T = calStable.temporal;
|
|
60
|
-
const { timeZone, weekStartDay, isDateDisabled } = calStable;
|
|
60
|
+
const { timeZone, today, weekStartDay, isDateDisabled } = calStable;
|
|
61
61
|
|
|
62
62
|
// --- Month navigation state (controlled/uncontrolled) ---
|
|
63
63
|
const isMonthControlled = monthProp !== undefined;
|
|
@@ -69,13 +69,8 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
69
69
|
if (monthProp) return { year: monthProp.year, month: monthProp.month };
|
|
70
70
|
if (defaultMonthProp)
|
|
71
71
|
return { year: defaultMonthProp.year, month: defaultMonthProp.month };
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const zdt = toZonedDateTime(calState.selected, timeZone, T);
|
|
75
|
-
return { year: zdt.year, month: zdt.month };
|
|
76
|
-
}
|
|
77
|
-
const now = T.Now.zonedDateTimeISO(timeZone);
|
|
78
|
-
return { year: now.year, month: now.month };
|
|
72
|
+
const anchor = resolveInitialAnchor(calState.selected, today, timeZone, T);
|
|
73
|
+
return { year: anchor.year, month: anchor.month };
|
|
79
74
|
});
|
|
80
75
|
|
|
81
76
|
const currentMonth = isMonthControlled
|
|
@@ -83,12 +78,9 @@ function MonthViewRoot(props: MonthViewRootProps) {
|
|
|
83
78
|
: internalMonth;
|
|
84
79
|
|
|
85
80
|
// --- Focus state ---
|
|
86
|
-
const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() =>
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
return T.Now.plainDateISO();
|
|
91
|
-
});
|
|
81
|
+
const [focusedDate, setFocusedDate] = useState<Temporal.PlainDate>(() =>
|
|
82
|
+
resolveInitialAnchor(calState.selected, today, timeZone, T),
|
|
83
|
+
);
|
|
92
84
|
|
|
93
85
|
const [gridHasFocus, setGridHasFocus] = useState(false);
|
|
94
86
|
const gridFocusedRef = useRef(false);
|
|
@@ -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(
|