Skip to content

Commit acf4f65

Browse files
authored
refactor: replace vaadin-grid-appear animation with ResizeObserver (#10302)
1 parent 8f87426 commit acf4f65

File tree

12 files changed

+52
-74
lines changed

12 files changed

+52
-74
lines changed

packages/grid-pro/test/keyboard-navigation.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@vaadin/chai-plugins';
22
import { sendKeys } from '@vaadin/test-runner-commands';
3-
import { aTimeout, fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers';
3+
import { aTimeout, fixtureSync, nextFrame, nextResize } from '@vaadin/testing-helpers';
44
import sinon from 'sinon';
55
import '../src/vaadin-grid-pro.js';
66
import '../src/vaadin-grid-pro-edit-column.js';
@@ -30,8 +30,7 @@ describe('keyboard navigation', () => {
3030
};
3131

3232
grid.items = createItems();
33-
// Wait for vaadin-grid-appear animation to finish
34-
await oneEvent(grid, 'animationend');
33+
await nextResize(grid);
3534
flushGrid(grid);
3635
});
3736

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@ import '@vaadin/component-base/src/styles/style-props.js';
77
import { css } from 'lit';
88

99
export const gridStyles = css`
10-
@keyframes vaadin-grid-appear {
11-
to {
12-
opacity: 1;
13-
}
14-
}
15-
1610
:host {
1711
display: flex;
18-
animation: 1ms vaadin-grid-appear;
1912
max-width: 100%;
2013
height: 400px;
2114
min-height: var(--_grid-min-height, 0);

packages/grid/src/vaadin-grid-column-auto-width-mixin.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ export const ColumnAutoWidthMixin = (superClass) =>
3030
];
3131
}
3232

33-
constructor() {
34-
super();
35-
this.addEventListener('animationend', this.__onAnimationEndAutoWidth);
36-
}
33+
/** @protected */
34+
updated(props) {
35+
super.updated(props);
3736

38-
/** @private */
39-
__onAnimationEndAutoWidth(e) {
40-
if (e.animationName.indexOf('vaadin-grid-appear') === 0) {
37+
// If the grid was hidden and is now visible
38+
if (props.has('__hostVisible') && !props.get('__hostVisible')) {
4139
this.__tryToRecalculateColumnWidthsIfPending();
4240
}
4341
}

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,6 @@ export const GridMixin = (superClass) =>
170170
};
171171
}
172172

173-
constructor() {
174-
super();
175-
this.addEventListener('animationend', this._onAnimationEnd);
176-
}
177-
178173
/** @private */
179174
get _firstVisibleIndex() {
180175
const firstVisibleItem = this.__getFirstVisibleItem();
@@ -279,6 +274,14 @@ export const GridMixin = (superClass) =>
279274
updated(props) {
280275
super.updated(props);
281276

277+
// If the grid was hidden and is now visible
278+
if (props.has('__hostVisible') && !props.get('__hostVisible')) {
279+
// Ensure header and footer have tabbable elements
280+
this._resetKeyboardNavigation();
281+
282+
requestAnimationFrame(() => this.__scrollToPendingIndexes());
283+
}
284+
282285
if (props.has('__headerRect') || props.has('__footerRect') || props.has('__itemsRect')) {
283286
setTimeout(() => this.__updateMinHeight());
284287
}
@@ -824,21 +827,6 @@ export const GridMixin = (superClass) =>
824827
this.__updateHorizontalScrollPosition();
825828
}
826829

827-
/** @private */
828-
_onAnimationEnd(e) {
829-
// ShadyCSS applies scoping suffixes to animation names
830-
if (e.animationName.indexOf('vaadin-grid-appear') === 0) {
831-
e.stopPropagation();
832-
833-
// Ensure header and footer have tabbable elements
834-
this._resetKeyboardNavigation();
835-
836-
requestAnimationFrame(() => {
837-
this.__scrollToPendingIndexes();
838-
});
839-
}
840-
}
841-
842830
/**
843831
* @param {!HTMLTableRowElement} row
844832
* @return {!GridItemModel}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ export const ResizeMixin = (superClass) =>
1313
class extends superClass {
1414
static get properties() {
1515
return {
16+
/** @private */
17+
__hostVisible: {
18+
type: Boolean,
19+
value: false,
20+
},
21+
1622
/** @private */
1723
__tableRect: Object,
1824

@@ -32,6 +38,11 @@ export const ResizeMixin = (superClass) =>
3238
super.ready();
3339

3440
const resizeObserver = new ResizeObserver((entries) => {
41+
const hostEntry = entries.findLast(({ target }) => target === this);
42+
if (hostEntry) {
43+
this.__hostVisible = this.checkVisibility();
44+
}
45+
3546
const tableEntry = entries.findLast(({ target }) => target === this.$.table);
3647
if (tableEntry) {
3748
this.__tableRect = tableEntry.contentRect;
@@ -53,6 +64,7 @@ export const ResizeMixin = (superClass) =>
5364
}
5465
});
5566

67+
resizeObserver.observe(this);
5668
resizeObserver.observe(this.$.table);
5769
resizeObserver.observe(this.$.header);
5870
resizeObserver.observe(this.$.items);

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2-
import { aTimeout, fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, nextFrame, nextResize } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import './grid-test-styles.js';
55
import '../all-imports.js';
@@ -79,10 +79,8 @@ describe('column auto-width', () => {
7979
`);
8080
spy = sinon.spy(grid, '_recalculateColumnWidths');
8181
columns = grid.querySelectorAll('vaadin-grid-column');
82-
// Show the grid and wait for animationend event ("vaadin-grid-appear")
83-
// to ensure the grid is in a consistent state before starting each test
8482
grid.hidden = false;
85-
await oneEvent(grid, 'animationend');
83+
await nextResize(grid);
8684
});
8785

