+ showAside?: boolean;
+}
+
+function positionMenu(menu: HTMLElement, toggle: HTMLElement, showAside: boolean) {
+ const toggleRect = toggle.getBoundingClientRect();
+ const menuBounds = menu.getBoundingClientRect();
+
+ menu.style.position = 'fixed';
+
+ if (showAside) {
+ let targetLeft = toggleRect.right;
+ const isRightOOB = toggleRect.right + menuBounds.width > window.innerWidth;
+ if (isRightOOB) {
+ targetLeft = Math.max(toggleRect.left - menuBounds.width, 0);
+ }
+
+ menu.style.top = toggleRect.top + 'px';
+ menu.style.left = targetLeft + 'px';
+ } else {
+ const isRightOOB = toggleRect.left + menuBounds.width > window.innerWidth;
+ let targetLeft = toggleRect.left;
+ if (isRightOOB) {
+ targetLeft = Math.max(toggleRect.right - menuBounds.width, 0);
+ }
+
+ menu.style.top = toggleRect.bottom + 'px';
+ menu.style.left = targetLeft + 'px';
+ }