Debian Codegraph @

node/@patternfly/react-core/src/components/Tabs/examples/Tabs.md

raw
1 ---
2 id: Tabs
3 section: components
4 cssPrefix: pf-v6-c-tabs
5 propComponents:
6 [
7 'Tabs',
8 'Tab',
9 'TabContent',
10 'TabContentBody',
11 'TabTitleText',
12 'TabTitleIcon',
13 'HorizontalOverflowObject',
14 'TabAction',
15 'PopperOptions'
16 ]
17 ouia: true
18 ---
19
20 import { createRef, Fragment, useEffect, useRef, useState } from 'react';
21 import RhUiUsersFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-users-fill-icon';
22 import RhUiContainerFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-container-fill-icon';
23 import DatabaseIcon from '@patternfly/react-icons/dist/esm/icons/database-icon';
24 import RhUiServerStackFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-server-stack-fill-icon';
25 import LaptopIcon from '@patternfly/react-icons/dist/esm/icons/laptop-icon';
26 import RhUiInfrastructureFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-infrastructure-fill-icon';
27 import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon';
28 import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon';
29
30 ## Examples
31
32 ### Default tabs
33
34 A `<Tabs>` component contains multiple `<Tab>` components that may be used to navigate between sets of content within a page.
35
36 You can adjust a tab in the following ways:
37
38 - To label a tab with text, pass a `<TabTitleText>` component into the `title` property of a `<Tab>`.
39 - To disable a tab, use the `isDisabled` property. Tabs using `isDisabled` are not perceivable or operable by users navigating via assistive technologies.
40 - To disable a tab, but keep it perceivable to assistive technology users, use the `isAriaDisabled` property. If a disabled tab has a tooltip, use this property instead of `isDisabled`.
41 - To add a tooltip to an aria-disabled tab, use the `tooltip` property.
42
43 Tabs can be styled as 'default' or 'boxed':
44
45 - Default tabs do not have any borders and use a bottom line to distinguish between a selected tab, a hovered tab, and an inactive tab.
46 - Boxed tabs are outlined to emphasize the area that a tab spans. To preview boxed tabs in the following examples, select the 'isBox' checkbox, which sets the `isBox` property to true.
47
48 ```ts file="./TabsDefault.tsx"
49
50 ```
51
52 ### Boxed secondary tabs
53
54 To change the background color of boxed tabs or the tab content, use the `variant` property.
55
56 Toggle the tab color by selecting the 'Tabs secondary variant' checkbox in the following example.
57
58 ```ts file="./TabsBoxSecondary.tsx"
59
60 ```
61
62 ### Vertical tabs
63
64 Vertical tabs are placed on the left-hand side of a page or container and may appear in both 'default' and 'boxed' tab variations.
65
66 To style tabs vertically, use the `isVertical` property.
67
68 ```ts file="./TabsVertical.tsx"
69
70 ```
71
72 ### Vertical expandable tabs
73
74 Vertical tabs can be made expandable to save space. Users can select the caret to expand a menu and switch between tabs.
75
76 Expandable tabs can be enabled at different breakpoints. The following example passes `expandable={{ default: 'expandable', md: 'nonExpandable', lg: 'expandable' }}` into the `<Tabs>` component.
77
78 To flag vertical tabs when they're expanded, use the `isExpanded` property.
79
80 ```ts file="./TabsVerticalExpandable.tsx"
81
82 ```
83
84 ### Vertical expandable uncontrolled
85
86 To flag the default expanded state for uncontrolled tabs, use the `defaultIsExpanded` property.
87
88 ```ts file="./TabsVerticalExpandableUncontrolled.tsx"
89
90 ```
91
92 ### Default overflow tabs
93
94 By default, overflow is applied when there are too many tabs for the width of the container they are in. This overflow can be navigated by side-scrolling within the tabs section, or by selecting the left and right arrows.
95
96 ```ts file="./TabsDefaultOverflow.tsx"
97
98 ```
99
100 ### Horizontal overflow tabs
101
102 Horizontal overflow can be used instead of the default application. To navigate horizontal overflow tabs users can select the last tab, labeled “more”, to see the overflowed content.
103
104 To enable horizontal overflow, use the `isOverflowHorizontal` property.
105
106 In the following example, select the 'Show overflowing tab count' checkbox to add a count of overflow items to the final “more” tab.
107
108 ```ts file="./TabsHorizontalOverflow.tsx"
109
110 ```
111
112 ### With tooltip react ref
113
114 When using a React ref to link a tooltip to a tab component via the `reference` property, you should avoid manually passing in a value of "off" to the `aria-live` property. Doing so may cause the tooltip to become less accessible to assistive technologies.
115
116 The tooltip should also have the `id` property passed in. The value of `id` should be passed into the tab's `aria-describedby` property. This ensures a tooltip used with a React ref will be announced by the JAWS and NVDA screen readers.
117
118 ```ts file="./TabsTooltipReactRef.tsx"
119
120 ```
121
122 ### Uncontrolled tabs
123
124 To allow the `<Tabs>` component to manage setting the active tab and displaying correct content itself, use uncontrolled tabs, as shown in the following example.
125
126 ```ts file="./TabsUncontrolled.tsx"
127
128 ```
129
130 ### With adjusted inset
131
132 To adjust the inset of tabs and visually separate them more, use the `inset` property. You can set the inset to "insetNone", "insetSm", "insetMd", "insetLg", "insetXl", or "inset2xl" at "default", "sm", "md", "lg, "xl, and "2xl" breakpoints.
133
134 ```ts file="./TabsInset.tsx"
135
136 ```
137
138 ### With page insets
139
140 To adjust the left padding of tabs, use the `usePageInsets` property. This property aligns the tabs padding with the default padding of the page section, which makes it easier to align tabs with page section content.
141
142 ```ts file="./TabsPageInsets.tsx"
143
144 ```
145
146 ### With icons and text
147
148 You can render different content in the `title` property of a tab to add icons and text.
149
150 To add an icon to a tab, pass a `<TabTitleIcon>` component that contains the icon of your choice into the `title`. To use an icon alongside styled text, keep the text in the `<TabTitleText>` component.
151
152 ```ts file="./TabsIconAndText.tsx"
153
154 ```
155
156 ### Subtabs
157
158 Use subtabs within other components, like modals. Subtabs have less visually prominent styling.
159
160 To apply subtab styling to tabs, use the `isSubtab` property.
161
162 For accessibility, give the primary tablist an accessible name (for example, `tabListAriaLabel="Primary"`) and give any subtab tablist an accessible name that matches the currently selected primary tab (for example, `tabListAriaLabel="Users"`).
163
164 ```ts file="./TabsSubtabs.tsx"
165
166 ```
167
168 ### Filled tabs with icons
169
170 To allow tabs to fill the available width of the page section, use the `isFilled` property.
171
172 ```ts file="./TabsFilledWithIcons.tsx"
173
174 ```
175
176 ### Tabs linked to nav elements
177
178 To let tabs link to nav elements, pass `{TabsComponent.nav}` into the `component` property.
179
180 Nav tabs should use the `href` property to link the tab to the URL of another page or page section. A tab with an `href` will render as an `<a>` instead of a `<button>`.
181
182 ```ts file="./TabsNav.tsx"
183
184 ```
185
186 ### Subtabs linked to nav elements
187
188 Subtabs can also link to nav elements.
189
190 ```ts file="./TabsNavSubtab.tsx"
191
192 ```
193
194 ### Tabs used for site navigation
195
196 Site navigation tabs
197
198 ```ts file="./TabsSiteNav.tsx"
199
200 ```
201
202 ### With separate content
203
204 If a `<TabContent>` component is defined outside of a `<Tabs>` component, use the `tabContentRef` and `tabContentId` properties. The `hidden` property is used on `TabContent` to set the initial visible content.
205
206 ```ts file="./TabsSeparateContent.tsx"
207
208 ```
209
210 ### With tab content with body and padding
211
212 To add a content body to a `<TabContent>` component, pass a `<TabContentBody>`. To add padding to the body section, use the `hasPadding` property.
213
214 ```ts file="./TabsContentWithBodyPadding.tsx"
215
216 ```
217
218 ### Children mounting on click
219
220 To mount tab children (add to the DOM) when a tab is clicked, use the `mountOnEnter` property.
221
222 Note that this property does not create the tab children until the tab is clicked, so they are not preloaded into the DOM.
223
224 ```ts file="./TabsChildrenMounting.tsx"
225
226 ```
227
228 ### Unmounting invisible children
229
230 To unmount tab children (remove from the DOM) when they are no longer visible, use the `unmountOnExit` property.
231
232 ```ts file="./TabsUnmountingInvisibleChildren.tsx"
233
234 ```
235
236 ### Toggled tab content
237
238 You may control tabs from outside of the tabs component. For example, select the "Hide tab 2" button below to make "Tab item 2" invisible.
239
240 The tab its content should only be mounted when the tab is visible.
241
242 ```ts file="./TabsToggledSeparateContent.tsx"
243
244 ```
245
246 ### Dynamic tabs
247
248 To enable closeable tabs, pass the `onClose` property to the `<Tabs>` component. To enable a button that adds new tabs, pass the `onAdd` property to `<Tabs>`.
249
250 ```ts file="./TabsDynamic.tsx"
251
252 ```
253
254 ### With help action popover
255
256 You may add a help action to a tab to provide users with additional context in a popover.
257
258 To render an action beside the tab content, use the `actions` property of a `<Tab>`. Pass a popover and a `<TabsAction>` component into the `actions` property.
259
260 ```ts file="./TabsHelp.tsx"
261
262 ```
263
264 ### With help and close actions
265
266 To add multiple actions to a tab, create a `<TabAction>` component for each action.
267
268 The following example passes in both help popover and close actions.
269
270 ```ts file="./TabsHelpAndClose.tsx"
271
272 ```