Skip to content

Commit f2e2534

Browse files
authored
fix: prevent column width calculation when grid is not visible (#5876)
* fix: prevent column width calculation when grid is not visible * fix import
1 parent 8c1b306 commit f2e2534

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import './vaadin-grid-column.js';
77
import './vaadin-grid-styles.js';
88
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
9+
import { isElementHidden } from '@vaadin/a11y-base/src/focus-utils.js';
910
import { TabindexMixin } from '@vaadin/a11y-base/src/tabindex-mixin.js';
1011
import { animationFrame, microTask } from '@vaadin/component-base/src/async.js';
1112
import { isAndroid, isChrome, isFirefox, isIOS, isSafari, isTouch } from '@vaadin/component-base/src/browser-utils.js';
@@ -506,6 +507,10 @@ class Grid extends ElementMixin(
506507
setTimeout(() => {
507508
this.__updateFooterPositioning();
508509
this.__updateColumnsBodyContentHidden();
510+
if (this._recalculateColumnWidthOnceVisible) {
511+
this._recalculateColumnWidthOnceVisible = false;
512+
this.recalculateColumnWidths();
513+
}
509514
}),
510515
).observe(this.$.table);
511516

@@ -717,12 +722,16 @@ class Grid extends ElementMixin(
717722
if (!this._columnTree) {
718723
return; // No columns
719724
}
725+
if (isElementHidden(this)) {
726+
this._recalculateColumnWidthOnceVisible = true;
727+
return;
728+
}
720729
if (this._cache.isLoading()) {
721730
this._recalculateColumnWidthOnceLoadingFinished = true;
722-
} else {
723-
const cols = this._getColumns().filter((col) => !col.hidden && col.autoWidth);
724-
this._recalculateColumnWidths(cols);
731+
return;
725732
}
733+
const cols = this._getColumns().filter((col) => !col.hidden && col.autoWidth);
734+
this._recalculateColumnWidths(cols);
726735
}
727736

728737
/** @private */

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ describe('column auto-width', () => {
4444
});
4545
}
4646

47+
class TestContainer extends HTMLElement {
48+
constructor() {
49+
super();
50+
this.attachShadow({ mode: 'open' });
51+
this.shadowRoot.innerHTML = `
52+
<slot name="custom-slot"></slot>
53+
`;
54+
}
55+
}
56+
57+
customElements.define('test-container', TestContainer);
58+
4759
beforeEach(async () => {
4860
grid = fixtureSync(`
4961
<vaadin-grid style="width: 600px; height: 200px;" hidden>
@@ -160,6 +172,31 @@ describe('column auto-width', () => {
160172
grid.recalculateColumnWidths();
161173
expectColumnWidthsToBeOk(columns, [114]);
162174
});
175+
176+
it('should defer calculating column widths when the grid is not visible', async () => {
177+
grid.items = testItems;
178+
await recalculateWidths();
179+
expectColumnWidthsToBeOk(columns, [74]);
180+
181+
// Move grid to a custom element without setting proper slot name beforehand
182+
// This reproduces a case in split-layout, which automatically applies slot
183+
// names to children, but only after they have been connected. In this case
184+
// columns should not resize while the grid is invisible, because their size
185+
// would collapse to zero.
186+
const container = fixtureSync('<test-container></test-container>');
187+
container.appendChild(grid);
188+
189+
// Column widths should not have recalculated
190+
await nextFrame();
191+
expectColumnWidthsToBeOk(columns, [74]);
192+
193+
// Apply correct slot name
194+
grid.slot = 'custom-slot';
195+
196+
// Column widths should recalculate, keeping the same width
197+
await recalculateWidths();
198+
expectColumnWidthsToBeOk(columns, [74]);
199+
});
163200
});
164201

165202
describe('tree column', () => {

0 commit comments

Comments
 (0)