Skip to content

Commit e047a44

Browse files
fix: update horizontal scroll position on data change (#10192) (#10214)
Co-authored-by: Ugur Saglam <106508695+ugur-vaadin@users.noreply.github.com>
1 parent 90b7dd4 commit e047a44

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,14 @@ export const GridMixin = (superClass) =>
266266
reorderElements: true,
267267
});
268268

269-
new ResizeObserver(() =>
269+
new ResizeObserver(() => {
270270
setTimeout(() => {
271271
this.__updateColumnsBodyContentHidden();
272-
}),
273-
).observe(this.$.table);
272+
});
273+
// Updating data can change the visibility of the scroll bar. Therefore,
274+
// the scroll position has to be recalculated.
275+
this.__updateHorizontalScrollPosition();
276+
}).observe(this.$.table);
274277

275278
const minHeightObserver = new ResizeObserver(() =>
276279
setTimeout(() => {

packages/grid/test/frozen-columns.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,31 @@ const frozenGridFixture = (frozen, frozenToEnd) => {
217217
expect(getComputedStyle(sizerCell).transform).to.equal('none');
218218
});
219219

220+
it('should update frozen-to-end positioning when scrollbar affects layout during data changes', async () => {
221+
grid.style.width = '200px';
222+
// Simulate a wide scrollbar like with Windows
223+
const scrollbarWidth = 17;
224+
const originalClientWidth = grid.$.table.clientWidth;
225+
grid.$.table.style.width = `${originalClientWidth - scrollbarWidth}px`;
226+
flushGrid(grid);
227+
await nextRender();
228+
229+
const getFrozenToEndHeaderCell = () => getRowCells(getRows(grid.$.header)[0])[2];
230+
const positionWithScrollbar = getFrozenToEndHeaderCell().getBoundingClientRect().x;
231+
232+
grid.items = Array.from({ length: 2 }, (_, i) => ({
233+
foo: `foo${i}`,
234+
bar: `bar${i}`,
235+
baz: `baz${i}`,
236+
}));
237+
grid.$.table.style.width = `${originalClientWidth}px`;
238+
flushGrid(grid);
239+
await nextRender();
240+
241+
const positionAfterDataChange = getFrozenToEndHeaderCell().getBoundingClientRect().x;
242+
expect(positionWithScrollbar).to.not.equal(positionAfterDataChange);
243+
});
244+
220245
['header', 'body'].forEach((container) => {
221246
describe(container, () => {
222247
const defaultCellWidth = 100;

0 commit comments

Comments
 (0)