From 5a64acd807a8bb315b784efac9bb827e6461ad45 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 16 Jul 2026 15:35:59 +0800 Subject: [PATCH] feat(webui): improve overflowing tab navigation Add edge fades and automatic active-tab reveal to shared horizontal tab lists, including revalidation when asynchronous labels or badges change width. Give configuration tabs complete tab and panel semantics, keyboard navigation, focus treatment, and 40px desktop or 44px mobile targets without changing their visual hierarchy. --- .../src/components/pages/config-page.tsx | 75 +++++++--- packages/webui/src/components/ui/tabs.tsx | 140 +++++++++++++++++- 2 files changed, 194 insertions(+), 21 deletions(-) diff --git a/packages/webui/src/components/pages/config-page.tsx b/packages/webui/src/components/pages/config-page.tsx index e24bf1e1..b62a2d8c 100644 --- a/packages/webui/src/components/pages/config-page.tsx +++ b/packages/webui/src/components/pages/config-page.tsx @@ -12,6 +12,7 @@ import { Check, Loader2, MousePointerClick, Plus, Save } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Skeleton } from '@/components/ui/skeleton'; import { Badge } from '@/components/ui/badge'; +import { ScrollableTabList } from '@/components/ui/tabs'; import { ConfirmDialog } from '@/components/confirm-dialog'; import { cn } from '@/lib/utils'; import type { @@ -202,19 +203,25 @@ export function ConfigPage() { counts={countMap(config.networks)} /> - {activeTab === 'general' ? ( - - ) : ( - openCreate(activeTab)} - onEdit={(idx) => openEdit(activeTab, idx)} - onDelete={(idx) => handleDelete(activeTab, idx)} - onToggleEnabled={(idx, v) => handleToggleEnabled(activeTab, idx, v)} - /> - )} +
+ {activeTab === 'general' ? ( + + ) : ( + openCreate(activeTab)} + onEdit={(idx) => openEdit(activeTab, idx)} + onDelete={(idx) => handleDelete(activeTab, idx)} + onToggleEnabled={(idx, v) => handleToggleEnabled(activeTab, idx, v)} + /> + )} +
)} @@ -318,19 +325,53 @@ interface TabStripProps { } function TabStrip({ activeTab, onChange, counts }: TabStripProps) { + const tabRefs = useRef<(HTMLButtonElement | null)[]>([]); + + const moveTab = (from: number, direction: 1 | -1 | 'home' | 'end') => { + const length = ALL_TABS.length; + const nextIndex = direction === 'home' + ? 0 + : direction === 'end' + ? length - 1 + : (from + direction + length) % length; + const next = ALL_TABS[nextIndex]; + if (!next) return; + onChange(next); + tabRefs.current[nextIndex]?.focus(); + }; + return ( -
- {ALL_TABS.map((key) => { + + {ALL_TABS.map((key, index) => { const label = key === 'general' ? '通用设置' : NETWORK_TABS[key].title; const count = key === 'general' ? null : counts[key]; const active = activeTab === key; return ( ); })} -
+ ); } diff --git a/packages/webui/src/components/ui/tabs.tsx b/packages/webui/src/components/ui/tabs.tsx index 3ffba350..14eb7f01 100644 --- a/packages/webui/src/components/ui/tabs.tsx +++ b/packages/webui/src/components/ui/tabs.tsx @@ -2,10 +2,140 @@ // spring-pill idiom of the debug page's segmented control but scaled up for // top-level section switching, with full roving-tabindex keyboard support // (←/→/Home/End). Generic over a string union of tab ids. -import { useId, useRef, type ReactNode } from 'react'; +import { + useCallback, + useEffect, + useId, + useRef, + useState, + type HTMLAttributes, + type ReactNode, +} from 'react'; import { motion } from 'motion/react'; import { cn } from '@/lib/utils'; +interface ScrollableTabListProps extends Omit, 'onScroll'> { + activeValue: string; +} + +/** + * Horizontal tab viewport with discoverable overflow and active-item tracking. + * + * Callers keep ownership of the tab visuals; active tabs only need the + * `data-tab-active` marker so the viewport can reveal them after a change. + */ +export function ScrollableTabList({ + activeValue, + className, + children, + ...props +}: ScrollableTabListProps) { + const viewportRef = useRef(null); + const didMount = useRef(false); + const [edges, setEdges] = useState({ start: false, end: false }); + + const updateEdges = useCallback(() => { + const viewport = viewportRef.current; + if (!viewport) return; + + const maxScroll = Math.max(0, viewport.scrollWidth - viewport.clientWidth); + const next = { + start: viewport.scrollLeft > 1, + end: viewport.scrollLeft < maxScroll - 1, + }; + setEdges((current) => ( + current.start === next.start && current.end === next.end ? current : next + )); + }, []); + + const revealActive = useCallback((behavior: ScrollBehavior) => { + const viewport = viewportRef.current; + const active = viewport?.querySelector('[data-tab-active="true"]'); + if (!viewport || !active) return; + + const edgePadding = 24; + const visibleStart = viewport.scrollLeft + edgePadding; + const visibleEnd = viewport.scrollLeft + viewport.clientWidth - edgePadding; + const itemStart = active.offsetLeft; + const itemEnd = itemStart + active.offsetWidth; + let nextLeft = viewport.scrollLeft; + + if (itemStart < visibleStart) nextLeft = itemStart - edgePadding; + else if (itemEnd > visibleEnd) nextLeft = itemEnd - viewport.clientWidth + edgePadding; + + viewport.scrollTo({ left: Math.max(0, nextLeft), behavior }); + }, []); + + useEffect(() => { + const viewport = viewportRef.current; + if (!viewport) return; + + const syncLayout = () => { + revealActive('auto'); + updateEdges(); + }; + const resizeObserver = new ResizeObserver(syncLayout); + resizeObserver.observe(viewport); + Array.from(viewport.children).forEach((child) => resizeObserver.observe(child)); + + const mutationObserver = new MutationObserver(() => { + resizeObserver.disconnect(); + resizeObserver.observe(viewport); + Array.from(viewport.children).forEach((child) => resizeObserver.observe(child)); + syncLayout(); + }); + mutationObserver.observe(viewport, { childList: true }); + syncLayout(); + + return () => { + mutationObserver.disconnect(); + resizeObserver.disconnect(); + }; + }, [revealActive, updateEdges]); + + useEffect(() => { + const reduceMotion = document.documentElement.dataset.reduceMotion === '1' + || window.matchMedia('(prefers-reduced-motion: reduce)').matches; + revealActive(didMount.current && !reduceMotion ? 'smooth' : 'auto'); + didMount.current = true; + + const frame = requestAnimationFrame(updateEdges); + return () => cancelAnimationFrame(frame); + }, [activeValue, revealActive, updateEdges]); + + return ( +
+
+ {children} +
+
+ ); +} + export interface TabItem { value: T; label: ReactNode; @@ -38,10 +168,11 @@ export function Tabs({ value, onChange, items, className, ...r }; return ( -
{items.map((it, i) => { const active = it.value === value; @@ -52,6 +183,7 @@ export function Tabs({ value, onChange, items, className, ...r role="tab" type="button" aria-selected={active} + data-tab-active={active ? 'true' : undefined} tabIndex={active ? 0 : -1} onClick={() => onChange(it.value)} onKeyDown={(e) => { @@ -84,6 +216,6 @@ export function Tabs({ value, onChange, items, className, ...r ); })} -
+ ); }