Tabs
Tabs put equivalent alternatives side by side, such as package managers, operating systems, YAML / TOML / JSON, or an environment variable and its configuration key, so each reader sees the one that applies to them. Do not use them for ordered steps or unrelated content: a reader sees one panel at a time and may never open the others.
There are two forms with the same runtime, markup and keyboard behavior. The
common one is a tab attribute on adjacent code fences (as well as filetree,
gallery and map data fences) or tables. When a panel needs prose, lists or
callouts, use the tabs/tab shortcodes.
Shortest form
Write two fences with tab, separated by nothing but a blank line.
The page itself contains two complete code blocks, each titled with its label, and the browser joins adjacent blocks into a tab set once the page loads. On GitHub, on paper and without JavaScript, readers see both blocks one after the other, so nothing is ever hidden from them.
Groups: links, sync and memory
Add group to the first block, and a value to every block of the set. The
set then gets a shareable URL hash #<group>-<value>, stays in step with other
sets of the same group, and remembers the reader’s choice.
tab is the label people read and value is the identifier in URLs and
storage, matching ^[a-z0-9][a-z0-9_-]*$, so you can reword a label without
breaking links. The pnpm panel above is #pkgmgr-pnpm: open this page with
that hash and it is selected.
Groups move together
The set below uses the same group="pkgmgr", so choosing a package manager in
either set switches the other. The choice is saved in localStorage under
td-tabs:v1:pkgmgr and applies to every pkgmgr set on every page of the
site: a reader who picks pnpm once sees pnpm everywhere.
This set has no yarn panel. When the group switches to a value a set lacks,
that set keeps its current panel, so a set always shows something. On load, a
set takes the first of these that it can use: the URL hash, the stored choice,
the shortcode’s default or the first block, the first tab. A hash selects a
panel without overwriting the reader’s stored choice.
Tables can be tabs too
The same attributes on table attribute lines join adjacent tables into a set.
| Parameter | Default |
|---|---|
shared_buffers |
25% RAM |
max_connections |
100 |
| Parameter | Default |
|---|---|
shared_buffers |
128MB |
max_connections |
100 |
A set holds only fences or only tables; an adjacent fence and table stay separate. To mix them, use the shortcode form.
A label and a filename together
A fence can carry both tab and title: the label goes in the tab bar and
the filename stays in the title bar inside the panel.
A lone block is just a titled block
A set needs at least two adjacent blocks of the same kind. A single block with
tab keeps the label as its title instead of growing a one-tab bar.
Only blank lines may come between the blocks of a set. Anything else ends it:
a paragraph, heading or list, an HTML comment (often a stray
<!-- prettier-ignore-end -->), or a block that declares its own group,
which starts a new set.
Tabs around running text
When a panel needs paragraphs, lists, callouts or several blocks, use the
tabs/tab shortcodes. Each tab body is full Markdown.
The repository ships .github/workflows/; a push to main builds and publishes.
baseURL has to be the repository’s Pages address.
Connect the repository in the Cloudflare dashboard; the build command is:
default picks the initial panel; it must match a child’s value and needs
group. Without group, the theme numbers the tabs tab1, tab2 … itself,
so a value is ignored with a warning, and the set switches locally without
touching the URL or storage.
The shortcode form checks the whole set at build time. The attribute form
checks each block’s own attributes at build time, but blocks only become a set
in the browser, so problems of the set as a whole, such as a later block
without value or two blocks with the same value, show up as warnings in the
browser console.
Output
Only pages that use tabs load tabs.js. Until it runs, and in print, RSS and
browsers without JavaScript, every panel is visible as a titled section with no
tab bar. In the Markdown output the fence form keeps its source fences,
{tab=} included, and each shortcode tab becomes its label in bold followed
by its body.
Parameter reference
Attributes on a fence info line or a table attribute line:
The tabs shortcode:
The tab shortcode:
In a group, a panel’s ID is <group>-<value>. When the same group appears
twice on one page, later sets get -2, -3 … suffixes and a deep link goes
to the first set; ungrouped sets get generated IDs. The storage key is
td-tabs:v1:<group>. A click or key press updates the hash with
replaceState and writes storage, while arriving with a hash only changes the
selection. Left and Right (mirrored on right-to-left pages), Home and End move
to a tab and select it, and focus stays on the tab.
Limits
- Invalid input warns and falls back to something safe, and build warnings
name the source position. Unusable
group,valueordefaultsettings are dropped, stray content insidetabsis ignored, a repeated shortcodevaluekeeps both tabs, and atabswithout children renders nothing. - An attribute-form set in which some block lacks a usable
valueloses its group and switches locally; the theme never invents a value for a group. Adjacent blocks that repeat avaluestay separate titled blocks. - A set holds only fences or only tables. Use the shortcodes to mix prose and code.
- Tabs are not a way to fold content away. To collapse long output, use
> [!DETAILS]; see Callouts. groupnames are shared across the site, so picking pnpm on one page also picks it in everypkgmgrset on other pages. Name groups by meaning, such aspkgmgroros, nevertabs1.
Related
- Code blocks — the rest of the fence attributes (title, copy, line numbers, folding)
- Tables — the rest of the table attribute line
- Callouts — for folding rather than juxtaposing
- Steps — tabs inside a procedure