Tabs unstable
Tabs: a list of tabs, each showing one panel when it is active. Pass the
active tab as value and update it when the tabs emit Selected. The
consumer's view supplies the markup, and the tabs add the ARIA, the ids,
the roving focus and the panel transitions. A headless component.
Details
Anatomy:
| Part | Element |
|---|---|
root |
any element, around the list and the panels |
list |
any element, around the tabs |
tab |
<button> or <a>, inside the list |
indicator |
any element, inside the list |
panel |
any element, inside the root |
root, list and indicator take the consumer's builder.
tab({ element, value }) and panel({ element, value }) also take the
value of an entry in the tabs prop. panel returns undefined while
its panel is hidden, unless it is kept mounted, and indicator returns
undefined while no tab is active, so the consumer writes
tabs.panel({ element: h.div, value: "a" })?.children(...). No part adds
markup.
Draw the root around the list and the panels: it points each tab at its
panel. The indicator must sit inside the list, since it is placed against
it, and the list needs position: relative or another containing block for
an absolutely positioned indicator.
Data attributes:
On every part:
data-orientation:horizontalorverticaldata-activation-direction: where the active tab moved, relative to the one before it:left,right,up,down, ornonebefore the first change
On tab, each present with an empty value or absent:
data-activedata-disabled
On panel:
data-index: the index of its tab intabsdata-hidden: present while the panel is hiddendata-starting-style: present for the first frame of a panel that becomes activedata-ending-style: present while a panel that stopped being active plays its exit, until its animations and transitions finish. An exit must start by the timeMutationObservercallbacks for the attribute have run. Panels do not wait for exits started in a later animation frame; a panel with no exit leaves before the next paint
On indicator, as custom properties in pixels, measured from the list's
padding box: --active-tab-left, --active-tab-right, --active-tab-top,
--active-tab-bottom, --active-tab-width and --active-tab-height.
The indicator is hidden until they are measured, including in server
HTML.
Keyboard:
One tab is in the page's Tab order: the one focus last left, or, while focus is outside the list, the active tab. A disabled active tab leaves the previous one in place, and with none the first enabled tab is used.
- Left and Right move focus between the tabs of a horizontal list, swapped in a right-to-left layout. Up and Down do it in a vertical list.
- Home and End move focus to the first and the last tab.
- The arrow keys wrap around, unless
loopFocusisfalse. - Space clicks the focused tab once, when the key goes down, with the
key's modifiers, so a link tab still follows its
href. Holding it down doesn't click again. - Enter clicks a
<button>or a link natively, and the tabs click an<a>that has nohref, once while the key is held. - With
activateOnFocus, moving focus activates the tab as well, unless a mouse button other than the main one is pressing it.
Disabled tabs take focus and are not skipped, so a screen reader can find them, but they never activate.
The tabs follow the nearest DirectionProvider above them or, without
one, the list's CSS direction.
Example Three tabs, one of them disabled
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Tabs } from "effect/ui/kit"import { Effect, Schema } from "effect"
const SettingsMessage = UiSchema.TaggedEnum({ SelectedTab: { value: Schema.String } })
const Settings = Html.component({ name: "Settings", model: Schema.Struct({ tab: Schema.String }), message: SettingsMessage}).make({ init: () => ({ tab: "overview" }), update: { SelectedTab: ($, { value }) => $.evolve({ tab: value }) }, view: (h, model) => Tabs(h, { props: { value: model.tab, tabs: [{ value: "overview" }, { value: "projects" }, { value: "account", disabled: true }] }, onEmit: { Selected: ({ value }) => SettingsMessage.SelectedTab({ value }) }, view: (tabs) => tabs.root(h.div.class("tabs")).children( tabs.list(h.div.class("list")).children( tabs.tab({ element: h.button.class("tab"), value: "overview" }).children("Overview"), tabs.tab({ element: h.button.class("tab"), value: "projects" }).children("Projects"), tabs.tab({ element: h.button.class("tab"), value: "account" }).children("Account"), tabs.indicator(h.span.class("indicator")) ), tabs.panel({ element: h.div, value: "overview" })?.children("Workspace stats"), tabs.panel({ element: h.div, value: "projects" })?.children("Milestones"), tabs.panel({ element: h.div, value: "account" })?.children("Profile") ) })})
// The active tab keeps the consumer's class and is selected; only its panel is drawn.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Settings, {}))if (!/<button class="tab"[^>]* role="tab"[^>]* aria-selected="true"/.test(html)) throw new Error("no selected tab")if (html.match(/role="tabpanel"/g)?.length !== 1) throw new Error("more than the active panel is drawn")Components
Creates tabs whose selected value, tabs, tab and panel parts and
Selected event use the supplied string-valued schema. Selected returns
a value from tabs, not an unchecked DOM string.
Details
The schema must encode to a string. Create the component once at module
level: each call creates a new component. The existing Tabs export
uses Schema.String.
Signature
declare function makeTabs<V extends string>(options: { readonly value: Codec<V, string>;}): Headless<{ readonly activateOnFocus?: boolean; readonly keepMounted?: boolean; readonly loopFocus?: boolean; readonly orientation?: "horizontal" | "vertical"; readonly tabs: readonly Array<{ readonly disabled?: boolean; readonly keepMounted?: boolean; value: V; }>; readonly value: V | null;}, { readonly _tag: "Selected"; readonly activationDirection: "left" | "right" | "none" | "up" | "down"; value: V;}, { activationDirection: "left" | "right" | "none" | "up" | "down"; indicator: <B extends PartTarget<string>>(element: B) => ReturnType<B["attr"]> | undefined; list: Part<string, [], never>; orientation: "horizontal" | "vertical"; panel: <B extends Keyed<string>>(__namedParameters: { readonly element: B; readonly value: V; }) => ReturnType<B["attr"]> | undefined & Pick<<B extends PartTarget<string> & { readonly key: (key: string) => PartTarget<string>; }>(element: B, value: V, ...rest: []) => ReturnType<B["attr"]> & Pick<Part<string, [value: V], never>, "~R">, "~R">; root: Part<string, [], never>; tab: <B extends Keyed<"a" | "button">>(__namedParameters: { readonly element: B; readonly value: V; }) => ReturnType<B["attr"]> & Pick<<B extends PartTarget<"a" | "button"> & { readonly key: (key: string) => PartTarget<... | ...>; }>(element: B, value: V, ...rest: []) => ReturnType<B["attr"]> & Pick<Part<"a" | "button", [value: V], never>, "~R">, "~R">; value: V | null;}, Exclude<PartRequirements<V>, FrameworkService>, never, never, never, never, never, { readonly direction: "left" | "right" | "none" | "up" | "down"; readonly focused: boolean; readonly highlighted: string | null; readonly indicator: boolean; readonly leaving: readonly Array<string>; readonly rtl: boolean; readonly secondary: string | null; readonly shown: string | null; readonly started: boolean;}, { readonly _tag: "ReleasedPointer";} | { readonly _tag: "RenderedFirstFrame"; readonly value: string;} | { readonly _tag: "FinishedTransition"; readonly value: string;} | { readonly _tag: "FoundDirection"; readonly rtl: boolean;} | { readonly _tag: "PressedKey"; readonly key: string; readonly value: string;} | { readonly _tag: "ClickedTab"; readonly value: string;} | { readonly _tag: "FocusedTab"; readonly value: string;} | { readonly _tag: "BlurredTab";} | { readonly _tag: "PressedTab"; readonly button: number; readonly value: string;} | { readonly _tag: "ChangedValue";} | { readonly _tag: "MeasuredActiveTab"; readonly sized: boolean;}, QueryEntries<{}, {}>>Example Tabs typed by a literal union
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { makeTabs } from "effect/ui/kit"import { Effect, Schema } from "effect"
const Section = Schema.Literals(["general", "billing"])const SectionTabs = makeTabs({ value: Section })const tabs = Section.literals.map((value) => ({ value }))
const Message = UiSchema.TaggedEnum({ Opened: { section: Section } })
const Settings = Html.component({ name: "Settings", model: Schema.Struct({ section: Section }), message: Message}).make({ init: () => ({ section: "general" as const }), update: { Opened: ($, { section }) => $.evolve({ section }) }, view: (h, model) => SectionTabs(h, { props: { value: model.section, tabs }, // `value` is "general" | "billing" onEmit: { Selected: ({ value }) => Message.Opened({ section: value }) }, view: (parts) => parts.root(h.div).children( parts.list(h.div).children( tabs.map(({ value }) => parts.tab({ element: h.button, value }).children(value)) ), tabs.map(({ value }) => parts.panel({ element: h.div, value })?.children(value)) ) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Settings, {}))if (!html.includes('aria-selected="true"')) throw new Error("no tab is selected")A headless tabs component. Call it with the consumer's h, the props, an
onEmit for Selected, and a view closure that draws tabs.root(h.div)
around tabs.list(h.div), with tabs.tab({ element: h.button, value }) for
each entry of tabs inside it, and the panels with
tabs.panel({ element: h.div, value }).
Details
The parts also return value, orientation and activationDirection, for
markup that depends on them.
Each part's own attributes win over the ones the consumer set on the builder before the part, including the ones a state leaves absent:
- every part:
data-orientationanddata-activation-direction list:role="tablist", andaria-orientation, written only for a vertical listtab:role="tab",id,aria-selected,aria-controls, added only while the view draws its panel,aria-disabled,tabindex,disabled, which it never sets so a disabled tab keeps its focus,type="button"on a<button>,data-activeanddata-disabledpanel:role="tabpanel",id,aria-labelledby,hidden,inerton a panel that isn't active,tabindex,data-index,data-hidden,data-starting-styleanddata-ending-styleindicator:role="presentation"andhidden
aria-controls and aria-labelledby are lists of ids, so the part adds
its id to the consumer's instead of replacing them.
A part writes them when it is applied, so an attribute the consumer chains after the part is the consumer's to answer for.
A tab handles click, keydown, focus and blur, and pointerdown with
activateOnFocus, on a layer of its own, so the consumer's handlers for them
still run. It cancels the keys it handles, and a click on a disabled tab,
so a disabled link doesn't follow its href. A click on an enabled link
is left alone and follows it.
Drawing a tab or a panel for a value that isn't in tabs, or two tabs
with one value, fails the render.
History restore saves nothing of the tabs' own state, which is focus,
transitions and the indicator's measurement. The active tab is the
consumer's value, which restores with the consumer.
Signature
declare const Tabs: Headless<{ readonly activateOnFocus?: boolean; readonly keepMounted?: boolean; readonly loopFocus?: boolean; readonly orientation?: "horizontal" | "vertical"; readonly tabs: readonly Array<{ readonly disabled?: boolean; readonly keepMounted?: boolean; value: string; }>; readonly value: string | null;}, { readonly _tag: "Selected"; readonly activationDirection: "left" | "right" | "none" | "up" | "down"; value: string;}, { activationDirection: "left" | "right" | "none" | "up" | "down"; indicator: <B extends PartTarget<string>>(element: B) => ReturnType<B["attr"]> | undefined; list: Part<string, [], never>; orientation: "horizontal" | "vertical"; panel: <B extends Keyed<string>>(__namedParameters: { readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> | undefined & Pick<<B extends PartTarget<string> & { readonly key: (key: string) => PartTarget<string>; }>(element: B, value: string, ...rest: []) => ReturnType<B["attr"]> & Pick<Part<string, [value: string], never>, "~R">, "~R">; root: Part<string, [], never>; tab: <B extends Keyed<"a" | "button">>(__namedParameters: { readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<<B extends PartTarget<"a" | "button"> & { readonly key: (key: string) => PartTarget<... | ...>; }>(element: B, value: string, ...rest: []) => ReturnType<B["attr"]> & Pick<Part<"a" | "button", [value: string], never>, "~R">, "~R">; value: string | null;}, never, never, never, never, never, never, { readonly direction: "left" | "right" | "none" | "up" | "down"; readonly focused: boolean; readonly highlighted: string | null; readonly indicator: boolean; readonly leaving: readonly Array<string>; readonly rtl: boolean; readonly secondary: string | null; readonly shown: string | null; readonly started: boolean;}, { readonly _tag: "ReleasedPointer";} | { readonly _tag: "RenderedFirstFrame"; readonly value: string;} | { readonly _tag: "FinishedTransition"; readonly value: string;} | { readonly _tag: "FoundDirection"; readonly rtl: boolean;} | { readonly _tag: "PressedKey"; readonly key: string; readonly value: string;} | { readonly _tag: "ClickedTab"; readonly value: string;} | { readonly _tag: "FocusedTab"; readonly value: string;} | { readonly _tag: "BlurredTab";} | { readonly _tag: "PressedTab"; readonly button: number; readonly value: string;} | { readonly _tag: "ChangedValue";} | { readonly _tag: "MeasuredActiveTab"; readonly sized: boolean;}, QueryEntries<{}, {}>>Models
Where a newly active tab sits relative to the one before it.
Signature
type TabsActivationDirection = typeof TabsActivationDirection.TypeWhat Tabs emits.
Signature
type TabsEvents<V extends string = string> = ReturnType<typeof eventsSchema>["Type"]The direction the tabs are laid out in.
Signature
type TabsOrientation = typeof TabsOrientation.TypeThe props of Tabs.
Signature
type TabsProps<V extends string = string> = ReturnType<typeof propsSchema>["Type"]One tab, in the order the list shows it.
Signature
type TabsTab<V extends string = string> = ReturnType<typeof tabSchema>["Type"]Schemas
TabsActivationDirection
unstableWhere a newly active tab sits relative to the one active before it, by
their order in tabs: left or right in a horizontal list, following the
text direction, up or down in a vertical one, and none when either
value is null or not a tab.
Signature
declare const TabsActivationDirection: Literals<readonly ["left", "right", "up", "down", "none"]>TabsEvents
unstableSelected asks for the tab the user activated, by a click, Enter or Space,
or focus with activateOnFocus, and where it sits relative to the active
one. Store value and pass it back as the value prop. A disabled tab, or
the one already active, emits nothing.
Signature
declare const TabsEvents: TaggedEnum<{ Selected: TaggedStruct<"Selected", { readonly activationDirection: Literals<readonly ["left", "right", "up", "down", "none"]>; value: String; }>;}>TabsOrientation
unstableThe direction the tabs are laid out in, which decides their arrow keys.
Signature
declare const TabsOrientation: Literals<readonly ["horizontal", "vertical"]>Props for the tabs and the active one.
Details
value: the active tab's value, ornullfor none. Required: the consumer owns it.tabs: every tab, in the order the list shows them. Each value is drawn once withtab, and may be drawn withpanel.orientation:"horizontal"by default.activateOnFocus: activates a tab when it takes focus, such as from the arrow keys, and not only when it is clicked. While a mouse button other than the main one presses a tab, untilpointeruporpointercancel, focus only focuses it.loopFocus: the arrow keys wrap from the last tab to the first and back.trueby default.keepMounted: keeps every panel drawn, andhidden, while its tab isn't active, askeepMountedon aTabsTabdoes for one.
Signature
declare const TabsProps: Struct<{ readonly activateOnFocus: optional<Boolean>; readonly keepMounted: optional<Boolean>; readonly loopFocus: optional<Boolean>; readonly orientation: optional<Literals<readonly ["horizontal", "vertical"]>>; readonly tabs: $Array<Struct<{ readonly disabled: optional<Boolean>; readonly keepMounted: optional<Boolean>; value: String; }>>; readonly value: NullOr<String>;}>One tab, in the order the list shows it.
Details
value: names the tab and its panel.disabled: the tab keeps its focus and its place in the arrow keys' order, and never activates.keepMounted: keeps this tab's panel drawn, andhidden, while the tab isn't active. Overrides thekeepMountedprop for this panel.
Signature
declare const TabsTab: Struct<{ readonly disabled: optional<Boolean>; readonly keepMounted: optional<Boolean>; value: String;}>