'use client'; import { useRef, useState, type KeyboardEvent, type ReactNode } from 'react'; export interface RecipeTab { id: string; title: string; content: ReactNode; } export default function RecipeTabs({ tabs }: { tabs: RecipeTab[] }) { const [activeId, setActiveId] = useState(tabs[0]?.id); const tabRefs = useRef>({}); // Arrow keys move between tabs (WAI-ARIA tabs pattern) const handleKeyDown = (e: KeyboardEvent, index: number) => { const offsets: Record = { ArrowRight: 1, ArrowLeft: -1 }; let next: number | undefined; if (e.key in offsets) next = (index + offsets[e.key] + tabs.length) % tabs.length; if (e.key === 'Home') next = 0; if (e.key === 'End') next = tabs.length - 1; if (next === undefined) return; e.preventDefault(); setActiveId(tabs[next].id); tabRefs.current[tabs[next].id]?.focus(); }; if (tabs.length === 0) return null; return (
{tabs.map((tab, index) => { const selected = tab.id === activeId; return ( ); })}
{/* All panels stay in the HTML so content is indexable; only the active one is shown */} {tabs.map((tab) => ( ))}
); }