Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui5-popover): prevent arrow placement over popover's rounded corners #4960

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/base/hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
EVvTm7WudS+zhfMPH4r1M1Y3GUo=
FQQQvmWOx6YzN2Vive7SCT5CiJ4=
101 changes: 29 additions & 72 deletions packages/main/src/Popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import browserScrollbarCSS from "./generated/themes/BrowserScrollbar.css.js";
import PopupsCommonCss from "./generated/themes/PopupsCommon.css.js";
import PopoverCss from "./generated/themes/Popover.css.js";

const arrowSize = 8;
const ARROW_SIZE = 8;

/**
* @public
Expand Down Expand Up @@ -303,10 +303,6 @@ class Popover extends Popup {
return 10; // px
}

static get ARROW_MARGIN() {
return 6; // px
}

onAfterRendering() {
if (!this.isOpen() && this.open) {
const opener = document.getElementById(this.opener);
Expand Down Expand Up @@ -463,12 +459,8 @@ class Popover extends Popup {
top = Math.max(top, this._top);
}

const isVertical = this.actualPlacementType === PopoverPlacementType.Top || this.actualPlacementType === PopoverPlacementType.Bottom;
const borderRadius = Number.parseInt(window.getComputedStyle(this).getPropertyValue("border-radius"));
const arrow = this._clampArrowPlacement(placement.arrow, isVertical, this._top, this._left, popoverSize, borderRadius);

this.arrowTranslateX = arrow.x;
this.arrowTranslateY = arrow.y;
this.arrowTranslateX = placement.arrow.x;
this.arrowTranslateY = placement.arrow.y;

top = this._adjustForIOSKeyboard(top);

Expand All @@ -483,59 +475,6 @@ class Popover extends Popup {
}
}

/**
* Restricts the arrow's coordinates within the bounds of the popover.
* @private
* @param {{x: number, y: number}} arrow arrow's coordinates
* @param {boolean} isVertical if the popover is placed vertically relative to the opener
* @param {number} top popover's top
* @param {number} left popover's left
* @param {{width: number, height: number}} popoverSize popover's width and height
* @param {number} borderRadius value of the border-radius property
* @returns {{x: number, y: number}} Arrow's coordinates
*/
_clampArrowPlacement({ x, y }, isVertical, top, left, { width, height }, borderRadius) {
const maxY = this._getArrowRange(height, borderRadius);
const maxX = this._getArrowRange(width, borderRadius);

if (isVertical) {
const popoverOnLeftBorderOffset = Popover.VIEWPORT_MARGIN - left;
const popoverOnRightBorderOffset = left + width + Popover.VIEWPORT_MARGIN - document.documentElement.clientWidth;

if (popoverOnLeftBorderOffset > 0) {
x = Math.max(x - popoverOnLeftBorderOffset, -maxX);
} else if (popoverOnRightBorderOffset > 0) {
x = Math.min(x + popoverOnRightBorderOffset, maxX);
}
}

if (!isVertical) {
const popoverOnTopBorderOffset = Popover.VIEWPORT_MARGIN - top;
const popoverOnBottomBorderOffset = top + height + Popover.VIEWPORT_MARGIN - document.documentElement.clientHeight;
if (popoverOnTopBorderOffset > 0) {
y = Math.max(y - popoverOnTopBorderOffset, -maxY);
} else if (popoverOnBottomBorderOffset > 0) {
y = Math.min(y + popoverOnBottomBorderOffset, maxY);
}
}

return {
x: Math.round(x),
y: Math.round(y),
};
}

/**
* Returns the allowed range for the popover arrow based on its dimension.
* @private
* @param {number} dimension the height or width of the popover
* @param {number} borderRadius border radius of the popover
* @returns {number}
*/
_getArrowRange(dimension, borderRadius) {
return Math.floor((dimension / 2) - (borderRadius + Popover.ARROW_MARGIN));
}

