diff --git a/frontend/packages/swarm-ui/src/agent/terminal/CommandMenu.css b/frontend/packages/swarm-ui/src/agent/terminal/CommandMenu.css index cfffa742f..1f5e42d74 100644 --- a/frontend/packages/swarm-ui/src/agent/terminal/CommandMenu.css +++ b/frontend/packages/swarm-ui/src/agent/terminal/CommandMenu.css @@ -15,6 +15,7 @@ } .ui-command-menu-list { + position: relative; max-height: 16em; margin: 0; padding: 0.25em; diff --git a/frontend/packages/swarm-ui/src/agent/terminal/CommandMenu.tsx b/frontend/packages/swarm-ui/src/agent/terminal/CommandMenu.tsx index 2f1d56103..69923f592 100644 --- a/frontend/packages/swarm-ui/src/agent/terminal/CommandMenu.tsx +++ b/frontend/packages/swarm-ui/src/agent/terminal/CommandMenu.tsx @@ -3,6 +3,7 @@ // marked, the typed part of each name emphasised. A pick comes from the mouse // here; the keys belong to the box (`AgentInputForm`), which owns the focus. // Rows take a mouse press before the box loses focus, so the box never blurs. +import { useEffect, useRef } from "preact/hooks"; import { splitName, type AgentCommand } from "./command-menu.js"; import "./CommandMenu.css"; @@ -22,9 +23,27 @@ export function CommandMenu({ onPick: (command: AgentCommand) => void; onHover: (index: number) => void; }) { + const list = useRef(null); + // Keep the selected row inside the list's own scroll window: the arrow keys + // move the selection, and the list does not follow it by itself. Only the + // list scrolls, never the page around it. + useEffect(() => { + const ul = list.current; + const row = ul?.children[selected] as HTMLElement | undefined; + if (!ul || !row) return; + if (row.offsetTop < ul.scrollTop) { + ul.scrollTop = row.offsetTop; + } else if ( + row.offsetTop + row.offsetHeight > + ul.scrollTop + ul.clientHeight + ) { + ul.scrollTop = row.offsetTop + row.offsetHeight - ul.clientHeight; + } + }, [selected, commands]); return (