Prioritize match outcomes, distinguish adjudicated results, and preserve detail context when navigating back.
105 lines
3.1 KiB
TypeScript
105 lines
3.1 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
import {
|
|
moveMatchDetailTabFocus,
|
|
type MatchDetailTab,
|
|
type MatchDetailTabId,
|
|
type MatchDetailTabNavigationKey,
|
|
} from "./match-detail-tabs";
|
|
|
|
export function MatchDetailTabBar({
|
|
tabs,
|
|
activeTab,
|
|
onChange,
|
|
}: {
|
|
tabs: readonly MatchDetailTab[];
|
|
activeTab: MatchDetailTabId;
|
|
onChange: (tab: MatchDetailTabId) => void;
|
|
}) {
|
|
const tabRefs = useRef<
|
|
Partial<Record<MatchDetailTabId, HTMLButtonElement | null>>
|
|
>({});
|
|
const previousActiveTabRef = useRef(activeTab);
|
|
|
|
useEffect(() => {
|
|
tabRefs.current[activeTab]?.scrollIntoView({
|
|
block: "nearest",
|
|
inline: "nearest",
|
|
});
|
|
if (previousActiveTabRef.current === activeTab) return;
|
|
|
|
const activePanel = document.getElementById(
|
|
`match-detail-panel-${activeTab}`,
|
|
);
|
|
const panelTop = activePanel?.getBoundingClientRect().top;
|
|
if (
|
|
activePanel &&
|
|
panelTop !== undefined &&
|
|
(panelTop < 96 || panelTop >= window.innerHeight)
|
|
) {
|
|
activePanel.scrollIntoView({ block: "start" });
|
|
}
|
|
previousActiveTabRef.current = activeTab;
|
|
}, [activeTab]);
|
|
|
|
function handleKeyDown(
|
|
event: React.KeyboardEvent<HTMLButtonElement>,
|
|
currentTab: MatchDetailTabId,
|
|
) {
|
|
if (!isNavigationKey(event.key)) return;
|
|
const nextTab = moveMatchDetailTabFocus(tabs, currentTab, event.key);
|
|
if (!nextTab) return;
|
|
|
|
event.preventDefault();
|
|
onChange(nextTab);
|
|
window.requestAnimationFrame(() => tabRefs.current[nextTab]?.focus());
|
|
}
|
|
|
|
return (
|
|
<div className="sticky top-12 z-40 -mx-4 border-y border-base-content/10 bg-base-100/95 px-4 backdrop-blur sm:-mx-6 sm:px-6">
|
|
<div
|
|
className="flex overflow-x-auto"
|
|
role="tablist"
|
|
aria-label="내전 상세"
|
|
>
|
|
{tabs.map((tab) => {
|
|
const selected = activeTab === tab.id;
|
|
return (
|
|
<button
|
|
key={tab.id}
|
|
ref={(element) => {
|
|
tabRefs.current[tab.id] = element;
|
|
}}
|
|
id={`match-detail-tab-${tab.id}`}
|
|
type="button"
|
|
role="tab"
|
|
aria-selected={selected}
|
|
aria-controls={`match-detail-panel-${tab.id}`}
|
|
tabIndex={selected ? 0 : -1}
|
|
onClick={() => onChange(tab.id)}
|
|
onKeyDown={(event) => handleKeyDown(event, tab.id)}
|
|
className={`min-h-11 flex-none whitespace-nowrap border-b-2 px-4 text-xs font-bold transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-primary ${
|
|
selected
|
|
? "border-primary bg-primary/5 text-base-content"
|
|
: "border-transparent text-base-content/45 hover:bg-base-content/[0.03] hover:text-base-content"
|
|
}`}
|
|
>
|
|
{tab.label}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function isNavigationKey(
|
|
key: string,
|
|
): key is MatchDetailTabNavigationKey {
|
|
return (
|
|
key === "ArrowLeft" ||
|
|
key === "ArrowRight" ||
|
|
key === "Home" ||
|
|
key === "End"
|
|
);
|
|
}
|