Tabs
TheTabs component provides a way to organize content into multiple panels that can be switched between. Material UI's Tabs component provides a consistent, accessible, and customizable navigation pattern.
Tabs are used to organize related content into separate views, allowing users to switch between different sections without navigating to different pages.
Basic Tabs
A simple tab navigation with text labels:
Content for Item One
jsxconst [value, setValue] = useState(0);const handleChange = (event, newValue) => {setValue(newValue);};<Box sx={{ width: "100%" }}><Box sx={{ borderBottom: 1, borderColor: "divider" }}><Tabs value={value} onChange={handleChange}><Tab label="Item One" /><Tab label="Item Two" /><Tab label="Item Three" /></Tabs></Box><TabPanel value={value} index={0}>Content for Item One</TabPanel><TabPanel value={value} index={1}>Content for Item Two</TabPanel><TabPanel value={value} index={2}>Content for Item Three</TabPanel></Box>
Tab Variants
Tabs support different visual variants:
Standard (Default)
Scrollable Tabs
When tabs don't fit in the available width, they become scrollable:
jsx<Tabsvalue={value}variant="scrollable"scrollButtons="auto"onChange={handleChange}><Tab label="Item One" /><Tab label="Item Two" />{/* More tabs */}</Tabs>
Full Width Tabs
Tabs that span the full width of their container:
jsx<Tabs value={value} variant="fullWidth" onChange={handleChange}><Tab label="Item One" /><Tab label="Item Two" /><Tab label="Item Three" /></Tabs>
Icons
Icon with Label
Tabs can include icons along with labels:
jsx<Tabs value={value} onChange={handleChange}><Tab icon={<PhoneIcon />} label="Recents" /><Tab icon={<FavoriteIcon />} label="Favorites" /><Tab icon={<PersonPinIcon />} label="Nearby" /></Tabs>
Icon Only
Tabs can display only icons:
jsx<Tabs value={value} onChange={handleChange}><Tab icon={<PhoneIcon />} /><Tab icon={<FavoriteIcon />} /><Tab icon={<PersonPinIcon />} /></Tabs>
Vertical Tabs
Tabs can be displayed vertically:
jsx<Box sx={{ flexGrow: 1, bgcolor: "background.paper", display: "flex", height: 224 }}><Tabsorientation="vertical"value={value}onChange={handleChange}sx={{ borderRight: 1, borderColor: "divider" }}><Tab label="Item One" /><Tab label="Item Two" /><Tab label="Item Three" /></Tabs><TabPanel value={value} index={0}>Content for Item One</TabPanel><TabPanel value={value} index={1}>Content for Item Two</TabPanel><TabPanel value={value} index={2}>Content for Item Three</TabPanel></Box>
Disabled Tabs
Individual tabs can be disabled:
jsx<Tabs value={value} onChange={handleChange}><Tab label="Active" /><Tab label="Disabled" disabled /><Tab label="Active" /></Tabs>
Colored Tabs
Tabs support different color schemes:
jsx<Tabsvalue={value}textColor="primary"indicatorColor="primary"onChange={handleChange}><Tab label="Item One" /><Tab label="Item Two" /></Tabs>
Common Props
The Tabs component accepts various props to customize its appearance and behavior:
jsx<Tabsvalue={0} // number - index of selected tabonChange={handleChange} // function - called when tab changesvariant="standard" // 'standard' | 'scrollable' | 'fullWidth'orientation="horizontal" // 'horizontal' | 'vertical'textColor="inherit" // 'inherit' | 'primary' | 'secondary'indicatorColor="primary" // 'primary' | 'secondary'scrollButtons="auto" // false | true | 'auto'centered={false} // boolean - center tabssx={{}} // object - custom styles><Tablabel="Label" // string | ReactNodeicon={<Icon />} // ReactNodedisabled={false} // booleanvalue={0} // number - tab index (if different from position)/></Tabs>
Accessibility
Material UI Tabs are built with accessibility in mind:
- ARIA attributes: Proper ARIA roles and attributes for screen readers
- Keyboard navigation: Arrow keys can be used to navigate between tabs
- Focus management: Proper focus handling when switching tabs
- Screen reader support: Tab labels and content are properly announced
- Tab panels: Use proper role="tabpanel" for tab content
- Active indicator: Clear visual indication of the active tab
Best Practices
- Keep tab labels concise - Short, clear labels work best
- Limit the number of tabs - Too many tabs can be overwhelming
- Use scrollable tabs for many items - Enable scrolling when you have many tabs
- Provide clear content - Each tab panel should have distinct, relevant content
- Consider mobile experience - Scrollable tabs work well on mobile devices
- Use icons appropriately - Icons can enhance understanding but shouldn't replace clear labels
- Maintain state properly - Use controlled components for predictable behavior
- Group related content - Tabs should contain related information
Examples
Controlled Tabs
jsxconst [value, setValue] = useState(0);const handleChange = (event, newValue) => {setValue(newValue);};<Box sx={{ width: "100%" }}><Tabs value={value} onChange={handleChange}><Tab label="Overview" /><Tab label="Details" /><Tab label="Settings" /></Tabs>{/* Tab content */}</Box>
Tabs with Navigation
jsxconst [value, setValue] = useState(0);const handleChange = (event, newValue) => {setValue(newValue);// Navigate or update content based on tab};<Tabs value={value} onChange={handleChange}><Tab label="Home" /><Tab label="Products" /><Tab label="About" /><Tab label="Contact" /></Tabs>
Tabs with Badges
jsximport { Badge } from "@mui/material";<Tabs value={value} onChange={handleChange}><Tablabel={<Badge badgeContent={4} color="primary">Messages</Badge>}/><Tab label="Notifications" /></Tabs>