Skip to content

Commit ae54fd9

Browse files
fix: Recalculate popover position on lazy content (#1129)
* Fix: Trigger popover update on slot content change Adds a slotchange event handler to the slot in UUIPopoverContainerElement. This ensures the popover updates when its slotted content changes by calling #initUpdate within a requestAnimationFrame. * use resize observer * Update uui-popover-container.element.ts * move to connectedCallback
1 parent 5ca8bc4 commit ae54fd9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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')) {
@@ -77,12 +79,32 @@ export class UUIPopoverContainerElement extends LitElement {
7779

7880
super.connectedCallback();
7981
this.addEventListener('beforetoggle', this.#onBeforeToggle);
82+
83+
if (!this.#sizeObserver) {
84+
this.#sizeObserver = new ResizeObserver(entries => {
85+
const element = entries[0]; // should be only one
86+
const width = element.contentRect.width;
87+
const height = element.contentRect.height;
88+
89+
if (width === this.#size.width && height === this.#size.height) {
90+
return; // no change
91+
}
92+
93+
this.#size = { width, height };
94+
this.#initUpdate();
95+
});
96+
97+
// start listening for size changes
98+
this.#sizeObserver.observe(this);
99+
}
80100
}
81101

82102
disconnectedCallback(): void {
83103
super.disconnectedCallback();
84104
this.removeEventListener('beforetoggle', this.#onBeforeToggle);
85105
this.#stopScrollListener();
106+
this.#sizeObserver?.disconnect();
107+
this.#sizeObserver = null;
86108
}
87109

88110
#onBeforeToggle = (event: any) => {

0 commit comments

Comments
 (0)