|
| 1 | +let anchors = 0 |
| 2 | + |
| 3 | +/** |
| 4 | + * Opens a dropdown's menu where it fits. Call it as soon as the menu is in the page, laid out by its |
| 5 | + * CSS against its positioning box (the trigger, or a split button): beside it, at its gap, with its |
| 6 | + * width and inline alignment. The menu opens below unless only above has room; with room on neither |
| 7 | + * side it takes the taller one and scrolls. Sideways it keeps 16px clear of the window's edges, and |
| 8 | + * on phones takes that whole width (long rows end in an ellipsis). |
| 9 | + * |
| 10 | + * The menu then moves to the top layer (a manual popover), so no scrolling box, such as the product |
| 11 | + * carousel's track, cuts it off, and stays tied to its box by CSS anchor positioning as the page |
| 12 | + * scrolls. A browser without anchor positioning keeps it in its box. |
| 13 | + */ |
| 14 | +export function placeMenu(menu: HTMLElement) { |
| 15 | + const box = menu.offsetParent |
| 16 | + if (!(box instanceof HTMLElement)) return |
| 17 | + // Measured as laid out, not as drawn: the opening animation scales the menu from a corner. |
| 18 | + menu.style.animation = "none" |
| 19 | + const left = 16 |
| 20 | + const right = document.documentElement.clientWidth - 16 |
| 21 | + menu.style.maxWidth = `${right - left}px` |
| 22 | + if (window.matchMedia("(max-width: 40rem)").matches) menu.style.width = `${right - left}px` |
| 23 | + const anchor = box.getBoundingClientRect() |
| 24 | + const laid = menu.getBoundingClientRect() |
| 25 | + const gap = Math.max(0, laid.top >= anchor.bottom ? laid.top - anchor.bottom : anchor.top - laid.bottom) |
| 26 | + const below = window.innerHeight - anchor.bottom - gap - 8 |
| 27 | + const above = anchor.top - gap - 8 |
| 28 | + const side = laid.height <= below || (laid.height > above && below >= above) ? "below" : "above" |
| 29 | + const room = side === "below" ? below : above |
| 30 | + const shift = Math.max(left - laid.left, Math.min(0, right - laid.right)) |
| 31 | + |
| 32 | + menu.dataset.side = side |
| 33 | + menu.style.setProperty("--menu-shift", side === "below" ? "-4px" : "4px") |
| 34 | + menu.style.transformOrigin = `${getComputedStyle(menu).transformOrigin.split(" ")[0]} ${side === "below" ? "top" : "bottom"}` |
| 35 | + menu.style.maxHeight = laid.height > room ? `${Math.floor(room)}px` : "" |
| 36 | + menu.style.overflowY = laid.height > room ? "auto" : "" |
| 37 | + if (!CSS.supports("anchor-name: --menu") || typeof menu.showPopover !== "function") { |
| 38 | + menu.style.top = side === "below" ? `calc(100% + ${gap}px)` : "auto" |
| 39 | + menu.style.bottom = side === "above" ? `calc(100% + ${gap}px)` : "auto" |
| 40 | + menu.style.translate = shift ? `${shift}px 0` : "" |
| 41 | + menu.style.animation = "" |
| 42 | + return |
| 43 | + } |
| 44 | + const name = box.style.getPropertyValue("anchor-name") || `--menu-${++anchors}` |
| 45 | + box.style.setProperty("anchor-name", name) |
| 46 | + menu.style.setProperty("position-anchor", name) |
| 47 | + menu.style.position = "fixed" |
| 48 | + menu.style.inset = "auto" |
| 49 | + menu.style.left = `calc(anchor(left) + ${laid.left + shift - anchor.left}px)` |
| 50 | + if (side === "below") menu.style.top = `calc(anchor(bottom) + ${gap}px)` |
| 51 | + if (side === "above") menu.style.bottom = `calc(anchor(top) + ${gap}px)` |
| 52 | + // Its width as laid out against its box; a percentage would now be of the window. |
| 53 | + menu.style.width = `${laid.width}px` |
| 54 | + menu.style.minWidth = "0" |
| 55 | + menu.popover = "manual" |
| 56 | + menu.showPopover() |
| 57 | + menu.style.animation = "" |
| 58 | +} |
0 commit comments