Disclosure›Tabs

core

Tabs

Tabbed content panels with heading-based tab labels

Source

import Markdoc from '@markdoc/markdoc';
import type { Node, RenderableTreeNode } from '@markdoc/markdoc';
const { Ast, Tag } = Markdoc;
import { headingsToList } from '../util.js';
import {
	createComponentRenderable,
	createContentModelSchema,
	bodyOnly,
	asNodes,
	renderNodes,
} from '../lib/index.js';
import { RenderableNodeCursor } from '../lib/renderable.js';
import { pageSectionProperties } from './common.js';

export const tab = createContentModelSchema({
	attributes: {
		name: { type: String, required: true },
		image: { type: String, required: false },
	},
	contentModel: bodyOnly(),
	transform(resolved, attrs, config) {
		let tabCursor = new RenderableNodeCursor<RenderableTreeNode>([]);

		if (attrs.image) {
			tabCursor = tabCursor.concat(
				Markdoc.transform(new Ast.Node('image', { src: attrs.image }), config),
			);
		}

		tabCursor = tabCursor.concat(new Tag('span', {}, [attrs.name ?? '']));

		const panel = renderNodes(resolved.body, config);

		const name = tabCursor.tag('span');
		const image = tabCursor.tag('svg');

		return [
			createComponentRenderable({
				rune: 'tab',
				tag: 'button',
				properties: { image },
				refs: { name },
				children: tabCursor.toArray(),
			}),
			createComponentRenderable({
				rune: 'tab-panel',
				tag: 'div',
				properties: {},
				children: panel.toArray(),
			}),
		];
	},
});

function convertHeadings(nodes: Node[]): Node[] {
	const converted = headingsToList()(nodes);
	const n = converted.length - 1;
	if (!converted[n] || converted[n].type !== 'list') return nodes;
	const tags = converted[n].children.map((item: Node) => {
		const heading = item.children[0];
		const image = Array.from(heading.walk()).find((n: Node) => n.type === 'image');
		const name = Array.from(heading.walk())
			.filter((n: Node) => n.type === 'text')
			.map((t: Node) => t.attributes.content);
		return new Ast.Node(
			'tag',
			{
				name,
				image: image ? image.attributes.src : undefined,
			},
			item.children.slice(1),
			'tab',
		);
	});

	converted.splice(n, 1, ...tags);
	return converted;
}

export const tabGroupSections = {
	preamble: 'preamble',
	eyebrow: 'eyebrow',
	headline: 'title',
	blurb: 'description',
} as const;

export const tabs = createContentModelSchema({
	sections: tabGroupSections,
	attributes: {},
	contentModel: () => ({
		type: 'custom' as const,
		processChildren: (nodes) => convertHeadings(nodes as Node[]),
		description:
			'Converts headings at the specified level to tab tags, extracting tab name and optional image from heading content.',
	}),
	transform(resolved, attrs, config) {
		const allChildren = asNodes(resolved.children);

		const headerAst: Node[] = [];
		const tabAst: Node[] = [];
		for (const child of allChildren) {
			if (child.type === 'tag' && (child as any).tag === 'tab') {
				tabAst.push(child);
			} else if (child.type === 'heading' || child.type === 'paragraph') {
				headerAst.push(child);
			}
		}

		const headerNodes = new RenderableNodeCursor(
			Markdoc.transform(headerAst, config) as RenderableTreeNode[],
		);
		const tabStream = new RenderableNodeCursor(
			Markdoc.transform(tabAst, config) as RenderableTreeNode[],
		);

		const tabItems = tabStream.tag('button').typeof('Tab');
		const panels = tabStream.tag('div').typeof('TabPanel');

		const tabList = tabItems.wrap('div', { role: 'tablist' });
		const panelList = panels.wrap('div');

		const children =
			headerNodes.count() > 0
				? [headerNodes.wrap('header').next(), tabList.next(), panelList.next()]
				: [tabList.next(), panelList.next()];

		return createComponentRenderable({
			rune: 'tab-group',
			tag: 'section',
			class: attrs.class,
			properties: {},
			refs: { ...pageSectionProperties(headerNodes), tabs: tabList, panels: panelList },
			children,
		});
	},
});

