close
  • English
  • Tabs/Tab

    Tabs and Tab switch between multiple content panes.

    Usage

    Tab 1
    Tab 2
    Tab 1 content
    index.mdx
    import { Tab, Tabs } from '@rspress/core/theme';
    
    <Tabs>
      <Tab label="Tab 1">Tab 1 content</Tab>
      <Tab label="Tab 2">Tab 2 content</Tab>
    </Tabs>

    Code blocks

    Tab 1
    Tab 2
    src/index.mjs
    import foo from 'foo';
    import bar from 'bar';
    index.mdx
    import { Tab, Tabs } from '@rspress/core/theme';
    
    <Tabs>
    <Tab label="Tab 1">
    
    ```tsx title="src/index.mjs"
    import foo from 'foo';
    import bar from 'bar';
    ```
    
    </Tab>
    <Tab label="Tab 2">
    
    ```tsx title="src/index.cjs"
    const foo = require('foo');
    const bar = require('bar');
    ```
    
    </Tab>
    </Tabs>

    Group synchronization

    Use the same groupId to synchronize the active tab across multiple Tabs.

    npm
    pnpm
    npm install rspress
    npm
    pnpm
    npm run dev
    index.mdx
    import { Tab, Tabs } from '@rspress/core/theme';
    
    <Tabs groupId="package-manager">
      <Tab label="npm">npm install rspress</Tab>
      <Tab label="pnpm">pnpm add rspress</Tab>
    </Tabs>
    
    <Tabs groupId="package-manager">
      <Tab label="npm">npm run dev</Tab>
      <Tab label="pnpm">pnpm dev</Tab>
    </Tabs>

    Custom labels

    Use label on each Tab to render custom tab labels.

    GitHub
    GitLab

    GitHub repository settings

    index.mdx
    import {
      IconGithub,
      IconGitlab,
      SvgWrapper,
      Tab,
      Tabs,
    } from '@rspress/core/theme';
    
    <Tabs>
      <Tab
        label={
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
            <SvgWrapper icon={IconGithub} width={16} height={16} />
            GitHub
          </span>
        }
      >
        GitHub repository settings
      </Tab>
      <Tab
        label={
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6 }}>
            <SvgWrapper icon={IconGitlab} width={16} height={16} />
            GitLab
          </span>
        }
      >
        GitLab project settings
      </Tab>
    </Tabs>

    Separate labels and content

    Use values to define tab labels separately from panel content. Each item provides a value, and the matching <Tab value="..."> supplies the content. Use defaultValue to preselect a tab by value.

    Apple
    Orange
    Banana
    This is an apple 🍎
    index.mdx
    import { Tab, Tabs } from '@rspress/core/theme';
    
    <Tabs
      defaultValue="apple"
      values={[
        { label: 'Apple', value: 'apple' },
        { label: 'Orange', value: 'orange' },
        { label: 'Banana', value: 'banana' },
      ]}
    >
      <Tab value="apple">This is an apple 🍎</Tab>
      <Tab value="orange">This is an orange 🍊</Tab>
      <Tab value="banana">This is a banana 🍌</Tab>
    </Tabs>

    Props

    interface TabsProps {
      children: React.ReactNode;
      values?: Array<{ label: React.ReactNode; value: string }>;
      defaultIndex?: number;
      defaultValue?: string;
      groupId?: string;
      tabPosition?: 'left' | 'center';
    }
    
    interface TabProps {
      label?: React.ReactNode;
      value?: string;
      children: React.ReactNode;
    }

    label accepts any renderable React node, so it can include icons or custom markup. Use defaultIndex to preselect a tab by index, or defaultValue to preselect a tab by value; groupId syncs selection across multiple Tabs; tabPosition sets list alignment.