Skip to content

fix(status): clamp heatmap keyboard focus when the matrix shrinks#1474

Merged
kylebernhardy merged 1 commit into
stagefrom
kyle/heatmap-focus-clamp
Jul 13, 2026
Merged

fix(status): clamp heatmap keyboard focus when the matrix shrinks#1474
kylebernhardy merged 1 commit into
stagefrom
kyle/heatmap-focus-clamp

Conversation

@kylebernhardy

Copy link
Copy Markdown
Member

Fixes #1443 — HeatmapMatrix keyboard focus becomes unreachable when the matrix shrinks.

The roving-tabindex active state in HeatmapMatrix was never clamped when rows/cols shrank after a data refresh — if the stored active cell was out of range, no gridcell got tabIndex=0 and the grid dropped out of the tab order.

Change

  • Derive the effective active cell at render time (Math.min(active[0], rows.length - 1), same for cols) — no effect, no extra render. Keyboard handlers stay consistent automatically since they receive the rendered (clamped) indices, and focusing the clamped cell re-syncs state via onFocus.
  • Regression test: moves focus to the last row/col via keyboard, re-renders with a 1×2 matrix, asserts exactly one gridcell keeps tabIndex=0 (clamped to the last surviving row/col) and arrow keys still navigate from it. Fails on stage without the fix.

Cross-model review: Codex and Gemini both reviewed the diff with no findings.

Generated by kAIle (Claude Fable 5).

Derive the effective roving-tabindex cell at render time with
Math.min against the current rows/cols, so exactly one gridcell keeps
tabIndex=0 after a data refresh shrinks the matrix. Adds a regression
test covering the shrink and arrow-key navigation from the clamped
cell.

Fixes #1443

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UZEaYdKFXuQw2Hf6XpPdEV

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a regression (#1443) where shrinking the heatmap matrix could cause the active cell in the roving tabindex state to go out of bounds, leaving the grid without any cell having tabIndex=0 and removing it from the tab order. This is resolved by clamping the active row and column indices to the current matrix dimensions during render. A corresponding unit test has been added to verify this behavior. I have no feedback to provide.

@github-actions

Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 47.51% 4487 / 9444
🔵 Statements 47.85% 4785 / 10000
🔵 Functions 39.45% 1068 / 2707
🔵 Branches 39.65% 2897 / 7306
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/features/instance/status/analytics/primitives/HeatmapMatrix.tsx 88.99% 72.91% 93.33% 92.06% 242, 251, 252, 273, 275-281, 287, 328-329, 331-332, 336-341, 343, 353
Generated in workflow #1443 for commit c79b995 by the Vitest Coverage Report Action

@kylebernhardy
kylebernhardy marked this pull request as ready for review July 12, 2026 02:54
@kylebernhardy
kylebernhardy requested a review from a team as a code owner July 12, 2026 02:54

@kriszyp kriszyp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, clean a11y fix. The render-time clamp is correct: Math.min(active[0], rows.length - 1) caps at the true last index (no off-by-one), passes through unchanged on grid growth, and the empty-grid -1 is provably inert since the clamped indices are only ever compared (never used to index an array), an empty grid renders no gridcell to match, and focusCell guards its ref lookup. Deriving isActive from the clamped values guarantees exactly one surviving cell keeps tabIndex=0, so keyboard users can always re-enter the grid.

The regression test genuinely exercises the shrink path — it moves focus to the corner [1,2], re-renders as 1x2, and asserts exactly one tabbable cell that it's the clamped last-surviving cell (src-1 -> dest-b), plus that arrow-nav still works from it. Verifies direction, not just non-crash.

One non-blocking suggestion: Math.max(0, Math.min(...)) would normalize the empty-grid case to 0 and make the derived indices unconditionally valid array positions — reads more obviously safe to a future editor. Purely optional; the current -1 is safe as written.

LGTM.

— Claude (Sonnet 5), reviewed via review-queue

@kylebernhardy
kylebernhardy added this pull request to the merge queue Jul 13, 2026
Merged via the queue into stage with commit 262ac1c Jul 13, 2026
3 checks passed
@kylebernhardy
kylebernhardy deleted the kyle/heatmap-focus-clamp branch July 13, 2026 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HeatmapMatrix keyboard focus becomes unreachable when the matrix shrinks

2 participants