Skip to content

Material 3

Components

Navigation rail

Navigation rails let people switch between UI views on mid-sized devices.

Inbox
AAli ConnorsBrunch this weekend?
SSandra AdamsPhotos from the summer trip
TTrevor HansenBirthday gift ideas
BBritta HoltRecipe to try on Friday
JJonas MeyerYour order has shipped
MMina ParkNotes from the review

Navigation rails let people switch between UI views on mid-sized devices. Use a navigation rail in a medium, expanded, large or extra-large window. A rail can contain:

  • Three to seven destinations
  • A menu button, which expands and collapses the rail
  • An optional FAB, for the key action of the app

Always put the rail in the same place, even on different screens of an app.

On smaller devices, use a navigation bar instead.

A rail is composed of these parts:

PartUse
NavigationRailThe rail
NavigationRailHeaderThe controls at the top of the rail. See Header.
NavigationRailItemA destination
NavigationRailSectionA group of secondary destinations. See Sections.
IconIn an item, its icon, sized and colored by the item
Text with slot="label"In an item, its label, which names the item
import {
  NavigationRail,
  NavigationRailHeader,
  NavigationRailItem,
  NavigationRailSection,
} from '@material-web/react'
import { Text } from 'react-aria-components'

Basic navigation rail

Put one NavigationRailItem per destination in a NavigationRail, each with its icon and its label.

Inbox
import {
  Fab,
  Icon,
  IconButton,
  NavigationRail,
  NavigationRailHeader,
  NavigationRailItem,
  NavigationRailSection,
} from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

export default function Basic() {
  const [isExpanded, setExpanded] = useState(false)
  const [page, setPage] = useState('Inbox')
  return (
    <div className="flex h-120 w-full">
      <nav aria-label="Mail" className="flex">
        <NavigationRail isExpanded={isExpanded}>
          <NavigationRailHeader>
            <IconButton
              aria-label={isExpanded ? 'Collapse' : 'Expand'}
              onPress={() => setExpanded(!isExpanded)}
            >
              <Icon name={isExpanded ? 'menu_open' : 'menu'} />
            </IconButton>
            <Fab isExtended={isExpanded} aria-label="Compose">
              <Icon name="edit" className="icon-filled" />
              <Text slot="label">Compose</Text>
            </Fab>
          </NavigationRailHeader>
          <NavigationRailItem isSelected={page === 'Inbox'} onChange={() => setPage('Inbox')}>
            <Icon name="inbox" />
            <Text slot="label">Inbox</Text>
          </NavigationRailItem>
          <NavigationRailItem isSelected={page === 'Sent'} onChange={() => setPage('Sent')}>
            <Icon name="send" />
            <Text slot="label">Sent</Text>
          </NavigationRailItem>
          <NavigationRailSection aria-labelledby="rail-labels">
            <Text slot="header" id="rail-labels">
              Labels
            </Text>
            <NavigationRailItem isSelected={page === 'Family'} onChange={() => setPage('Family')}>
              <Icon name="label" />
              <Text slot="label">Family</Text>
            </NavigationRailItem>
          </NavigationRailSection>
        </NavigationRail>
      </nav>
      <div className="m-space-200 min-w-0 flex-1 rounded-shape-xl bg-primary-container p-space-300 text-title-lg text-on-primary-container">
        {page}
      </div>
    </div>
  )
}

A rail comes in two variants, collapsed and expanded, which transform into each other when the menu button is selected. Set isExpanded to expand the rail, and change it in the onPress of the menu button, as the first example does. The page beside the rail adjusts to fit.

Collapsed rail

A collapsed rail (the default) is 96dp wide, with each item's label under its icon. It runs along the leading edge of the window.

