diff --git a/src/components/ContentPanel.tsx b/src/components/ContentPanel.tsx index c218312e..5975ee8f 100644 --- a/src/components/ContentPanel.tsx +++ b/src/components/ContentPanel.tsx @@ -100,6 +100,9 @@ const ContentPanel: React.FC = ({ const [showArticleMenu, setShowArticleMenu] = useState(false) const [showVideoMenu, setShowVideoMenu] = useState(false) const [showExternalMenu, setShowExternalMenu] = useState(false) + const [articleMenuOpenUpward, setArticleMenuOpenUpward] = useState(false) + const [videoMenuOpenUpward, setVideoMenuOpenUpward] = useState(false) + const [externalMenuOpenUpward, setExternalMenuOpenUpward] = useState(false) const articleMenuRef = useRef(null) const videoMenuRef = useRef(null) const externalMenuRef = useRef(null) @@ -161,6 +164,35 @@ const ContentPanel: React.FC = ({ } }, [showArticleMenu, showVideoMenu, showExternalMenu]) + // Check available space and position menu upward if needed + useEffect(() => { + const checkMenuPosition = (menuRef: React.RefObject, setOpenUpward: (value: boolean) => void) => { + if (!menuRef.current) return + + const menuWrapper = menuRef.current + const menuElement = menuWrapper.querySelector('.article-menu') as HTMLElement + if (!menuElement) return + + const rect = menuWrapper.getBoundingClientRect() + const viewportHeight = window.innerHeight + const spaceBelow = viewportHeight - rect.bottom + const menuHeight = menuElement.offsetHeight || 300 // estimate if not rendered yet + + // Open upward if there's not enough space below (with 20px buffer) + setOpenUpward(spaceBelow < menuHeight + 20 && rect.top > menuHeight) + } + + if (showArticleMenu) { + checkMenuPosition(articleMenuRef, setArticleMenuOpenUpward) + } + if (showVideoMenu) { + checkMenuPosition(videoMenuRef, setVideoMenuOpenUpward) + } + if (showExternalMenu) { + checkMenuPosition(externalMenuRef, setExternalMenuOpenUpward) + } + }, [showArticleMenu, showVideoMenu, showExternalMenu]) + const readingStats = useMemo(() => { const content = markdown || html || '' if (!content) return null @@ -588,7 +620,7 @@ const ContentPanel: React.FC = ({ {showVideoMenu && ( -
+
{showExternalMenu && ( -
+
{showArticleMenu && ( -
+