Tabs
@codeui/styles v0.3.0
Named panels that share one content area, in plain HTML.
Load @codeui/styles/tabs.css, or all.css. The Solid component is documented in Tabs.
Demo
<div class="cui-tabs" data-cui="tabs" data-orientation="horizontal" data-variant="default"> <div class="cui-tabs-list" data-cui="tabs-list" data-orientation="horizontal" role="tablist" aria-orientation="horizontal" aria-label="Snippet details"> <button type="button" class="cui-tabs-header" style="background: none; border: 0; font-family: inherit" role="tab" id="tab-code" aria-controls="panel-code" aria-selected="true" data-orientation="horizontal" data-selected> Code </button> <button type="button" class="cui-tabs-header" style="background: none; border: 0; font-family: inherit" role="tab" id="tab-settings" aria-controls="panel-settings" aria-selected="false" tabindex="-1" data-orientation="horizontal">Settings</button> <button type="button" class="cui-tabs-header" style="background: none; border: 0; font-family: inherit" role="tab" id="tab-history" aria-controls="panel-history" aria-selected="false" aria-disabled="true" disabled data-orientation="horizontal" data-disabled>History</button> </div> <div class="cui-tabs-content" role="tabpanel" id="panel-code" aria-labelledby="tab-code" tabindex="0" data-orientation="horizontal" data-selected>Your TypeScript snippet.</div> <div class="cui-tabs-content" role="tabpanel" id="panel-settings" aria-labelledby="tab-settings" tabindex="0" data-orientation="horizontal" hidden>Font, theme, and export settings.</div> <div class="cui-tabs-content" role="tabpanel" id="panel-history" aria-labelledby="tab-history" tabindex="0" data-orientation="horizontal" hidden>Revision history.</div></div>Markup
| Part | Element | Class | data-cui |
|---|---|---|---|
| Root | div | cui-tabs | tabs |
| List | div | cui-tabs-list | tabs-list |
| Header | button | cui-tabs-header | None |
| Panel | div | cui-tabs-content | None |
| Indicator | span or div | cui-tabs-indicator | tabs-indicator |
| Attribute | Values | Default |
|---|---|---|
data-orientation on the root, list and indicator | horizontal, vertical | horizontal |
data-selected on a header | Presence | Off |
The root class sets the colors. The CSS reads no variant attribute: the inline variant is the indicator element. The helper still emits data-variant for adapters.
Variants
Inline indicator
<div class="cui-tabs" data-cui="tabs" data-orientation="horizontal" data-variant="inline"> <div class="cui-tabs-list" data-cui="tabs-list" data-orientation="horizontal" role="tablist" aria-orientation="horizontal" aria-label="Snippet details"> <button type="button" class="cui-tabs-header" style="background: none; border: 0; font-family: inherit" role="tab" id="tab-code" aria-controls="panel-code" aria-selected="true" data-orientation="horizontal" data-selected> Code <span class="cui-tabs-indicator" data-cui="tabs-indicator" data-orientation="horizontal" role="presentation" style="left: 0; right: 0"></span> </button> <button type="button" class="cui-tabs-header" style="background: none; border: 0; font-family: inherit" role="tab" id="tab-settings" aria-controls="panel-settings" aria-selected="false" tabindex="-1" data-orientation="horizontal">Settings</button> <button type="button" class="cui-tabs-header" style="background: none; border: 0; font-family: inherit" role="tab" id="tab-history" aria-controls="panel-history" aria-selected="false" aria-disabled="true" disabled data-orientation="horizontal" data-disabled>History</button> </div> <div class="cui-tabs-content" role="tabpanel" id="panel-code" aria-labelledby="tab-code" tabindex="0" data-orientation="horizontal" data-selected>Your TypeScript snippet.</div> <div class="cui-tabs-content" role="tabpanel" id="panel-settings" aria-labelledby="tab-settings" tabindex="0" data-orientation="horizontal" hidden>Font, theme, and export settings.</div> <div class="cui-tabs-content" role="tabpanel" id="panel-history" aria-labelledby="tab-history" tabindex="0" data-orientation="horizontal" hidden>Revision history.</div></div>The indicator is a 3px bar under the selected header. It is absolutely positioned inside the nearest positioned ancestor. An adapter puts it in the list and sets left and width from the selected header. Without a script, put it inside the selected header with left: 0; right: 0, because the header is positioned too.
The default variant has no indicator. The selected header changes only its text color.
Vertical orientation
<div class="cui-tabs" data-cui="tabs" data-orientation="vertical" data-variant="inline"> <div class="cui-tabs-list" data-cui="tabs-list" data-orientation="vertical" role="tablist" aria-orientation="vertical" aria-label="Snippet details"> <button type="button" class="cui-tabs-header" style="background: none; border: 0; font-family: inherit" role="tab" id="tab-code" aria-controls="panel-code" aria-selected="true" data-orientation="vertical" data-selected> Code <span class="cui-tabs-indicator" data-cui="tabs-indicator" data-orientation="vertical" role="presentation" style="top: 0; bottom: 0"></span> </button> <button type="button" class="cui-tabs-header" style="background: none; border: 0; font-family: inherit" role="tab" id="tab-settings" aria-controls="panel-settings" aria-selected="false" tabindex="-1" data-orientation="vertical">Settings</button> <button type="button" class="cui-tabs-header" style="background: none; border: 0; font-family: inherit" role="tab" id="tab-history" aria-controls="panel-history" aria-selected="false" aria-disabled="true" disabled data-orientation="vertical" data-disabled>History</button> </div> <div class="cui-tabs-content" role="tabpanel" id="panel-code" aria-labelledby="tab-code" tabindex="0" data-orientation="vertical" data-selected>Your TypeScript snippet.</div> <div class="cui-tabs-content" role="tabpanel" id="panel-settings" aria-labelledby="tab-settings" tabindex="0" data-orientation="vertical" hidden>Font, theme, and export settings.</div> <div class="cui-tabs-content" role="tabpanel" id="panel-history" aria-labelledby="tab-history" tabindex="0" data-orientation="vertical" hidden>Revision history.</div></div>Set data-orientation="vertical" on the root, the list and the indicator. The root becomes a row, the headers stack in a column, and the line moves to the right of the list. The vertical indicator is 2px wide; set top and height, or top: 0; bottom: 0 inside the selected header.
States
Selected
data-selected on a header sets the primary text color. Set aria-selected="true" on the same header. Hide inactive panels with the hidden attribute.
Disabled
The CSS has no disabled style for a header. Set disabled and aria-disabled="true" so the header leaves the tab order. It looks like an unselected header.
Focus
A header shows the 2px focus ring on :focus-visible. The indicator slides in 160ms when it moves. Set html[data-cui-input="keyboard"] while the user navigates with arrow keys, and the indicator moves without a transition. Reduced motion never slides it.
What you supply
- A button reset on the headers. The header class sets padding, type and color, but not
background,borderorfont-family. The demos reset them inline; a page-wide reset works too. role="tablist"andaria-orientationon the list, and an accessible label.role="tab",aria-selectedandaria-controlson headers.role="tabpanel"andaria-labelledbyon panels.- Roving focus: only the selected header has
tabindex="0". Left and Right Arrow move between horizontal headers, Up and Down Arrow between vertical ones, and Home and End go to the ends. - Moving
data-selectedandaria-selected, and togglinghiddenon the panels. - The indicator geometry, when the indicator lives in the list.
Helper
import "@codeui/styles/tabs.css";
import { applyStyleProps } from "@codeui/styles";
import { tabsStyleProps } from "@codeui/styles/tabs";
const root = document.querySelector<HTMLElement>("[data-tabs]")!;
const list = root.querySelector<HTMLElement>("[role=tablist]")!;
applyStyleProps(root, tabsStyleProps({ orientation: "vertical", variant: "inline" }));
applyStyleProps(list, tabsStyleProps.list({ orientation: "vertical" }));
// root: class="cui-tabs" data-cui="tabs" data-orientation="vertical" data-variant="inline"