Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 36 ยท Navigation and Data Display

Tabs, Menus and Breadcrumbs

Use Material UI Tabs, TabPanel patterns, Menu, MenuItem and Breadcrumbs: controlled value, anchorEl state, keyboard behavior and accessible markup.

Once the shell is in place, people need smaller ways to move around: switching between views of the same data (tabs), picking an action from a list that opens on demand (menus), and knowing where they are in a hierarchy (breadcrumbs). Material UI covers all three with components that handle keyboard behavior and ARIA attributes for you, as long as you wire them up correctly.

MUI cannot run in the live editors on this site, so the real code below is read-only. The runnable examples are plain React stand-ins that implement the same keyboard and state patterns.

Tabs

Tabs is a controlled component: you own the selected value in state and update it in onChange. Tab children each have a label and, for accessibility, an id plus an aria-controls that points to their panel.

JSX
// Real Material UI, read-only
import { useState } from 'react';
import Tabs from '@mui/material/Tabs';
import Tab from '@mui/material/Tab';
import Box from '@mui/material/Box';

function a11yProps(index) {
  return { id: `tab-${index}`, 'aria-controls': `tabpanel-${index}` };
}

function TabPanel({ value, index, children }) {
  return (
    <div role="tabpanel" hidden={value !== index} id={`tabpanel-${index}`} aria-labelledby={`tab-${index}`}>
      {value === index && <Box sx={{ p: 2 }}>{children}</Box>}
    </div>
  );
}

export default function ProjectTabs() {
  const [value, setValue] = useState(0);
  return (
    <>
      <Tabs value={value} onChange={(event, next) => setValue(next)} aria-label="Project sections"
        variant="scrollable" scrollButtons="auto">
        <Tab label="Overview" {...a11yProps(0)} />
        <Tab label="Activity" {...a11yProps(1)} />
        <Tab label="Files" {...a11yProps(2)} disabled />
      </Tabs>
      <TabPanel value={value} index={0}>Overview content</TabPanel>
      <TabPanel value={value} index={1}>Activity content</TabPanel>
    </>
  );
}

Things worth knowing:

  • value can be any number or string, as long as each Tab has a matching value. If you omit value on Tab, its index is used.
  • variant="scrollable" lets many tabs scroll horizontally; variant="fullWidth" spreads them evenly; centered centers them.
  • orientation="vertical" gives a side tab list (also change the arrow keys' direction: MUI handles that).
  • Keyboard: arrow keys move focus between tabs (and, with the default selectionFollowsFocus false, Enter or Space selects). Home and End jump to the ends.
  • Tabs for navigation should be links. Give each Tab component={Link} and href/to, and set value from the current route, so the back button and deep links work. Tabs that change the URL are better than hidden state.
  • Tab content that is expensive: render it only when selected (as TabPanel above does), or keep it mounted with hidden if you need to preserve its state.

Try it: accessible tabs

This stand-in implements the ARIA tab pattern: roving tabIndex, arrow keys, Home/End and a panel linked by ids. Click a tab or focus the tab list and use the arrow keys.

Tabs with arrow-key navigation

Notice that only the selected tab is in the page's tab order (tabIndex=0); the arrow keys move between the others. That is how MUI's Tabs behaves too.

A Menu is a list of actions in a popover. It is anchored to the element that opened it, and it is open when anchorEl is not null. The standard state is const [anchorEl, setAnchorEl] = useState(null).

JSX
// Real Material UI, read-only
import { useState } from 'react';
import Button from '@mui/material/Button';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
import Divider from '@mui/material/Divider';

export default function AccountMenu() {
  const [anchorEl, setAnchorEl] = useState(null);
  const open = Boolean(anchorEl);
  const close = () => setAnchorEl(null);

  return (
    <>
      <Button
        id="account-button"
        aria-controls={open ? 'account-menu' : undefined}
        aria-haspopup="true"
        aria-expanded={open ? 'true' : undefined}
        onClick={(e) => setAnchorEl(e.currentTarget)}
      >
        Account
      </Button>
      <Menu
        id="account-menu"
        anchorEl={anchorEl}
        open={open}
        onClose={close}
        slotProps={{ list: { 'aria-labelledby': 'account-button' } }}
      >
        <MenuItem onClick={close}>Profile</MenuItem>
        <MenuItem onClick={close}>Settings</MenuItem>
        <Divider />
        <MenuItem onClick={close}>Log out</MenuItem>
      </Menu>
    </>
  );
}

Menu focuses the first item when it opens, moves with the arrow keys, supports type-ahead (typing "S" jumps to "Settings"), and closes on Escape or a click outside. When it closes, focus returns to the button. Use anchorOrigin and transformOrigin to choose which corner of the anchor the menu attaches to, and MenuItem selected for a "select-like" menu. For picking one value from a form, use Select instead; a menu is for actions, a select is for values.

A context menu uses anchorReference="anchorPosition" with anchorPosition={{ top, left }} taken from the contextmenu event.

Try it: a menu with keyboard support

A menu with anchor state and arrow keys

Breadcrumbs renders a trail of links separated by a character or icon, wrapped in a <nav aria-label="breadcrumb">. Pass links and end with plain text for the current page:

JSX
// Real Material UI, read-only
import Breadcrumbs from '@mui/material/Breadcrumbs';
import Link from '@mui/material/Link';
import Typography from '@mui/material/Typography';

<Breadcrumbs aria-label="breadcrumb" maxItems={3} separator="โ€บ">
  <Link underline="hover" color="inherit" href="/">Home</Link>
  <Link underline="hover" color="inherit" href="/catalog">Catalog</Link>
  <Link underline="hover" color="inherit" href="/catalog/shoes">Shoes</Link>
  <Typography aria-current="page" sx={{ color: 'text.primary' }}>Trail runner</Typography>
</Breadcrumbs>

maxItems collapses the middle of a long trail into an ellipsis button; itemsBeforeCollapse and itemsAfterCollapse control how many items stay visible at each end.

Try it: breadcrumbs with collapsing

Breadcrumbs that collapse

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does a Menu know whether it is open?

    The usual pattern is const [anchorEl, setAnchorEl] = useState(null) and open = Boolean(anchorEl). The menu is positioned against anchorEl.
  2. 2. Which tab should be in the page's tab order (tabIndex 0) in the ARIA tab pattern?

    Roving tabindex keeps one tab stop for the whole list. Arrow keys move focus within it.
  3. 3. How should the current page appear at the end of a Breadcrumbs trail?

    The current page is not a destination, so it is plain text marked with aria-current="page".
  4. 4. What is the best practice when tabs change the section of the app?

    URL-driven tabs preserve history and allow sharing the exact view.

Summary

  • Tabs is controlled by value; give tabs ids and aria-controls, and use links when tabs navigate.
  • Menu opens from an anchorEl state, handles arrow keys, type-ahead and Escape, and returns focus to its button.
  • Breadcrumbs wraps a trail in <nav aria-label="breadcrumb">; the current page is plain text with aria-current="page".
  • maxItems collapses long trails.

Next: Tables and Pagination