feat: auto-scroll to selected menu item on model open

This commit is contained in:
kangfenmao 2025-01-19 15:47:19 +08:00
parent 67b63ee07a
commit afc2e2f595

View File

@ -32,6 +32,7 @@ const PopupContainer: React.FC<PopupContainerProps> = ({ model, resolve }) => {
const inputRef = useRef<InputRef>(null) const inputRef = useRef<InputRef>(null)
const { providers } = useProviders() const { providers } = useProviders()
const [pinnedModels, setPinnedModels] = useState<string[]>([]) const [pinnedModels, setPinnedModels] = useState<string[]>([])
const scrollContainerRef = useRef<HTMLDivElement>(null)
useEffect(() => { useEffect(() => {
const loadPinnedModels = async () => { const loadPinnedModels = async () => {
@ -162,6 +163,17 @@ const PopupContainer: React.FC<PopupContainerProps> = ({ model, resolve }) => {
open && setTimeout(() => inputRef.current?.focus(), 0) open && setTimeout(() => inputRef.current?.focus(), 0)
}, [open]) }, [open])
useEffect(() => {
if (open && model) {
setTimeout(() => {
const selectedElement = document.querySelector('.ant-menu-item-selected')
if (selectedElement && scrollContainerRef.current) {
selectedElement.scrollIntoView({ block: 'center', behavior: 'auto' })
}
}, 100) // Small delay to ensure menu is rendered
}
}, [open, model])
return ( return (
<Modal <Modal
centered centered
@ -199,7 +211,7 @@ const PopupContainer: React.FC<PopupContainerProps> = ({ model, resolve }) => {
/> />
</HStack> </HStack>
<Divider style={{ margin: 0, borderBlockStartWidth: 0.5 }} /> <Divider style={{ margin: 0, borderBlockStartWidth: 0.5 }} />
<Scrollbar style={{ height: '50vh' }}> <Scrollbar style={{ height: '50vh' }} ref={scrollContainerRef}>
<Container> <Container>
{filteredItems.length > 0 ? ( {filteredItems.length > 0 ? (
<StyledMenu <StyledMenu