Skip to content

Commit e6d3dda

Browse files
committed
use resize observer
1 parent 164c170 commit e6d3dda

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

packages/uui-popover-container/lib/uui-popover-container.element.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
22
import { findAncestorByAttributeValue } from '@umbraco-ui/uui-base/lib/utils';
3-
import { css, html, LitElement } from 'lit';
3+
import { css, html, LitElement, PropertyValues } from 'lit';
44
import { property, state } from 'lit/decorators.js';
55

66
export type PopoverContainerPlacement =
@@ -69,6 +69,8 @@ export class UUIPopoverContainerElement extends LitElement {
6969

7070
#targetElement: HTMLElement | null = null;
7171
#scrollParents: Element[] = [];
72+
#sizeObserver: ResizeObserver | null = null;
73+
#size: { width: number; height: number } = { width: 0, height: 0 };
7274

7375
connectedCallback(): void {
7476
if (!this.hasAttribute('popover')) {
@@ -79,10 +81,33 @@ export class UUIPopoverContainerElement extends LitElement {
7981
this.addEventListener('beforetoggle', this.#onBeforeToggle);
8082
}
8183

84+
protected override firstUpdated(_changedProperties: PropertyValues): void {
85+
super.firstUpdated(_changedProperties);
86+
87+
// set up an observer that just logs the new width
88+
this.#sizeObserver = new ResizeObserver(entries => {
89+
const element = entries[0]; // should be only one
90+
const width = element.contentRect.width;
91+
const height = element.contentRect.height;
92+
93+
if (width === this.#size.width && height === this.#size.height) {
94+
return; // no change
95+
}
96+
97+
this.#size = { width, height };
98+
this.#initUpdate();
99+
});
100+
101+
// start listening for size changes
102+
this.#sizeObserver.observe(this);
103+
}
104+
82105
disconnectedCallback(): void {
83106
super.disconnectedCallback();
84107
this.removeEventListener('beforetoggle', this.#onBeforeToggle);
85108
this.#stopScrollListener();
109+
this.#sizeObserver?.disconnect();
110+
this.#sizeObserver = null;
86111
}
87112

88113
#onBeforeToggle = (event: any) => {
@@ -351,14 +376,8 @@ export class UUIPopoverContainerElement extends LitElement {
351376
this.#scrollParents.push(document.body);
352377
}
353378

354-
#onSlotChange() {
355-
requestAnimationFrame(() => {
356-
this.#initUpdate();
357-
});
358-
}
359-
360379
render() {
361-
return html`<slot @slotchange=${this.#onSlotChange}></slot>`;
380+
return html`<slot></slot>`;
362381
}
363382

364383
static styles = [

0 commit comments

Comments
 (0)