You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/circuit-ui/components/Tabs/Tabs.mdx
+21-46Lines changed: 21 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,8 @@ The Tabs component allows users to navigate between different views or sections
19
19
20
20
### Group related content
21
21
22
-
Tabs help optimize space by organizing large amounts of content into easily scannable sections, reducing cognitive load for users. They provide simple navigation between related sections that don’t require side-by-side comparison.
22
+
Tabs help optimize space by organizing large amounts of content into easily scannable sections, reducing cognitive load for users.
23
+
They provide simple navigation between related sections that don’t require side-by-side comparison.
23
24
24
25
Examples include:
25
26
@@ -45,66 +46,40 @@ You can use the Tabs component out of the box to create a tabbed view by providi
45
46
46
47
### Use subcomponents independently
47
48
48
-
If you need finer control over your layout, you can use the `Tab`, `TabList`, and `TabPanel` components independently. This gives you flexibility to customize the structure and behavior of your tabs while still relying on the core functionality provided by Tabs.
49
+
For finer control over layout, use `TabList` and `TabPanel` independently. Pass your tabs via the `tabs` prop.
50
+
`TabList` manages selection state, keyboard navigation, and accessibility attributes internally. Use the `onTabChange` callback to track which panel to show.
49
51
50
-
⚠️ When doing so, you are responsible for ensuring accessibility and managing state correctly.
52
+
- Using `TabList`:
53
+
- Provide a `tabs` array with a unique `id` and label for each tab.
54
+
- Use `onTabChange` to receive the selected tab's id and toggle panel visibility.
55
+
- Using `TabPanel`:
56
+
- Assign a unique `id` following the `panel-{id}` convention to match `TabList`'s `aria-controls`.
57
+
- Set `aria-labelledby` to `tab-{id}` to reference the corresponding tab.
51
58
52
-
- Using Tab:
53
-
- Provide a unique `id` for each tab.
54
-
- Add an `aria-controls` attribute that points to the id of its corresponding panel.
55
-
- Implement keyboard navigation (e.g., arrow keys, Home/End) to support keyboard users, or use the `useTabState` helper [hook](#usetabstate) to manage state and interactions. This is not necessary for [tab-like navigation](#tab-like-navigation).
56
-
- Using TabPanel:
57
-
- Assign a unique id to each panel.
58
-
- Ensure this id matches the `aria-controls` value of its corresponding tab.
59
-
- Set `aria-labelledby` to reference the id of the associated tab.
60
-
61
-
<Storyof={Stories.ControlledState} />
59
+
<Storyof={Stories.WithTabPanel} />
62
60
63
61
#### useTabState
64
62
65
-
Use this hook to manage tab state and handle user interactions. The hook accepts an array of tab IDs and an optional initially selected index, and returns:
66
-
-`selectedId` : the id of the currently selected tab.
67
-
-`onTabKeyDown` : an `onKeyDown` handler for your Tab component that manages arrow-key navigation and updates selectedId.
68
-
-`onTabClick` : an `onClick` handler for your Tab component that updates selectedId when a tab is clicked.
63
+
If you need even finer control and want to build a fully custom tab implementation, `useTabState` is available as a standalone hook.
64
+
It accepts an array of tab IDs and an optional initially selected index, and returns `selectedId`, an `onTabKeyDown` handler for arrow-key navigation, and an `onTabClick` handler for click selection.
If you need to implement a tab-like navigation rather than true tab semantics, you can control the rendering behavior of the TabList and Tab components to achieve this.
86
-
- Set the `as` prop of Tablist to `"navigation"` to render a native `<nav>` containing an element with the role `list` to wrap its children.
87
-
- Set the `as` prop of Tab component to `"listitem"` to wrap your tab in an element with role `listitem`.
77
+
If you need to implement a tab-like navigation rather than true tab semantics, set the `as` prop of `TabList` to `"navigation"`. This renders a native `<nav>` containing an element with the role `list` to wrap its children.
88
78
89
-
The components would render styled to look like tabs but without any tab ARIA roles. This ensures a correctly structured and accessible navigation pattern that visually matches the tab design.
90
-
91
-
```tsx
92
-
<TabListas="navigation">
93
-
<Tabas="listitem"href="/home">Home</Tab>
94
-
<Tabas="listitem"href="/about">About</Tab>
95
-
<Tabas="listitem"href="/contact">Contact</Tab>
96
-
</TabList>
97
-
```
79
+
The component renders styled to look like tabs but without any tab ARIA roles. This ensures a correctly structured and accessible navigation pattern that visually matches the tab design.
98
80
99
81
<Storyof={TabListStories.Navigation} />
100
82
101
-
### With links
102
-
103
-
Tabs render as buttons by default. In navigation contexts, you can pass a `href` prop to render them as links.
104
-
Avoid using external links, as this can create an inconsistent user experience across tabs. Instead, place the external link outside the Tabs component to maintain predictable behavior.
105
-
106
-
<Storyof={Stories.Links} />
107
-
108
83
### Stretched
109
84
110
85
Set the `stretched` prop to true to make all tabs expand and share the available space evenly.
0 commit comments