Skip to content

Commit a3876e6

Browse files
authored
fix: update horizontal scroll position on data change (#10192)
* fix: update horizontal scroll position on data change * refactor: update scroll position on table resize
1 parent 1d27210 commit a3876e6

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
@@ -263,11 +263,14 @@ export const GridMixin = (superClass) =>
263263
__disableHeightPlaceholder: true,
264264
});
265265

266-
new ResizeObserver(() =>
266+
new ResizeObserver(() => {
267267
setTimeout(() => {
268268
this.__updateColumnsBodyContentHidden();
269-
}),
270-
).observe(this.$.table);
269+
});
270+
// Updating data can change the visibility of the scroll bar. Therefore,
271+
// the scroll position has to be recalculated.
272+
this.__updateHorizontalScrollPosition();
273+
}).observe(this.$.table);
271274

272275
const minHeightObserver = new ResizeObserver(() =>
273276
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)