astro-better-tabs

Tabbed content panels for MDX pages. Renders without JavaScript (CSS-only fallback), then enhances with a small script to handle tab switching without page reloads.

Current version: 0.3.1 --npm

Installation

$ npm install astro-better-tabs

Import the CSS in your global stylesheet:

@import "astro-better-tabs/style.css";

Usage

import Tabs from 'astro-better-tabs/Tabs.astro';
import TabItem from 'astro-better-tabs/TabItem.astro';

<Tabs>
  <TabItem label="npm">
    ```shell-session
    npm install my-package
    ```
  </TabItem>
  <TabItem label="yarn">
    ```shell-session
    yarn add my-package
    ```
  </TabItem>
  <TabItem label="pnpm">
    ```shell-session
    pnpm add my-package
    ```
  </TabItem>
</Tabs>

Live example

$ npm install astro-better-tabs
$ yarn add astro-better-tabs
$ pnpm add astro-better-tabs

Tabs props

PropTypeDefaultDescription
syncKeystringShare tab state across multiple <Tabs> groups with the same key

TabItem props

PropTypeDefaultDescription
labelstring(required)Tab button text

Synchronized tabs

Set the same syncKey on multiple <Tabs> groups to synchronize them. Selecting “yarn” in one group automatically selects “yarn” in all others.

<Tabs syncKey="pkg-manager">
  <TabItem label="npm">npm install foo</TabItem>
  <TabItem label="yarn">yarn add foo</TabItem>
</Tabs>

<Tabs syncKey="pkg-manager">
  <TabItem label="npm">npm install bar</TabItem>
  <TabItem label="yarn">yarn add bar</TabItem>
</Tabs>

CSS-only fallback

Without JavaScript, only the first tab’s content is visible via :checked + .tab-label + .tab-panel { display: block }. JavaScript reorganizes the DOM on load to properly show the selected tab.

Styling

The component exposes CSS custom properties on .tabs-group for independent light and dark mode theming:

PropertyControls
--tabs-bgtab bar background
--tabs-bordercontainer border
--tabs-panel-bgcontent panel background
--tabs-panel-bordercontent panel top border
--tabs-active-bgactive tab label background
--tabs-active-coloractive tab label text color

Override to match your brand:

/* light mode */
.tabs-group {
  --tabs-border:       #e5e7eb;
  --tabs-bg:           #f9fafb;
  --tabs-active-bg:    #ffffff;
  --tabs-active-color: #7c3aed;
}

/* dark mode */
.dark .tabs-group {
  --tabs-border:       #1c0e00;
  --tabs-bg:           #111111;
  --tabs-panel-bg:     #080808;
  --tabs-active-bg:    #080808;
  --tabs-active-color: #fb923c;
}