Skip to content

Navigation rail

Expressive

Top-level destinations along the start edge, collapsed or expanded, in medium windows.

import { NavigationRail, NavigationRailItem, useNavigationRail } from '@windmorph/react';

Props

By name or description, across the 38 props below

NavigationRail 16 props

  • childrentype ReactNodeRequired

    NavigationRailItem elements.

  • expandedtype boolean

    Whether the rail is expanded (controlled).

  • defaultExpandedtype booleandefault false

    Whether the rail starts expanded (uncontrolled).

  • modaltype booleandefault false

    Expand over the page as a modal sheet with a scrim instead of pushing content. Pressing outside, Escape or choosing an item collapses it.

  • hideOnCollapsetype boolean | "compact"default false

    With modal, show nothing while collapsed (the expanded rail slides in). compact does so only below 600px wide, in CSS so the server's HTML is right: one rail then serves as the rail on larger windows and as the drawer on a phone.

  • headertype ReactNode | ((state: { expanded: boolean; toggle: () => void; }) => ReactNode)

    Content above the items, e.g. a menu button and a FAB; a function receives the state.

  • itemAligntype "center" | "top"default "top"

    Align the items as a group to the top, or to the middle of the space below the header (M3 recommends centre on tablets, within easier reach). The header stays at the top.

  • dividertype booleandefault false

    Draw a divider on the edge next to the content.

  • indicatorMotiontype "expand" | "slide"default "expand"

    How the current item's pill moves to a newly chosen item. expand (M3: "the active indicator expands from the center of the icon") grows it in place. slide moves it from the previously chosen item on the fast spatial spring while the rail is expanded (in place or modal), as in Google's Expressive showcase; an extension beyond the M3 spec. The collapsed rail always expands, and reduced motion fades the pill in place.

  • reftype Ref<HTMLElement>

    Ref to the root element.

Events

  • onExpandedChangetype ((expanded: boolean) => void)

    Called when the rail expands or collapses.

Text

  • aria-labeltype string

    Accessible name for the navigation rail. Required unless aria-labelledby is set.

  • aria-labelledbytype string

    The id of the element that names the navigation rail. Required unless aria-label is set.

Also accepts the root element's HTML attributes (id, data-*, event handlers).

NavigationRailItem 22 props

  • icontype ReactElement<unknown, string | JSXElementConstructor<any>>Required

    The icon. Hidden from assistive tech; the label names the item.

  • childrentype ReactNodeRequired

    The label.

  • currentIcontype ReactElement<unknown, string | JSXElementConstructor<any>>

    Icon shown while the item is the current destination, e.g. a filled variant.

  • currenttype booleandefault false

    Marks the current destination (aria-current="page"): the page the app is on, which the item shows with its active indicator.

  • badgetype ReactElement<BadgeProps, string | JSXElementConstructor<any>>

    A Badge for dynamic information, such as a count. It overlaps the icon's top end, or sits after the label in the expanded rail. It describes the item to assistive tech with its text, or with badgeLabel.

  • badgeLabeltype string

    What the badge means, read after the item's name, e.g. "3 new messages". Defaults to the badge's label or text; a small (dot) badge reads its labels.newNotification ("New notification"), as M3 asks of badges without a count.

  • reftype Ref<HTMLButtonElement> | Ref<HTMLAnchorElement>

    Ref to the root element: the <button>, or the <a> with href.

  • disabledtype booleandefault false

    Disables the element: it can't be pressed or focused, and shows the disabled style.

  • valuetype string | number | readonly string[]

    The value a submit button sends with its form, under its name. On a toggle (toggle) it's a string naming the button in a selection group instead, required to take part in the group's selection.

  • draggedtype booleandefault false

    The element is being dragged (e.g. a card picked up to move it): sets data-dragged for the dragged state layer. A consumer's own data-dragged attribute counts too.

  • preventFocusOnPresstype booleandefault false

    Don't move focus to the element on press (set by menu triggers).

  • astype "button"default "button"

    The element rendered: a native <button>.

  • hreftype string

    Renders a link. Navigation goes through React Aria's RouterProvider when one is set.

Events

  • onReselecttype (() => void)

    Called when the current destination is pressed again. M3: re-selecting the active destination resets the page's scroll position to the top, so scroll to the top here.

Also accepts every <a> or <button> attribute.

Tokens

M3's component tokens as CSS variables. Each is unset until you set it, on :root, a theme or any ancestor, and falls back to the default shown. The root carries data-variant (and data-size where it has sizes), which picks the variables that apply. See Component tokens in the Customisation guide.

