diff --git a/src-web/components/core/Dropdown.tsx b/src-web/components/core/Dropdown.tsx index 1bf45ea1..e82794e5 100644 --- a/src-web/components/core/Dropdown.tsx +++ b/src-web/components/core/Dropdown.tsx @@ -229,17 +229,8 @@ const Menu = forwardRef, MenuPro defaultSelectedIndex ?? null, [defaultSelectedIndex], ); - const [menuStyles, setMenuStyles] = useState({}); const [filter, setFilter] = useState(''); - // Calculate the max height so we can scroll - const initMenu = useCallback((el: HTMLDivElement | null) => { - if (el === null) return {}; - const windowBox = document.documentElement.getBoundingClientRect(); - const menuBox = el.getBoundingClientRect(); - setMenuStyles({ maxHeight: windowBox.height - menuBox.top - 5 }); - }, []); - const handleClose = useCallback(() => { onClose(); setSelectedIndex(null); @@ -355,11 +346,12 @@ const Menu = forwardRef, MenuPro [handleClose, handleNext, handlePrev, handleSelect, items, selectedIndex], ); - const { containerStyles, triangleStyles } = useMemo<{ - containerStyles: CSSProperties; - triangleStyles: CSSProperties | null; + const styles = useMemo<{ + container: CSSProperties; + menu: CSSProperties; + triangle: CSSProperties; }>(() => { - if (triggerShape == null) return { containerStyles: {}, triangleStyles: null }; + if (triggerShape == null) return { container: {}, triangle: {}, menu: {} }; const menuMarginY = 5; const docRect = document.documentElement.getBoundingClientRect(); @@ -371,27 +363,31 @@ const Menu = forwardRef, MenuPro const onRight = horizontalSpaceRemaining < 200; const upsideDown = heightBelow < heightAbove && heightBelow < items.length * 25 + 20 + 200; const triggerWidth = triggerShape.right - triggerShape.left; - const containerStyles = { - top: !upsideDown ? top + menuMarginY : undefined, - bottom: upsideDown - ? docRect.height - top - (triggerShape.top - triggerShape.bottom) + menuMarginY - : undefined, - right: onRight ? docRect.width - triggerShape.right : undefined, - left: !onRight ? triggerShape.left : undefined, - minWidth: fullWidth ? triggerWidth : undefined, - maxWidth: '40rem', + return { + container: { + top: !upsideDown ? top + menuMarginY : undefined, + bottom: upsideDown + ? docRect.height - top - (triggerShape.top - triggerShape.bottom) + menuMarginY + : undefined, + right: onRight ? docRect.width - triggerShape.right : undefined, + left: !onRight ? triggerShape.left : undefined, + minWidth: fullWidth ? triggerWidth : undefined, + maxWidth: '40rem', + }, + triangle: { + width: '0.4rem', + height: '0.4rem', + ...(onRight + ? { right: width / 2, marginRight: '-0.2rem' } + : { left: width / 2, marginLeft: '-0.2rem' }), + ...(upsideDown + ? { bottom: '-0.2rem', rotate: '225deg' } + : { top: '-0.2rem', rotate: '45deg' }), + }, + menu: { + maxHeight: `${(upsideDown ? heightAbove : heightBelow) - 15}px`, + }, }; - const triangleStyles: CSSProperties = { - width: '0.4rem', - height: '0.4rem', - ...(onRight - ? { right: width / 2, marginRight: '-0.2rem' } - : { left: width / 2, marginLeft: '-0.2rem' }), - ...(upsideDown - ? { bottom: '-0.2rem', rotate: '225deg' } - : { top: '-0.2rem', rotate: '45deg' }), - }; - return { containerStyles, triangleStyles }; }, [fullWidth, items.length, triggerShape]); const filteredItems = useMemo( @@ -435,61 +431,58 @@ const Menu = forwardRef, MenuPro role="menu" aria-orientation="vertical" dir="ltr" - style={containerStyles} + style={styles.container} className={classNames(className, 'outline-none my-1 pointer-events-auto fixed z-50')} > - {triangleStyles && showTriangle && ( + {showTriangle && ( )} - {containerStyles && ( - - {filter && ( - - -
{filter}
-
- )} - {filteredItems.length === 0 && ( - No matches - )} - {filteredItems.map((item, i) => { - if (item.hidden) { - return null; - } - if (item.type === 'separator') { - return ( - - {item.label} - - ); - } + + {filter && ( + + +
{filter}
+
+ )} + {filteredItems.length === 0 && ( + No matches + )} + {filteredItems.map((item, i) => { + if (item.hidden) { + return null; + } + if (item.type === 'separator') { return ( - + + {item.label} + ); - })} -
- )} + } + return ( + + ); + })} +