View packages/runes/src/tags/tabs.ts on GitHub

Tabbed content panels. Headings become tab labels, the content below each heading becomes the tab panel. To let several panels stay open at once, use accordion; for one collapsible block, use details; to uncover content in order, one step at a time, use reveal.

Heading-based tabs

Headings are automatically converted into tab labels.

{% tabs %}
## npm

```shell
npm install @refrakt-md/runes
```

## yarn

```shell
yarn add @refrakt-md/runes
```

## pnpm

```shell
pnpm add @refrakt-md/runes
```
{% /tabs %}
<section data-rune="tab-group">
  <div role="tablist" data-name="tabs">
    <button data-rune="tab">
      <span data-name="name">
      </span>
    </button>
    <button data-rune="tab">
      <span data-name="name">
      </span>
    </button>
    <button data-rune="tab">
      <span data-name="name">
      </span>
    </button>
  </div>
  <div data-name="panels">
    <div data-rune="tab-panel">
      <div class="rf-codeblock">
        <pre data-language="shell">
          <code data-language="shell">npm install @refrakt-md/runes
</code>
        </pre>
      </div>
    </div>
    <div data-rune="tab-panel">
      <div class="rf-codeblock">
        <pre data-language="shell">
          <code data-language="shell">yarn add @refrakt-md/runes
</code>
        </pre>
      </div>
    </div>
    <div data-rune="tab-panel">
      <div class="rf-codeblock">
        <pre data-language="shell">
          <code data-language="shell">pnpm add @refrakt-md/runes
</code>
        </pre>
      </div>
    </div>
  </div>
</section>
npm install @refrakt-md/runes
yarn add @refrakt-md/runes
pnpm add @refrakt-md/runes
<section class="rf-tabs" data-rune="tab-group" data-density="full">
  <div role="tablist" data-name="tabs" class="rf-tabs__tabs">
    <button class="rf-tab" data-rune="tab" data-density="full" data-state="inactive">
      <span data-name="name" class="rf-tab__name">
        npm
      </span>
    </button>
    <button class="rf-tab" data-rune="tab" data-density="full" data-state="inactive">
      <span data-name="name" class="rf-tab__name">
        yarn
      </span>
    </button>
    <button class="rf-tab" data-rune="tab" data-density="full" data-state="inactive">
      <span data-name="name" class="rf-tab__name">
        pnpm
      </span>
    </button>
  </div>
  <div data-name="panels" class="rf-tabs__panels">
    <div class="rf-tab-panel" data-rune="tab-panel" data-density="full" data-state="inactive">
      <div class="rf-codeblock">
        <pre data-language="shell"><code data-language="shell">npm install @refrakt-md/runes
</code></pre>
      </div>
    </div>
    <div class="rf-tab-panel" data-rune="tab-panel" data-density="full" data-state="inactive">
      <div class="rf-codeblock">
        <pre data-language="shell"><code data-language="shell">yarn add @refrakt-md/runes
</code></pre>
      </div>
    </div>
    <div class="rf-tab-panel" data-rune="tab-panel" data-density="full" data-state="inactive">
      <div class="rf-codeblock">
        <pre data-language="shell"><code data-language="shell">pnpm add @refrakt-md/runes
</code></pre>
      </div>
    </div>
  </div>
</section>

How Markdown is read

You writeIt becomes
A paragraph before the first tab headingA blurb above the tab bar
A heading (the first heading level you use)A tab's label; the content up to the next heading of that level is its panel
A {% tab %} tagA tab, labelled by its name

Explicit tab tags

Use {% tab %} tags for more control over tab names and content.

{% tabs %}
	{% tab name="JavaScript" %}
	Content for the JavaScript tab.
	{% /tab %}

	{% tab name="TypeScript" %}
	Content for the TypeScript tab.
	{% /tab %}
{% /tabs %}
<section data-rune="tab-group">
  <div role="tablist" data-name="tabs">
    <button data-rune="tab">
      <span data-name="name">JavaScript</span>
    </button>
    <button data-rune="tab">
      <span data-name="name">TypeScript</span>
    </button>
  </div>
  <div data-name="panels">
    <div data-rune="tab-panel">
      <p>Content for the JavaScript tab.</p>
    </div>
    <div data-rune="tab-panel">
      <p>Content for the TypeScript tab.</p>
    </div>
  </div>
