Tabs
Tabs whose state is a Datastar signal.
Account
Change your name here, then save.
Password
Change your password here.
Also uses Button, Card, Input and Label.
tabs-demo.tsx
import { Button } from './ui/button.tsx'
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from './ui/card.tsx'
import { Input } from './ui/input.tsx'
import { Label } from './ui/label.tsx'
import { createTabs } from './ui/tabs.tsx'
// The $settings signal holds the open tab, starting with "account".
const { Tabs, TabsList, TabsTrigger, TabsContent } = createTabs('settings', 'account')
export default () => (
<Tabs style="width: 100%; max-width: 24rem">
<TabsList>
<TabsTrigger value="account">Account</TabsTrigger>
<TabsTrigger value="password">Password</TabsTrigger>
</TabsList>
<TabsContent value="account">
<Card>
<CardHeader>
<CardTitle>Account</CardTitle>
<CardDescription>Change your name here, then save.</CardDescription>
</CardHeader>
<CardContent style="display: grid; gap: 0.75rem">
<Label for="tabs-name">Name</Label>
<Input id="tabs-name" value="Pedro Duarte" />
</CardContent>
<CardFooter>
<Button>Save changes</Button>
</CardFooter>
</Card>
</TabsContent>
<TabsContent value="password">
<Card>
<CardHeader>
<CardTitle>Password</CardTitle>
<CardDescription>Change your password here.</CardDescription>
</CardHeader>
<CardContent style="display: grid; gap: 0.75rem">
<Label for="tabs-password">New password</Label>
<Input id="tabs-password" type="password" />
</CardContent>
<CardFooter>
<Button>Save password</Button>
</CardFooter>
</Card>
</TabsContent>
</Tabs>
)
Installation
dui add tabsOr copy ui/tabs.tsx and ui/tabs.css into your project, with lib/jsx/jsx-runtime.ts, lib/jsx/jsx-dev-runtime.ts, lib/css.d.ts, and lib/theme.css.
Examples
Driven by a signal
$12 / month
$120 / year
Also uses Button.
tabs-signal.tsx
import { Button } from './ui/button.tsx'
import { createTabs } from './ui/tabs.tsx'
const { Tabs, TabsList, TabsTrigger, TabsContent } = createTabs('billing', 'monthly')
// The open tab is a signal, so anything on the page can read it or switch tabs.
export default () => (
<Tabs style="align-items: center">
<TabsList>
<TabsTrigger value="monthly">Monthly</TabsTrigger>
<TabsTrigger value="yearly">Yearly</TabsTrigger>
</TabsList>
<TabsContent value="monthly">
<p style="font-size: 1.5rem; font-weight: 600; margin: 0.5rem 0">$12 / month</p>
</TabsContent>
<TabsContent value="yearly">
<p style="font-size: 1.5rem; font-weight: 600; margin: 0.5rem 0">$120 / year</p>
</TabsContent>
<Button
variant="link"
size="sm"
data-on:click="$billing = $billing === 'monthly' ? 'yearly' : 'monthly'"
>
Switch to <span data-text="$billing === 'monthly' ? 'yearly' : 'monthly'">yearly</span>
</Button>
</Tabs>
)
Compared to shadcn/ui
- Make a set with
createTabs(signal, defaultValue), instead of givingTabsadefaultValue: each part needs to know the signal, and the page is rendered showing the tab it starts on. - The open tab is a Datastar signal, so anything on the page can read it or switch tabs, where shadcn has
valueandonValueChange. - Radix moves between tabs with the arrow keys; here each tab is in the Tab order. Closed panels are hidden from everyone.