Color mode

Code Tabs

Groups adjacent code blocks with tab="..." metadata into an accessible tab UI.

日本語

Installation

ts
import { defineConfig } from "@riebeckite/core";
import { codeTabs } from "@riebeckite/plugin-code-tabs";
 
export default defineConfig({
  // ...
  plugins: [codeTabs()],
});

The plugin registers its own style.css and client entry (initCodeTabs).

Config

ts
codeTabs({
  syncTabs: false,
});
Option Type Default Description
syncTabs boolean false When enabled, selecting a label such as TypeScript also selects the same label in other code-tab groups on the page.

Syntax

md
```dart tab="Flutter"
void main() {}
```
 
```ts tab="React"
console.log("Hello");
```

Regular code blocks without tab="..." are not changed.

Grouping rules

Adjacent tab="..." code blocks become one group. Any other content ends the current group.

md
```dart tab="Dart"
```
```ts tab="TypeScript"
```

The two blocks above are one group.

md
```dart tab="Dart"
```
 
Text between blocks.
 
```ts tab="TypeScript"
```

The paragraph separates them into different groups.

Using with code-enhance

@riebeckite/plugin-code-tabs does not import or depend on @riebeckite/plugin-code-enhance. It detects both normal <pre><code> blocks and enhanced rehype-pretty-code figures, then wraps the whole rendered code block as a panel. Syntax highlighting, filename headers, copy buttons, wrap, collapse, line numbers, and diff/highlight markup are preserved as much as possible.

Place codeTabs() after codeEnhance() if both plugins are enabled so tabs wrap the enhanced code block output.

Accessibility

The generated HTML uses role="tablist", role="tab", and role="tabpanel". The client supports click, ArrowLeft, ArrowRight, Home, End, Enter, and Space, and keeps aria-selected and tabindex in sync. Without JavaScript, all panels remain visible so every code block can still be read.

Exports

  • codeTabs(options?) — plugin factory
  • rehypeCodeTabs(options?) — rehype transform
  • initCodeTabs(options?) — client initializer
  • Types: CodeTabsOptions, CodeTabsClientOptions

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/code-tabs/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Code Tabs
4 +
5 + Groups adjacent code blocks with `tab="..."` metadata into an accessible tab UI.
6 +
7 + [日本語](./code-tabs.md)
8 +
9 + ## Installation
10 +
11 + ```ts
12 + import { defineConfig } from "@riebeckite/core";
13 + import { codeTabs } from "@riebeckite/plugin-code-tabs";
14 +
15 + export default defineConfig({
16 + // ...
17 + plugins: [codeTabs()],
18 + });
19 + ```
20 +
21 + The plugin registers its own `style.css` and client entry (`initCodeTabs`).
22 +
23 + ## Config
24 +
25 + ```ts
26 + codeTabs({
27 + syncTabs: false,
28 + });
29 + ```
30 +
31 + | Option | Type | Default | Description |
32 + | ------ | ---- | ------- | ----------- |
33 + | `syncTabs` | `boolean` | `false` | When enabled, selecting a label such as `TypeScript` also selects the same label in other code-tab groups on the page. |
34 +
35 + ## Syntax
36 +
37 + ````md
38 + ```dart tab="Flutter"
39 + void main() {}
40 + ```
41 +
42 + ```ts tab="React"
43 + console.log("Hello");
44 + ```
45 + ````
46 +
47 + Regular code blocks without `tab="..."` are not changed.
48 +
49 + ## Grouping rules
50 +
51 + Adjacent `tab="..."` code blocks become one group. Any other content ends the
52 + current group.
53 +
54 + ````md
55 + ```dart tab="Dart"
56 + ```
57 + ```ts tab="TypeScript"
58 + ```
59 + ````
60 +
61 + The two blocks above are one group.
62 +
63 + ````md
64 + ```dart tab="Dart"
65 + ```
66 +
67 + Text between blocks.
68 +
69 + ```ts tab="TypeScript"
70 + ```
71 + ````
72 +
73 + The paragraph separates them into different groups.
74 +
75 + ## Using with code-enhance
76 +
77 + `@riebeckite/plugin-code-tabs` does not import or depend on
78 + `@riebeckite/plugin-code-enhance`. It detects both normal `<pre><code>` blocks
79 + and enhanced `rehype-pretty-code` figures, then wraps the whole rendered code
80 + block as a panel. Syntax highlighting, filename headers, copy buttons, wrap,
81 + collapse, line numbers, and diff/highlight markup are preserved as much as
82 + possible.
83 +
84 + Place `codeTabs()` after `codeEnhance()` if both plugins are enabled so tabs wrap
85 + the enhanced code block output.
86 +
87 + ## Accessibility
88 +
89 + The generated HTML uses `role="tablist"`, `role="tab"`, and `role="tabpanel"`.
90 + The client supports click, `ArrowLeft`, `ArrowRight`, `Home`, `End`, `Enter`, and
91 + `Space`, and keeps `aria-selected` and `tabindex` in sync. Without JavaScript,
92 + all panels remain visible so every code block can still be read.
93 +
94 + ## Exports
95 +
96 + - `codeTabs(options?)` — plugin factory
97 + - `rehypeCodeTabs(options?)` — rehype transform
98 + - `initCodeTabs(options?)` — client initializer
99 + - Types: `CodeTabsOptions`, `CodeTabsClientOptions`
100 +
101 + ## See also
102 +
103 + - [Plugin guide](../reference/plugin-api.en.md)
104 +