Skip to content

Lesson 23 ยท Navigation and Data Display

App Bar and Drawer Navigation

Build the shell of an app with Material UI AppBar, Toolbar and Drawer: permanent, temporary and mini variants, responsive layout and accessible menu buttons.

Almost every app needs a frame: a bar at the top with the title and a few actions, and a side menu that lists the sections. In Material UI the bar is AppBar with a Toolbar inside it, and the side menu is Drawer. Together they make the classic responsive shell: a permanent sidebar on wide screens, a slide-in menu behind a hamburger button on phones.

MUI is not available in the live editors here. The real code is read-only; the runnable examples are stand-ins built from plain React and CSS that behave like AppBar and Drawer.

AppBar and Toolbar

AppBar is a Paper with a color and an elevation; it positions itself and spans the width. Toolbar lays out its children in a row with sensible height and padding. You nearly always use them together.

JSX
// Real Material UI, read-only
import AppBar from '@mui/material/AppBar';
import Toolbar from '@mui/material/Toolbar';
import Typography from '@mui/material/Typography';
import IconButton from '@mui/material/IconButton';
import MenuIcon from '@mui/icons-material/Menu';

<AppBar position="fixed" color="primary" elevation={1}>
  <Toolbar>
    <IconButton color="inherit" edge="start" aria-label="open navigation" onClick={toggle}>
      <MenuIcon />
    </IconButton>
    <Typography variant="h6" component="h1" sx={{ flexGrow: 1 }}>
      Orbit
    </Typography>
    <Button color="inherit">Log in</Button>
  </Toolbar>
</AppBar>
<Toolbar /> {/* an empty Toolbar as a spacer under a fixed bar */}

Key points:

  • position can be fixed (always visible), sticky, static, absolute or relative. A fixed bar leaves the page flow, so add an empty <Toolbar /> after it as a spacer, or your content hides underneath.
  • color can be primary, secondary, default, transparent or inherit. In dark mode a primary AppBar gets a dark surface automatically.
  • Typography with component="h1" keeps the heading semantic while variant="h6" controls the look. Appearance and semantics are separate on purpose.
  • An IconButton that holds only an icon needs aria-label, or screen readers announce an unlabeled button.
  • sx={{ flexGrow: 1 }} on the title pushes the buttons that come after it to the right edge.

Drawer variants

Drawer has three variants:

Variant Behavior Typical use
temporary (default) Overlays the page with a backdrop; closes on backdrop click or Escape Phones
persistent Slides in and out without a backdrop; page content is pushed Tablets, tools
permanent Always visible, no open state Desktop sidebars
JSX
// Real Material UI, read-only
import Drawer from '@mui/material/Drawer';
import List from '@mui/material/List';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';

const DRAWER_WIDTH = 240;

function Nav({ items, selected, onSelect }) {
  return (
    <List>
      {items.map((item) => (
        <ListItemButton key={item.id} selected={item.id === selected} onClick={() => onSelect(item.id)}>
          <ListItemIcon>{item.icon}</ListItemIcon>
          <ListItemText primary={item.label} />
        </ListItemButton>
      ))}
    </List>
  );
}

<Drawer
  variant="temporary"
  open={mobileOpen}
  onClose={() => setMobileOpen(false)}
  ModalProps={{ keepMounted: true }} // better mobile performance and SEO
  sx={{ display: { xs: 'block', md: 'none' }, '& .MuiDrawer-paper': { width: DRAWER_WIDTH } }}
>
  <Nav {...props} />
</Drawer>

<Drawer
  variant="permanent"
  open
  sx={{ display: { xs: 'none', md: 'block' }, '& .MuiDrawer-paper': { width: DRAWER_WIDTH } }}
>
  <Nav {...props} />
</Drawer>

The pattern is to render two drawers and show one with the responsive display rule. The content area then gets ml: { md: '240px' } (or sits in a flex row) so it does not slide under the permanent drawer. ListItemButton renders a real <button> (or an <a> if you give it href or component={Link}), with focus styles, a ripple and selected support built in.

With a router, pass component={RouterLink} to="/reports" to ListItemButton. Then the item is a real link: it can be opened in a new tab, and selected can be driven by the current location.

Try it: a responsive shell

The stand-in below follows the pattern. Below 700 px of preview width the menu is a temporary overlay behind the hamburger; above it, the menu is permanent. Resize the preview, or use the pane's resize handle if it has one, and click the items.

AppBar plus a responsive Drawer

Mini drawers and collapsing

A "mini" variant shows icons only when collapsed and the full labels when expanded. It is built with a permanent drawer whose width changes (and overflowX: 'hidden') with a transition from theme.transitions. The icon column stays visible, so users keep orientation; add Tooltip around each icon so the label is still reachable when the drawer is collapsed.

Try it: expand and collapse a mini drawer

A mini drawer with tooltips

Accessibility checklist

  • Give the navigation landmark a name: <nav aria-label="Main"> (MUI's Drawer content is yours to label).
  • Icon-only buttons need aria-label; add aria-expanded to the one that toggles the menu.
  • Mark the current page with aria-current="page"; MUI's selected is visual only.
  • Keep the temporary drawer's focus trap and Escape handling. Drawer gives you this because it is built on Modal.
  • Do not hide the only route to a page inside the drawer on mobile: the page must still be reachable by link.

Next, tabs, menus and breadcrumbs let people move within a page or a section.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do you add an empty <Toolbar /> after a fixed AppBar?

    position="fixed" removes the bar from the flow. An empty Toolbar has the same height and pushes the content down.
  2. 2. Which Drawer variant overlays the page with a backdrop?

    temporary is a modal drawer with a backdrop, closed by backdrop click or Escape. persistent pushes content and permanent is always shown.
  3. 3. What does ModalProps={{ keepMounted: true }} do on a temporary Drawer?

    Normally closed modals unmount. keepMounted leaves the content in the DOM, so opening is faster and the links are present for crawlers.
  4. 4. How should an icon-only hamburger IconButton be labeled?

    Without text, a screen reader has nothing to announce, so provide an accessible name with aria-label.

Summary

  • AppBar plus Toolbar form the header; use a spacer Toolbar under a fixed bar.
  • Drawer is temporary, persistent or permanent; show a temporary one on phones and a permanent one on desktops with responsive display.
  • Build the menu from List and ListItemButton; use selected and, with a router, component and to.
  • Label landmarks and icon buttons, and mark the current page with aria-current.

Next: Tabs, Menus and Breadcrumbs