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.
This commit is contained in:
2 files changed
+194
-21
No files matched your search
@@ -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' ? (
|
||||
<GeneralSettingsTab config={config} onChange={setConfig} />
|
||||
) : (
|
||||
<NetworkTabView
|
||||
kind={activeTab}
|
||||
config={config}
|
||||
statusByName={liveStatusByName}
|
||||
onCreateClick={() => openCreate(activeTab)}
|
||||
onEdit={(idx) => openEdit(activeTab, idx)}
|
||||
onDelete={(idx) => handleDelete(activeTab, idx)}
|
||||
onToggleEnabled={(idx, v) => handleToggleEnabled(activeTab, idx, v)}
|
||||
/>
|
||||
)}
|
||||
<div
|
||||
id={`config-panel-${activeTab}`}
|
||||
role="tabpanel"
|
||||
aria-labelledby={`config-tab-${activeTab}`}
|
||||
>
|
||||
{activeTab === 'general' ? (
|
||||
<GeneralSettingsTab config={config} onChange={setConfig} />
|
||||
) : (
|
||||
<NetworkTabView
|
||||
kind={activeTab}
|
||||
config={config}
|
||||
statusByName={liveStatusByName}
|
||||
onCreateClick={() => openCreate(activeTab)}
|
||||
onEdit={(idx) => openEdit(activeTab, idx)}
|
||||
onDelete={(idx) => handleDelete(activeTab, idx)}
|
||||
onToggleEnabled={(idx, v) => handleToggleEnabled(activeTab, idx, v)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -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 (
|
||||
<div className="flex gap-1 overflow-x-auto border-b [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
|
||||
{ALL_TABS.map((key) => {
|
||||
<ScrollableTabList
|
||||
activeValue={activeTab}
|
||||
role="tablist"
|
||||
aria-label="节点配置分类"
|
||||
className="gap-1 border-b"
|
||||
>
|
||||
{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 (
|
||||
<button
|
||||
key={key}
|
||||
ref={(element) => { tabRefs.current[index] = element; }}
|
||||
id={`config-tab-${key}`}
|
||||
role="tab"
|
||||
type="button"
|
||||
aria-selected={active}
|
||||
aria-controls={`config-panel-${key}`}
|
||||
data-tab-active={active ? 'true' : undefined}
|
||||
tabIndex={active ? 0 : -1}
|
||||
onClick={() => onChange(key)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'ArrowRight') { event.preventDefault(); moveTab(index, 1); }
|
||||
else if (event.key === 'ArrowLeft') { event.preventDefault(); moveTab(index, -1); }
|
||||
else if (event.key === 'Home') { event.preventDefault(); moveTab(index, 'home'); }
|
||||
else if (event.key === 'End') { event.preventDefault(); moveTab(index, 'end'); }
|
||||
}}
|
||||
className={cn(
|
||||
'group relative inline-flex shrink-0 items-center gap-1.5 whitespace-nowrap px-3 py-2 text-sm transition-colors cursor-pointer',
|
||||
'group relative inline-flex min-h-11 shrink-0 items-center gap-1.5 whitespace-nowrap px-3 py-2 text-sm outline-none transition-colors cursor-pointer md:min-h-10',
|
||||
'focus-visible:ring-[3px] focus-visible:ring-ring/40',
|
||||
active ? 'text-foreground' : 'text-muted-foreground hover:text-foreground',
|
||||
)}
|
||||
>
|
||||
@@ -353,7 +394,7 @@ function TabStrip({ activeTab, onChange, counts }: TabStripProps) {
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</ScrollableTabList>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<HTMLAttributes<HTMLDivElement>, '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<HTMLDivElement>(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<HTMLElement>('[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 (
|
||||
<div className="relative min-w-0">
|
||||
<div
|
||||
ref={viewportRef}
|
||||
onScroll={updateEdges}
|
||||
data-scroll-start={edges.start ? '' : undefined}
|
||||
data-scroll-end={edges.end ? '' : undefined}
|
||||
className={cn(
|
||||
'flex min-w-0 overflow-x-auto scroll-px-6 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden',
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className={cn(
|
||||
'pointer-events-none absolute inset-y-0 left-0 z-20 w-6 bg-gradient-to-r from-background via-background/80 to-transparent transition-opacity duration-150',
|
||||
edges.start ? 'opacity-100' : 'opacity-0',
|
||||
)}
|
||||
/>
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className={cn(
|
||||
'pointer-events-none absolute inset-y-0 right-0 z-20 w-6 bg-gradient-to-l from-background via-background/80 to-transparent transition-opacity duration-150',
|
||||
edges.end ? 'opacity-100' : 'opacity-0',
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export interface TabItem<T extends string> {
|
||||
value: T;
|
||||
label: ReactNode;
|
||||
@@ -38,10 +168,11 @@ export function Tabs<T extends string>({ value, onChange, items, className, ...r
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
<ScrollableTabList
|
||||
activeValue={value}
|
||||
role="tablist"
|
||||
aria-label={rest['aria-label']}
|
||||
className={cn('relative flex items-center gap-1 overflow-x-auto', className)}
|
||||
className={cn('items-center gap-1', className)}
|
||||
>
|
||||
{items.map((it, i) => {
|
||||
const active = it.value === value;
|
||||
@@ -52,6 +183,7 @@ export function Tabs<T extends string>({ 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<T extends string>({ value, onChange, items, className, ...r
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</ScrollableTabList>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user