Skip to content

Commit 8f02c86

Browse files
vaadin-botysnbytrk
andauthored
fix: use composed focus target when scrolling oversized row into view (#12629) (CP: 24.10) (#12687)
This PR cherry-picks changes from the original PR #12629 to branch 24.10. --- #### Original PR description > ## Description > > When a grid row is larger than the viewport, the `focusin` handler in `vaadin-grid-scroll-mixin.js` scrolls the focus event target into view (#9475). If the focused element is inside a nested shadow root, for example a nested `<vaadin-grid all-rows-visible>` rendered in row details, `e.target` as seen by the outer grid is retargeted to the nested component's host element. That host can be thousands of pixels tall, so aligning its bottom with the footer scrolls the actually focused cell far out of view. This happens whenever focus is restored without a preceding mousedown, for example when the browser window regains focus after switching windows, or when tabbing. > > This PR uses `composedPath[0]`, the element that actually received focus, as the scroll target instead of `e.target`, so the outer grid keeps the focused nested cell in view instead of jumping. `_onFocusIn` in the keyboard navigation mixin already resolves the focus target the same way. > > Tests: two new tests with a nested grid in row details. One asserts that the outer scroll position does not change when the focused nested cell is already visible, the other asserts that a nested cell outside the viewport is scrolled into view. Both fail before the change (scrollTop 170 -> 743; cell 212 px outside the viewport) and pass after it. > > Fixes #10823 > > ## Type of change > > - [x] Bugfix > - [ ] Feature > > ## Checklist > > - [x] I have read the contribution guide: https://vaadin.com/docs/latest/contributing/pr > - [x] I have added a description following the guideline. > - [x] The issue is created in the corresponding repository and I have referenced it. > - [x] I have added tests to ensure my change is effective and works as intended. > - [x] New and existing tests are passing locally with my change. > - [x] I have performed self-review and corrected misspellings. > - [ ] I have not completed some of the steps above and my pull request can be closed immediately. Co-authored-by: ysnbytrk <ysnbytrk@gmail.com>
1 parent fa53af5 commit 8f02c86

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎packages/grid/src/vaadin-grid-scroll-mixin.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,17 @@ export const ScrollMixin = (superClass) =>
128128
// is inside the viewport. If the whole row fits into the viewport, then scroll
129129
// the row into view. This ensures that labels, helper texts and other related
130130
// elements of focusable elements within cells also become visible. When the row
131-
// is larger than the viewport, scroll the focus event target into the viewport.
131+
// is larger than the viewport, scroll the actually focused element into the viewport.
132132
// This works better when focusing elements within cells, which could otherwise
133133
// still be outside the viewport when scrolling to the top or bottom of the row.
134+
// The composed path is used instead of the event target, which would be
135+
// retargeted to the host element when focus lands inside a nested shadow root.
134136
const tableHeight = this.$.table.clientHeight;
135137
const headerHeight = this.$.header.clientHeight;
136138
const footerHeight = this.$.footer.clientHeight;
137139
const viewportHeight = tableHeight - headerHeight - footerHeight;
138140
const isRowLargerThanViewport = row.clientHeight > viewportHeight;
139-
const scrollTarget = isRowLargerThanViewport ? e.target : row;
141+
const scrollTarget = isRowLargerThanViewport ? composedPath[0] : row;
140142

141143
this.__scrollIntoViewport(scrollTarget);
142144
}

‎packages/grid/test/scroll-into-view.test.js‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,54 @@ describe('scroll into view', () => {
135135
verifyNotFullyVisible(secondRow);
136136
});
137137
});
138+
139+
describe('nested grid in row details', () => {
140+
let nestedGrid, nestedFirstCell, nestedMiddleCell;
141+
142+
beforeEach(async () => {
143+
grid.rowDetailsRenderer = (root) => {
144+
if (!root.firstElementChild) {
145+
root.innerHTML = `
146+
<vaadin-grid all-rows-visible>
147+
<vaadin-grid-column path="name"></vaadin-grid-column>
148+
</vaadin-grid>
149+
`;
150+
root.firstElementChild.items = Array.from({ length: 20 }, (_, i) => ({ name: `Nested ${i}` }));
151+
}
152+
};
153+
grid.openItemDetails(grid.items[0]);
154+
flushGrid(grid);
155+
await nextFrame();
156+
157+
nestedGrid = grid.querySelector('vaadin-grid');
158+
flushGrid(nestedGrid);
159+
await nextFrame();
160+
await nextFrame();
161+
162+
nestedFirstCell = getContainerCell(nestedGrid.$.items, 0, 0);
163+
nestedMiddleCell = getContainerCell(nestedGrid.$.items, 10, 0);
164+
});
165+
166+
it('should not change scroll position when focused nested grid cell is visible', async () => {
167+
// Scroll the outer grid so that the first nested cell is at the top of the viewport
168+
const scrollerTop = grid.$.scroller.getBoundingClientRect().top;
169+
grid.$.table.scrollTop = nestedFirstCell.getBoundingClientRect().top - scrollerTop;
170+
await nextFrame();
171+
verifyFullyVisible(nestedFirstCell);
172+
const scrollTop = grid.$.table.scrollTop;
173+
174+
nestedFirstCell.focus();
175+
176+
expect(grid.$.table.scrollTop).to.equal(scrollTop);
177+
verifyFullyVisible(nestedFirstCell);
178+
});
179+
180+
it('should scroll focused nested grid cell into view', () => {
181+
verifyNotFullyVisible(nestedMiddleCell);
182+
183+
nestedMiddleCell.focus();
184+
185+
verifyFullyVisible(nestedMiddleCell);
186+
});
187+
});
138188
});

0 commit comments

Comments
 (0)