Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use a delay in the app layout resize observer (#5575) #5679

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/app-layout/src/vaadin-app-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,10 @@ class AppLayout extends ElementMixin(ThemableMixin(ControllerMixin(PolymerElemen
this._updateOverlayMode();

this._navbarSizeObserver = new ResizeObserver(() => {
this._blockAnimationUntilAfterNextRender();
this._updateOffsetSize();
requestAnimationFrame(() => {
this._blockAnimationUntilAfterNextRender();
this._updateOffsetSize();
});
});
this._navbarSizeObserver.observe(this.$.navbarTop);
this._navbarSizeObserver.observe(this.$.navbarBottom);
Expand Down
31 changes: 27 additions & 4 deletions packages/app-layout/test/app-layout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ import sinon from 'sinon';
import '../vaadin-app-layout.js';
import '../vaadin-drawer-toggle.js';

/**
* Resolves once the function is invoked on the given object.
*/
function onceInvoked(object, functionName) {
return new Promise((resolve) => {
sinon.replace(object, functionName, (...args) => {
sinon.restore();
object[functionName](...args);
resolve();
});
});
}

/**
* Resolves once the ResizeObserver has processed a resize.
*/
async function onceResized(layout) {
await onceInvoked(layout, '_updateOffsetSize');
}

describe('vaadin-app-layout', () => {
let layout;

Expand Down Expand Up @@ -90,12 +110,12 @@ describe('vaadin-app-layout', () => {
navbarContent.style.height = '50px';
navbarContent.setAttribute('slot', 'navbar');
layout.appendChild(navbarContent);
await nextFrame();
await onceResized(layout);
const initialOffset = parseInt(getComputedStyle(layout).getPropertyValue('padding-top'));
expect(initialOffset).to.be.greaterThan(0);
// Increase navbar content size and measure increase
navbarContent.style.height = '100px';
await nextFrame();
await onceResized(layout);
const updatedOffset = parseInt(getComputedStyle(layout).getPropertyValue('padding-top'));
expect(updatedOffset).to.equal(initialOffset + 50);
});
Expand Down Expand Up @@ -134,12 +154,12 @@ describe('vaadin-app-layout', () => {
navbarContent.style.height = '50px';
navbarContent.setAttribute('slot', 'navbar touch-optimized');
layout.appendChild(navbarContent);
await nextFrame();
await onceResized(layout);
const initialOffset = parseInt(getComputedStyle(layout).getPropertyValue('padding-bottom'));
expect(initialOffset).to.be.greaterThan(0);
// Increase navbar content size and measure increase
navbarContent.style.height = '100px';
await nextFrame();
await onceResized(layout);
const updatedOffset = parseInt(getComputedStyle(layout).getPropertyValue('padding-bottom'));
expect(updatedOffset).to.equal(initialOffset + 50);
});
Expand Down Expand Up @@ -167,6 +187,9 @@ describe('vaadin-app-layout', () => {
toggle = layout.querySelector('#toggle');
drawer = layout.shadowRoot.querySelector('[part=drawer]');
backdrop = layout.shadowRoot.querySelector('[part=backdrop]');
// Wait for the initial resize observer callback
await onceResized(layout);
await nextFrame();
}

describe('desktop layout', () => {
Expand Down