</section>

Content for the JavaScript tab.

Content for the TypeScript tab.

<section class="rf-tabs" data-rune="tab-group" data-density="full">
  <div role="tablist" data-name="tabs" class="rf-tabs__tabs">
    <button class="rf-tab" data-rune="tab" data-density="full" data-state="inactive">
      <span data-name="name" class="rf-tab__name">JavaScript</span>
    </button>
    <button class="rf-tab" data-rune="tab" data-density="full" data-state="inactive">
      <span data-name="name" class="rf-tab__name">TypeScript</span>
    </button>
  </div>
  <div data-name="panels" class="rf-tabs__panels">
    <div class="rf-tab-panel" data-rune="tab-panel" data-density="full" data-state="inactive">
      <p>Content for the JavaScript tab.</p>
    </div>
    <div class="rf-tab-panel" data-rune="tab-panel" data-density="full" data-state="inactive">
      <p>Content for the TypeScript tab.</p>
    </div>
  </div>
</section>

Attributes

Universal attributes

bg

The background layer: image, video, gradient, flat overlay wash and legibility scrim, in a single injected layer behind the rune's content.

AttributeTypeRequiredDescription
bgstring—Background preset applied to this block
bg-fromstring—Gradient start colour — a semantic token name (→ var(--rf-color-*))
bg-gradient"to-t" | "to-b" | "to-l" | "to-r" | "to-tr" | "to-br" | "to-bl" | "to-tl"—Gradient direction (bounded named set)
bg-gradient-type"linear" | "radial" | "conic"—Gradient type
bg-tostring—Gradient end colour — a semantic token name
bg-viastring—Optional middle gradient stop — a semantic token name
scrim"top" | "bottom" | "left" | "right" | "none"—Scrim direction (heaviest edge); presence turns the scrim on, "none" opts out of the default cover scrim
scrim-blur"none" | "sm" | "md" | "lg"—Frost scrim blur amount
scrim-strength"sm" | "md" | "lg"—Gradient scrim strength
scrim-tone"dark" | "light"—Whether the scrim darkens (for light text) or lightens (for dark text)
scrim-type"gradient" | "frost"—Scrim treatment: gradient (default) or frost (backdrop blur)
elevation

How far the rune sits off the page, from sunken to overlay. The theme decides what each step looks like.

AttributeTypeRequiredDescription
elevation"sunken" | "flush" | "flat" | "raised" | "floating" | "overlay" | "none" | "sm" | "md" | "lg"—Surface depth, from sunken to overlay. Deprecated: none, sm, md and lg (read as flat, raised, raised and floating)
inset

Internal padding override.

AttributeTypeRequiredDescription
inset"flush" | "tight" | "default" | "loose" | "breathe"—Inner padding of this block
motion

Scroll-reveal entrance. You choose its character; the theme decides how it moves.

AttributeTypeRequiredDescription
reveal"none" | "fade" | "slide" | "scale" | "blur"—Scroll-reveal entrance character (none|fade|slide|scale|blur); the theme owns the choreography
staggerboolean—Cascade this block's items in as it reveals (no-op on single-child runes)
prominence

Header emphasis. Scales a rune's header, or its featured quotation; the theme decides the type size for each step.

AttributeTypeRequiredDescription
prominence"quiet" | "normal" | "prominent" | "display" | "marquee"—Emphasis of a page-section header or a featured quotation (only on runes that have one)
spacing

Block-level rhythm override.

AttributeTypeRequiredDescription
spacing"flush" | "tight" | "default" | "loose" | "breathe"—Vertical spacing above and below this block
substrate

Generated pattern fills behind the content, drawn by the theme.

AttributeTypeRequiredDescription
substrate"dots" | "grid" | "lines" | "cross" | "checker" | "none"—Generated surface pattern
substrate-fill"inherit" | "inset"—Surface fill the pattern sits on (full colour stays with tint)
substrate-opacity"sm" | "md" | "lg"—Pattern ink strength
substrate-size"sm" | "md" | "lg"—Pattern cell size
substrate-target"self" | "media"—Which surface the pattern fills (overrides the rune/theme default)
tint

