Switch between panels, or between views of the same data.
Website refresh ships on December 12. 14 of 22 issues are done.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { FormField, Input } from "./ui/uicontext/components/FormControls.ts"import { Tabs } from "./ui/uicontext/components/kit/Tabs.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ panel: { inlineSize: "min(100%, 30rem)", minBlockSize: 140 },})const Tab = Schema.Literals(["overview", "members", "settings"])const Message = UiSchema.TaggedEnum({ SelectedTab: { tab: Tab }, UpdatedName: { name: Schema.String },})export const ProjectTabs = Html.component({ name: "ProjectTabs", model: Schema.Struct({ tab: Tab, name: Schema.String }), message: Message,}).make({ init: () => ({ tab: "overview", name: "Website refresh" }), update: { SelectedTab: ($, { tab }) => $.evolve({ tab }), UpdatedName: ($, { name }) => $.evolve({ name }), }, view: (h, { tab, name }) => h.div.attrs(sx(styles.panel)).children( Tabs(h, { label: "Project", value: tab, items: [ { value: "overview", label: "Overview" }, { value: "members", label: "Members" }, // keeps a half-typed name while another tab is open { value: "settings", label: "Settings", keepMounted: true }, ], onSelect: (tab) => Message.SelectedTab({ tab }), panel: (tab) => { switch (tab) { case "overview": return h.p.children(`${name} ships on December 12. 14 of 22 issues are done.`) case "members": return h.p.children("Ada Lovelace, Grace Hopper and Scout work on this project.") case "settings": return FormField(h, { id: "project-name", label: "Project name" }, (attrs) => Input(h).attrs(attrs).value(name).on("input", ({ value }) => Message.UpdatedName({ name: value }))) } }, }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/tabsUsage
import { Tabs } from "./ui/uicontext/components/kit/Tabs.ts"const Tab = Schema.Literals(["activity", "comments"])const Message = UiSchema.TaggedEnum({ SelectedTab: { tab: Tab } })// `tab` lives in your Model; SelectedTab sets itexport const IssueTabs = (h: Html.H<typeof Message.Type>, tab: typeof Tab.Type) => Tabs(h, { label: "Issue", value: tab, items: [ { value: "activity", label: "Activity" }, { value: "comments", label: "Comments" }, ], onSelect: (tab) => Message.SelectedTab({ tab }), panel: (tab) => h.p.children(tab === "activity" ? "Grace moved this to In Review." : "No comments yet."), })Examples
Filters over one list
With panelMode: "shared" every tab drives the same panel, for views of the same data. A disabled tab stays visible.
More tabs than room
The row scrolls sideways and fades at the edge that hides more. The selected tab stays in view, whether a click, the arrow keys or the app picked it.
Secrets: the Invoice sync workflow
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Tabs } from "./ui/uicontext/components/kit/Tabs.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ // narrower than the tabs, as in a side panel or on a phone frame: { inlineSize: "min(100%, 22rem)" }, panel: { margin: 0, color: color.muted, fontSize: font.sm },})const Section = Schema.Literals(["overview", "activity", "runs", "files", "connections", "secrets", "members", "settings"])type Section = typeof Section.Typeconst labels: Record<Section, string> = { overview: "Overview", activity: "Activity", runs: "Runs", files: "Files", connections: "Connections", secrets: "Secrets", members: "Members", settings: "Settings",}const sections = Section.literals.map((value) => ({ value, label: labels[value] }))const Message = UiSchema.TaggedEnum({ SelectedSection: { section: Section } })export const WorkflowSections = Html.component({ name: "WorkflowSections", model: Schema.Struct({ section: Section }), message: Message,}).make({ // opens on a tab past the edge: it scrolls into view init: () => ({ section: "secrets" }), update: { SelectedSection: ($, { section }) => $.evolve({ section }) }, view: (h, { section }) => h.div.attrs(sx(styles.frame)).children( Tabs(h, { label: "Workflow", value: section, items: sections, panelMode: "shared", onSelect: (section) => Message.SelectedSection({ section }), panel: (section) => h.p.attrs(sx(styles.panel)).children(`${labels[section]}: the Invoice sync workflow`), }), ),})API reference
Options
| Name | Type | Description |
|---|---|---|
labelrequired | string | The tab list's accessible name: "Views". |
valuerequired | string | null | The selected tab's value, or null for none. The caller keeps it. |
itemsrequired | TabItem<string>[] | The tabs, each with a value, a label and an optional glyph. |
onSelectrequired | (value: string) => Message | The message for a tab the person selected, by click or arrow keys. |
panelrequired | (value: string) => View.Input | Draws the panel for a tab's value. |
panelMode | "shared" | shared keeps one panel mounted across tabs, for tabs that change the data of one continuing view; otherwise each tab has its own panel. |
actions | View.Input | Controls at the end of the tab row, such as a filter or a New button. |
beforePanels | View.Input | Content between the tab row and the panels, such as a toolbar. |
style | Style | StyleX styles for the root. |
toolbarStyle | Style | StyleX styles for the row that holds the tabs and actions. |
listStyle | Style | StyleX styles for the tab list. |
tabStyle | Style | StyleX styles for every tab. |
currentStyle | Style | StyleX styles for the selected tab, applied last. |
Accessibility
- Follows the tabs pattern: arrow keys move between tabs, Home and End jump to the ends, Enter or Space selects.
- Each panel is labelled by its tab. Set
keepMountedon a tab whose panel holds unsaved input.