diff --git a/src/apps/calendar/MonthGrid.tsx b/src/apps/calendar/MonthGrid.tsx index 7009242..4307db2 100644 --- a/src/apps/calendar/MonthGrid.tsx +++ b/src/apps/calendar/MonthGrid.tsx @@ -49,11 +49,19 @@ export function MonthGrid({ const cellRefs = useRef(new Map()); // Exactly one cell must be tab-focusable, or a keyboard user who tabs away - // and back can never re-enter the grid. Prefer the selected day, then - // today if it's in the displayed month, and only otherwise fall back to - // the 1st — e.g. after PageUp/PageDown lands on a month with neither. + // and back can never re-enter the grid. Prefer the selected day — but only + // when it's actually one of the rendered cells: `selectedDate` survives + // month navigation (so the agenda can keep showing it), so after Prev/Next/ + // PageUp/PageDown it commonly points outside the newly displayed grid. + // Otherwise prefer today if it's in the displayed month, and only + // otherwise fall back to the 1st. + const selectedInView = selectedDate ? weeks.some((week) => week.some((date) => localDateKey(date) === selectedDate)) : false; const isTodayInMonth = today.getFullYear() === monthAnchor.getFullYear() && today.getMonth() === monthIndex; - const focusKey = selectedDate ?? (isTodayInMonth ? todayKey : localDateKey(new Date(monthAnchor.getFullYear(), monthIndex, 1))); + const focusKey = selectedInView + ? selectedDate! + : isTodayInMonth + ? todayKey + : localDateKey(new Date(monthAnchor.getFullYear(), monthIndex, 1)); const focusDate = (date: Date) => { const key = localDateKey(date); @@ -101,21 +109,20 @@ export function MonthGrid({ }; return ( -
-
+
+
{WEEKDAY_LABELS.map((label) => (
{label}
))}
-
+
{isLoading - ? Array.from({ length: 35 }).map((_, index) => ( + ? Array.from({ length: weeks.length * 7 }).map((_, index) => (