Fix/memory leak popup (#2274)

* fix(popup): start positioner only if active

* test(popup): add test for positioning behavior on active state change

* fix(popup): ensure positioner starts only when active and anchor is present
This commit is contained in:
Emanuele Mastaglia
2024-12-02 19:25:54 +01:00
committed by GitHub
parent 9f401f8e8b
commit a764672d03
2 changed files with 25 additions and 2 deletions

View File

@@ -272,8 +272,8 @@ export default class SlPopup extends ShoelaceElement {
}
private start() {
// We can't start the positioner without an anchor
if (!this.anchorEl) {
// We can't start the positioner without an anchor or when the popup is inactive
if (!this.anchorEl || !this.active) {
return;
}

View File

@@ -1,10 +1,33 @@
import '../../../dist/shoelace.js';
import { expect, fixture, html } from '@open-wc/testing';
import type SlPopup from './popup.js';
describe('<sl-popup>', () => {
let element: SlPopup;
it('should render a component', async () => {
const el = await fixture(html` <sl-popup></sl-popup> `);
expect(el).to.exist;
});
it('should properly handle positioning when active changes', async () => {
element = await fixture('<sl-popup></sl-popup>');
element.active = true;
await element.updateComplete;
// SImulate a scroll event
const event = new Event('scroll');
window.dispatchEvent(event);
element.active = false;
await element.updateComplete;
// The component should not throw an error when the window is scrolled
expect(() => {
element.active = true;
window.dispatchEvent(event);
}).not.to.throw();
});
});