Tabs organize content into multiple sections and allow users to navigate between them. The content under the set of tabs should be related and form a coherent unit.
Content
TabList follows the Collection Components API, accepting both static and dynamic collections. This example shows a dynamic collection, passing a list of objects to the items prop, and a function to render the children.
import {Tabs, TabList, Tab, TabPanel, Collection} from '@react-spectrum/s2/Tabs';
import {ActionButtonGroup, ActionButton} from '@react-spectrum/s2/ActionButtonGroup';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import {useState} from 'react';
import AddCircle from '@react-spectrum/s2/icons/AddCircle';
import RemoveCircle from '@react-spectrum/s2/icons/RemoveCircle';
function Example() {
let [tabs, setTabs] = useState([
{id: 1, title: 'Tab 1', content: 'Tab body 1'},
{id: 2, title: 'Tab 2', content: 'Tab body 2'},
{id: 3, title: 'Tab 3', content: 'Tab body 3'}
]);
let addTab = () => {
setTabs(tabs => [
...tabs,
{
id: tabs.length + 1,
title: `Tab ${tabs.length + 1}`,
content: `Tab body ${tabs.length + 1}`
}
]);
};
let removeTab = () => {
if (tabs.length > 1) {
setTabs(tabs => tabs.slice(0, -1));
}
};
return (
<Tabs aria-label="Tabs" styles={style({width: 'full'})}>
<div className={style({display: 'flex', alignItems: 'center'})}>
<TabList
items={tabs}
styles={style({flexShrink: 1, flexGrow: 1, flexBasis: 'auto'})}>
{item => <Tab>{item.title}</Tab>}
</TabList>
<ActionButtonGroup density="compact" size="S">
<ActionButton onPress={addTab} aria-label="Add tab">
<AddCircle />
</ActionButton>
<ActionButton onPress={removeTab} aria-label="Remove tab">
<RemoveCircle />
</ActionButton>
</ActionButtonGroup>
</div>
<Collection items={tabs}>
{item => <TabPanel>{item.content}</TabPanel>}
</Collection>
</Tabs>
)
}
Icons
Tag supports icons and text as children. Text is always required, but can be hidden when the tabs are expanded using the labelBehavior prop.
Overflow behavior
Horizontal tabs automatically collapse into a Picker when space is limited.
import {Tabs, TabList, Tab, TabPanel} from '@react-spectrum/s2/Tabs';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
<div
className={style({
width: 320,
maxWidth: 'full',
padding: 16,
boxSizing: 'border-box',
borderWidth: 1,
borderStyle: 'solid',
borderColor: 'gray-300',
borderRadius: 'default',
overflow: 'hidden',
resize: 'horizontal'
})}>
<Tabs aria-label="Tabs">
<TabList>
<Tab id="home">Home</Tab>
<Tab id="profile">Profile</Tab>
<Tab id="contact">Contact</Tab>
<Tab id="about">About</Tab>
</TabList>
<TabPanel id="home">
Welcome home
</TabPanel>
<TabPanel id="profile">
View your profile
</TabPanel>
<TabPanel id="contact">
Find your contacts
</TabPanel>
<TabPanel id="about">
Learn more
</TabPanel>
</Tabs>
</div>
Selection
Use the defaultSelectedKey or selectedKey prop to set the selected tab. The selected key corresponds to the id prop of a <Tab>. Tabs can be disabled with the isDisabled prop. See the selection guide for more details.
Selected tab: files
import {Tabs, TabList, Tab, TabPanel, type Key} from '@react-spectrum/s2/Tabs';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import Home from '@react-spectrum/s2/illustrations/gradient/generic2/Home';
import Folder from '@react-spectrum/s2/illustrations/gradient/generic2/FolderOpen';
import Search from '@react-spectrum/s2/illustrations/gradient/generic2/Search';
import Settings from '@react-spectrum/s2/illustrations/gradient/generic1/GearSetting';
import {useState} from 'react';
function Example() {
let [tab, setTab] = useState<Key>("files");
return (
<div>
<Tabs
aria-label="Tabs"
selectedKey={tab}
onSelectionChange={setTab}
>
<TabList aria-label="Tabs">
<Tab id="home">Home</Tab>
<Tab id="files">Files</Tab>
<Tab id="search" isDisabled>Search</Tab>
<Tab id="settings">Settings</Tab>
</TabList>
<TabPanel id="home">
<div className={style({size: 'full', display: 'flex', alignItems: 'center', justifyContent: 'center'})}>
<Home />
</div>
</TabPanel>
<TabPanel id="files">
<div className={style({size: 'full', display: 'flex', alignItems: 'center', justifyContent: 'center'})}>
<Folder />
</div>
</TabPanel>
<TabPanel id="search">
<div className={style({size: 'full', display: 'flex', alignItems: 'center', justifyContent: 'center'})}>
<Search />
</div>
</TabPanel>
<TabPanel id="settings">
<div className={style({size: 'full', display: 'flex', alignItems: 'center', justifyContent: 'center'})}>
<Settings />
</div>
</TabPanel>
</Tabs>
<p>Selected tab: {tab}</p>
</div>
);
}
API
<Tabs>
<TabList>
<Tab>
<Icon />
<Text />
</Tab>
</TabList>
<TabPanel />
</Tabs>
Tabs
Tabs organize content into multiple sections and allow users to navigate between them. The content under the set of tabs should be related and form a coherent unit.
| Name | Type | Default |
|---|---|---|
styles | StylesPropWithHeight | Default: — |
Spectrum-defined styles, returned by the | ||
children | ReactNode | Default: — |
The content to display in the tabs. | ||
density | 'compact' | 'regular' | Default: 'regular'
|
The amount of space between the tabs. | ||
labelBehavior | 'show' | 'hide' | Default: 'show'
|
Defines if the text within the tabs should be hidden and only the icon should be shown. The text is always visible when the item is collapsed into a picker. | ||
keyboardActivation | 'automatic' | 'manual' | Default: 'automatic'
|
Whether tabs are activated automatically on focus or manually. | ||
orientation | Orientation | Default: 'horizontal'
|
The orientation of the tabs. | ||
isDisabled | boolean | Default: — |
Whether the TabList is disabled. Shows that a selection exists, but is not available in that circumstance. | ||
selectedKey | Key | Default: — |
The currently selected key in the collection (controlled). | ||
defaultSelectedKey | Key | Default: — |
The initial selected keys in the collection (uncontrolled). | ||
onSelectionChange | | Default: — |
Handler that is called when the selection changes. | ||
disabledKeys | Iterable | Default: — |
The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. | ||
shouldFocusWrap | boolean | Default: true
|
Whether focus should wrap around when the end/start is reached. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||
TabList
| Name | Type | |
|---|---|---|
styles | StylesProp | |
Spectrum-defined styles, returned by the | ||
children | ReactNode | | |
The content to display in the tablist. | ||
items | Iterable | |
Item objects in the collection. | ||
dependencies | ReadonlyArray | |
Values that should invalidate the item cache when using dynamic collections. | ||
aria-describedby | string | |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | |
Sets inline style for the
element. Only use as a last resort. Use the | ||
Tab
| Name | Type | |
|---|---|---|
children | ReactNode | |
The content to display in the tab. | ||
id | Key | |
The unique id of the tab. | ||
isDisabled | boolean | |
Whether the tab is disabled. | ||
styles | StylesProp | |
Spectrum-defined styles, returned by the | ||
onHoverStart | | |
Handler that is called when a hover interaction starts. | ||
onHoverEnd | | |
Handler that is called when a hover interaction ends. | ||
onHoverChange | | |
Handler that is called when the hover state changes. | ||
onFocus | | |
Handler that is called when the element receives focus. | ||
onBlur | | |
Handler that is called when the element loses focus. | ||
onFocusChange | | |
Handler that is called when the element's focus status changes. | ||
onPress | | |
Handler that is called when the press is released over the target. | ||
onPressStart | | |
Handler that is called when a press interaction starts. | ||
onPressEnd | | |
Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. | ||
onPressChange | | |
Handler that is called when the press state changes. | ||
onPressUp | | |
Handler that is called when a press is released over the target, regardless of whether it started on the target or not. | ||
href | Href | |
A URL to link to. See MDN. | ||
hrefLang | string | |
Hints at the human language of the linked URL. SeeMDN. | ||
target | HTMLAttributeAnchorTarget | |
The target window for the link. See MDN. | ||
rel | string | |
The relationship between the linked resource and the current page. See MDN. | ||
download | boolean | string | |
Causes the browser to download the linked URL. A string may be provided to suggest a file name. See MDN. | ||
ping | string | |
A space-separated list of URLs to ping when the link is followed. See MDN. | ||
referrerPolicy | HTMLAttributeReferrerPolicy | |
How much of the referrer to send when following the link. See MDN. | ||
routerOptions | RouterOptions | |
Options for the configured client side router. | ||
aria-label | string | |
Defines a string value that labels the current element. | ||
aria-labelledby | string | |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | |
Sets inline style for the
element. Only use as a last resort. Use the | ||
TabPanel
| Name | Type | Default |
|---|---|---|
styles | StylesPropWithHeight | Default: — |
Spectrum-defined styles, returned by the | ||
children | ReactNode | Default: — |
The content to display in the tab panels. | ||
shouldForceMount | boolean | Default: false
|
Whether 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. | ||
id | Key | Default: — |
The unique id of the tab. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||