From f13850e739cafabebfbccafe3d862b29776d8dfa Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Mon, 16 Sep 2024 18:04:02 -0400 Subject: [PATCH] Fix scrolling menu --- webview-ui/src/components/ContextMenu.tsx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/webview-ui/src/components/ContextMenu.tsx b/webview-ui/src/components/ContextMenu.tsx index ec184b1..ffba82f 100644 --- a/webview-ui/src/components/ContextMenu.tsx +++ b/webview-ui/src/components/ContextMenu.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from "react" +import React, { useEffect, useState, useRef } from "react" import { getContextMenuOptions } from "../utils/mention-context" interface ContextMenuProps { @@ -21,11 +21,28 @@ const ContextMenu: React.FC = ({ selectedType, }) => { const [filteredOptions, setFilteredOptions] = useState(getContextMenuOptions(searchQuery, selectedType)) + const menuRef = useRef(null) useEffect(() => { setFilteredOptions(getContextMenuOptions(searchQuery, selectedType)) }, [searchQuery, selectedType]) + useEffect(() => { + if (menuRef.current) { + const selectedElement = menuRef.current.children[selectedIndex] as HTMLElement + if (selectedElement) { + const menuRect = menuRef.current.getBoundingClientRect() + const selectedRect = selectedElement.getBoundingClientRect() + + if (selectedRect.bottom > menuRect.bottom) { + menuRef.current.scrollTop += selectedRect.bottom - menuRect.bottom + } else if (selectedRect.top < menuRect.top) { + menuRef.current.scrollTop -= menuRect.top - selectedRect.top + } + } + } + }, [selectedIndex]) + return (
= ({ }} onMouseDown={onMouseDown}>