Skip to content

Sub Tabs

An in page tab list with tab panels. A panel is built the first time its tab opens.

Decision

When Use
Splitting one page into sections the reader switches between Sub Tabs
The dashboard tab bar setTabs, on Tabs

Usage

import { createSubTabs } from './configurationpage?name=myplugin_stolon.js';

var tabs = createSubTabs({
    tabs: [{ id: 'general', label: 'General' }, { id: 'logs', label: 'Logs' }],
    render: function (tab, panel) { return buildPanel(tab.id); }
});
container.appendChild(tabs.element);

Classes: stolon-subtabs, stolon-subtab-list, stolon-subtab, stolon-subtab-panel. The handle is { element, getValue, setValue, setItems, getPanel, destroy }.

States and Modifiers

  • Left and Right move and select, wrapping and skipping a disabled tab. Home and End jump to the ends.
  • Only the selected tab has tabindex="0".
  • render runs once, the first time a tab opens, and returns a node or an escaped HTML string, or fills the panel itself.
  • A tab may carry plain text for its panel instead of a render.
  • An id is read as a string everywhere, so 2 and '2' name the same tab.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createSubTabs

function createSubTabs(options?: SubTabsOptions): SubTabsHandle

An in-page tab list with tab panels. Left and Right move between tabs, Home and End jump to the ends, and the focused tab is the selected one. A panel is built the first time its tab opens.

Option Type What it does
tabs SubTab[] array of { id, label, text, disabled } where text is plain text for the panel [optional]
value string | number id of the tab to open first (default the first enabled tab)
label string aria-label of the tab list (default 'Sections')
render (tab: SubTab, panel: HTMLElement): Node | string | void function(tab, panel) called once, the first time the tab opens, that returns a node or an escaped HTML string for the panel or fills the panel itself [optional]
onChange (id: string, tab: SubTab): void function(id, tab) called when the selected tab changes [optional]
Handle member Type
getValue (): string | null
setValue (id: string | number): void
setItems (next: SubTab[], value?: string | number): void
getPanel (id: string | number): HTMLElement | null
destroy (): void
Type What it is
SubTab One tab of a sub tab list. text is plain text for its panel.
Class What it looks like
stolon-subtabs In-page tab list over its panels, which scrolls sideways when the tabs do not fit
stolon-subtab One tab, underlined in the accent when active
stolon-subtab-panel Panel under the tabs, which takes focus without a ring unless a keyboard user lands on it

Example

Example Code

<!-- title: Sub Tabs -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
mount.appendChild(base.createSubTabs({
    tabs: [{ id: 'general', label: 'General', text: 'General settings live here.' }, { id: 'advanced', label: 'Advanced' }, { id: 'off', label: 'Disabled', disabled: true }, { id: 'logs', label: 'Logs' }],
    render: (tab) => tab.id === 'general' ? null : '<p style="margin:0">Built the first time ' + base.escapeHtml(tab.label) + ' opened.</p>'
}).element);
</script>