Code Group
One code block with tabs for its variants, such as npm, pnpm and yarn. Groups with the same sync-key switch together and remember the choice.
<arc-code-group>Overview
CodeGroup puts several arc-code-block elements behind one set of tabs, so variants of the same thing (an install command for three package managers, or one command for two editions of a product) take the space of one block. Each child becomes a tab, named by its label, or by its filename or language when it has no label. The group draws the frame, the tab strip and one copy button that copies whichever block is showing, and the blocks drop their own headers.
Give related groups the same sync-key and they switch together: pick pnpm in one and every group on the page with that key shows pnpm, matched by tab name. The choice is saved in localStorage under the key, so the next page opens on it too. A group without the tab the reader picked keeps what it had.
The tabs follow the ARIA tabs pattern: one tab stop, the arrow keys move and select, Home and End jump to the ends. Picking a tab fires arc-change with the tab's name and index. Before the component upgrades, and without JavaScript, the blocks show one after another with their own headers, so nothing is hidden. A server render opens on the first tab, and a remembered choice applies once the page is live.
CodeGroup doesn't import arc-code-block, so it's in the main barrel. Import @arclux/arc-ui/code-block for the blocks themselves, and shiki if you want them colored.
Guidelines
Do
- Use it for real alternatives a reader picks one of: package managers, operating systems, editions
- Give every block a short `label`. The tab names are all a reader has to choose by
- Use the same `sync-key` and the same tab names across a page or a whole docs site
- Keep the blocks parallel, so switching tabs changes only what differs
Don't
- Don't use it for steps a reader runs in order. Those go in separate blocks, one after another
- Don't put anything but `arc-code-block` elements inside. Other children are ignored
- Don't give tabs in synced groups different names for the same thing, or they won't follow each other
Features
- The copy button is icon-only
Preview
npm install @arclux/arc-ui
pnpm add @arclux/arc-ui
yarn add @arclux/arc-ui
npm install shiki @shikijs/langs
pnpm add shiki @shikijs/langs
yarn add shiki @shikijs/langs
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-code-group label="Package manager" sync-key="pm">
<arc-code-block label="npm" language="bash" prompt code="npm install @arclux/arc-ui"></arc-code-block>
<arc-code-block label="pnpm" language="bash" prompt code="pnpm add @arclux/arc-ui"></arc-code-block>
<arc-code-block label="yarn" language="bash" prompt code="yarn add @arclux/arc-ui"></arc-code-block>
</arc-code-group>
<script type="module">
import '@arclux/arc-ui/code-block';
import '@arclux/arc-ui/code-group';
</script>import { CodeGroup } from '@arclux/arc-ui-react';
import { CodeBlock } from '@arclux/arc-ui-react/CodeBlock';
export function Install() {
return (
<CodeGroup label="Package manager" syncKey="pm">
<CodeBlock label="npm" language="bash" prompt="" code="npm install @arclux/arc-ui" />
<CodeBlock label="pnpm" language="bash" prompt="" code="pnpm add @arclux/arc-ui" />
</CodeGroup>
);
}<script setup>
import { CodeGroup } from '@arclux/arc-ui-vue';
import { CodeBlock } from '@arclux/arc-ui-vue/CodeBlock';
</script>
<template>
<CodeGroup label="Package manager" sync-key="pm">
<CodeBlock label="npm" language="bash" prompt="" code="npm install @arclux/arc-ui" />
<CodeBlock label="pnpm" language="bash" prompt="" code="pnpm add @arclux/arc-ui" />
</CodeGroup>
</template>API
sync-keystring''- Groups with the same key switch together, matched by tab name, and the choice is remembered in localStorage (
arc-code-group:<key>). labelstring''- Accessible name for the tab list, such as "Package manager".
selectednumber0- Index of the visible block. Out-of-range values fall back to the first block.
Events
arc-changedetail:{ value: string, index: number }- The user picked a tab.
detail.valueis the tab's name,detail.indexits position. Not fired on groups that follow along throughsync-key.