UnderlineNav

Use an underlined nav to allow tab like navigation with overflow behaviour in your UI.
  • Beta
  • Reviewed for accessibility
This documentation is moving to a new home. Please update any link or bookmark that points to this page. The new content can be found here.
import {UnderlineNav} from '@primer/react'

Examples

Simple

With Icons

Overflow Behaviour

Component first hides icons if they present to optimize for space and show as many items as possible. If there is still an overflow, it will display the items that don't fit in the More menu.

Loading State For Counters

With React Router

import {Link, useMatch, useResolvedPath} from 'react-router-dom'
import {UnderlineNav} from '@primer/react'
function UnderlineNavItem({to, children, ...rest}) {
const resolved = useResolvedPath(to)
const isCurrent = useMatch({path: resolved.pathname, end: true})
return (
<UnderlineNav.Item as={Link} to={to} aria-current={isCurrent ? 'page' : undefined} {...rest}>
{children}
</UnderlineNav.Item>
)
}
const Navigation = () => {
return (
<UnderlineNav aria-label="Repository">
<UnderlineNavItem to="/code" counter={4}>
Code
</UnderlineNavItem>
<UnderlineNavItem to="/issues" counter={44}>
Issues
</UnderlineNavItem>
<UnderlineNavItem to="/pulls">Pull Requests</UnderlineNavItem>
</UnderlineNav>
)
}

With Next.js

import {useRouter} from 'next/router'
import Link from 'next/link'
import {UnderlineNav} from '@primer/react'
function UnderlineNavItem({href, children, ...rest}) {
const router = useRouter()
const isCurrent = typeof href === 'string' ? router.asPath === href : router.pathname === href.pathname
return (
<UnderlineNav.Item as={Link} href={href} aria-current={isCurrent ? 'page' : undefined} {...rest}>
{children}
</UnderlineNav.Item>
)
}
const Navigation = () => {
return (
<UnderlineNav aria-label="Repository">
<UnderlineNavItem href="/code" counter={4}>
Code
</UnderlineNavItem>
<UnderlineNavItem href="/issues" counter={44}>
Issues
</UnderlineNavItem>
<UnderlineNavItem href="/pulls">Pull Requests</UnderlineNavItem>
</UnderlineNav>
)
}

Props

UnderlineNav

NameTypeDefaultDescription
afterSelect
(event) => void

The handler that gets called when a nav link child is selected

aria-label
string

A unique name for the rendered 'nav' landmark. It will also be used to label the arrow buttons that control the scroll behaviour on coarse pointer devices. (I.e. 'Scroll ${aria-label} left/right')

children Required
UnderlineNav.Item[]
loadingCounters
boolean
false

Whether the navigation items are in loading state. Component waits for all the counters to finish loading to prevent multiple layout shifts.

sx
SystemStyleObject

UnderlineNav.Item

NameTypeDefaultDescription
aria-current
| 'page' | 'step' | 'location' | 'date' | 'time' | true | false
false

Set aria-current to "page" to indicate that the item represents the current page. Set aria-current to "location" to indicate that the item represents the current location on a page. For more information about aria-current, see MDN.

counter
number

The number to display in the counter label.

href
string

The URL that the item navigates to. 'href' is passed to the underlying '<a>' element. If 'as' is specified, the component may need different props and 'href' is ignored. (Required prop for the react router is 'to' for example)

icon
Component

The leading icon comes before item label

onSelect
(event) => void

The handler that gets called when a nav link is selected. For example, a manual route binding can be done here(I.e. 'navigate(href)' for the react router.)

as
React.ElementType
"a"

The underlying element to render — either a HTML element name or a React component.

sx
SystemStyleObject
Additional props are passed to the <a> element. See a docs for a list of props accepted by the <a> element.

Status

Alpha

  • Component props and basic example usage of the component are documented on primer.style/react.
  • Component does not have any unnecessary third-party dependencies.
  • Component can adapt to different themes.
  • Component can adapt to different screen sizes.
  • Component has robust unit test coverage (100% where achievable).
  • Component has visual regression coverage of its default and interactive states.
  • Component does not introduce any axe violations.
  • Component has been manually reviewed by the accessibility team and any resulting issues have been addressed.

Beta

  • Component is used in a production application.
  • Common usage examples are documented on primer.style/react.
  • Common usage examples are documented in storybook stories.
  • Component has been reviewed by a systems designer and any resulting issues have been addressed.
  • Component does not introduce any performance regressions.

Stable

  • Component API has been stable with no breaking changes for at least one month.
  • Feedback on API usability has been sought from developers using the component and any resulting issues have been addressed.
  • Component has corresponding design guidelines documented in the interface guidelines.
  • Component has corresponding Figma component in the Primer Web library.
  • Tooling (such as linters, codemods, etc.) exists to prevent further use of alternatives.