Skip to content

Commit 9e6488a

Browse files
committed
added comments and changed names for better readability
1 parent be46936 commit 9e6488a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class UUIPopoverContainerElement extends LitElement {
5252
@state()
5353
_actualPlacement: PopoverContainerPlacement = this._placement;
5454

55-
#target: HTMLElement | null = null;
55+
#targetElement: HTMLElement | null = null;
5656

5757
connectedCallback(): void {
5858
super.connectedCallback();
@@ -65,13 +65,13 @@ export class UUIPopoverContainerElement extends LitElement {
6565
return;
6666
}
6767

68-
this.addEventListener('beforetoggle', this.#beforeToggle);
68+
this.addEventListener('beforetoggle', this.#onBeforeToggle);
6969
}
7070

7171
disconnectedCallback(): void {
7272
super.disconnectedCallback();
7373

74-
this.removeEventListener('beforetoggle', this.#beforeToggle);
74+
this.removeEventListener('beforetoggle', this.#onBeforeToggle);
7575
}
7676

7777
#updatePadding = () => {
@@ -94,16 +94,16 @@ export class UUIPopoverContainerElement extends LitElement {
9494
(this.style as any)[paddingSide] = `10px`;
9595
};
9696

97-
#beforeToggle = async (event: any) => {
98-
this.#target = this.#findAncestorWithAttribute(
97+
#onBeforeToggle = async (event: any) => {
98+
this.#targetElement = this.#findAncestorWithAttribute(
9999
this,
100100
'popovertarget',
101101
this.id
102102
);
103103

104104
// Dispatch a custom event that can be listened to by the popover target.
105105
// Mostly used for UUIButton.
106-
this.#target?.dispatchEvent(
106+
this.#targetElement?.dispatchEvent(
107107
new CustomEvent('uui-popover-before-toggle', {
108108
bubbles: false,
109109
composed: false,
@@ -129,13 +129,17 @@ export class UUIPopoverContainerElement extends LitElement {
129129
#initUpdate = () => {
130130
this._actualPlacement = this._placement;
131131
this.style.opacity = '0';
132+
// 3 iterations makes the popover flip back to the initial position if theres no space for it on either side.
132133
this.#updatePosition(3);
133134
};
134135

135136
#updatePosition = (iteration: number) => {
136137
this.#updatePadding();
138+
139+
// Iterations makes sure that we don't overflow the stack.
140+
// That could happen if the is no space for the popover on either side, which without iterations, would make it flip back and forth until the stack overflows
137141
iteration--;
138-
if (this.#target === null) return;
142+
if (this.#targetElement === null) return;
139143

140144
const isTopPlacement = this._actualPlacement.indexOf('top') !== -1;
141145
const isBottomPlacement = this._actualPlacement.indexOf('bottom') !== -1;
@@ -145,7 +149,7 @@ export class UUIPopoverContainerElement extends LitElement {
145149
const isStart = this._actualPlacement.indexOf('-start') !== -1;
146150
const isEnd = this._actualPlacement.indexOf('-end') !== -1;
147151

148-
const targetRect = this.#target.getBoundingClientRect();
152+
const targetRect = this.#targetElement.getBoundingClientRect();
149153
const popoverRect = this.getBoundingClientRect();
150154

151155
let top = 0;
@@ -215,6 +219,8 @@ export class UUIPopoverContainerElement extends LitElement {
215219
);
216220

217221
const topClamp = Math.max(topTargetVsScreenTop, topTargetVsScreenBottom);
222+
// if we're currently in a top or bottom placement and the popover is outside the screen, and we have more iterations left.
223+
// Then flip the placement to opposite side
218224
if (
219225
topClamp !== top &&
220226
(isTopPlacement || isBottomPlacement) &&
@@ -237,6 +243,8 @@ export class UUIPopoverContainerElement extends LitElement {
237243
);
238244

239245
const leftClamp = Math.max(leftTargetVsScreenLeft, leftTargetVsScreenRight);
246+
// if we're currently in a left or right placement and the popover is outside the screen, and we have more iterations left.
247+
// Then flip the placement to opposite side
240248
if (
241249
leftClamp !== left &&
242250
(isLeftPlacement || isRightPlacement) &&

0 commit comments

Comments
 (0)