/**
* Adjust the desired top position to compensate for shift of the screen
* caused by opened keyboard on iOS which affects all elements with position:fixed.
Expand Down Expand Up @@ -605,7 +544,7 @@ class Popover extends Popup {
popoverSize.height = targetRect.height;
}

const arrowOffset = this.hideArrow ? 0 : arrowSize;
const arrowOffset = this.hideArrow ? 0 : ARROW_SIZE;

// calc popover positions
switch (placementType) {
Expand Down Expand Up @@ -673,7 +612,8 @@ class Popover extends Popup {
this._top = Math.round(top);
}

const arrowPos = this.getArrowPosition(targetRect, popoverSize, left, top, isVertical);
const borderRadius = Number.parseInt(window.getComputedStyle(this).getPropertyValue("border-radius"));
const arrowPos = this.getArrowPosition(targetRect, popoverSize, left, top, isVertical, borderRadius);

return {
arrow: arrowPos,
Expand All @@ -687,33 +627,50 @@ class Popover extends Popup {
* Calculates the position for the arrow.
* @private
* @param targetRect BoundingClientRect of the target element
* @param popoverSize Width and height of the popover
* @param {{width: number, height: number}} popoverSize Width and height of the popover
* @param left Left offset of the popover
* @param top Top offset of the popover
* @param isVertical if the popover is positioned vertically to the target element
* @param isVertical If the popover is positioned vertically to the target element
* @param {number} borderRadius Value of the border-radius property
* @returns {{x: number, y: number}} Arrow's coordinates
*/
getArrowPosition(targetRect, popoverSize, left, top, isVertical) {
getArrowPosition(targetRect, { width, height }, left, top, isVertical, borderRadius) {
let arrowXCentered = this.horizontalAlign === PopoverHorizontalAlign.Center || this.horizontalAlign === PopoverHorizontalAlign.Stretch;

if (this.horizontalAlign === PopoverHorizontalAlign.Right && left <= targetRect.left) {
arrowXCentered = true;
}

if (this.horizontalAlign === PopoverHorizontalAlign.Left && left + popoverSize.width >= targetRect.left + targetRect.width) {
if (this.horizontalAlign === PopoverHorizontalAlign.Left && left + width >= targetRect.left + targetRect.width) {
arrowXCentered = true;
}

let arrowTranslateX = 0;
if (isVertical && arrowXCentered) {
arrowTranslateX = targetRect.left + targetRect.width / 2 - left - popoverSize.width / 2;
arrowTranslateX = targetRect.left + targetRect.width / 2 - left - width / 2;
}

let arrowTranslateY = 0;
if (!isVertical) {
arrowTranslateY = targetRect.top + targetRect.height / 2 - top - popoverSize.height / 2;
arrowTranslateY = targetRect.top + targetRect.height / 2 - top - height / 2;
}

// Restricts the arrow's translate value along each dimension,
// so that the arrow does not clip over the popover's rounded borders.
const safeRangeForArrowY = height / 2 - borderRadius - ARROW_SIZE / 2;
arrowTranslateY = clamp(
arrowTranslateY,
-safeRangeForArrowY,
safeRangeForArrowY,
);

const safeRangeForArrowX = width / 2 - borderRadius - ARROW_SIZE / 2;
arrowTranslateX = clamp(
arrowTranslateX,
-safeRangeForArrowX,
safeRangeForArrowX,
);

return {
x: Math.round(arrowTranslateX),
y: Math.round(arrowTranslateY),
Expand Down
5 changes: 3 additions & 2 deletions packages/main/test/pages/PopoverArrowBounds.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<link rel="stylesheet" href="styles/PopoverArrowBounds.css">
</head>

<body>
<body class="sapUiSizeCompact">
<ui5-button id="myBtn1" data-placement-type="Left" class="myBtn">1</ui5-button>
<ui5-button id="myBtn2" data-placement-type="Left" class="myBtn">2</ui5-button>
<ui5-button id="myBtn2" data-placement-type="Left" data-vertical-align="Top" class="myBtn">2</ui5-button>
<ui5-button id="myBtn3" data-placement-type="Bottom" class="myBtn">3</ui5-button>
<ui5-button id="myBtn4" data-placement-type="Top" class="myBtn">4</ui5-button>
<ui5-button id="myBtn5" data-placement-type="Left" class="myBtn">5</ui5-button>
Expand All @@ -37,6 +37,7 @@
Array.from(document.querySelectorAll(".myBtn")).forEach(function (btn) {
btn.addEventListener("click", function() {
myPopover.placementType = btn.dataset.placementType;
myPopover.verticalAlign = btn.dataset.verticalAlign;
myPopover.showAt(btn);
});
});
Expand Down
10 changes: 7 additions & 3 deletions packages/main/test/pages/styles/PopoverArrowBounds.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}

#myBtn2 {
top: 0;
top: 40px;
right: 0;
}

Expand Down Expand Up @@ -40,6 +40,10 @@
}

.customSize {
height: 100px;
width: 100px;
height: 200px;
width: 200px;
}

#myPopover::part(content) {
padding: 0;
}