Skip to content

Commit 9b975c0

Browse files
authored
fix: exclude non-visible body cells from grid column auto width calc (#2072)
1 parent a0bad25 commit 9b975c0

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ class GridElement extends ElementMixin(
457457
_isInViewport(item) {
458458
const scrollTargetRect = this.$.table.getBoundingClientRect();
459459
const itemRect = item.getBoundingClientRect();
460-
const headerHeight = this.$.header.offsetHeight;
461-
const footerHeight = this.$.footer.offsetHeight;
460+
const headerHeight = this.$.header.getBoundingClientRect().height;
461+
const footerHeight = this.$.footer.getBoundingClientRect().height;
462462
return (
463463
itemRect.bottom > scrollTargetRect.top + headerHeight && itemRect.top < scrollTargetRect.bottom - footerHeight
464464
);
@@ -567,11 +567,16 @@ class GridElement extends ElementMixin(
567567
cols.forEach((col) => {
568568
col._currentWidth = 0;
569569
// Note: _allCells only contains cells which are currently rendered in DOM
570-
col._allCells.forEach((c) => {
571-
// Add 1px buffer to the offset width to avoid too narrow columns (sub-pixel rendering)
572-
const cellWidth = c.offsetWidth + 1;
573-
col._currentWidth = Math.max(col._currentWidth, cellWidth);
574-
});
570+
col._allCells
571+
.filter((c) => {
572+
// Exclude body cells that are out of the visible viewport
573+
return !this.$.items.contains(c) || this._isInViewport(c.parentElement);
574+
})
575+
.forEach((c) => {
576+
// Add 1px buffer to the offset width to avoid too narrow columns (sub-pixel rendering)
577+
const cellWidth = c.offsetWidth + 1;
578+
col._currentWidth = Math.max(col._currentWidth, cellWidth);
579+
});
575580
});
576581
// [write] Set column widths to fit widest measured content
577582
cols.forEach((col) => {

packages/vaadin-grid/test/column-auto-width.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ describe('column auto-width', function () {
113113
await recalculateWidths();
114114
expectColumnWidthsToBeOk(columns, [42, 62, 84, 107]);
115115
});
116+
117+
it('should exclude non-visible body cells from grid column auto width calc', async () => {
118+
// Assign more items to the grid. The last one with the long content, while in the DOM,
119+
// will end up outside the visible viewport and therefor should not affect the
120+
// calculated column auto-width
121+
grid.items = [...testItems, { a: 'a' }, { a: 'aaaaaaaaaaaaaaaaaaaaa' }];
122+
123+
await recalculateWidths();
124+
expectColumnWidthsToBeOk(columns);
125+
});
116126
});
117127

118128
describe('async recalculateWidth columns', () => {

0 commit comments

Comments
 (0)