2026-11-17 10:32:10 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { type KeyboardEvent, type ReactNode, useEffect, useRef, useState } from "react";
|
|
|
|
|
|
|
|
|
|
export type Tab = { id: string; label: string; content: ReactNode };
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tabs with the chosen one kept in the URL hash (#matrix), so a reload or a
|
|
|
|
|
* shared link opens the same tab. Hidden panels stay mounted: a running
|
|
|
|
|
* analysis keeps polling while another tab is open.
|
|
|
|
|
*/
|
2026-11-17 10:43:03 +00:00
|
|
|
/** Opens a tab from anywhere on the page (also when the hash is already that tab). */
|
|
|
|
|
export function openTab(id: string) {
|
|
|
|
|
history.replaceState(null, "", `#${id}`);
|
|
|
|
|
window.dispatchEvent(new HashChangeEvent("hashchange"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 10:32:10 +00:00
|
|
|
export function Tabs({ tabs, label }: { tabs: Tab[]; label: string }) {
|
|
|
|
|
const [active, setActive] = useState(tabs[0]?.id);
|
|
|
|
|
const buttons = useRef<(HTMLButtonElement | null)[]>([]);
|
|
|
|
|
|
2026-11-17 10:43:03 +00:00
|
|
|
const root = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
// A link to #<tab> (on load, or from elsewhere on the page) opens that tab and scrolls to it.
|
2026-11-17 10:32:10 +00:00
|
|
|
useEffect(() => {
|
2026-11-17 10:43:03 +00:00
|
|
|
const open = () => {
|
|
|
|
|
const id = window.location.hash.slice(1);
|
|
|
|
|
if (!tabs.some((t) => t.id === id)) return;
|
|
|
|
|
setActive(id);
|
|
|
|
|
root.current?.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
|
|
|
};
|
|
|
|
|
open();
|
|
|
|
|
window.addEventListener("hashchange", open);
|
|
|
|
|
return () => window.removeEventListener("hashchange", open);
|
2026-11-17 10:32:10 +00:00
|
|
|
}, [tabs]);
|
|
|
|
|
|
|
|
|
|
function choose(id: string) {
|
|
|
|
|
setActive(id);
|
|
|
|
|
history.replaceState(null, "", `#${id}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onKey(e: KeyboardEvent, i: number) {
|
|
|
|
|
const step = e.key === "ArrowRight" ? 1 : e.key === "ArrowLeft" ? -1 : 0;
|
|
|
|
|
const to = e.key === "Home" ? 0 : e.key === "End" ? tabs.length - 1 : step ? (i + step + tabs.length) % tabs.length : -1;
|
|
|
|
|
if (to < 0) return;
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
choose(tabs[to].id);
|
|
|
|
|
buttons.current[to]?.focus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-11-17 10:43:03 +00:00
|
|
|
<div className="tabs" ref={root}>
|
2026-11-17 10:32:10 +00:00
|
|
|
<div className="tab-list" role="tablist" aria-label={label}>
|
|
|
|
|
{tabs.map((t, i) => (
|
|
|
|
|
<button
|
|
|
|
|
key={t.id}
|
|
|
|
|
ref={(el) => {
|
|
|
|
|
buttons.current[i] = el;
|
|
|
|
|
}}
|
|
|
|
|
type="button"
|
|
|
|
|
role="tab"
|
|
|
|
|
id={`tab-${t.id}`}
|
|
|
|
|
aria-controls={`panel-${t.id}`}
|
|
|
|
|
aria-selected={active === t.id}
|
|
|
|
|
tabIndex={active === t.id ? 0 : -1}
|
|
|
|
|
className="tab"
|
|
|
|
|
onClick={() => choose(t.id)}
|
|
|
|
|
onKeyDown={(e) => onKey(e, i)}
|
|
|
|
|
>
|
|
|
|
|
{t.label}
|
|
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
{tabs.map((t) => (
|
|
|
|
|
<div
|
|
|
|
|
key={t.id}
|
|
|
|
|
role="tabpanel"
|
|
|
|
|
id={`panel-${t.id}`}
|
|
|
|
|
aria-labelledby={`tab-${t.id}`}
|
|
|
|
|
hidden={active !== t.id}
|
|
|
|
|
className="tab-panel"
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
>
|
|
|
|
|
{t.content}
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|