From 1fc1ae38414cdfeb5b6d5e49f7f37c15e17ba273 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Tue, 22 Sep 2026 17:35:32 +0800 Subject: [PATCH] fix(traffic): keep the keyboard-selected row in view Arrow keys move the highlight in the traffic table, but the handler calls preventDefault() to stop the page from scrolling and nothing scrolls instead, so pressing Down past the bottom of the visible area took the highlight off-screen, and Up past the top did the same. - After each Up/Down the selected row is scrolled into view with block: "nearest": only as far as needed. It also runs when the cursor is already at the first or last row, so a key press brings back a row that was scrolled away with the wheel. - The row is found by request id (data-row), not by index, so the grouped view works too. A row inside a collapsed session is not in the table; then nothing scrolls. - Request rows get scroll-margin-top equal to the pinned header (36px), so a row moving up stops below the header, not under it. - Request rows get a 100vw inline scroll margin, so "nearest" never scrolls sideways when the table is wider than the window. Without it Chromium shifts the table by its 20px padding at either end and WebKit jumps from the far right back to the far left; a margin of exactly 20px is not enough in WebKit, which rounds the scroll width up and leaves the row a fraction of a pixel short at the far right. - Up/Down are left alone while the session drawer is open, as they already were for the request drawer. Otherwise the list behind the drawer would now scroll with the cursor. Co-Authored-By: Claude Opus 5 --- src/App.tsx | 37 ++++++++++++++++++++++++++---------- src/traffic/RequestTable.tsx | 16 ++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 18dd7141..9c8c0fea 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -412,6 +412,8 @@ export default function App() { * 那一行有什么特别。 */ const [cursor, setCursor] = useState(-1); + /** 流量页的滚动层。键盘选中一行之后,在它里面找到那一行滚进视野 */ + const listRef = useRef(null); const [tab, setTab] = useState("dashboard"); /** 从客户端页点了某把密钥:密钥页打开时定位到那一行 */ const [focusKey, setFocusKey] = useState(null); @@ -559,7 +561,8 @@ export default function App() { * **「用鼠标一行行点太慢」**,而 Requests 是主战场。 * * 两个边界:在输入框里打字时不接管方向键(否则光标动不了); - * 抽屉开着时 `↑↓` 也不动,那时用户在看详情而不是挑行。 + * 抽屉开着时 `↑↓` 也不动,那时用户在看详情而不是挑行。请求和会话的 + * 抽屉都算:选中的行会滚进视野,漏掉一个,背后的列表就跟着光标滚走了。 */ useEffect(() => { const onKey = (e: KeyboardEvent) => { @@ -611,15 +614,29 @@ export default function App() { setOpen(null); return; } - if (open !== null) return; + if (open !== null || openSession !== null) return; if (e.key === "ArrowDown" || e.key === "ArrowUp") { e.preventDefault(); - setCursor((c) => { - const next = e.key === "ArrowDown" ? c + 1 : c - 1; - // 第一次按方向键从第一行开始,而不是从「上一行」跳到末尾 - if (c < 0) return e.key === "ArrowDown" ? 0 : 0; - return Math.max(0, Math.min(rows.length - 1, next)); - }); + const step = e.key === "ArrowDown" ? 1 : -1; + // 第一次按方向键从第一行开始,而不是从「上一行」跳到末尾 + const next = + cursor < 0 ? 0 : Math.max(0, Math.min(rows.length - 1, cursor + step)); + setCursor(next); + /* + **选中的那一行要一直看得见。**方向键自带的滚动上面拦掉了,不补的话 + 按过可视区的下沿,高亮就跑到屏幕外面去了。只滚刚好够的距离 + (`nearest`);让开吸顶的表头、不横着滚,靠的是行上的 scroll-margin, + 见 `Row`。光标到了头、没动,也照样滚:用滚轮翻走之后,按一下方向键 + 就回到选中的那一行。 + + **按 id 找行,不按下标。**归组时表里的顺序不是 `rows` 的顺序,折起来 + 的组里的行也不在表里 —— 那时找不到,就不滚。 + */ + const id = rows[next]?.id; + if (id !== undefined) + listRef.current + ?.querySelector(`[data-row="${id}"]`) + ?.scrollIntoView({ block: "nearest" }); } else if (e.key === "Enter" && cursor >= 0 && rows[cursor]) { e.preventDefault(); setOpen(rows[cursor].id); @@ -627,7 +644,7 @@ export default function App() { }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); - }, [tab, rows, cursor, open]); + }, [tab, rows, cursor, open, openSession]); // 换页时把抽屉关掉 —— 它是请求页的东西,留在别的页上没有意义, // 而那时 Esc 也不接管了(键盘那个 effect 只在请求页挂) @@ -1261,7 +1278,7 @@ export default function App() { onChanged={() => setNudge((n) => n + 1)} /> ) : ( -
+
{/* 详情走**浮层**,不拆栏。 diff --git a/src/traffic/RequestTable.tsx b/src/traffic/RequestTable.tsx index 59557dc1..507d6f2a 100644 --- a/src/traffic/RequestTable.tsx +++ b/src/traffic/RequestTable.tsx @@ -438,12 +438,28 @@ function Row({ ]} > { onSelect(); onOpen(r.id); }} className={ "cursor-pointer border-b border-neutral-100 hover:bg-neutral-50 dark:border-neutral-900 dark:hover:bg-neutral-900 " + + /* + 这两样是给键盘导航的 `scrollIntoView` 用的(见 App)。 + + **上边让出吸顶的表头**,不然往上翻时这一行停在表头 + 底下。36px 是表头的高:排序钮 24px,加「状态」那一格 + 的 `py-1.5`。 + + **左右各放出一整屏宽,横向就不会滚。**窗口窄、表横着 + 滚的时候,一行横跨整张表,总有一截在视野外,`nearest` + 会为它横着滚:Chromium 把表推开 20px,左右边距没了; + WebKit 从最右一下跳回最左。只放出外面那块的 `px-5` + 在 WebKit 里不够 —— 它把滚动宽度向上取整,滚到最右时 + 行边差零点几像素够不着可视区的右沿。 + */ + "scroll-mt-9 scroll-mx-[100vw] " + (selected ? "bg-neutral-100 dark:bg-neutral-800" : fresh