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: update form-layout when its parent becomes visible #7988

Merged
merged 3 commits into from
Oct 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/form-layout/src/vaadin-form-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ class FormLayout extends ResizeMixin(ElementMixin(ThemableMixin(PolymerElement))
_labelsOnTop: {
type: Boolean,
},

/** @private */
__isVisible: {
type: Boolean,
},
};
}

Expand All @@ -264,6 +269,22 @@ class FormLayout extends ResizeMixin(ElementMixin(ThemableMixin(PolymerElement))
this.addEventListener('animationend', this.__onAnimationEnd);
}

constructor() {
super();

this.__intersectionObserver = new IntersectionObserver(([entry]) => {
if (!entry.isIntersecting) {
// Prevent possible jump when layout becomes visible
this.$.layout.style.opacity = 0;
}
if (!this.__isVisible && entry.isIntersecting) {
this._updateLayout();
this.$.layout.style.opacity = '';
}
this.__isVisible = entry.isIntersecting;
});
}

/** @protected */
connectedCallback() {
super.connectedCallback();
Expand All @@ -272,6 +293,7 @@ class FormLayout extends ResizeMixin(ElementMixin(ThemableMixin(PolymerElement))
requestAnimationFrame(() => this._updateLayout());

this._observeChildrenColspanChange();
this.__intersectionObserver.observe(this);
}

/** @protected */
Expand All @@ -280,6 +302,7 @@ class FormLayout extends ResizeMixin(ElementMixin(ThemableMixin(PolymerElement))

this.__mutationObserver.disconnect();
this.__childObserver.disconnect();
this.__intersectionObserver.disconnect();
}

/** @private */
Expand Down
36 changes: 34 additions & 2 deletions packages/form-layout/test/form-layout.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@vaadin/chai-plugins';
import { aTimeout, fixtureSync, nextRender } from '@vaadin/testing-helpers';
import { aTimeout, fixtureSync, nextFrame, nextRender } from '@vaadin/testing-helpers';
import sinon from 'sinon';
import '@polymer/polymer/lib/elements/dom-repeat.js';
import '@vaadin/text-field/vaadin-text-field.js';
Expand Down Expand Up @@ -519,7 +519,10 @@ describe('form layout', () => {
beforeEach(() => {
container = fixtureSync(`
<div hidden>
<vaadin-form-layout></vaadin-form-layout>
<vaadin-form-layout>
<div>Foo</div>
<div>Bar</div>
</vaadin-form-layout>
</div>
`);
layout = container.querySelector('vaadin-form-layout');
Expand All @@ -546,6 +549,35 @@ describe('form layout', () => {
ev.animationName = 'foo';
layout.dispatchEvent(ev);
});

it('should update layout when its parent becomes visible', async () => {
layout.responsiveSteps = [{ columns: 1 }];
await nextRender();

container.hidden = false;

// Wait for intersection observer
await nextFrame();
await nextFrame();

expect(parseFloat(getParsedWidth(layout.children[0]).percentage)).to.be.closeTo(100, 0.1);
expect(parseFloat(getParsedWidth(layout.children[1]).percentage)).to.be.closeTo(100, 0.1);
});

it('should change layout opacity when its parent becomes visible', async () => {
// Wait for intersection observer
await nextFrame();
await nextFrame();
expect(layout.$.layout.style.opacity).to.equal('0');

container.hidden = false;

// Wait for intersection observer
await nextFrame();
await nextFrame();

expect(layout.$.layout.style.opacity).to.equal('');
});
});

describe('mutations', () => {
Expand Down