Per-rune colour override: a named tint from the theme registry, with inline per-token overrides layered on top.

AttributeTypeRequiredDescription
tintstring—Color tint preset applied to this block
tint-mode"auto" | "dark" | "light"—Whether the tint adapts to auto, dark, or light mode
width

The track a block rune occupies.

AttributeTypeRequiredDescription
width"compact" | "narrow" | "content" | "wide" | "full"—Maximum width constraint for this block
Not availableWhy
dropcap, readingthis rune declares no prose body
framethis rune declares neither a `frameTarget` nor a media section

tab

AttributeTypeRequiredDescription
namestring✓
imagestring—

Universal attributes

bg

The background layer: image, video, gradient, flat overlay wash and legibility scrim, in a single injected layer behind the rune's content.

AttributeTypeRequiredDescription
bgstring—Background preset applied to this block
bg-fromstring—Gradient start colour — a semantic token name (→ var(--rf-color-*))
bg-gradient"to-t" | "to-b" | "to-l" | "to-r" | "to-tr" | "to-br" | "to-bl" | "to-tl"—Gradient direction (bounded named set)
bg-gradient-type"linear" | "radial" | "conic"—Gradient type
bg-tostring—Gradient end colour — a semantic token name
bg-viastring—Optional middle gradient stop — a semantic token name
scrim"top" | "bottom" | "left" | "right" | "none"—Scrim direction (heaviest edge); presence turns the scrim on, "none" opts out of the default cover scrim
scrim-blur"none" | "sm" | "md" | "lg"—Frost scrim blur amount
scrim-strength"sm" | "md" | "lg"—Gradient scrim strength
scrim-tone"dark" | "light"—Whether the scrim darkens (for light text) or lightens (for dark text)
scrim-type"gradient" | "frost"—Scrim treatment: gradient (default) or frost (backdrop blur)
elevation

How far the rune sits off the page, from sunken to overlay. The theme decides what each step looks like.

AttributeTypeRequiredDescription
elevation"sunken" | "flush" | "flat" | "raised" | "floating" | "overlay" | "none" | "sm" | "md" | "lg"—Surface depth, from sunken to overlay. Deprecated: none, sm, md and lg (read as flat, raised, raised and floating)
inset

Internal padding override.

AttributeTypeRequiredDescription
inset"flush" | "tight" | "default" | "loose" | "breathe"—Inner padding of this block
motion

Scroll-reveal entrance. You choose its character; the theme decides how it moves.

AttributeTypeRequiredDescription
reveal"none" | "fade" | "slide" | "scale" | "blur"—Scroll-reveal entrance character (none|fade|slide|scale|blur); the theme owns the choreography
staggerboolean—Cascade this block's items in as it reveals (no-op on single-child runes)
spacing

Block-level rhythm override.

AttributeTypeRequiredDescription
spacing"flush" | "tight" | "default" | "loose" | "breathe"—Vertical spacing above and below this block
substrate

Generated pattern fills behind the content, drawn by the theme.

AttributeTypeRequiredDescription
substrate"dots" | "grid" | "lines" | "cross" | "checker" | "none"—Generated surface pattern
substrate-fill"inherit" | "inset"—Surface fill the pattern sits on (full colour stays with tint)
substrate-opacity"sm" | "md" | "lg"—Pattern ink strength
substrate-size"sm" | "md" | "lg"—Pattern cell size
substrate-target"self" | "media"—Which surface the pattern fills (overrides the rune/theme default)
tint

Per-rune colour override: a named tint from the theme registry, with inline per-token overrides layered on top.

AttributeTypeRequiredDescription
tintstring—Color tint preset applied to this block
tint-mode"auto" | "dark" | "light"—Whether the tint adapts to auto, dark, or light mode
width

The track a block rune occupies.

AttributeTypeRequiredDescription
width"compact" | "narrow" | "content" | "wide" | "full"—Maximum width constraint for this block
Not availableWhy
dropcap, readingthis rune declares no prose body
framethis rune declares neither a `frameTarget` nor a media section
prominencethis rune has no page-section header or featured quotation