1. Components
  2. Navigation
  3. Tabs

Tabs

Tabs organize content into multiple sections and allow users to navigate between them

Overview content
<Tabs>
  <TabList>
    <Tab id="overview">Overview</Tab>
    <Tab id="usage">Usage</Tab>
    <Tab id="settings">Settings</Tab>
  </TabList>
  <TabPanel id="overview">Overview content</TabPanel>
  <TabPanel id="usage">Usage content</TabPanel>
  <TabPanel id="settings">Settings content</TabPanel>
</Tabs>

Installation

npx dotui-cli@latest add tabs

Controlled

Use the selectedKey and onSelectionChange props to control the selected tab.

Overview content

Selected tab: overview

const [selectedTab, setSelectedTab] = React.useState<Key>("overview");
return (
  <Tabs selectedKey={selectedTab} onSelectionChange={setSelectedTab}>
    <TabList>
      <Tab id="overview">Overview</Tab>
      <Tab id="usage">Usage</Tab>
      <Tab id="settings">Settings</Tab>
    </TabList>
    <TabPanel id="overview">Overview content</TabPanel>
    <TabPanel id="usage">Usage content</TabPanel>
    <TabPanel id="settings">Settings content</TabPanel>
  </Tabs>
)

Options

Variant

Use the variant prop to set the visual style of the tabs.

Overview content
<Tabs variant="solid">
  <TabList>
    <Tab id="overview">Overview</Tab>
    <Tab id="usage">Usage</Tab>
    <Tab id="settings">Settings</Tab>
  </TabList>
  <TabPanel id="overview">Overview content</TabPanel>
  <TabPanel id="usage">Usage content</TabPanel>
  <TabPanel id="settings">Settings content</TabPanel>
</Tabs>

Orientation

Use the orientation prop to set the orientation of the tabs.

Overview content
<Tabs orientation="vertical">
  <TabList>
    <Tab id="overview">Overview</Tab>
    <Tab id="usage">Usage</Tab>
    <Tab id="settings">Settings</Tab>
  </TabList>
  <TabPanel id="overview">Overview content</TabPanel>
  <TabPanel id="usage">Usage content</TabPanel>
  <TabPanel id="settings">Settings content</TabPanel>
</Tabs>

Disabled

Use the isDisabled prop on Tabs or Tab to disable the entire tab list or individual tabs.

Overview content
Overview content
<Tabs isDisabled> {/* All tabs disabled */}
  <TabList>
    <Tab id="overview">Overview</Tab>
    <Tab id="usage">Usage</Tab>
    <Tab id="settings">Settings</Tab>
  </TabList>
  <TabPanel id="overview">Overview content</TabPanel>
  <TabPanel id="usage">Usage content</TabPanel>
  <TabPanel id="settings">Settings content</TabPanel>
</Tabs>

<Tabs>
  <TabList>
    <Tab id="overview">Overview</Tab>
    <Tab id="usage">Usage</Tab>
    <Tab id="settings" isDisabled>Settings</Tab> {/* Individual tab disabled */}
  </TabList>
  <TabPanel id="overview">Overview content</TabPanel>
  <TabPanel id="usage">Usage content</TabPanel>
  <TabPanel id="settings">Settings content</TabPanel>
</Tabs>

Keyboard activation

Prevent keyboard activation of tabs by setting keyboardActivation prop to 'manual'.

Overview content
<Tabs keyboardActivation="manual">
  <TabList>
    <Tab id="overview">Overview</Tab>
    <Tab id="usage">Usage</Tab>
    <Tab id="settings">Settings</Tab>
  </TabList>
  <TabPanel id="overview">Overview content</TabPanel>
  <TabPanel id="usage">Usage content</TabPanel>
  <TabPanel id="settings">Settings content</TabPanel>
</Tabs>

API Reference

Tabs

