Skip to main content

Tabs Component

The <Tabs> component renders a list of tabs with an animated indicator and shows the content of the selected tab with the <Tabs.content> subcomponent.

Basic Usage​

The component requires a tabs prop. Every tab is rendered with its label (or id if there is no label). The content of each tab is wrapped in <Tabs.content> with the matching tabId.

import { Tabs, Text } from "react-better-html";

function App() {
return (
<Tabs
tabs={[
{ id: "profile", label: "Profile" },
{ id: "security", label: "Security" },
{ id: "billing", label: "Billing" },
]}
>
<Tabs.content tabId="profile">
<Text>Profile settings</Text>
</Tabs.content>
<Tabs.content tabId="security">
<Text>Security settings</Text>
</Tabs.content>
<Tabs.content tabId="billing">
<Text>Billing settings</Text>
</Tabs.content>
</Tabs>
);
}

Common Props​

  • tabs - array of tabs (see Tab object)
  • style - default, borderRadiusTop, box or bubble
  • direction - horizontal or vertical (defaults to horizontal). See Vertical tabs
  • verticalLinePosition - the side of the indicator line when the direction is vertical - left or right (defaults to left)
  • iconPosition - the icon position for all tabs - left or right (defaults to left). A tab's own iconPosition takes priority
  • width - the width of the whole component (defaults to 100%)
  • height - the height of the whole component (defaults to auto). When set, the tabs and the content scroll inside of it
  • tabWidth - the width for all tabs (defaults to fit-content). A tab's own width takes priority
  • alignTabs - the alignment of the tabs and the rightElement (defaults to flex-end for horizontal and stretch for vertical direction)
  • rightElement - an element rendered on the right side of the tabs row (e.g. a button). With vertical direction it is rendered under the tabs
  • accentColor - the color of the indicator and the selected tab (defaults to primary)
  • name - the name of the tab group. Required when there is more than one <Tabs> on the page
  • gap - the space between the tabs
  • contentGap - the space between the tabs and the content (defaults to theme.styles.space)
  • borderRadius - the border radius of the tabs
  • noBottomLine - hides the indicator line
  • onChange - called with the id of the newly selected tab (also called on mount)
  • All margin props (margin, marginBottom, ...)

Tab object​

  • id - the unique identifier of the tab
  • label - the text of the tab (defaults to the id)
  • hidden - hides the tab
  • icon - the name of the icon of type IconName
  • iconPosition - whether to put the icon on the left or on the right side of the label (defaults to the iconPosition prop of <Tabs>)
  • iconColor - the color of the icon (defaults to the color of the label)
  • iconSize - the size of the icon (defaults to 16)
  • width - the width of the tab (defaults to the tabWidth prop of <Tabs>)

Icons​

Every tab can have an icon next to its label. The icon follows the color of the label, so it changes together with the selected state.

import { Tabs } from "react-better-html";

function App() {
return (
<Tabs
tabs={[
{ id: "search", label: "Search", icon: "magnifyingGlass" },
{ id: "filters", label: "Filters", icon: "filter" },
{ id: "info", label: "Info", icon: "infoI", iconPosition: "left" }
]}
iconPosition="right"
/>
);
}

The iconPosition prop of <Tabs> is applied to all tabs. A tab with its own iconPosition always uses it instead.

Tab width​

By default every tab is as wide as its content. The width can be set for all tabs with the tabWidth prop or for a single tab with the width field of the tab. A tab's own width always takes priority.

import { Tabs } from "react-better-html";

function App() {
return (
<Tabs
tabs={[
{ id: "day", label: "Day" },
{ id: "week", label: "Week" },
{ id: "month", label: "Month" },
{ id: "year", label: "Year" },
]}
tabWidth="100%"
/>
);
}

The tabs share the available horizontal space equally - 4 tabs with "100%" width are 25% wide each.

A tab with "100%" width never gets smaller than its content. When the container is smaller than the tabs, the tabs row becomes horizontally scrollable.

Vertical tabs​

With direction="vertical" the tabs are rendered in a column and the content is shown on the right side of them. All styles are supported. The indicator line is on the left side of the tabs by default and can be moved to the right side with verticalLinePosition="right". The borderRadiusTop style rounds the corners on the opposite side of the line.

import { Tabs, Text } from "react-better-html";

function App() {
return (
<Tabs
direction="vertical"
style="bubble"
tabs={[
{ id: "profile", label: "Profile", icon: "infoI" },
{ id: "security", label: "Security", icon: "filter" },
{ id: "billing", label: "Billing", icon: "magnifyingGlass" }
]}
>
<Tabs.content tabId="profile">
<Text>Profile settings</Text>
</Tabs.content>
<Tabs.content tabId="security">
<Text>Security settings</Text>
</Tabs.content>
<Tabs.content tabId="billing">
<Text>Billing settings</Text>
</Tabs.content>
</Tabs>
);
}

By default the component is as high as its content. Set the height prop to limit it - the tabs column and the content then scroll inside of it.

<Tabs
direction="vertical"
height={400}
tabs={tabs}
>
...
</Tabs>

All tabs have the same width - the width of the widest tab. Use tabWidth (e.g. tabWidth={200}) to set a fixed width of the tabs column. The labels are aligned to the left.

Hidden tabs​

A tab with hidden: true is not rendered. If the selected tab gets hidden, the first visible tab is selected.

import { Tabs } from "react-better-html";

function App({ isAdmin }) {
return (
<Tabs
tabs={[
{ id: "overview", label: "Overview" },
{ id: "admin", label: "Admin", hidden: !isAdmin }
]}
/>
);
}

Multiple tab groups​

<Tabs.content> is connected to its <Tabs> by the group name. It does not need to be a child of the <Tabs> component - it can be anywhere in the tree.

import { Div, Tabs, Text } from "react-better-html";

function App() {
return (
<Div.column gap={20}>
<Tabs name="period" tabs={[{ id: "week" }, { id: "month" }]} />
<Tabs.content tabsGroupName="period" tabId="week" isInitialTab>
<Text>This week</Text>
</Tabs.content>
<Tabs.content tabsGroupName="period" tabId="month">
<Text>This month</Text>
</Tabs.content>
</Div.column>
);
}

Tabs.content​

  • tabId - the id of the tab this content belongs to
  • tabsGroupName - the name of the <Tabs> group
  • tabWithDot - renders a small dot on the tab (e.g. for unread items or errors inside the tab)
  • isInitialTab - renders the content before the <Tabs> group has registered (avoids a flash of empty content)
<Tabs.content
tabId="security"
tabWithDot={hasSecurityWarnings}
>
...
</Tabs.content>

Ref API​

The tabs can be controlled using a ref:

type TabsRef = {
selectedTab: string;
selectTab: (tabId: string) => void;
};
  • selectedTab - the id of the selected tab
  • selectTab() - selects a tab by its id

Used With Plugins​

With the react-router-dom plugin the selected tab is saved in the URL as a query parameter (?tab=security or ?{name}=security when name is passed) and restored when the page is opened. Pass keepUrlHistory={false} to replace the URL instead of adding a new history entry on every tab change.