make sure popup position is never off screen to the left

This commit is contained in:
tess 2024-11-12 21:16:59 +01:00
parent 917e67d356
commit 101ca9e0f7
1 changed files with 10 additions and 0 deletions

View File

@ -15,6 +15,8 @@ export function calcPopupPosition(el: HTMLElement, props: {
const contentWidth = el.offsetWidth; const contentWidth = el.offsetWidth;
const contentHeight = el.offsetHeight; const contentHeight = el.offsetHeight;
const LEFT_MARGIN = 4;
let rect: DOMRect; let rect: DOMRect;
if (props.anchorElement) { if (props.anchorElement) {
@ -39,6 +41,8 @@ export function calcPopupPosition(el: HTMLElement, props: {
left = window.innerWidth - contentWidth + window.scrollX - 1; left = window.innerWidth - contentWidth + window.scrollX - 1;
} }
left = Math.max(LEFT_MARGIN, left);
return [left, top]; return [left, top];
}; };
@ -60,6 +64,8 @@ export function calcPopupPosition(el: HTMLElement, props: {
left = window.innerWidth - contentWidth + window.scrollX - 1; left = window.innerWidth - contentWidth + window.scrollX - 1;
} }
left = Math.max(LEFT_MARGIN, left);
return [left, top]; return [left, top];
}; };
@ -75,6 +81,8 @@ export function calcPopupPosition(el: HTMLElement, props: {
top = props.y; top = props.y;
} }
left = Math.max(LEFT_MARGIN, left);
top -= (el.offsetHeight / 2); top -= (el.offsetHeight / 2);
if (top + contentHeight - window.scrollY > window.innerHeight) { if (top + contentHeight - window.scrollY > window.innerHeight) {
@ -106,6 +114,8 @@ export function calcPopupPosition(el: HTMLElement, props: {
top -= (el.offsetHeight / 2); top -= (el.offsetHeight / 2);
} }
left = Math.max(LEFT_MARGIN, left);
if (top + contentHeight - window.scrollY > window.innerHeight) { if (top + contentHeight - window.scrollY > window.innerHeight) {
top = window.innerHeight - contentHeight + window.scrollY - 1; top = window.innerHeight - contentHeight + window.scrollY - 1;
} }