PropTypeDefaultDescription
variant'solid' | 'underline''underline'The visual style of the tabs.
isDisabledboolean-Whether the TabList is disabled. Shows that a selection exists, but is not available in that circumstance.
disabledKeysIterable<Key>-The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.
selectedKeyKey | null-The currently selected key in the collection (controlled).
defaultSelectedKeyKey-The initial selected key in the collection (uncontrolled).
keyboardActivation'automatic' | 'manual''automaticWhether tabs are activated automatically on focus or manually.
orientation'horizontal' | 'vertical''horizontalThe orientation of the tabs.
childrenReactNode | (values: TabsRenderProps & {defaultChildren: ReactNode | undefined}) => ReactNode-The children of the component. A function may be provided to alter the children based on component state.
classNamestring | (values: TabsRenderProps & {defaultClassName: string | undefined}) => string-The CSS className for the element. A function may be provided to compute the style based on component state.
styleCSSProperties | (values: TabsRenderProps & {defaultStyle: CSSProperties}) => CSSProperties-The inline style for the element. A function may be provided to compute the style based on component state.
EventTypeDescription
onSelectionChange(key: Key) => voidHandler that is called when the selection changes.
Data attributeDescription
[data-orientation="horizontal | vertical"]The orientation of the tabs.

TabList

PropTypeDefaultDescription
dependenciesany[]-Values that should invalidate the item cache when using dynamic collections.
itemsIterable<T>-Item objects in the collection.
childrenReactNode | (values: TabListRenderProps & {defaultChildren: ReactNode | undefined}) => ReactNode-The children of the component. A function may be provided to alter the children based on component state.
classNamestring-The CSS className for the element.
styleCSSProperties | (values: TabListRenderProps & {defaultStyle: CSSProperties}) => CSSProperties-The inline style for the element. A function may be provided to compute the style based on component state.
Data attributeDescription
[data-orientation="horizontal | vertical"]The orientation of the tabs.

Tab

PropTypeDefaultDescription
idKey-The unique id of the tab.
isDisabledboolean-Whether the tab is disabled.
hrefHref-A URL to link to.
hrefLangstring-Hints at the human language of the linked URL.
targetHTMLAttributeAnchorTarget-The target window for the link.
relstring-The relationship between the linked resource and the current page.
downloadboolean | string-Causes the browser to download the linked URL. A string may be provided to suggest a file name.
pingstring-A space-separated list of URLs to ping when the link is followed.
referrerPolicyHTMLAttributeReferrerPolicy-How much of the referrer to send when following the link.
routerOptionsRouterOptions-Options for the configured client side router.
childrenReactNode | (values: TabRenderProps & {defaultChildren: ReactNode | undefined}) => ReactNode-The children of the component. A function may be provided to alter the children based on component state.
classNamestring-The CSS className for the element.
styleCSSProperties | (values: TabRenderProps & {defaultStyle: CSSProperties}) => CSSProperties-The inline style for the element. A function may be provided to compute the style based on component state.
Data attributeDescription
[data-hovered]Whether the tab is currently hovered with a mouse.
[data-pressed]Whether the tab is currently in a pressed state.
[data-selected]Whether the tab is currently selected.
[data-focused]Whether the tab is focused, either via a mouse or keyboard.
[data-focus-visible]Whether the tab is keyboard focused.
[data-disabled]Whether the tab is disabled.

TabPanel

PropTypeDefaultDescription
shouldForceMountbooleanfalseWhether to mount the tab panel in the DOM even when it is not currently selected. Inactive tab panels are inert and cannot be interacted with. They must be styled appropriately so this is clear to the user visually.
childrenReactNode | (values: TabPanelRenderProps & {defaultChildren: ReactNode | undefined}) => ReactNode-The children of the component. A function may be provided to alter the children based on component state.
classNamestring-The CSS className for the element.
styleCSSProperties | (values: TabPanelRenderProps & {defaultStyle: CSSProperties}) => CSSProperties-The inline style for the element. A function may be provided to compute the style based on component state.
Data attributeDescription
[data-focused]Whether the tab panel is focused, either via a mouse or keyboard.
[data-focus-visible]Whether the tab panel is keyboard focused.
[data-inert]Whether the tab panel is currently non-interactive. This occurs when the shouldForceMount prop is true, and the corresponding tab is not selected.

Accessibility

Keyboard interactions

KeyDescription
TabMoves focus into the tabs component and places focus on the selected tab item. If the tabs component is already in focus, moves focus to the next element in the page tab sequence.
ArrowRight ArrowDownMoves focus to the next tab item. If focus is on the last tab item, moves focus to the first tab item.
ArrowLeft ArrowUpMoves focus to the previous tab item. If focus is on the first tab item, moves focus to the last tab item.
Space EnterActivates the tab item in focus.

Last updated on 10/11/2024

dotUI

Bringing singularity to the web.

Built by mehdibha. The source code is available on GitHub.