Document tabs over the page: open, close, drag to reorder, and every tab one press away when they overflow.
Overview
Customers
Billing
Acme Corporation (edited)
Globex
Overview
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { closeTab, moveTab, StripTab, TabStrip } from "./ui/uicontext/components/kit/TabStrip.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ frame: { inlineSize: "min(100%, 36rem)", overflow: "hidden", borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.lg, backgroundColor: color.raised }, page: { minBlockSize: 120, padding: space.xl, backgroundColor: color.panel, color: color.muted, fontSize: font.sm },})const Message = UiSchema.TaggedEnum({ OpenedTab: { id: Schema.String }, ClosedTab: { id: Schema.String }, MovedTab: { id: Schema.String, index: Schema.Number }, AddedTab: {},})const start: ReadonlyArray<StripTab> = [ { id: "overview", label: "Overview", closable: false }, { id: "customers", label: "Customers" }, { id: "billing", label: "Billing" }, { id: "acme", label: "Acme Corporation", dirty: true }, { id: "globex", label: "Globex" },]// The page keeps the tabs and which is open; the strip asks to show, close, move and add themexport const OpenTabs = Html.component({ name: "OpenTabs", model: Schema.Struct({ tabs: Schema.Array(StripTab), current: Schema.NullOr(Schema.String), added: Schema.Number }), message: Message,}).make({ init: () => ({ tabs: start, current: "overview", added: 0 }), update: { OpenedTab: ($, { id }) => $.evolve({ current: id }), ClosedTab: ($, { id }) => $.evolve(closeTab($.model.tabs, id, $.model.current)), MovedTab: ($, { id, index }) => $.evolve({ tabs: moveTab($.model.tabs, id, index) }), AddedTab: ($) => { const id = `report-${$.model.added + 1}` return $.evolve({ tabs: [...$.model.tabs, { id, label: `Report ${$.model.added + 1}` }], current: id, added: $.model.added + 1 }) }, }, view: (h, model) => h.div.attrs(sx(styles.frame)).children( TabStrip(h, { props: { label: "Open tabs", tabs: model.tabs, current: model.current, addLabel: "New report" }, onEmit: { RequestedSelect: ({ id }) => Message.OpenedTab({ id }), RequestedClose: ({ id }) => Message.ClosedTab({ id }), RequestedMove: ({ id, index }) => Message.MovedTab({ id, index }), RequestedNew: () => Message.AddedTab(), }, }), h.div.attrs(sx(styles.page)).children( model.tabs.find((tab) => tab.id === model.current)?.label ?? "No tabs open.", ), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/tab-stripUsage
import { closeTab, moveTab, type StripTab, TabStrip } from "./ui/uicontext/components/kit/TabStrip.ts"type Model = { readonly tabs: ReadonlyArray<StripTab>; readonly current: string | null }// the app keeps the tabs; `closeTab` also picks the tab to show nextexport const Tabs = <M>(h: Html.H<M>, model: Model, changed: (next: Model) => M) => TabStrip(h, { props: { label: "Open tabs", tabs: model.tabs, current: model.current }, onEmit: { RequestedSelect: ({ id }) => changed({ ...model, current: id }), RequestedClose: ({ id }) => changed(closeTab(model.tabs, id, model.current)), RequestedMove: ({ id, index }) => changed({ ...model, tabs: moveTab(model.tabs, id, index) }), RequestedNew: () => undefined, }, })API reference
Props
| Name | Type | Description |
|---|---|---|
labelrequired | string | The tab list's accessible name: "Open tabs" |
tabsrequired | StripTab[] | — |
currentrequired | string | null | The shown tab's id |
addLabel | string | A + button at the end that asks for a new tab, named by this: "New tab" |
agentId | string | — |
Events
| Event | Payload | Description |
|---|---|---|
RequestedSelect | { id: string } | Show this tab: set current |
RequestedClose | { id: string } | Close this tab: drop it, and if it was shown, show closeTab's next |
RequestedMove | { id: string; index: number } | Move this tab to index in the new order |
RequestedNew | {} | The + button |
Agent actions
| Action | Description |
|---|---|
RequestedSelect | Show a tab, by id |
RequestedClose | Close a tab, by id |
RequestedMove | Move a tab, by id, to an index in the new order |
Accessibility
- A
tablistwith one Tab stop on the open tab: Left and Right move, Home and End jump, Enter or Space shows the focused tab, Delete closes it and Alt+Shift with the arrows moves it. - Each × is named for its tab (“Close Billing”) and left out of the Tab order, since Delete does the same; a tab with unsaved changes reads “(edited)”.
- Agents read the tabs and show, close or move one by id.