Clock
import { Icon, NavigationRail, NavigationRailItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

const destinations = [
  ['Alarm', 'alarm'],
  ['Clock', 'schedule'],
  ['Timer', 'hourglass_empty'],
] as const

export default function Collapsed() {
  const [page, setPage] = useState<string>('Clock')
  return (
    <div className="flex h-96">
      <NavigationRail aria-label="Main">
        {destinations.map(([name, icon]) => (
          <NavigationRailItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
            <Icon name={icon} />
            <Text slot="label">{name}</Text>
          </NavigationRailItem>
        ))}
      </NavigationRail>
      <div className="m-space-200 min-w-0 flex-1 rounded-shape-xl bg-primary-container p-space-300 text-title-lg text-on-primary-container">
        {page}
      </div>
    </div>
  )
}

Expanded rail

An expanded rail is as wide as its content, from 220 to 360dp, with each item's icon and label side by side. A label too long for that wraps. Use an expanded rail when there are secondary destinations that have lower priority than the main navigation items.

Clock
import { Icon, NavigationRail, NavigationRailItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

const destinations = [
  ['Alarm', 'alarm'],
  ['Clock', 'schedule'],
  ['Timer', 'hourglass_empty'],
] as const

export default function Expanded() {
  const [page, setPage] = useState<string>('Clock')
  return (
    <div className="flex h-96">
      <NavigationRail aria-label="Main" isExpanded>
        {destinations.map(([name, icon]) => (
          <NavigationRailItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
            <Icon name={icon} />
            <Text slot="label">{name}</Text>
          </NavigationRailItem>
        ))}
      </NavigationRail>
      <div className="m-space-200 min-w-0 flex-1 rounded-shape-xl bg-primary-container p-space-300 text-title-lg text-on-primary-container">
        {page}
      </div>
    </div>
  )
}

Slots

Item

  1. label
SlotChildUse
labelA TextThe label, which names the item

Write an item's icon and label as its children. The Icon is sized and colored by the item.

Section

  1. header
SlotChildUse
headerA Text, first in the sectionThe header of a section

Give the header an id, and set the section's aria-labelledby to that id. Without a header, name the section with an aria-label.

Write a NavigationRailHeader first in the rail, then the items, then any sections. The rail shows its children in the order written.

The header shows its children in a column, such as a menu IconButton and a FAB. The first example has both.

PartUse
Menu buttonChange the menu icon when the rail is expanded, to show that it can be collapsed.
FABPass the rail's isExpanded to the FAB as its isExtended. An extended FAB widens the expanded rail. A FAB in the header has no shadow until it is hovered.
LogoOptional. Avoid a logo that could be mistaken for a button, and do not use one as the menu button.

Sections

Put secondary destinations in a NavigationRailSection, after the rail's own items. A section is shown only in an expanded rail. For a section header, see Slots.

Clock
import {
  Icon,
  NavigationRail,
  NavigationRailItem,
  NavigationRailSection,
} from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

const destinations = [
  ['Alarm', 'alarm'],
  ['Clock', 'schedule'],
  ['Timer', 'hourglass_empty'],
] as const

const secondary = [
  ['Sounds', 'music_note'],
  ['Wind down', 'self_improvement'],
] as const

export default function Sections() {
  const [page, setPage] = useState<string>('Clock')
  const item = ([name, icon]: readonly [string, string]) => (
    <NavigationRailItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
      <Icon name={icon} />
      <Text slot="label">{name}</Text>
    </NavigationRailItem>
  )
  return (
    <div className="flex h-120">
      <NavigationRail aria-label="Main" isExpanded>
        {destinations.map(item)}
        <NavigationRailSection aria-label="More">{item(['Bedtime', 'bed'])}</NavigationRailSection>
        <NavigationRailSection aria-labelledby="sleep-well">
          <Text slot="header" id="sleep-well">
            Sleep well
          </Text>
          {secondary.map(item)}
        </NavigationRailSection>
      </NavigationRail>
      <div className="m-space-200 min-w-0 flex-1 rounded-shape-xl bg-primary-container p-space-300 text-title-lg text-on-primary-container">
        {page}
      </div>
    </div>
  )
}

Selection

Set isSelected on the NavigationRailItem of the page being shown, and handle a press with onChange or onPress. A selected item shows the active indicator and fills its Icon.

<NavigationRailItem isSelected={page === 'inbox'} onChange={() => setPage('inbox')}>
  <Icon name="inbox" />
  <Text slot="label">Inbox</Text>
</NavigationRailItem>
  • Your own state: Selecting an item does not deselect the others, and a press on the selected item calls onChange(false), so keep the selection in your own state.
  • Uncontrolled: With defaultSelected instead, an item keeps its own selection, and a second press turns it off.

Disabled

Set isDisabled to disable an item. It cannot be pressed or focused.

Clock
import { Icon, NavigationRail, NavigationRailItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

const destinations = [
  ['Alarm', 'alarm'],
  ['Clock', 'schedule'],
  ['Timer', 'hourglass_empty'],
] as const

export default function Disabled() {
  const [page, setPage] = useState<string>('Clock')
  return (
    <div className="flex h-96">
      <NavigationRail aria-label="Main">
        {destinations.map(([name, icon]) => (
          <NavigationRailItem
            key={name}
            isSelected={page === name}
            isDisabled={name === 'Timer'}
            onChange={() => setPage(name)}
          >
            <Icon name={icon} />
            <Text slot="label">{name}</Text>
          </NavigationRailItem>
        ))}
      </NavigationRail>
      <div className="m-space-200 min-w-0 flex-1 rounded-shape-xl bg-primary-container p-space-300 text-title-lg text-on-primary-container">
        {page}
      </div>
    </div>
  )
}

Arrangement

Set arrangement to top (the default) or center. With center, the items are in the middle of the rail, and the header stays at its top. On tablets, use center to make the items easier to reach.

Clock
import {
  Icon,
  IconButton,
  NavigationRail,
  NavigationRailHeader,
  NavigationRailItem,
} from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

const destinations = [
  ['Alarm', 'alarm'],
  ['Clock', 'schedule'],
  ['Timer', 'hourglass_empty'],
] as const

export default function Center() {
  const [isExpanded, setExpanded] = useState(false)
  const [page, setPage] = useState<string>('Clock')
  return (
    <div className="flex h-120">
      <NavigationRail aria-label="Main" arrangement="center" isExpanded={isExpanded}>
        <NavigationRailHeader>
          <IconButton
            aria-label={isExpanded ? 'Collapse' : 'Expand'}
            onPress={() => setExpanded(!isExpanded)}
          >
            <Icon name={isExpanded ? 'menu_open' : 'menu'} />
          </IconButton>
        </NavigationRailHeader>
        {destinations.map(([name, icon]) => (
          <NavigationRailItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
            <Icon name={icon} />
            <Text slot="label">{name}</Text>
          </NavigationRailItem>
        ))}
      </NavigationRail>
      <div className="m-space-200 min-w-0 flex-1 rounded-shape-xl bg-primary-container p-space-300 text-title-lg text-on-primary-container">
        {page}
      </div>
    </div>
  )
}

The sections are centered together with the items. This also applies to a collapsed rail, where the sections are hidden but keep their space.

Placement

The rail is not positioned for you, and is as tall as its container stretches it. Put it first in a row with the page, such as <div className="flex h-dvh">, so that it is on the leading edge of the window: the left side in a left-to-right language, and the right side in a right-to-left one.

The rail is not a landmark. Put it in a <nav> of your own, with flex so that the rail is stretched, as in the first example, or name it with an aria-label.

Hidden when collapsed

Set isHiddenOnCollapse to hide the rail while it is collapsed, for an immersive page. A hidden rail takes no space and cannot be reached, so expand it from a menu button of your own, outside the rail. Once expanded, it is shown beside the page, which makes room for it.

8:30
import {
  Icon,
  IconButton,
  NavigationRail,
  NavigationRailHeader,
  NavigationRailItem,
} from '@material-web/react'
import { useRef, useState } from 'react'
import { Text } from 'react-aria-components'
import { flushSync } from 'react-dom'

const destinations = [
  ['Alarm', 'alarm'],
  ['Clock', 'schedule'],
  ['Timer', 'hourglass_empty'],
] as const

export default function HiddenOnCollapse() {
  const [isExpanded, setExpanded] = useState(false)
  const [page, setPage] = useState<string>('Clock')
  const show = useRef<HTMLButtonElement>(null)
  const hide = useRef<HTMLButtonElement>(null)
  // Each menu button takes the keyboard focus from the other.
  const turn = (next: boolean) => {
    flushSync(() => setExpanded(next))
    ;(next ? hide : show).current?.focus()
  }
  return (
    <div className="flex h-96">
      <NavigationRail aria-label="Main" isExpanded={isExpanded} isHiddenOnCollapse>
        <NavigationRailHeader>
          <IconButton ref={hide} aria-label="Hide navigation" onPress={() => turn(false)}>
            <Icon name="menu_open" />
          </IconButton>
        </NavigationRailHeader>
        {destinations.map(([name, icon]) => (
          <NavigationRailItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
            <Icon name={icon} />
            <Text slot="label">{name}</Text>
          </NavigationRailItem>
        ))}
      </NavigationRail>
      <div className="relative flex min-w-0 flex-1 items-center justify-center bg-primary-container text-display-lg text-on-primary-container">
        {!isExpanded && (
          <IconButton
            ref={show}
            aria-label="Show navigation"
            className="absolute start-7 top-13"
            onPress={() => turn(true)}
          >
            <Icon name="menu" />
          </IconButton>
        )}
        8:30
      </div>
    </div>
  )
}
  • Layout: Such a rail is never shown collapsed, and its sections are always shown.
  • Focus: Move the keyboard focus to the rail's menu button when it expands, and back to the page's menu button when it collapses.

Third-party routing library

An item takes no href. To move between the pages of an app, call the router in the item's onChange or onPress, and set isSelected from the current location. See Routing.

With React Router:

import { Icon, NavigationRailItem } from '@material-web/react'
import { Text } from 'react-aria-components'
import { useLocation, useNavigate } from 'react-router'

function InboxItem() {
  const { pathname } = useLocation()
  const navigate = useNavigate()
  return (
    <NavigationRailItem isSelected={pathname === '/inbox'} onPress={() => navigate('/inbox')}>
      <Icon name="inbox" />
      <Text slot="label">Inbox</Text>
    </NavigationRailItem>
  )
}

The navigation bar page has a whole example.

Customization

Pass classes in className to restyle the rail, its header, an item or a section. A class of yours replaces the component's own class for the same property. See Styling for the classes, the states and the theme.

Set className="bg-transparent" to turn the rail's fill off, and className="border-e border-outline-variant" to show a divider at the edge next to the content.

Clock
import { Icon, NavigationRail, NavigationRailItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

const destinations = [
  ['Alarm', 'alarm'],
  ['Clock', 'schedule'],
  ['Timer', 'hourglass_empty'],
] as const

export default function Divider() {
  const [page, setPage] = useState<string>('Clock')
  return (
    <div className="flex h-96 bg-surface-container-low">
      <NavigationRail aria-label="Main" className="border-e border-outline-variant bg-transparent">
        {destinations.map(([name, icon]) => (
          <NavigationRailItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
            <Icon name={icon} />
            <Text slot="label">{name}</Text>
          </NavigationRailItem>
        ))}
      </NavigationRail>
      <div className="min-w-0 flex-1 p-space-300 text-title-lg">{page}</div>
    </div>
  )
}

Pass a function as an item's children to render them from its state, as on a navigation bar item: isHovered, isPressed, isFocused, isFocusVisible, isSelected and isDisabled. In this example, an item shows another icon when it is selected.

Saved
import { Icon, NavigationRail, NavigationRailItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'

const destinations = ['Explore', 'Saved', 'Updates']

export default function FunctionChildren() {
  const [page, setPage] = useState('Saved')
  return (
    <div className="flex h-96">
      <NavigationRail aria-label="Main">
        {destinations.map((name) => (
          <NavigationRailItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
            {({ isSelected }) => (
              <>
                <Icon name={isSelected ? 'check_circle' : 'circle'} />
                <Text slot="label">{name}</Text>
              </>
            )}
          </NavigationRailItem>
        ))}
      </NavigationRail>
      <div className="m-space-200 min-w-0 flex-1 rounded-shape-xl bg-primary-container p-space-300 text-title-lg text-on-primary-container">
        {page}
      </div>
    </div>
  )
}
  • States: Style an item's state with its variant, such as hover:, pressed: or selected:. See States.
  • Functions: The className and style of a rail, an item and a section can be functions of their state too. See Children as a function.
  • Every navigation rail: To restyle all the navigation rails at once, change the theme.