A component for toggling between related panels on the same page. The Tabs component extends Base UI's Tabs. It adds an animated Tabs.Indicator, horizontal and vertical orientations, and a segmented variant on Tabs.List.
Here is the component implementation code that you can copy to your project:
// https://devie-ui.com/components/tabs
// https://base-ui.com/react/components/tabs
import { Tabs as BaseTabs } from "@base-ui/react/tabs";
import clsx from "clsx";
import styles from "./Tabs.module.scss";
const Root = BaseTabs.Root;
function List({ className, variant = "underline", ...props }: Tabs.List.Props) {
return (
<BaseTabs.List
className={clsx(
styles.list,
variant === "segmented" && styles.listSegmented,
className,
)}
{...props}
/>
);
}
function Tab({ className, ...props }: Tabs.Tab.Props) {
return <BaseTabs.Tab className={clsx(styles.tab, className)} {...props} />;
}
function Indicator({ className, ...props }: Tabs.Indicator.Props) {
return (
<BaseTabs.Indicator
className={clsx(styles.indicator, className)}
{...props}
/>
);
}
function Panel({ className, ...props }: Tabs.Panel.Props) {
return (
<BaseTabs.Panel className={clsx(styles.panel, className)} {...props} />
);
}
const Tabs = {
Root,
List,
Tab,
Indicator,
Panel,
};
namespace Tabs {
export type Variant = "underline" | "segmented";
export namespace Root {
export type Props = BaseTabs.Root.Props;
export type ChangeEventDetails = BaseTabs.Root.ChangeEventDetails;
}
export namespace List {
export interface Props extends Omit<BaseTabs.List.Props, "className"> {
className?: string;
variant?: Variant;
}
}
export namespace Tab {
export type Props = Omit<BaseTabs.Tab.Props, "className"> & {
className?: string;
};
export type State = BaseTabs.Tab.State;
}
export namespace Indicator {
export type Props = Omit<BaseTabs.Indicator.Props, "className"> & {
className?: string;
};
}
export namespace Panel {
export type Props = Omit<BaseTabs.Panel.Props, "className"> & {
className?: string;
};
}
}
export default Tabs;Set defaultValue on Tabs.Root to choose the initial tab, or use value and onValueChange to control it. Place Tabs.Indicator inside Tabs.List.
Set orientation on Tabs.Root to vertical.
Set variant on Tabs.List to segmented for a raised pill inside a sunken track. The default is underline, and both work in either orientation.
Pass a className to Tabs.Indicator to restyle it.