8886
it('should have correct column widths when items are set', async () => {
@@ -188,9 +186,9 @@ describe('column auto-width', () => {
188186
</vaadin-grid>
189187
`);
190188

191-
await nextFrame();
189+
await nextResize(grid);
192190
grid.hidden = false;
193-
await oneEvent(grid, 'animationend');
191+
await nextResize(grid);
194192
expectColumnWidthsToBeOk(grid.querySelectorAll('vaadin-grid-column'), [107]);
195193
});
196194

packages/grid/test/drag-and-drop.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@vaadin/chai-plugins';
22
import { resetMouse, sendMouse, sendMouseToElement } from '@vaadin/test-runner-commands';
3-
import { aTimeout, fixtureSync, listenOnce, nextFrame, oneEvent } from '@vaadin/testing-helpers';
3+
import { aTimeout, fixtureSync, listenOnce, nextFrame, nextResize } from '@vaadin/testing-helpers';
44
import sinon from 'sinon';
55
import './grid-test-styles.js';
66
import '../src/vaadin-grid.js';
@@ -114,9 +114,9 @@ describe('drag and drop', () => {
114114
<vaadin-grid-column path="last" header="Last name"></vaadin-grid-column>
115115
</vaadin-grid>
116116
`);
117-
await nextFrame();
117+
await nextResize(grid);
118118
grid.hidden = false;
119-
await oneEvent(grid, 'animationend');
119+
await nextResize(grid);
120120
await aTimeout(1);
121121

122122
dragData = {};

packages/grid/test/hidden-grid.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@vaadin/chai-plugins';
22
import { sendKeys } from '@vaadin/test-runner-commands';
3-
import { fixtureSync, nextFrame, nextRender, oneEvent } from '@vaadin/testing-helpers';
3+
import { fixtureSync, nextFrame, nextRender, nextResize } from '@vaadin/testing-helpers';
44
import sinon from 'sinon';
55
import './grid-test-styles.js';
66
import '../src/vaadin-grid.js';
@@ -31,14 +31,14 @@ describe('hidden grid', () => {
3131

3232
it('should have content on appear', async () => {
3333
grid.removeAttribute('hidden');
34-
await oneEvent(grid, 'animationend');
34+
await nextResize(grid);
3535
await nextFrame();
3636
expect(getBodyCellContent(grid, 0, 0).textContent).to.equal('0');
3737
});
3838

3939
it('should make it possible to Tab to header', async () => {
4040
grid.removeAttribute('hidden');
41-
await oneEvent(grid, 'animationend');
41+
await nextResize(grid);
4242
await nextFrame();
4343

4444
await sendKeys({ press: 'Tab' });

packages/grid/test/resizing.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2-
import { aTimeout, fixtureSync, nextFrame, nextResize, oneEvent } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, nextFrame, nextResize } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import './grid-test-styles.js';
55
import '../all-imports.js';
@@ -38,9 +38,9 @@ describe('resizing', () => {
3838
root.textContent = 'footer';
3939
};
4040
grid.dataProvider = infiniteDataProvider;
41-
await nextFrame();
41+
await nextResize(grid);
4242
grid.hidden = false;
43-
await oneEvent(grid, 'animationend');
43+
await nextResize(grid);
4444
flushGrid(grid);
4545
});
4646

packages/grid/test/row-height.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2-
import { aTimeout, fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, nextFrame, nextResize } from '@vaadin/testing-helpers';
33
import './grid-test-styles.js';
44
import '../src/vaadin-grid.js';
55
import { flushGrid, getRowCells, getRows, infiniteDataProvider, scrollToEnd } from './helpers.js';
@@ -54,14 +54,14 @@ describe('rows', () => {
5454
async function init(fixtureFactory) {
5555
grid = fixtureFactory();
5656

57-
await nextFrame();
57+
await nextResize(grid);
5858

5959
grid.dataProvider = infiniteDataProvider;
6060
grid.hidden = false;
6161
flushGrid(grid);
6262
header = grid.$.header;
6363

64-
await oneEvent(grid, 'animationend');
64+
await nextResize(grid);
6565
flushGrid(grid);
6666

6767
rows = getRows(grid.$.items);

0 commit comments

Comments
 (0)