Navigation rail tokens (md-comp-nav-rail)
VariableDefaultPaintsCompose
--md-comp-nav-rail-collapsed-container-colorvar(--md-sys-color-surface)The collapsed rail.NavigationRailCollapsedTokens.ContainerColor
--md-comp-nav-rail-expanded-container-colorvar(--md-sys-color-surface)The expanded rail, beside the content.NavigationRailExpandedTokens.ContainerColor
--md-comp-nav-rail-expanded-modal-container-colorvar(--md-sys-color-surface-container)The modal expanded rail, over the content.NavigationRailExpandedTokens.ModalContainerColor
--md-comp-nav-rail-expanded-modal-container-shapevar(--md-sys-shape-corner-large)The modal expanded rail's end corners.NavigationRailExpandedTokens.ModalContainerShape
--md-comp-nav-rail-item-active-indicator-colorvar(--md-sys-color-secondary-container)The current item's pill.NavigationRailTokens.ItemActiveIndicatorColor
--md-comp-nav-rail-item-active-icon-colorvar(--md-sys-color-on-secondary-container)The current item's icon.NavigationRailTokens.ItemActiveIconColor
--md-comp-nav-rail-item-inactive-icon-colorvar(--md-sys-color-on-surface-variant)The other items’ icons.NavigationRailTokens.ItemInactiveIconColor
--md-comp-nav-rail-item-active-label-text-colorvar(--md-sys-color-secondary)Varies with layout.The current item's label: `secondary` below its icon, `on-secondary-container` beside it (in the pill).NavigationRailTokens.ItemActiveLabelTextColor
--md-comp-nav-rail-item-inactive-label-text-colorvar(--md-sys-color-on-surface-variant)The other items’ labels.NavigationRailTokens.ItemInactiveLabelTextColor

Hooks

useNavigationRail

useNavigationRail signature
useNavigationRail(): NavigationRailState

The rail around a component, for a custom destination that sits in a NavigationRail beside its items and changes with it. Throws outside a rail.

Returns NavigationRailState

  • expandedtype booleanRequired

    Whether the rail shows its labels beside the icons (expanded, or the modal sheet).

  • modaltype booleanRequired

    Whether this is the rail's modal sheet.

  • closetype () => voidRequired

    Collapses the modal sheet; does nothing in the rail itself.

Spec status

Built to the M3 Expressive navigation rail spec. Each item of its variants and configurations links to a live example; where the M3 Expressive column of the spec differs from the original M3 one, it wins.

  • Collapsed navigation rail

    Built

    Variant · States, Inline

  • Expanded navigation rail

    Built

    Variant · Inline, States

  • Expanded layout: standard

    Built

    Configuration · Inline

  • Expanded layout: modal

    Built

    Configuration · Modal

  • Expanded behavior: hide when collapsed

    Built

    Configuration · Hide on collapse

  • With a menu and a FAB

    Built

    Use case · Inline

  • Badges, selected icons and disabled items

    Built

    Use case · States

  • Mode change motion

    Differs

    Items switch layout at once and the labels fade in at their new place, while the rail's width does the spring, instead of cross-fading each label as it slides.

  • itemAlign="center" with a top header

    Extension

    Centres the items in the space below the header, which stays at the top, and an overflowing rail still scrolls to every item.

  • onReselect

    Extension

    Runs when the current destination is pressed again, for resetting its scroll position to the top.

  • --m3-rail-collapsed-width

    Extension

    A narrower collapsed rail, such as M3's 80px baseline, keeps the expand motion and every inset.

  • indicatorMotion="slide"

    Extension

    Not in the M3 spec, where the active indicator expands from the centre of the icon: on the expanded rail the pill slides from the previous item on the fast spatial spring, as in Google's Expressive showcase. The collapsed rail always expands, and reduced motion fades the pill in place.

  • Navigation rail (baseline)

    Not built

    Not recommended in M3 Expressive; use the collapsed rail, or set --m3-rail-collapsed-width to 80px for its width.

  • Navigation drawer

    Not built

    Deprecated in M3 Expressive; use the expanded rail, modal for the modal drawer.

Bundle size

Gzipped, as published: the component and the library code it imports. React, React Aria, Motion and the other dependencies aren't counted, and your bundler's minifier makes it smaller still. CI fails a change that grows it past its budget.

  • NavigationRail, NavigationRailItem: 29.6 kB

Differences from Compose

  • The expanded width is the widest label plus 104px of item chrome, clamped to 220–360px, exposed as the --m3-rail-width variable, so a consumer w-* class still wins.
  • As in Compose, the items' height and spacing spring with the rail's width (64 → 48px and 4 → 0px) and each icon moves to its new place on the default spatial spring. Where Compose cross-fades each label while sliding it, the labels fade in at their new place. Under reduced motion it all changes at once.
  • The modal rail's sheet is portalled to the end of the page, so a token set inline on an element around the rail doesn't reach it. Set the --md-comp-nav-rail-expanded-modal-* tokens on :root or a theme, in a class on a ThemeScope (overlays opened in it carry its class), or with classNames.sheet.
  • The current destination is aria-current="page", styled through data-current: it is the page the app is on, not a selection, so the prop is current (selected, its name before 0.2.9, still works).