|
| 1 | +import { autoUpdate, flip, offset, size, useFloating } from "@floating-ui/react"; |
| 2 | +import { useMemo } from "react"; |
| 3 | + |
| 4 | +// Menu can grow to at most this height; floating-ui shrinks it further when the |
| 5 | +// available viewport space is smaller. Mirrors the previous SCSS cap. |
| 6 | +const MAX_MENU_HEIGHT = 320; |
| 7 | +// Gap between the input and the menu, and breathing room from the viewport edge. |
| 8 | +const MENU_OFFSET = 4; |
| 9 | +const VIEWPORT_PADDING = 8; |
| 10 | + |
| 11 | +export function useFloatingMenu(open: boolean): ReturnType<typeof useFloating> { |
| 12 | + const middleware = useMemo( |
| 13 | + () => [ |
| 14 | + offset(MENU_OFFSET), |
| 15 | + flip({ |
| 16 | + crossAxis: false, |
| 17 | + fallbackStrategy: "bestFit", |
| 18 | + padding: VIEWPORT_PADDING |
| 19 | + }), |
| 20 | + size({ |
| 21 | + padding: VIEWPORT_PADDING, |
| 22 | + apply({ rects, elements, availableHeight }) { |
| 23 | + Object.assign(elements.floating.style, { |
| 24 | + width: `${rects.reference.width}px`, |
| 25 | + maxHeight: `${Math.min(availableHeight, MAX_MENU_HEIGHT)}px` |
| 26 | + }); |
| 27 | + } |
| 28 | + }) |
| 29 | + ], |
| 30 | + [] |
| 31 | + ); |
| 32 | + |
| 33 | + const result = useFloating({ |
| 34 | + open, |
| 35 | + placement: "bottom-start", |
| 36 | + strategy: "fixed", |
| 37 | + middleware, |
| 38 | + whileElementsMounted: autoUpdate |
| 39 | + }); |
| 40 | + |
| 41 | + const floatingStyles = |
| 42 | + open && result.isPositioned |
| 43 | + ? result.floatingStyles |
| 44 | + : { ...result.floatingStyles, visibility: "hidden" as const }; |
| 45 | + |
| 46 | + return { ...result, floatingStyles }; |
| 47 | +} |
0 commit comments