Tabs
A list of tabs, each showing one panel while it is active.
Overview
Requests are up 12% this week, with a p95 latency of 182 ms.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { TabsIndicator, TabsList, TabsPanel, TabsRoot, TabsTab } from "#ui/tabs"
const sections = [
{ value: "overview", label: "Overview", text: "Requests are up 12% this week, with a p95 latency of 182 ms." },
{ value: "analytics", label: "Analytics", text: "4,812 visitors from 63 countries. Most came from search." },
{ value: "reports", label: "Reports", text: "Three reports are scheduled. The next one goes out on Monday." },
{ value: "notifications", label: "Notifications", text: "You have no unread notifications." }
]
const TabsUnderlineMessage = UiSchema.TaggedEnum({ SelectedTab: { value: Schema.String } })
export const TabsUnderline = Html.component({
name: "TabsUnderline",
model: Schema.Struct({ tab: Schema.String }),
message: TabsUnderlineMessage
}).make({
init: () => ({ tab: "overview" }),
update: {
SelectedTab: ($, { value }) => $.evolve({ tab: value })
},
view: (h, model) =>
Tabs(h, {
props: { value: model.tab, tabs: sections.map(({ value }) => ({ value })) },
onEmit: { Selected: ({ value }) => TabsUnderlineMessage.SelectedTab({ value }) },
view: (tabs) =>
TabsRoot(h, tabs, { class: "max-w-lg" }).children(
TabsList(h, tabs).children(
sections.map(({ label, value }) =>
TabsTab(h, tabs, { value }).children(label)
),
// Placed by the --active-tab-left and --active-tab-width variables the kit measures.
TabsIndicator(h, tabs)
),
sections.map(({ label, text, value }) =>
TabsPanel(h, tabs, { value })?.children(
h.h4.class("m-0 text-sm font-semibold").children(label),
h.p.class("m-0 text-[13px] text-muted-foreground").children(text)
)
)
)
})
})
Installation
Set up the project
Once per project: the packages, Tailwind CSS, the theme tokens and the
cnhelper. Follow Installation.Copy the component into your project
The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.
src/ui/tabs.ts import type { Component } from "@effect/ui" import type { Html } from "@effect/ui/html" import type { Tabs } from "@effect/ui-kit" import { cn } from "#lib/cn" /** What the `view` closure of the kit's `Tabs` receives. */ export type TabsParts = Component.Headless.Parts<typeof Tabs> export type TabsVariant = keyof typeof lists interface ClassOption { readonly class?: string | undefined } const lists = { underline: "gap-1 data-[orientation=horizontal]:border-b data-[orientation=vertical]:w-44", segmented: "w-fit gap-0.5 rounded-md bg-muted p-[3px] data-[orientation=vertical]:w-44" } as const const tab = "relative z-10 inline-flex shrink-0 cursor-pointer items-center justify-center gap-1.5 rounded-sm px-3 " + "text-[13px] font-medium whitespace-nowrap text-muted-foreground no-underline outline-offset-0 select-none " + "transition-colors duration-150 hover:text-foreground data-[active]:text-foreground " + "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 data-[disabled]:hover:text-muted-foreground " + "data-[orientation=vertical]:justify-start [&_svg]:size-3.5 [&_svg]:shrink-0 " + "group-data-[variant=underline]/tabs-list:h-9 group-data-[variant=segmented]/tabs-list:h-7 " + "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:h-8 " + "group-data-[fill]/tabs-list:flex-1" /** Around the list and the panels. A vertical root puts the list beside the panels. */ export const TabsRoot = <Msg>(h: Html.H<Msg>, tabs: TabsParts, options: ClassOption = {}) => tabs.root( h.div.class(cn( "flex w-full gap-4 data-[orientation=horizontal]:flex-col data-[orientation=vertical]:gap-6", options.class )) ) /** * The row or column of tabs. `underline` draws a line under the active tab, * `segmented` a raised pill behind it; a vertical list always fills the * active tab's background. `fill` stretches the tabs over the list's width. */ export const TabsList = <Msg>( h: Html.H<Msg>, tabs: TabsParts, options: ClassOption & { readonly variant?: TabsVariant | undefined readonly fill?: boolean | undefined } = {} ) => tabs.list( h.div .attrs({ "data-variant": options.variant ?? "underline", "data-fill": options.fill === true ? "" : undefined }) .class(cn( "group/tabs-list relative isolate flex shrink-0 data-[orientation=vertical]:flex-col", "data-[fill]:w-full", lists[options.variant ?? "underline"], options.class )) ) /** One tab, a button. */ export const TabsTab = <Msg>( h: Html.H<Msg>, tabs: TabsParts, options: ClassOption & { readonly value: string } ) => tabs.tab({ element: h.button.class(cn(tab, options.class)), value: options.value }) /** * One tab that is a link: a click and Enter also follow its `href`. With * `follow: false` the tab only activates and the address stays, for a list * whose panels are all on this page. */ export const TabsLink = <Msg>( h: Html.H<Msg>, tabs: TabsParts, options: ClassOption & { readonly value: string; readonly href: string; readonly follow?: boolean | undefined } ) => { const link = h.a.attr("href", options.href).class(cn(tab, options.class)) return tabs.tab({ element: options.follow === false ? link.on("click", () => undefined, { preventDefault: "always" }) : link, value: options.value }) } /** * The mark that slides to the active tab, placed with the `--active-tab-*` * variables the kit measures. Put it inside the list. `undefined` while no tab * is active. */ export const TabsIndicator = <Msg>(h: Html.H<Msg>, tabs: TabsParts, options: ClassOption = {}) => tabs.indicator( h.span.class(cn( "pointer-events-none absolute top-0 left-0 transition-[translate,width,height] duration-200 ease-out", "w-[var(--active-tab-width)] translate-x-[var(--active-tab-left)]", "group-data-[variant=underline]/tabs-list:data-[orientation=horizontal]:top-auto", "group-data-[variant=underline]/tabs-list:data-[orientation=horizontal]:-bottom-px", "group-data-[variant=underline]/tabs-list:data-[orientation=horizontal]:h-0.5", "group-data-[variant=underline]/tabs-list:data-[orientation=horizontal]:bg-brand", "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:h-[var(--active-tab-height)]", "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:translate-y-[var(--active-tab-top)]", "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:rounded-sm", "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:bg-accent", "group-data-[variant=segmented]/tabs-list:h-[var(--active-tab-height)]", "group-data-[variant=segmented]/tabs-list:translate-y-[var(--active-tab-top)]", "group-data-[variant=segmented]/tabs-list:rounded-sm", "group-data-[variant=segmented]/tabs-list:bg-background", "group-data-[variant=segmented]/tabs-list:shadow-sm", options.class )) ) /** The content of one tab: `undefined` while hidden, unless it is kept mounted. Fades in. */ export const TabsPanel = <Msg>( h: Html.H<Msg>, tabs: TabsParts, options: ClassOption & { readonly value: string } ) => tabs.panel({ element: h.div.class(cn( "min-w-0 flex-1 rounded-sm text-[13px] transition-opacity duration-200", "data-[starting-style]:opacity-0 data-[ending-style]:hidden", options.class )), value: options.value })Import it
import { TabsRoot, TabsList, TabsTab, TabsLink, TabsIndicator, TabsPanel } from "#ui/tabs"
Usage
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { TabsIndicator, TabsList, TabsPanel, TabsRoot, TabsTab } from "#ui/tabs"
const sections = [
{ value: "overview", label: "Overview", text: "Requests are up 12% this week, with a p95 latency of 182 ms." },
{ value: "analytics", label: "Analytics", text: "4,812 visitors from 63 countries. Most came from search." },
{ value: "reports", label: "Reports", text: "Three reports are scheduled. The next one goes out on Monday." },
{ value: "notifications", label: "Notifications", text: "You have no unread notifications." }
]
const TabsUnderlineMessage = UiSchema.TaggedEnum({ SelectedTab: { value: Schema.String } })
export const TabsUnderline = Html.component({
name: "TabsUnderline",
model: Schema.Struct({ tab: Schema.String }),
message: TabsUnderlineMessage
}).make({
init: () => ({ tab: "overview" }),
update: {
SelectedTab: ($, { value }) => $.evolve({ tab: value })
},
view: (h, model) =>
Tabs(h, {
props: { value: model.tab, tabs: sections.map(({ value }) => ({ value })) },
onEmit: { Selected: ({ value }) => TabsUnderlineMessage.SelectedTab({ value }) },
view: (tabs) =>
TabsRoot(h, tabs, { class: "max-w-lg" }).children(
TabsList(h, tabs).children(
sections.map(({ label, value }) =>
TabsTab(h, tabs, { value }).children(label)
),
// Placed by the --active-tab-left and --active-tab-width variables the kit measures.
TabsIndicator(h, tabs)
),
sections.map(({ label, text, value }) =>
TabsPanel(h, tabs, { value })?.children(
h.h4.class("m-0 text-sm font-semibold").children(label),
h.p.class("m-0 text-[13px] text-muted-foreground").children(text)
)
)
)
})
})
Examples
Segmented
The same indicator as a raised pill behind the active tab.
$12
per seat, billed every month
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { TabsIndicator, TabsList, TabsPanel, TabsRoot, TabsTab } from "#ui/tabs"
const plans = [
{ value: "monthly", label: "Monthly", price: "$12", note: "per seat, billed every month" },
{ value: "yearly", label: "Yearly", price: "$10", note: "per seat, billed once a year" },
{ value: "lifetime", label: "Lifetime", price: "$290", note: "per seat, paid once" }
]
const TabsSegmentedMessage = UiSchema.TaggedEnum({ SelectedPlan: { value: Schema.String } })
export const TabsSegmented = Html.component({
name: "TabsSegmented",
model: Schema.Struct({ plan: Schema.String }),
message: TabsSegmentedMessage
}).make({
init: () => ({ plan: "monthly" }),
update: {
SelectedPlan: ($, { value }) => $.evolve({ plan: value })
},
view: (h, model) =>
Tabs(h, {
props: { value: model.plan, tabs: plans.map(({ value }) => ({ value })) },
onEmit: { Selected: ({ value }) => TabsSegmentedMessage.SelectedPlan({ value }) },
view: (tabs) =>
TabsRoot(h, tabs, { class: "w-auto items-center" }).children(
TabsList(h, tabs, { variant: "segmented" }).children(
TabsIndicator(h, tabs),
plans.map(({ label, value }) => TabsTab(h, tabs, { value }).children(label))
),
plans.map(({ note, price, value }) =>
TabsPanel(h, tabs, { value })?.children(
h.p.class("m-0 text-3xl font-semibold tabular-nums").children(price),
h.p.class("m-0 text-[13px] text-muted-foreground").children(note)
)
)
)
})
})
Vertical
Up and Down move between the tabs, and with activateOnFocus they switch the panel too.
General
The workspace name, its URL and its default language.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { TabsIndicator, TabsList, TabsPanel, TabsRoot, TabsTab } from "#ui/tabs"
const settings = [
{ value: "general", label: "General", text: "The workspace name, its URL and its default language." },
{ value: "members", label: "Members", text: "Invite people, change their roles or remove them." },
{ value: "billing", label: "Billing", text: "Your plan, payment method and past invoices." },
{ value: "integrations", label: "Integrations", text: "Connect the tools your team already uses." },
{ value: "advanced", label: "Advanced", text: "Export your data or delete the workspace." }
]
const TabsVerticalMessage = UiSchema.TaggedEnum({ SelectedSetting: { value: Schema.String } })
export const TabsVertical = Html.component({
name: "TabsVertical",
model: Schema.Struct({ setting: Schema.String }),
message: TabsVerticalMessage
}).make({
init: () => ({ setting: "general" }),
update: {
SelectedSetting: ($, { value }) => $.evolve({ setting: value })
},
view: (h, model) =>
// Up and Down move between the tabs, and with `activateOnFocus` the arrow keys also switch panel.
Tabs(h, {
props: {
value: model.setting,
tabs: settings.map(({ value }) => ({ value })),
orientation: "vertical",
activateOnFocus: true
},
onEmit: { Selected: ({ value }) => TabsVerticalMessage.SelectedSetting({ value }) },
view: (tabs) =>
TabsRoot(h, tabs, { class: "max-w-lg" }).children(
TabsList(h, tabs).children(
settings.map(({ label, value }) =>
TabsTab(h, tabs, { value }).children(label)
),
TabsIndicator(h, tabs)
),
settings.map(({ label, text, value }) =>
TabsPanel(h, tabs, { value })?.children(
h.h4.class("m-0 text-sm font-semibold").children(label),
h.p.class("m-0 text-[13px] text-muted-foreground").children(text)
)
)
)
})
})
Icons and a disabled tab
A disabled tab keeps its place in the arrow keys' order and never activates.
The page as your visitors see it.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { TabsIndicator, TabsList, TabsPanel, TabsRoot, TabsTab } from "#ui/tabs"
const views = [
{
value: "preview",
label: "Preview",
icon: "M2 12s3.6-7 10-7 10 7 10 7-3.6 7-10 7S2 12 2 12zm10 3a3 3 0 100-6 3 3 0 000 6z",
text: "The page as your visitors see it.",
disabled: false
},
{
value: "code",
label: "Code",
icon: "M16 18l6-6-6-6M8 6l-6 6 6 6",
text: "The source, ready to copy.",
disabled: false
},
{
value: "history",
label: "History",
icon: "M3 12a9 9 0 109-9 9.7 9.7 0 00-6.7 2.8L3 8M3 3v5h5M12 7v5l4 2",
text: "Every published version, newest first.",
disabled: false
},
{
value: "insights",
label: "Insights",
icon: "M3 3v18h18M7 16l4-5 4 3 5-7",
text: "Insights come with the Pro plan.",
disabled: true
}
]
const TabsIconsMessage = UiSchema.TaggedEnum({ SelectedView: { value: Schema.String } })
const icon = <M>(h: Html.H<M>, path: string) =>
h.el("svg")
.attrs({
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
"stroke-width": "2",
"stroke-linecap": "round",
"stroke-linejoin": "round",
"aria-hidden": "true"
})
.children(h.el("path").attr("d", path))
export const TabsIcons = Html.component({
name: "TabsIcons",
model: Schema.Struct({ view: Schema.String }),
message: TabsIconsMessage
}).make({
init: () => ({ view: "preview" }),
update: {
SelectedView: ($, { value }) => $.evolve({ view: value })
},
view: (h, model) =>
// A disabled tab keeps its place in the arrow keys' order, so a screen reader finds it, and never activates.
Tabs(h, {
props: { value: model.view, tabs: views.map(({ disabled, value }) => ({ value, disabled })) },
onEmit: { Selected: ({ value }) => TabsIconsMessage.SelectedView({ value }) },
view: (tabs) =>
TabsRoot(h, tabs, { class: "max-w-lg" }).children(
TabsList(h, tabs, { variant: "segmented" }).children(
TabsIndicator(h, tabs),
views.map(({ icon: path, label, value }) =>
TabsTab(h, tabs, { value }).children(icon(h, path), label)
)
),
views.map(({ text, value }) =>
TabsPanel(h, tabs, { value })?.children(
h.p.class("m-0 text-[13px] text-muted-foreground").children(text)
)
)
)
})
})
Forms in panels
With keepMounted a hidden panel stays in the page, so a half-filled form survives a switch.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { TabsIndicator, TabsList, TabsPanel, TabsRoot, TabsTab } from "#ui/tabs"
const input =
"h-8.5 rounded-md border border-input bg-background px-3 text-[13px] font-normal shadow-sm outline-none " +
"transition-[border-color,box-shadow] duration-150 focus:border-ring focus:ring-[3px] focus:ring-ring/15"
const TabsFormsMessage = UiSchema.TaggedEnum({
SelectedTab: { value: Schema.String },
UpdatedName: { value: Schema.String },
UpdatedUsername: { value: Schema.String },
SubmittedAccount: {},
SubmittedPassword: {}
})
export const TabsForms = Html.component({
name: "TabsForms",
model: Schema.Struct({
tab: Schema.String,
name: Schema.String,
username: Schema.String,
saved: Schema.NullOr(Schema.String)
}),
message: TabsFormsMessage
}).make({
init: () => ({ tab: "account", name: "Ada Lovelace", username: "@ada", saved: null }),
update: {
SelectedTab: ($, { value }) => $.evolve({ tab: value, saved: null }),
UpdatedName: ($, { value }) => $.evolve({ name: value }),
UpdatedUsername: ($, { value }) => $.evolve({ username: value }),
SubmittedAccount: ($) => $.evolve({ saved: "Account saved" }),
SubmittedPassword: ($) => $.evolve({ saved: "Password changed" })
},
view: (h, model) =>
// `keepMounted` keeps the hidden panel in the page, so what's typed in the password form survives a switch.
Tabs(h, {
props: { value: model.tab, tabs: [{ value: "account" }, { value: "password" }], keepMounted: true },
onEmit: { Selected: ({ value }) => TabsFormsMessage.SelectedTab({ value }) },
view: (tabs) =>
TabsRoot(h, tabs, { class: "max-w-96" }).children(
TabsList(h, tabs, { variant: "segmented", fill: true }).children(
TabsIndicator(h, tabs),
TabsTab(h, tabs, { value: "account" }).children("Account"),
TabsTab(h, tabs, { value: "password" }).children("Password")
),
TabsPanel(h, tabs, { value: "account" })?.children(
h.form.class("flex flex-col gap-3.5 rounded-lg border bg-card p-5 shadow-sm").on("submit", TabsFormsMessage.SubmittedAccount()).children(
h.h4.class("m-0 text-sm font-semibold").children("Account"),
h.p.class("m-0 text-[13px] text-muted-foreground").children("Change how you appear to your team."),
h.label.class("flex flex-col gap-1.5 text-[13px] font-medium").children(
"Name",
h.input.class(input).value(model.name)
.on("input", ({ value }) => TabsFormsMessage.UpdatedName({ value }))
),
h.label.class("flex flex-col gap-1.5 text-[13px] font-medium").children(
"Username",
h.input.class(input).value(model.username)
.on("input", ({ value }) => TabsFormsMessage.UpdatedUsername({ value }))
),
h.div.class("flex flex-wrap items-center gap-2.5").children(
Button(h, { props: { type: "submit" } }).children("Save changes"),
h.span.class("text-xs text-muted-foreground tabular-nums").children(model.saved)
)
)
),
TabsPanel(h, tabs, { value: "password" })?.children(
h.form.class("flex flex-col gap-3.5 rounded-lg border bg-card p-5 shadow-sm").on("submit", TabsFormsMessage.SubmittedPassword()).children(
h.h4.class("m-0 text-sm font-semibold").children("Password"),
h.p.class("m-0 text-[13px] text-muted-foreground").children("You'll be signed out everywhere else."),
h.label.class("flex flex-col gap-1.5 text-[13px] font-medium").children(
"Current password",
h.input.class(input).attrs({ type: "password", autocomplete: "current-password" })
),
h.label.class("flex flex-col gap-1.5 text-[13px] font-medium").children(
"New password",
h.input.class(input).attrs({ type: "password", autocomplete: "new-password" })
),
h.div.class("flex flex-wrap items-center gap-2.5").children(
Button(h, { props: { type: "submit" } }).children("Change password"),
h.span.class("text-xs text-muted-foreground tabular-nums").children(model.saved)
)
)
)
)
})
})
Link tabs
A tab can be an <a>, with an address to copy or open in a new tab. Here follow is off, so a click only activates.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { TabsIndicator, TabsLink, TabsList, TabsPanel, TabsRoot } from "#ui/tabs"
const pages = [
{ value: "readme", label: "Readme", text: "Install the package, then draw your first component." },
{ value: "changelog", label: "Changelog", text: "0.1.0 is the first public preview." },
{ value: "license", label: "License", text: "MIT. Use it for anything." }
]
const TabsLinksMessage = UiSchema.TaggedEnum({ SelectedPage: { value: Schema.String } })
export const TabsLinks = Html.component({
name: "TabsLinks",
model: Schema.Struct({ page: Schema.String }),
message: TabsLinksMessage
}).make({
init: () => ({ page: "readme" }),
update: {
SelectedPage: ($, { value }) => $.evolve({ page: value })
},
view: (h, model) =>
// A tab can be an <a>, so it has an address to copy or open in a new tab. A click follows the href unless
// `follow` is false, as here, where the panels are on this page and the Model holds the active one.
Tabs(h, {
props: { value: model.page, tabs: pages.map(({ value }) => ({ value })) },
onEmit: { Selected: ({ value }) => TabsLinksMessage.SelectedPage({ value }) },
view: (tabs) =>
TabsRoot(h, tabs, { class: "max-w-lg" }).children(
TabsList(h, tabs).attr("aria-label", "Package").children(
pages.map(({ label, value }) =>
TabsLink(h, tabs, { value, href: `#${value}`, follow: false }).children(label)
),
TabsIndicator(h, tabs)
),
pages.map(({ text, value }) =>
TabsPanel(h, tabs, { value })?.children(
h.p.class("m-0 text-[13px] text-muted-foreground").children(text)
)
)
)
})
})
Anatomy
The parts the view closure receives. Apply each one to an element of your own.
| Part | Description |
|---|---|
root | Any element around the list and the panels. It points each tab at its panel. |
list | Any element around the tabs: the tablist. |
tab({ element, value }) | A <button> or an <a> in the list. |
indicator | Any element in the list, placed against it. undefined while no tab is active, hidden until measured. |
panel({ element, value }) | Any element in the root. undefined while hidden, unless kept mounted. |
API reference
Props
| Prop | Type | Description |
|---|---|---|
value | string | null | The active tab. Required: the consumer owns it. |
tabs | ReadonlyArray<{ value, disabled?, keepMounted? }> | Every tab, in the order the list shows them. |
orientation | "horizontal" | "vertical" | Which arrow keys move between tabs. Default "horizontal". |
activateOnFocus | boolean | Moving focus with the arrow keys also activates the tab. |
loopFocus | boolean | The arrow keys wrap around. Default true. |
keepMounted | boolean | Keeps every panel drawn, hidden while its tab isn't active. |
Events
| Event | Description |
|---|---|
Selected({ value, activationDirection }) | Asks for value to be the tab the user activated. |
Data attributes
| Attribute | Description |
|---|---|
data-orientation | On every part: horizontal or vertical. |
data-activation-direction | On every part: left, right, up, down or none, where the active tab moved. |
data-active | On the active tab. |
data-disabled | On a disabled tab. |
data-hidden | On a panel that is kept mounted while hidden. |
data-starting-style / data-ending-style | On a panel for its first frame, and while it plays its exit. |
--active-tab-left / -top / -width / -height | CSS variables on the indicator, in pixels from the list's padding box. |
Related components
The components this page's file and examples import.
- Button: A native button that can stay focusable while disabled.