swarm ui: the command menu follows the keyboard selection
This commit is contained in:
parent
6d968c7fe6
commit
76882ec3b8
2 changed files with 20 additions and 0 deletions
|
|
@ -15,6 +15,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.ui-command-menu-list {
|
.ui-command-menu-list {
|
||||||
|
position: relative;
|
||||||
max-height: 16em;
|
max-height: 16em;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0.25em;
|
padding: 0.25em;
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
// marked, the typed part of each name emphasised. A pick comes from the mouse
|
// 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.
|
// 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.
|
// 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 { splitName, type AgentCommand } from "./command-menu.js";
|
||||||
import "./CommandMenu.css";
|
import "./CommandMenu.css";
|
||||||
|
|
||||||
|
|
@ -22,9 +23,27 @@ export function CommandMenu({
|
||||||
onPick: (command: AgentCommand) => void;
|
onPick: (command: AgentCommand) => void;
|
||||||
onHover: (index: number) => void;
|
onHover: (index: number) => void;
|
||||||
}) {
|
}) {
|
||||||
|
const list = useRef<HTMLUListElement>(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 (
|
return (
|
||||||
<div class="ui-command-menu">
|
<div class="ui-command-menu">
|
||||||
<ul
|
<ul
|
||||||
|
ref={list}
|
||||||
class="ui-command-menu-list"
|
class="ui-command-menu-list"
|
||||||
role="listbox"
|
role="listbox"
|
||||||
id={id}
|
id={id}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue