Handle preferred side functionality
Browse files- src/popover.ts +18 -7
src/popover.ts
CHANGED
@@ -65,7 +65,7 @@ function getPopoverDimensions() {
|
|
65 |
};
|
66 |
}
|
67 |
|
68 |
-
function
|
69 |
alignment: Alignment,
|
70 |
config: {
|
71 |
elementDimensions: DOMRect;
|
@@ -161,6 +161,7 @@ export function repositionPopover(element: Element) {
|
|
161 |
|
162 |
// Configure the popover positioning
|
163 |
const requiredAlignment: Alignment = "start";
|
|
|
164 |
const popoverPadding = STAGE_PADDING;
|
165 |
|
166 |
const popoverDimensions = getPopoverDimensions();
|
@@ -168,20 +169,30 @@ export function repositionPopover(element: Element) {
|
|
168 |
const elementDimensions = element.getBoundingClientRect();
|
169 |
|
170 |
const topValue = elementDimensions.top - popoverDimensions!.height;
|
171 |
-
|
172 |
|
173 |
const bottomValue = window.innerHeight - (elementDimensions.bottom + popoverDimensions!.height);
|
174 |
-
|
175 |
|
176 |
const leftValue = elementDimensions.left - popoverDimensions!.width;
|
177 |
-
|
178 |
|
179 |
const rightValue = window.innerWidth - (elementDimensions.right + popoverDimensions!.width);
|
180 |
-
|
181 |
|
182 |
const noneOptimal = !isTopOptimal && !isBottomOptimal && !isLeftOptimal && !isRightOptimal;
|
183 |
let popoverRenderedSide: Side;
|
184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
if (noneOptimal) {
|
186 |
const leftValue = window.innerWidth / 2 - popoverDimensions?.realWidth! / 2;
|
187 |
const bottomValue = 10;
|
@@ -196,7 +207,7 @@ export function repositionPopover(element: Element) {
|
|
196 |
window.innerWidth - popoverDimensions?.realWidth - popoverArrowDimensions.width
|
197 |
);
|
198 |
|
199 |
-
const topToSet =
|
200 |
elementDimensions,
|
201 |
popoverDimensions,
|
202 |
popoverPadding,
|
@@ -214,7 +225,7 @@ export function repositionPopover(element: Element) {
|
|
214 |
rightValue,
|
215 |
window.innerWidth - popoverDimensions?.realWidth - popoverArrowDimensions.width
|
216 |
);
|
217 |
-
const topToSet =
|
218 |
elementDimensions,
|
219 |
popoverDimensions,
|
220 |
popoverPadding,
|
|
|
65 |
};
|
66 |
}
|
67 |
|
68 |
+
function calculateTopForLeftRight(
|
69 |
alignment: Alignment,
|
70 |
config: {
|
71 |
elementDimensions: DOMRect;
|
|
|
161 |
|
162 |
// Configure the popover positioning
|
163 |
const requiredAlignment: Alignment = "start";
|
164 |
+
const requiredSide: Side = "left";
|
165 |
const popoverPadding = STAGE_PADDING;
|
166 |
|
167 |
const popoverDimensions = getPopoverDimensions();
|
|
|
169 |
const elementDimensions = element.getBoundingClientRect();
|
170 |
|
171 |
const topValue = elementDimensions.top - popoverDimensions!.height;
|
172 |
+
let isTopOptimal = topValue >= 0;
|
173 |
|
174 |
const bottomValue = window.innerHeight - (elementDimensions.bottom + popoverDimensions!.height);
|
175 |
+
let isBottomOptimal = bottomValue >= 0;
|
176 |
|
177 |
const leftValue = elementDimensions.left - popoverDimensions!.width;
|
178 |
+
let isLeftOptimal = leftValue >= 0;
|
179 |
|
180 |
const rightValue = window.innerWidth - (elementDimensions.right + popoverDimensions!.width);
|
181 |
+
let isRightOptimal = rightValue >= 0;
|
182 |
|
183 |
const noneOptimal = !isTopOptimal && !isBottomOptimal && !isLeftOptimal && !isRightOptimal;
|
184 |
let popoverRenderedSide: Side;
|
185 |
|
186 |
+
if (requiredSide === "top" && isTopOptimal) {
|
187 |
+
isRightOptimal = isLeftOptimal = isBottomOptimal = false;
|
188 |
+
} else if (requiredSide === "bottom" && isBottomOptimal) {
|
189 |
+
isRightOptimal = isLeftOptimal = isTopOptimal = false;
|
190 |
+
} else if (requiredSide === "left" && isLeftOptimal) {
|
191 |
+
isRightOptimal = isTopOptimal = isBottomOptimal = false;
|
192 |
+
} else if (requiredSide === "right" && isRightOptimal) {
|
193 |
+
isLeftOptimal = isTopOptimal = isBottomOptimal = false;
|
194 |
+
}
|
195 |
+
|
196 |
if (noneOptimal) {
|
197 |
const leftValue = window.innerWidth / 2 - popoverDimensions?.realWidth! / 2;
|
198 |
const bottomValue = 10;
|
|
|
207 |
window.innerWidth - popoverDimensions?.realWidth - popoverArrowDimensions.width
|
208 |
);
|
209 |
|
210 |
+
const topToSet = calculateTopForLeftRight(requiredAlignment, {
|
211 |
elementDimensions,
|
212 |
popoverDimensions,
|
213 |
popoverPadding,
|
|
|
225 |
rightValue,
|
226 |
window.innerWidth - popoverDimensions?.realWidth - popoverArrowDimensions.width
|
227 |
);
|
228 |
+
const topToSet = calculateTopForLeftRight(requiredAlignment, {
|
229 |
elementDimensions,
|
230 |
popoverDimensions,
|
231 |
popoverPadding,
|