Skip to content

fix: focus same row when tabbing to new body cell outside viewport - #13031

Merged
web-padawan merged 2 commits into
mainfrom
fix/grid-tab-focus-same-row
Oct 8, 2026
Merged

web-padawan merged 2 commits into
mainfrom
fix/grid-tab-focus-same-row

Conversation

@vursen

@vursen vursen commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Description

Related to #12779

When Tab enters the body and the remembered cell is scrolled out of view horizontally, grid focuses the first visible column's cell on the same row instead. That cell was looked up by position in column._cells, which assumes all columns store their cells in the same row order. A column added later stores them in a different order, so focus landed on another row.

  • Looked up the target cell through the row element instead of by position in column._cells
  • Flushed the pending lazy column update before resolving the Tab target, so tabbing right after a horizontal scroll focuses a body cell instead of leaving the grid
  • Added a test for a column added later, and made the lazy column test tab right after scrolling

Type of change

  • Bugfix

How to test

  1. Open dev/grid.html and make the window narrow enough for the grid to scroll horizontally
  2. In the browser console, add a column:
    const column = document.createElement('vaadin-grid-column');
    column.path = 'name';
    column.width = '200px';
    document.querySelector('vaadin-grid').appendChild(column);
  3. Scroll the grid to the right and click a cell in the new column, not on the first row
  4. Press Shift+Tab to move focus to the header, then scroll the grid back to the start
  5. Press Tab: focus should land on the same row as the cell clicked in step 3, not one row above

🤖 Generated with Claude Code

@vursen
vursen force-pushed the fix/grid-tab-focus-same-row branch 5 times, most recently from 0959ef5 to f954814 Compare October 8, 2026 08:51
@vursen vursen changed the title fix: focus same row when tabbing to body cell outside viewport fix: focus same row when tabbing to new body cell outside viewport Oct 8, 2026
@vursen
vursen force-pushed the fix/grid-tab-focus-same-row branch 2 times, most recently from 3f0bfd8 to b91c34c Compare October 8, 2026 10:33
The fallback that moves focus to the first visible column looked up the
cell by its position in `column._cells`. Columns do not share one row
order there once a column is added, so Tab could land on a different
row. Look the cell up through the row element instead.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@vursen
vursen force-pushed the fix/grid-tab-focus-same-row branch from b91c34c to 16715df Compare October 8, 2026 10:36
@vursen
vursen marked this pull request as ready for review October 8, 2026 10:54

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the changes — left 1 comment.

Finding
⚠️ With lazy column rendering the lookup can return undefined, which later leads to undefined.focus()

const rowIndex = focusStepTarget._column._cells.indexOf(focusStepTarget);
focusStepTarget = firstVisibleColumn._cells[rowIndex];
const row = focusStepTarget.parentElement;
focusStepTarget = [...row.children].find((cell) => cell._column === firstVisibleColumn);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

⚠️ With lazy column rendering the lookup can return undefined, which later leads to undefined.focus()

In columnRendering="lazy" mode, body cells of out-of-viewport columns are removed from their row, and __updateColumnsBodyContentHidden() only runs 100 ms after the last scroll event. Inside that window firstVisibleColumn is computed from the fresh scrollLeft while the row still holds the cells from the previous scroll position, so the first visible column has no cell in row.children and find yields undefined.

Two results:

  • _onTabKeyDown returns early, so Tab escapes the grid instead of entering the body.
  • _onFocusIn calls _predictFocusStepTarget(...).focus() without a null check, so tabbing into a grid with hidden header rows throws.

The previous column._cells[rowIndex] lookup kept working for detached cells. row.__cells, which __initRow maintains in column order including detached cells, would preserve that.

vaadin-grid-keyboard-navigation-mixin.js:680 · correctness

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed by adding this._debounceColumnContentVisibility?.flush() in _predictFocusStepTarget.

Right after a horizontal scroll in lazy column rendering mode the rows
keep the cells of the previous scroll position for up to 100 ms, so the
first visible column can have no cell in the row and Tab left the grid.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@sonarqubecloud

sonarqubecloud Bot commented Oct 8, 2026

Copy link
Copy Markdown

@web-padawan
web-padawan merged commit c7a7ae3 into main Oct 8, 2026
15 checks passed
@web-padawan
web-padawan deleted the fix/grid-tab-focus-same-row branch October 8, 2026 12:14
@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 25.4.0-alpha3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants