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.
- Basic
- Border Radius Top
- Box
- Bubble
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>
);
}
import { Tabs } from "react-better-html";
function App() {
return (
<Tabs
tabs={[
{ id: "profile", label: "Profile" },
{ id: "security", label: "Security" },
]}
style="borderRadiusTop"
/>
);
}
import { Tabs } from "react-better-html";
function App() {
return (
<Tabs
tabs={[
{ id: "profile", label: "Profile" },
{ id: "security", label: "Security" },
]}
style="box"
/>
);
}
import { Tabs } from "react-better-html";
function App() {
return (
<Tabs
tabs={[
{ id: "profile", label: "Profile" },
{ id: "security", label: "Security" },
]}
style="bubble"
/>
);
}
The selected tab background slides between the tabs.
Common Props
tabs- array of tabs (see Tab object)style-default,borderRadiusTop,boxorbubbledirection-horizontalorvertical(defaults tohorizontal). See Vertical tabsverticalLinePosition- the side of the indicator line when the direction isvertical-leftorright(defaults toleft)iconPosition- the icon position for all tabs -leftorright(defaults toleft). A tab's owniconPositiontakes prioritywidth- the width of the whole component (defaults to100%)height- the height of the whole component (defaults toauto). When set, the tabs and the content scroll inside of ittabWidth- the width for all tabs (defaults tofit-content). A tab's ownwidthtakes priorityalignTabs- the alignment of the tabs and therightElement(defaults toflex-endfor horizontal andstretchfor 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 tabsaccentColor- the color of the indicator and the selected tab (defaults toprimary)name- the name of the tab group. Required when there is more than one<Tabs>on the pagegap- the space between the tabscontentGap- the space between the tabs and the content (defaults totheme.styles.space)borderRadius- the border radius of the tabsnoBottomLine- hides the indicator lineonChange- called with theidof the newly selected tab (also called on mount)- All margin props (
margin,marginBottom, ...)
Tab object
id- the unique identifier of the tablabel- the text of the tab (defaults to theid)hidden- hides the tabicon- the name of the icon of type IconNameiconPosition- whether to put the icon on theleftor on therightside of the label (defaults to theiconPositionprop of<Tabs>)iconColor- the color of the icon (defaults to the color of the label)iconSize- the size of the icon (defaults to16)width- the width of the tab (defaults to thetabWidthprop 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.
- Full Width
- Mixed
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.
import { Tabs } from "react-better-html";
function App() {
return (
<Tabs
tabs={[
{ id: "overview", label: "Overview" },
{ id: "activity", label: "Activity" },
{ id: "settings", label: "Settings", width: 120 },
]}
tabWidth="100%"
/>
);
}
The settings tab is always 120px wide and the other tabs take the rest of the space.
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- theidof the tab this content belongs totabsGroupName- thenameof the<Tabs>grouptabWithDot- 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- theidof the selected tabselectTab()- selects a tab by itsid
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.