Skip to content

Commit

Permalink
fix(ui5-popover): address code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TeodorTaushanov committed Nov 24, 2023
1 parent 345bafe commit 0e45bdc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/base/src/util/getParentElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getParentElement = (el: HTMLElement) => {
return (el.parentElement ? el.parentNode : (el.parentNode as ShadowRoot).host) as HTMLElement;
};

export default getParentElement;
19 changes: 10 additions & 9 deletions packages/main/src/Popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import DOMReference from "@ui5/webcomponents-base/dist/types/DOMReference.js";
import { getClosedPopupParent } from "@ui5/webcomponents-base/dist/util/PopupUtils.js";
import clamp from "@ui5/webcomponents-base/dist/util/clamp.js";
import isElementContainingBlock from "@ui5/webcomponents-base/dist/util/isElementContainingBlock.js";
import getParentElement from "@ui5/webcomponents-base/dist/util/getParentElement.js";
import Popup from "./Popup.js";
import type { PopupBeforeCloseEventDetail as PopoverBeforeCloseEventDetail } from "./Popup.js";
import PopoverPlacementType from "./types/PopoverPlacementType.js";
Expand Down Expand Up @@ -472,9 +473,9 @@ class Popover extends Popup {
this.arrowTranslateY = placement!.arrow.y;

top = this._adjustForIOSKeyboard(top);
const containingBlockClientRect = this._getContainingBlockClientRect();
left -= containingBlockClientRect.left;
top -= containingBlockClientRect.top;
const containingBlockClientLocation = this._getContainingBlockClientLocation();
left -= containingBlockClientLocation.left;
top -= containingBlockClientLocation.top;

Object.assign(this.style, {
top: `${top}px`,
Expand Down Expand Up @@ -503,15 +504,15 @@ class Popover extends Popup {
return top + (Number.parseInt(this.style.top || "0") - actualTop);
}

_getContainingBlockClientRect() {
let parentNode = this.parentElement ? this.parentNode as HTMLElement : (this.parentNode as ShadowRoot).host as HTMLElement;
_getContainingBlockClientLocation() {
let parentElement = getParentElement(this);

while (parentNode) {
if (isElementContainingBlock(parentNode)) {
return parentNode.getBoundingClientRect();
while (parentElement) {
if (isElementContainingBlock(parentElement)) {
return parentElement.getBoundingClientRect();
}

parentNode = parentNode.parentElement ? parentNode.parentNode as HTMLElement : (parentNode.parentNode as ShadowRoot).host as HTMLElement;
parentElement = getParentElement(parentElement);
}

return { left: 0, top: 0 };
Expand Down

0 comments on commit 0e45bdc

Please sign in to comment.