Tabs
A component for organizing content into tabbed sections.
Follow the installation and quick start below to use Tabs in your project, or read the complete setup guide.
On this page
Installation
1. Initialize once
Run from an existing React project with Node.js 18 or later. Skip this step if the project already has u-lib.json. Initialization copies the shared stylesheet and adds its import to your application entry.
npx @scriptwithahmad/u-lib@0.2.2 init2. Add Tabs and example components
This command includes the UI components used by this page's usage examples. The CLI copies editable source into your components directory and installs each component's dependencies. Existing source files are preserved.
npx @scriptwithahmad/u-lib@0.2.2 add tabs button card input labelChoose the tab for your package manager. Yarn commands require modern Yarn; Yarn Classic users can use npx. Use --dry-run to preview changes. See the installation guide for custom paths, JavaScript output and package-import alternatives.
Quick start
Copy this example into src/Example.tsx (or Example.tsx at the project root when you do not use src). Imports assume the default CLI paths. From a nested file such as src/app/page.tsx, adjust ./components to ../components. Keep the client directive in Next.js when using state or event handlers.
"use client";
import { useId, useState } from "react";
import { Button } from "./components/ui/button";
import { Card, CardHeader, CardDescription, CardContent, CardFooter, CardTitle } from "./components/ui/card";
import { Input } from "./components/ui/input";
import { Label } from "./components/ui/label";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs";
export default function TabUi() {
const id = useId();
const [saved, setSaved] = useState("");
return <Tabs defaultValue="account" className="w-full max-w-md" onValueChange={() => setSaved("")}>
<TabsList className="h-11 w-full rounded-2xl p-1 [&>.ui-tab-indicator]:rounded-xl"><TabsTrigger value="account" className="h-full flex-1 rounded-xl">Account</TabsTrigger><TabsTrigger value="password" className="h-full flex-1 rounded-xl">Password</TabsTrigger></TabsList>
<TabsContent value="account"><form onSubmit={(event) => { event.preventDefault(); setSaved("Account changes saved in this demo."); }} onChange={() => setSaved("")}><Card>
<CardHeader><CardTitle>Account</CardTitle><CardDescription>Update your demo profile.</CardDescription></CardHeader>
<CardContent className="space-y-3"><div className="space-y-1"><Label htmlFor={`${id}-name`}>Name</Label><Input id={`${id}-name`} defaultValue="Muhammad Ahmad" required /></div><div className="space-y-1"><Label htmlFor={`${id}-username`}>Username</Label><Input id={`${id}-username`} defaultValue="@ahmad" required /></div></CardContent>
<CardFooter><Button type="submit" className="h-10 w-full rounded-2xl">Save changes</Button></CardFooter>
</Card></form></TabsContent>
<TabsContent value="password"><form onSubmit={(event) => { event.preventDefault(); setSaved("Password updated in this demo."); }} onChange={() => setSaved("")}><Card>
<CardHeader><CardTitle>Password</CardTitle><CardDescription>Try the password form. Use at least 8 characters.</CardDescription></CardHeader>
<CardContent className="space-y-3"><div className="space-y-1"><Label htmlFor={`${id}-current`}>Current password</Label><Input id={`${id}-current`} type="password" autoComplete="current-password" required /></div><div className="space-y-1"><Label htmlFor={`${id}-new`}>New password</Label><Input id={`${id}-new`} type="password" autoComplete="new-password" minLength={8} required /></div></CardContent>
<CardFooter><Button type="submit" className="h-10 w-full rounded-2xl">Save password</Button></CardFooter>
</Card></form></TabsContent>
{saved && <p role="status" className="mt-3 text-sm">{saved}</p>}
</Tabs>;
}Keep the shared stylesheet imported once. Replace sample image URLs and data with your own. Examples may use layout utilities beyond the precompiled component styles; compile added Tailwind classes in your app or replace them with your own CSS. Customize local source or className, and preserve accessible names and keyboard behavior.
Usage examples
Try each example, then switch to Code to copy its complete implementation. These catalog snippets use the website's @/components/ui alias; replace it with your local generated path as shown in Quick start.
Basic Usage
A complete tabs example. Copy this into a client component.
Loading example…
Props & configuration
Everything you need to fine-tune this component.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | The controlled active tab. |
defaultValue | string | — | Initially selected tab. |
onValueChange | (value: string) => void | — | Called when the selected tab changes. |
orientation | "horizontal" | "vertical" | horizontal | Tab list orientation. |
