Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
🐛 Take the visual viewport into account for Safari when positioning t…
Browse files Browse the repository at this point in the history
…he menu
  • Loading branch information
lowiebenoot committed Jan 9, 2024
1 parent d7e3063 commit 44d20d8
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/components/menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,39 @@ const Menu = <S,>({
const { top, left, height, width } = anchorElement.getBoundingClientRect();
const { height: menuHeight, width: menuWidth } = menuRef.current.getBoundingClientRect();

let leftOffset = 0;
let topOffset = 0;

if (/^((?!chrome|android).)*safari/i.test(navigator.userAgent)) {
leftOffset = window.visualViewport?.offsetLeft || 0;
topOffset = window.visualViewport?.offsetTop || 0;
}

if (positionState === POSITION.TOP_LEFT) {
return {
top: top + height + 3,
left,
top: top + height + 3 + topOffset,
left: left + leftOffset,
};
}

if (positionState === POSITION.TOP_RIGHT) {
return {
top: top + height + 3,
left: left + width - menuWidth,
top: top + height + 3 + topOffset,
left: left + width - menuWidth + leftOffset,
};
}

if (positionState === POSITION.BOTTOM_LEFT) {
return {
top: -1 * (menuHeight + 3) + top,
left,
top: -1 * (menuHeight + 3) + top + topOffset,
left: left + leftOffset,
};
}

if (positionState === POSITION.BOTTOM_RIGHT) {
return {
top: -1 * (menuHeight + 3) + top,
left: left + width - menuWidth,
top: -1 * (menuHeight + 3) + top + topOffset,
left: left + width - menuWidth + leftOffset,
};
}
}
Expand Down

0 comments on commit 44d20d8

Please sign in to comment.