Navigation bars let people switch between UI views on smaller devices. A navigation bar gives access to three to five destinations at the bottom of the window, each represented by an icon and label text. Use a navigation bar for:
- Three to five main pages in the product
- Mobile or tablet only
On larger devices, use a navigation rail instead.
A navigation bar is composed of these parts:
| Part | Use |
|---|---|
NavigationBar | The bar, which holds the items |
NavigationBarItem | A destination, with its icon and label. See Slots. |
import { Icon, NavigationBar, NavigationBarItem } from '@material-web/react'
import { Text } from 'react-aria-components'Basic navigation bar
Put one NavigationBarItem per destination in a NavigationBar, each with its icon and its label.
import { Icon, NavigationBar, NavigationBarItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'
const destinations = [
['Home', 'home'],
['Browse', 'explore'],
['Radio', 'radio'],
['Library', 'library_music'],
] as const
export default function Basic() {
const [page, setPage] = useState<string>('Home')
return (
<div className="flex h-80 w-full flex-col">
<div className="flex-1 overflow-y-auto p-space-300 text-headline-sm">{page}</div>
<nav aria-label="Music">
<NavigationBar>
{destinations.map(([name, icon]) => (
<NavigationBarItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
<Icon name={icon} />
<Text slot="label">{name}</Text>
</NavigationBarItem>
))}
</NavigationBar>
</nav>
</div>
)
}The items can be vertical or horizontal. Set layout on the NavigationBar to vertical (the default) or horizontal.
Vertical items
A vertical item shows its label under its icon. Use vertical items in a compact window, like a phone.
import { Icon, NavigationBar, NavigationBarItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'
const destinations = [
['Home', 'home'],
['Browse', 'explore'],
['Library', 'library_music'],
] as const
export default function Vertical() {
const [page, setPage] = useState<string>('Home')
return (
<div className="flex h-64 flex-col justify-end">
<NavigationBar aria-label="Main">
{destinations.map(([name, icon]) => (
<NavigationBarItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
<Icon name={icon} />
<Text slot="label">{name}</Text>
</NavigationBarItem>
))}
</NavigationBar>
</div>
)
}Horizontal items
A horizontal item shows its icon and its label side by side. Use horizontal items in a medium window, like a tablet: set layout="horizontal" and arrangement="centered".
import { Icon, NavigationBar, NavigationBarItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'
const destinations = [
['Home', 'home'],
['Browse', 'explore'],
['Library', 'library_music'],
] as const
export default function Horizontal() {
const [page, setPage] = useState<string>('Home')
return (
<div className="flex h-64 flex-col justify-end">
<NavigationBar aria-label="Main" layout="horizontal" arrangement="centered">
{destinations.map(([name, icon]) => (
<NavigationBarItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
<Icon name={icon} />
<Text slot="label">{name}</Text>
</NavigationBarItem>
))}
</NavigationBar>
</div>
)
}Note: The bar does not switch its layout or its arrangement on its own as the window changes size.
Slots
- label
| Slot | Child | Use |
|---|---|---|
label | A Text | The label, which names the item |
Write an item's icon and label as its children. The Icon is sized and colored by the item. A Text with another slot, or with none, throws an error.
For an item with an icon alone, set an aria-label.
Selection
Set isSelected on the NavigationBarItem of the view being shown, and handle a press with onChange or onPress. A selected item shows the active indicator and fills its Icon.
<NavigationBarItem isSelected={page === 'home'} onChange={() => setPage('home')}>
<Icon name="home" />
<Text slot="label">Home</Text>
</NavigationBarItem>- 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
defaultSelectedinstead, an item keeps its own selection, and a second press turns it off.
Disabled
Set isDisabled on an item to disable it. It cannot be pressed, and the arrow keys skip it.
import { Icon, NavigationBar, NavigationBarItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'
const destinations = [
['Home', 'home'],
['Browse', 'explore'],
['Radio', 'radio'],
['Library', 'library_music'],
] as const
export default function Disabled() {
const [page, setPage] = useState<string>('Home')
return (
<div className="flex h-64 flex-col justify-end">
<NavigationBar aria-label="Main">
{destinations.map(([name, icon]) => (
<NavigationBarItem
key={name}
isSelected={page === name}
isDisabled={name === 'Radio'}
onChange={() => setPage(name)}
>
<Icon name={icon} />
<Text slot="label">{name}</Text>
</NavigationBarItem>
))}
</NavigationBar>
</div>
)
}Arrangement
Set arrangement to equal-weight (the default) or centered. It is independent of layout.
equal-weightdivides the bar equally between the items. Use it in a compact window.centeredgroups the items in the middle of the bar. Use it in a medium window. Three items take 60% of its width between them, four 70%, five 80%, six 90%, and seven or more all of it. An item whose content needs more room takes up to an equal share.
import { Icon, NavigationBar, NavigationBarItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'
const destinations = [
['Home', 'home'],
['Browse', 'explore'],
['Radio', 'radio'],
['Library', 'library_music'],
] as const
export default function Arrangements() {
const [page, setPage] = useState<string>('Home')
const items = destinations.map(([name, icon]) => (
<NavigationBarItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
<Icon name={icon} />
<Text slot="label">{name}</Text>
</NavigationBarItem>
))
return (
<div className="flex h-64 flex-col justify-end gap-space-300">
<NavigationBar aria-label="Equal weight" layout="horizontal" arrangement="equal-weight">
{items}
</NavigationBar>
<NavigationBar aria-label="Centered" layout="vertical" arrangement="centered">
{items}
</NavigationBar>
</div>
)
}A component of your own between the bar and its items can render several items.
Placement
The bar is as wide as its container and is not positioned for you. Place it at the bottom of the window yourself:
- In a column: Make the bar the last row of a column as tall as the window, under content that scrolls, as in this example.
- Fixed: Or fix it to the bottom of the window, with
className="fixed inset-x-0 bottom-0 z-navigation"on its<nav>. See Stacking order.
Song 1
Song 2
Song 3
Song 4
Song 5
Song 6
Song 7
Song 8
Song 9
Song 10
Song 11
Song 12
import { Icon, NavigationBar, NavigationBarItem } from '@material-web/react'
import { useState } from 'react'
import { Text } from 'react-aria-components'
const destinations = [
['Home', 'home'],
['Browse', 'explore'],
['Radio', 'radio'],
['Library', 'library_music'],
] as const
const rows = Array.from({ length: 12 }, (_, index) => (
<p key={index} className="m-0 px-space-300 py-space-150 text-body-lg">
Song {index + 1}
</p>
))
export default function FixedPositioning() {
const [page, setPage] = useState<string>('Home')
return (
<div className="flex h-80 flex-col">
<div tabIndex={0} className="focus-ring-inset flex-1 overflow-y-auto">
{rows}
</div>
<nav aria-label="Music player">
<NavigationBar>
{destinations.map(([name, icon]) => (
<NavigationBarItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
<Icon name={icon} />
<Text slot="label">{name}</Text>
</NavigationBarItem>
))}
</NavigationBar>
</nav>
</div>
)
}The bar is not a landmark. Put it in a <nav> of your own, or name it with an aria-label.
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, NavigationBar, NavigationBarItem } from '@material-web/react'
import { Text } from 'react-aria-components'
import { useLocation, useNavigate } from 'react-router'
const destinations = [
['/', 'Home', 'home'],
['/browse', 'Browse', 'explore'],
['/library', 'Library', 'library_music'],
] as const
function MainNavigation() {
const { pathname } = useLocation()
const navigate = useNavigate()
return (
<nav aria-label="Main">
<NavigationBar>
{destinations.map(([path, name, icon]) => (
<NavigationBarItem
key={path}
isSelected={pathname === path}
onPress={() => navigate(path)}
>
<Icon name={icon} />
<Text slot="label">{name}</Text>
</NavigationBarItem>
))}
</NavigationBar>
</nav>
)
}Customization
Pass classes in className to restyle the bar or one item. A class of yours replaces the component's own class for the same property. See Styling for the classes, the states and the theme.
Pass a function as an item's children to render them from its state: isHovered, isPressed, isFocused, isFocusVisible, isSelected and isDisabled. In this example, an item shows another icon when it is selected.
import { Icon, NavigationBar, NavigationBarItem } 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-64 flex-col justify-end">
<NavigationBar aria-label="Main">
{destinations.map((name) => (
<NavigationBarItem key={name} isSelected={page === name} onChange={() => setPage(name)}>
{({ isSelected }) => (
<>
<Icon name={isSelected ? 'check_circle' : 'circle'} />
<Text slot="label">{name}</Text>
</>
)}
</NavigationBarItem>
))}
</NavigationBar>
</div>
)
}- States: Style an item's state with its variant, such as
hover:,pressed:orselected:. See States. - Functions: The
classNameandstyleof an item can be functions of the same state, and the children of aNavigationBarcan be a function too. See Children as a function. - Every navigation bar: To restyle all the navigation bars at once, change the theme.
Use cases
Material lists what people should be able to do with assistive technology:
- Move between navigation destinations
- Select a particular navigation destination from a set
- Get appropriate feedback based on input type
Interaction and style
- States: Hovering an item shows a state layer in its indicator's box, and a press shows a ripple there. Keyboard focus shows a focus ring inside the item's edges, and a press with a mouse or touch does not. A disabled item shows no state layer.
- Touch target: An item takes a press anywhere in its part of the bar, which is at least 64dp tall, more than the 48dp minimum touch target.
- Color contrast: With the default colors, the icons keep 3:1 contrast and the labels 4.5:1 with what is behind them, in the light and dark schemes at every contrast level. Keep that contrast when you change the bar's colors.
- Text size: A vertical item's label wraps as the text grows, and the bar grows taller with it. A horizontal item's label stays on one line, and is cut where the item has no more room. Material advises keeping the full label visible at up to twice the text size, so keep labels short.
- Motion: With reduced motion, the active indicator appears at once.
- Forced colors: In forced colors, the active indicator is drawn in the system's highlight color, and a disabled item in its gray text color.
- Right to left: The bar is mirrored in a right-to-left direction, with the first item at the right end.
- Selected icon: A selected item fills its
Icon, as Material advises. For an icon with no filled style, Material advises a heavier one: give it a font weight class, such asfont-semibold, while the item is selected. See Customization.
Focus
- Tab moves the focus into the bar once: to its first item, as Material advises, or to the item focused last. The next Tab leaves the bar, and Shift+Tab comes back to the item focused last.
- A disabled item cannot be focused.
Keyboard navigation
| Keys | Actions |
|---|---|
| Tab | Move the focus into the bar, or out of it |
| Left or Right arrow | Move the focus to the previous or next item |
| Space or Enter | Select the focused item |
- The arrow keys skip a disabled item, and stop at the first and the last.
- They are reversed in a right-to-left locale, which is set with an
I18nProvider, not withdir. - An item's
onKeyDownis not called for the arrow keys.
Labeling elements
- The bar is a toolbar, and each item a toggle button. Name the bar with an
aria-label, or put it in a<nav>. - The label names each item. Material advises a name that matches the destination, and a more descriptive one where the label is ambiguous: set an
aria-labelthat includes the label, such as Music library for an item labeled Library. - For an item with no label, set an
aria-label. - A selected item reports it with
aria-pressed.
NavigationBar
layout'horizontal' | 'vertical'Default'vertical'The navigation items can be vertical or horizontal.
They can be vertical, with the text below the icon and indicator, or horizontal, with the icon and text beside each other inside the indicator.
vertical: Use vertical items in compact windows, like mobilehorizontal: Use horizontal items in medium windows, like tablets
arrangement'equal-weight' | 'centered'Default'equal-weight'Vertical navigation items dynamically change width to equally fit the container.
Horizontal items are centered in the nav bar with outer margins.
equal-weight: The items are equally distributed on the Short Navigation Bar. This configuration is recommended for small width screens.centered: The items are centered on the Short Navigation Bar. This configuration is recommended for medium width screens.
SpecsGuidelinesreference / kotlin / androidx / compose / material3 / ShortNavigationBarArrangement
Props of NavigationBar (10)
classNameClassNameOrFunction<ToolbarRenderProps>Default'react-aria-Toolbar'The CSS className for the element. A function may be provided to compute the class based on component state.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit
nullvalue indicates that the local props completely override all props received from a parent.childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
styleCSSProperties | (values: ToolbarRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties | undefinedThe inline style for the element. A function may be provided to compute the style based on component state.
renderDOMRenderFunction<'div', ToolbarRenderProps>Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if
<button>is expected, you cannot render an<a>). - Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
- You must render the expected element type (e.g. if
refRef<HTMLDivElement>
NavigationBar also takes the 70 attributes and events of the element it renders.
NavigationBarItem
Props of NavigationBarItem (37)
classNameClassNameOrFunction<ToggleButtonRenderProps>Default'react-aria-ToggleButton'The CSS className for the element. A function may be provided to compute the class based on component state.
idKeyWhen used in a ToggleButtonGroup, an identifier for the item in
selectedKeys. When used standalone, a DOM id.aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
onClick(e: MouseEvent<FocusableElement>) => voidNot recommended – use
onPressinstead.onClickis an alias foronPressprovided for compatibility with other libraries.onPressprovides additional event details for non-mouse interactions.isDisabledbooleanWhether the button is disabled.
onPress(e: PressEvent) => voidHandler that is called when the press is released over the target.
onPressStart(e: PressEvent) => voidHandler that is called when a press interaction starts.
onPressEnd(e: PressEvent) => voidHandler that is called when a press interaction ends, either over the target or when the pointer leaves the target.
onPressChange(isPressed: boolean) => voidHandler that is called when the press state changes.
onPressUp(e: PressEvent) => voidHandler that is called when a press is released over the target, regardless of whether it started on the target or not.
autoFocusbooleanWhether the element should receive focus on render.
onFocus(e: FocusEvent<Target>) => voidHandler that is called when the element receives focus.
onBlur(e: FocusEvent<Target>) => voidHandler that is called when the element loses focus.
onFocusChange(isFocused: boolean) => voidHandler that is called when the element's focus status changes.
onKeyDown(e: KeyboardEvent) => voidHandler that is called when a key is pressed.
onKeyUp(e: KeyboardEvent) => voidHandler that is called when a key is released.
aria-disabled'true' | 'false' | booleanIndicates whether the element is disabled to users of assistive technology.
aria-expanded'true' | 'false' | booleanIndicates whether the element, or another grouping element it controls, is currently expanded or collapsed.
aria-haspopup'dialog' | 'menu' | 'true' | 'false' | 'grid' | 'listbox' | 'tree' | booleanIndicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element.
aria-controlsstringIdentifies the element (or elements) whose contents or presence are controlled by the current element.
aria-pressed'true' | 'false' | 'mixed' | booleanIndicates the current "pressed" state of toggle buttons.
preventFocusOnPressbooleanWhether to prevent focus from moving to the button when pressing it.
Caution, this can make the button inaccessible and should only be used when alternative keyboard interaction is provided, such as ComboBox's MenuTrigger or a NumberField's increment/decrement control.
excludeFromTabOrderbooleanWhether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available.
isSelectedbooleanWhether the element should be selected (controlled).
defaultSelectedbooleanWhether the element should be selected (uncontrolled).
onChange(isSelected: boolean) => voidHandler that is called when the element's selection state changes.
onHoverStart(e: HoverEvent) => voidHandler that is called when a hover interaction starts.
onHoverEnd(e: HoverEvent) => voidHandler that is called when a hover interaction ends.
onHoverChange(isHovering: boolean) => voidHandler that is called when the hover state changes.
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit
nullvalue indicates that the local props completely override all props received from a parent.childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
styleCSSProperties | (values: ToggleButtonRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties | undefinedThe inline style for the element. A function may be provided to compute the style based on component state.
renderDOMRenderFunction<'button', ToggleButtonRenderProps>Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if
<button>is expected, you cannot render an<a>). - Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
- You must render the expected element type (e.g. if
refRef<HTMLButtonElement>
NavigationBarItem also takes the 69 attributes and events of the element it renders.