Skip to content

Commit 5a13239

Browse files
authored
refactor: introduce grid ResizeMixin for resize observers (#10301)
1 parent 61a9dd7 commit 5a13239

File tree

4 files changed

+93
-20
lines changed

4 files changed

+93
-20
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
updateState,
3232
} from './vaadin-grid-helpers.js';
3333
import { KeyboardNavigationMixin } from './vaadin-grid-keyboard-navigation-mixin.js';
34+
import { ResizeMixin } from './vaadin-grid-resize-mixin.js';
3435
import { RowDetailsMixin } from './vaadin-grid-row-details-mixin.js';
3536
import { ScrollMixin } from './vaadin-grid-scroll-mixin.js';
3637
import { SelectionMixin } from './vaadin-grid-selection-mixin.js';
@@ -57,6 +58,7 @@ import { StylingMixin } from './vaadin-grid-styling-mixin.js';
5758
* @mixes EventContextMixin
5859
* @mixes StylingMixin
5960
* @mixes DragAndDropMixin
61+
* @mixes ResizeMixin
6062
*/
6163
export const GridMixin = (superClass) =>
6264
class extends ColumnAutoWidthMixin(
@@ -73,7 +75,7 @@ export const GridMixin = (superClass) =>
7375
FilterMixin(
7476
ColumnReorderingMixin(
7577
ColumnResizingMixin(
76-
EventContextMixin(DragAndDropMixin(StylingMixin(TabindexMixin(superClass)))),
78+
EventContextMixin(DragAndDropMixin(StylingMixin(TabindexMixin(ResizeMixin(superClass))))),
7779
),
7880
),
7981
),
@@ -263,25 +265,6 @@ export const GridMixin = (superClass) =>
263265
__disableHeightPlaceholder: true,
264266
});
265267

266-
new ResizeObserver(() => {
267-
setTimeout(() => {
268-
this.__updateColumnsBodyContentHidden();
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);
274-
275-
const minHeightObserver = new ResizeObserver(() =>
276-
setTimeout(() => {
277-
this.__updateMinHeight();
278-
}),
279-
);
280-
281-
minHeightObserver.observe(this.$.header);
282-
minHeightObserver.observe(this.$.items);
283-
minHeightObserver.observe(this.$.footer);
284-
285268
this._tooltipController = new TooltipController(this);
286269
this.addController(this._tooltipController);
287270
this._tooltipController.setManual(true);
@@ -292,6 +275,23 @@ export const GridMixin = (superClass) =>
292275
});
293276
}
294277

278+
/** @protected */
279+
updated(props) {
280+
super.updated(props);
281+
282+
if (props.has('__headerRect') || props.has('__footerRect') || props.has('__itemsRect')) {
283+
setTimeout(() => this.__updateMinHeight());
284+
}
285+
286+
if (props.has('__tableRect')) {
287+
setTimeout(() => this.__updateColumnsBodyContentHidden());
288+
289+
// Updating data can change the visibility of the scroll bar. Therefore,
290+
// the scroll position has to be recalculated.
291+
this.__updateHorizontalScrollPosition();
292+
}
293+
}
294+
295295
/** @private */
296296
__getBodyCellCoordinates(cell) {
297297
if (this.$.items.contains(cell) && cell.localName === 'td') {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2016 - 2025 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import type { Constructor } from '@open-wc/dedupe-mixin';
7+
8+
export declare function ResizeMixin<T extends Constructor<HTMLElement>>(base: T): Constructor<ResizeMixinClass> & T;
9+
10+
declare class ResizeMixinClass {}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2016 - 2025 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
7+
/**
8+
* A mixin to observe size changes of the grid and its main parts.
9+
*
10+
* @polymerMixin
11+
*/
12+
export const ResizeMixin = (superClass) =>
13+
class extends superClass {
14+
static get properties() {
15+
return {
16+
/** @private */
17+
__tableRect: Object,
18+
19+
/** @private */
20+
__headerRect: Object,
21+
22+
/** @private */
23+
__itemsRect: Object,
24+
25+
/** @private */
26+
__footerRect: Object,
27+
};
28+
}
29+
30+
/** @protected */
31+
ready() {
32+
super.ready();
33+
34+
const resizeObserver = new ResizeObserver((entries) => {
35+
const tableEntry = entries.findLast(({ target }) => target === this.$.table);
36+
if (tableEntry) {
37+
this.__tableRect = tableEntry.contentRect;
38+
}
39+
40+
const headerEntry = entries.findLast(({ target }) => target === this.$.header);
41+
if (headerEntry) {
42+
this.__headerRect = headerEntry.contentRect;
43+
}
44+
45+
const itemsEntry = entries.findLast(({ target }) => target === this.$.items);
46+
if (itemsEntry) {
47+
this.__itemsRect = itemsEntry.contentRect;
48+
}
49+
50+
const footerEntry = entries.findLast(({ target }) => target === this.$.footer);
51+
if (footerEntry) {
52+
this.__footerRect = footerEntry.contentRect;
53+
}
54+
});
55+
56+
resizeObserver.observe(this.$.table);
57+
resizeObserver.observe(this.$.header);
58+
resizeObserver.observe(this.$.items);
59+
resizeObserver.observe(this.$.footer);
60+
}
61+
};

packages/grid/test/typings/grid.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type { ColumnReorderingMixinClass } from '../../src/vaadin-grid-column-re
2323
import type { DataProviderMixinClass } from '../../src/vaadin-grid-data-provider-mixin';
2424
import type { DragAndDropMixinClass } from '../../src/vaadin-grid-drag-and-drop-mixin';
2525
import type { EventContextMixinClass } from '../../src/vaadin-grid-event-context-mixin';
26+
import type { ResizeMixinClass } from '../../src/vaadin-grid-resize-mixin';
2627
import type { RowDetailsMixinClass } from '../../src/vaadin-grid-row-details-mixin';
2728
import type { ScrollMixinClass } from '../../src/vaadin-grid-scroll-mixin';
2829
import type { GridSelectionColumnBaseMixinClass } from '../../src/vaadin-grid-selection-column-base-mixin';
@@ -85,6 +86,7 @@ assertType<ColumnReorderingMixinClass>(narrowedGrid);
8586
assertType<EventContextMixinClass<TestGridItem>>(narrowedGrid);
8687
assertType<StylingMixinClass<TestGridItem>>(narrowedGrid);
8788
assertType<DragAndDropMixinClass<TestGridItem>>(narrowedGrid);
89+
assertType<ResizeMixinClass>(narrowedGrid);
8890

8991
narrowedGrid.addEventListener('active-item-changed', (event) => {
9092
assertType<GridActiveItemChangedEvent<TestGridItem>>(event);

0 commit comments

Comments
 (0)