Popover positioning
Browse files- src/popover.ts +32 -6
src/popover.ts
CHANGED
|
@@ -64,20 +64,46 @@ function calculatePopoverPosition(element: Element) {
|
|
| 64 |
const popoverArrowDimensions = popover.arrow.getBoundingClientRect();
|
| 65 |
const elementDimensions = element.getBoundingClientRect();
|
| 66 |
|
| 67 |
-
const
|
| 68 |
-
const
|
| 69 |
|
| 70 |
-
const topValue = elementDimensions.top -
|
| 71 |
const isTopOptimal = topValue >= 0;
|
| 72 |
|
| 73 |
-
const bottomValue = window.innerHeight - (elementDimensions.bottom +
|
| 74 |
const isBottomOptimal = bottomValue >= 0;
|
| 75 |
|
| 76 |
-
const leftValue = elementDimensions.left -
|
| 77 |
const isLeftOptimal = leftValue >= 0;
|
| 78 |
|
| 79 |
-
const rightValue = window.innerWidth - (elementDimensions.right +
|
| 80 |
const isRightOptimal = rightValue >= 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
}
|
| 82 |
|
| 83 |
function createPopover(): PopoverDOM {
|
|
|
|
| 64 |
const popoverArrowDimensions = popover.arrow.getBoundingClientRect();
|
| 65 |
const elementDimensions = element.getBoundingClientRect();
|
| 66 |
|
| 67 |
+
const popoverPaddedWidth = popoverDimensions.width + popoverPadding;
|
| 68 |
+
const popoverPaddedHeight = popoverDimensions.height + popoverPadding;
|
| 69 |
|
| 70 |
+
const topValue = elementDimensions.top - popoverPaddedHeight;
|
| 71 |
const isTopOptimal = topValue >= 0;
|
| 72 |
|
| 73 |
+
const bottomValue = window.innerHeight - (elementDimensions.bottom + popoverPaddedHeight);
|
| 74 |
const isBottomOptimal = bottomValue >= 0;
|
| 75 |
|
| 76 |
+
const leftValue = elementDimensions.left - popoverPaddedWidth;
|
| 77 |
const isLeftOptimal = leftValue >= 0;
|
| 78 |
|
| 79 |
+
const rightValue = window.innerWidth - (elementDimensions.right + popoverPaddedWidth);
|
| 80 |
const isRightOptimal = rightValue >= 0;
|
| 81 |
+
|
| 82 |
+
const noneOptimal = !isTopOptimal && !isBottomOptimal && !isLeftOptimal && !isRightOptimal;
|
| 83 |
+
if (noneOptimal) {
|
| 84 |
+
return {
|
| 85 |
+
left: window.innerWidth / 2 - popoverDimensions.width / 2,
|
| 86 |
+
bottom: 10,
|
| 87 |
+
};
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
// @todo placement based on the side and alignment
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
function getLeftValueAfterAlignment(element: Element) {
|
| 94 |
+
if (!popover) {
|
| 95 |
+
return;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
const popoverRect = popover.wrapper.getBoundingClientRect();
|
| 99 |
+
const elementRect = element.getBoundingClientRect();
|
| 100 |
+
|
| 101 |
+
const requiredAlignment = 'left';
|
| 102 |
+
const popoverWidth = popoverRect.width;
|
| 103 |
+
const pos = element.getBoundingClientRect().left;
|
| 104 |
+
const end = window.innerWidth;
|
| 105 |
+
const elementLength = elementRect.width;
|
| 106 |
+
const extraPadding = popover.arrow.getBoundingClientRect().width;
|
| 107 |
}
|
| 108 |
|
| 109 |
function createPopover(): PopoverDOM {
|