Tabs
Tabs are navigation solutions for alternating between content that is at the same level of the hierarchy.
Examplesβ
Fluidβ
This property will make the TabList
, match the container width. By default, it has the value set to false
.
function Example() {
const state = useTabState()
return (
<Tabs state={state}>
<TabList fluid aria-label="fluid-tabs">
<Tab id="1">Tab 1</Tab>
<Tab id="2">Tab 2</Tab>
<Tab id="3">Tab 3</Tab>
<Tab id="4">Tab 4</Tab>
</TabList>
<TabPanel id="1" csx={{ padding: 3 }}>
Tab 1
</TabPanel>
<TabPanel id="2" csx={{ padding: 3 }}>
Tab 2
</TabPanel>
<TabPanel id="3" csx={{ padding: 3 }}>
Tab 3
</TabPanel>
<TabPanel id="4" csx={{ padding: 3 }}>
Tab 4
</TabPanel>
</Tabs>
)
}
Default selected tabβ
You can set the default selected tab by passing an id to selectedId on useTabState.
function Example() {
const state = useTabState({ selectedId: '2' })
return (
<Tabs state={state}>
<TabList fluid aria-label="my-tabs">
<Tab id="1">Tab 1</Tab>
<Tab id="2">Tab 2</Tab>
<Tab id="3">Tab 3</Tab>
</TabList>
<TabPanel id="1" csx={{ padding: 3 }}>
Tab 1
</TabPanel>
<TabPanel id="2" csx={{ padding: 3 }}>
Tab 2
</TabPanel>
<TabPanel id="3" csx={{ padding: 3 }}>
Tab 3
</TabPanel>
</Tabs>
)
}
Select a Tab manuallyβ
You can set the selected tab by passing an id
to the select
returned by the useTabState
.
function Example() {
const state = useTabState()
return (
<Tabs state={state}>
<TabList fluid aria-label="my-tabs">
<Tab id="1">Tab 1</Tab>
<Tab id="2">Tab 2</Tab>
</TabList>
<TabPanel id="1" csx={{ padding: 3 }}>
<Button onClick={() => state.select('2')}>Go to Tab 2!</Button>
</TabPanel>
<TabPanel id="2" csx={{ padding: 3 }}>
<Button onClick={() => state.select('1')}>Go to Tab 1!</Button>
</TabPanel>
</Tabs>
)
}
Manual Activationβ
By default, a Tab is selected when it gets focused, which reveals its corresponding TabPanel. This behavior can be changed by setting manual to true on useTabState.
function Example() {
const state = useTabState({ manual: true })
return (
<Tabs state={state}>
<TabList fluid aria-label="my-tabs">
<Tab id="1">Tab 1</Tab>
<Tab id="2">Tab 2</Tab>
</TabList>
<TabPanel id="1" csx={{ padding: 3 }}>
Tab 1
</TabPanel>
<TabPanel id="2" csx={{ padding: 3 }}>
Tab 2
</TabPanel>
</Tabs>
)
}
Usageβ
import { Tabs, Tab, TabList, TabPanel, useTabState } from '@vtex/admin-ui'
function Example() {
const state = useTabState()
return (
<Tabs state={state}>
<TabList aria-label="Usage Tabs">
<Tab id="1">Tab 1</Tab>
<Tab id="2">Tab 2</Tab>
</TabList>
<TabPanel id="1" csx={{ padding: 3 }}>
Tab 1
</TabPanel>
<TabPanel id="2" csx={{ padding: 3 }}>
Tab 2
</TabPanel>
</Tabs>
)
}
Accessibilityβ
- Each
TabPanel
should have anid
correspondent to the id of itsTab
. - Every
TabList
should have thearia-label
defined to guarantee accessibility.
Propsβ
Tabsβ
Name | Type | Description | Required | Default |
---|---|---|---|---|
state | TabStateReturn | Tabs state | β | - |
children | ReactNode | Tabs children | β | - |
Tabβ
It also accepts all props of button
JSX element.
Name | Type | Description | Required | Default |
---|---|---|---|---|
id | string | Tab's id | π« | - |
csx | StyleObject | Custom styles | π« | {} |
TabListβ
It also accepts all props of div
JSX element.
Name | Type | Description | Required | Default |
---|---|---|---|---|
fluid | boolean | Whether the TabList width should match the container or not | π« | false |
csx | StyleObject | Custom styles | π« | {} |
TabPanelβ
It also accepts all props of div
JSX element.
Name | Type | Description | Required | Default |
---|---|---|---|---|
id | string | The same id as the correspondent Tab's id | π« | - |
csx | StyleObject | Custom styles | π« | {} |
useTabStateβ
Hook parameters.
Name | Type | Description | Required | Default |
---|---|---|---|---|
selectedId | string | The current selected tab's id | π« | - |
baseId | string | ID that will serve as a base for all the items IDs. | π« | - |
manual | boolean | Whether the tab selection should be manual or not | π« | false |
loop | boolean | Loops from the last item to the first item and vice-versa. | π« | false |
TabStateReturnβ
Properties returned by the useTabState
hook.
Name | Type | Description | Required | Default |
---|---|---|---|---|
selectedId | string | The current selected tab's id | π« | - |
select | () => void | Moves into and selects a tab by its id | π« | - |
first | () => void | Moves focus to the first item | π« | - |
last | () => void | Moves focus to the last item | π« | - |
next | () => void | Moves focus to the next item | π« | - |
previous | () => void | Moves focus to the previous item | π« | - |
Doβsβ
- Use short labels. Prefer two words maximum.
- Use tabs in multiple places of your page hierarchy: as a top-level navigation or inside another component.
- Avoid using icons as the single source of information to characterize tabs.
- Do not nest tabs, the user should be always aware of where theyβre standing.
- Avoid using more than five tabs per page, or component.
- Avoid forcing the user to alternate back-and-forth between tabs to execute an important task.
π For UX Writing guidelines, access https://uxwriting.vtex.com