Skip to content

Commit 8a24f5a

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

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

packages/grid/src/vaadin-grid.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { isAndroid, isChrome, isFirefox, isIOS, isSafari, isTouch } from '@vaadi
1212
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
1313
import { Debouncer } from '@vaadin/component-base/src/debounce.js';
1414
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
15+
import { isElementHidden } from '@vaadin/component-base/src/focus-utils.js';
1516
import { TabindexMixin } from '@vaadin/component-base/src/tabindex-mixin.js';
1617
import { processTemplates } from '@vaadin/component-base/src/templates.js';
1718
import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js';
@@ -464,7 +465,15 @@ class Grid extends ElementMixin(
464465
reorderElements: true,
465466
});
466467

467-
new ResizeObserver(() => setTimeout(() => this.__updateFooterPositioning())).observe(this.$.footer);
468+
new ResizeObserver(() =>
469+
setTimeout(() => {
470+
this.__updateFooterPositioning();
471+
if (this._recalculateColumnWidthOnceVisible) {
472+
this._recalculateColumnWidthOnceVisible = false;
473+
this.recalculateColumnWidths();
474+
}
475+
}),
476+
).observe(this.$.table);
468477

469478
processTemplates(this);
470479

@@ -648,12 +657,16 @@ class Grid extends ElementMixin(
648657
if (!this._columnTree) {
649658
return; // No columns
650659
}
660+
if (isElementHidden(this)) {
661+
this._recalculateColumnWidthOnceVisible = true;
662+
return;
663+
}
651664
if (this._cache.isLoading()) {
652665
this._recalculateColumnWidthOnceLoadingFinished = true;
653-
} else {
654-
const cols = this._getColumns().filter((col) => !col.hidden && col.autoWidth);
655-
this._recalculateColumnWidths(cols);
666+
return;
656667
}
668+
const cols = this._getColumns().filter((col) => !col.hidden && col.autoWidth);
669+
this._recalculateColumnWidths(cols);
657670
}
658671

659672
/** @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)