/** Peer dep imports */ import React, { useState, useEffect } from "react"; /** Local imports */ import { classNames } from "./utils"; /** Type imports */ import type { Dispatch, SetStateAction } from "react"; export type TabProps = { /** Unique ID for each tab */ id: string; /** Text label for each tab */ label: string; /** The JSX content for each tab */ children: React.ReactNode; }; /** * Wrapper component for tab content. */ export function Tab({ children }: TabProps) { return children as React.ReactElement; } export type TabsProps = { /** The React children to map and render from */ children: React.ReactElement[]; /** State tuple to pass that receives the active tab */ active: [string, Dispatch>]; }; /** * Global UI for tabbed content. */ export function Tabs({ children, active: [activeTab, setActiveTab], }: TabsProps) { const [button, setButton] = useState(null); useEffect(() => { if (setActiveTab) { setActiveTab(children[0].props.id); } }, []); return (
{React.Children.map(children, (child: React.ReactElement) => { const classes = { tabs__tabs__item: true, active: activeTab === child.props.id, }; return (
<>{child.props.children}
); })}
); }