Skip to content

Basic Components

[02] Basic Components

NavLink

A navigation link for sidebars, nav menus and tab bars. Supports active state, icons, start and bottom indicator lines, and renders as anchor or button.

SourceEdit this page

NavLink is one entry in a sidebar or a tab bar and points at one destination. For a link inside a sentence, use Link; for a list of actions that opens from a trigger, use Menu. In its <button> form (no href), NavLink defaults to type="button", so a click inside a <form> does not submit the form.

Basic usage

NavLink renders as an <a> when href is provided. Defaults to sm size, primary appearance, outline variant, wFull and textLeft layout, ideal for stacking inside a sidebar Col.

<Col className="w-64">
<NavLink href="#">Home</NavLink>
<NavLink href="#">Dashboard</NavLink>
<NavLink href="#">Settings</NavLink>
</Col>

Sizes

NavLink supports five sizes: xs, sm (default), md, lg, xl. Size drives font-size, padding, gap, and border-radius simultaneously via CSS variables. At every size the label is centered by the height of its capital letters: NavLink trims its label span (vane-nav-link-label) the way Button trims its text.

<Col className="w-72">
<NavLink xs href="#"><Home size={12} /> XS NavLink</NavLink>
<NavLink href="#"><Home size={14} /> SM NavLink</NavLink>
<NavLink md href="#"><Home size={16} /> MD NavLink</NavLink>
<NavLink lg href="#"><Home size={18} /> LG NavLink</NavLink>
<NavLink xl href="#"><Home size={20} /> XL NavLink</NavLink>
</Col>

Active state

Use the active prop to indicate the current page. Active NavLinks get a data-active attribute for styling; the href (anchor) form also gets aria-current="page" (the no-href button form omits it).

<Col className="w-64">
<NavLink href="#">Home</NavLink>
<NavLink href="#" active>Dashboard</NavLink>
<NavLink href="#">Settings</NavLink>
<NavLink href="#">Profile</NavLink>
</Col>

With icon

Drop an icon directly inside the NavLink. gap is on by default, so spacing is automatic. React elements rendered before any text become the leading icon; text is auto-wrapped in a label span with truncation; elements after text become trailing accessories.

<Col className="w-64">
<NavLink href="#"><Home size={16} /> Home</NavLink>
<NavLink href="#" active><FileText size={16} /> Documents</NavLink>
<NavLink href="#"><Users size={16} /> Team</NavLink>
<NavLink href="#"><Settings size={16} /> Settings</NavLink>
</Col>

Appearances and variants

NavLinks default to primary outline. Use filled for solid backgrounds. Active state works with all appearances.

<Row flexWrap>
<Col className="w-48">
<Text sm fontSemibold secondary>Outline (default)</Text>
<NavLink href="#" active>Primary</NavLink>
<NavLink href="#" success active>Success</NavLink>
<NavLink href="#" danger active>Danger</NavLink>
<NavLink href="#" secondary active>Secondary</NavLink>
</Col>
<Col className="w-48">
<Text sm fontSemibold secondary>Filled</Text>
<NavLink href="#" filled active>Primary</NavLink>
<NavLink href="#" success filled active>Success</NavLink>
<NavLink href="#" danger filled active>Danger</NavLink>
<NavLink href="#" secondary filled active>Secondary</NavLink>
</Col>
</Row>

Shapes

NavLink is rounded by default. sharp fits a full-bleed sidebar where the item runs to the panel edge; pill suits a horizontal nav.

<Col className="w-72">
<NavLink href="#">Rounded, the default</NavLink>
<NavLink sharp href="#">Sharp</NavLink>
<NavLink pill href="#">Pill</NavLink>
</Col>

Disabled state

Use disabled to prevent interaction. When disabled is combined with href, the href is dropped and it renders as a non-navigable anchor (role="link", aria-disabled="true") that stays focusable but cannot be followed.

<Col className="w-64">
<NavLink href="#"><Home size={16} /> Active Link</NavLink>
<NavLink href="#" disabled><Lock size={16} /> Disabled Link</NavLink>
<NavLink href="#" active><Settings size={16} /> Current Page</NavLink>
<NavLink href="#" disabled><Users size={16} /> Restricted</NavLink>
</Col>

As Button (no href)

Without href, NavLink renders as a <button>, useful for menu items that trigger actions instead of navigating.

<Col className="w-64">
<NavLink onClick={() => alert('Profile')}><User size={16} /> Profile</NavLink>
<NavLink onClick={() => alert('Sign out')}><LogOut size={16} /> Sign out</NavLink>
</Col>

Sidebar navigation

A real-world sidebar pattern with icons, active state, dividers, and trailing badges. The active item is semibold on a tinted background, the label takes the free space so a trailing Badge sits at the row's end, and icons take the NavLink's size.

<Card className="w-64" noGap>
<Text sm fontSemibold secondary>Navigation</Text>
<Divider />
<NavLink href="#"><Home /> Home</NavLink>
<NavLink href="#" active><FileText /> Documents</NavLink>
<NavLink href="#"><Mail /> Messages <Badge xs info>3</Badge></NavLink>
<NavLink href="#"><Bell /> Notifications <Badge xs danger>12</Badge></NavLink>
<NavLink href="#"><Star /> Favorites</NavLink>
<Divider />
<NavLink href="#"><Users /> Team</NavLink>
<NavLink href="#"><Settings /> Settings</NavLink>
</Card>

Horizontal navigation

NavLink is wFull so sidebar items fill the column. In a horizontal nav or a tab bar, add wFit so each item keeps its own width.

<Row>
<NavLink href="#" active wFit>Description</NavLink>
<NavLink href="#" wFit>Reviews</NavLink>
<NavLink href="#" wFit>Shipping</NavLink>
</Row>

Indicator lines with indicatorStart and indicatorBottom

To mark the current item with a line as well as a tint, add indicatorStart or indicatorBottom. indicatorStart draws a 1px rail on the start edge of every link, and on the active link it turns into a 2px mark in the accent color. Stacked links join their rails into one line down the sidebar.

<Col noGap className="w-56">
<NavLink href="#" indicatorStart ghost sharp secondary>Installation</NavLink>
<NavLink href="#" indicatorStart ghost sharp active>Core concepts</NavLink>
<NavLink href="#" indicatorStart ghost sharp secondary>Theming</NavLink>
</Col>

indicatorBottom draws an accent bar under the active link, or under a link with aria-selected="true" such as a role="tab" tab, for a tab bar. Put the tabs in a Row with noGap and borderB, so the bar sits on the row's bottom border.

<Row noGap borderB>
<NavLink href="#" indicatorBottom ghost sharp active wFit>Docs</NavLink>
<NavLink href="#" indicatorBottom ghost sharp secondary wFit>Playground</NavLink>
<NavLink href="#" indicatorBottom ghost sharp secondary wFit>Changelog</NavLink>
</Row>

Both indicators are inset shadows, so the text does not move when the active item changes. Inside a dir="rtl" container, the start rail moves to the right edge. The examples pair the indicators with ghost and sharp, so the lines meet square corners, and with secondary on the inactive links. The indicator props need @vaneui/ui 1.4.5 or later, and the aria-selected bar on indicatorBottom needs 1.5.0.

Selected options in a listbox

A NavLink with aria-selected="true" takes the hover fill, so the highlighted option in a listbox, such as a search result picked with the arrow keys, stays visible while the pointer is elsewhere. Give the container role="listbox" and each NavLink role="option". With indicatorStart, the selected option also gets the accent mark.

<Col noGap role="listbox" aria-label="Search results" className="w-56">
<NavLink role="option" aria-selected="true" indicatorStart ghost sharp>Button</NavLink>
<NavLink role="option" aria-selected="false" indicatorStart ghost sharp secondary>Badge</NavLink>
<NavLink role="option" aria-selected="false" indicatorStart ghost sharp secondary>Card</NavLink>
</Col>

In a search box built as a combobox, keyboard focus stays in the input, and aria-activedescendant on the input names the selected option's id. MenuItem takes the same fill for aria-selected="true", but its default role="menuitem" has no selected state, so use NavLink for options.

Keyboard focus

NavLink renders a keyboard focus-visible outline by default so keyboard users can see where they are. Opt out with noFocusVisible.

Next.js Link integration

Use the tag prop to render NavLink as a Next.js Link for client-side navigation.

import Link from 'next/link';
import { NavLink } from '@vaneui/ui';
<NavLink href="/docs" tag={Link} active={pathname === '/docs'}>
Documentation
</NavLink>

Customizing

Set app-wide NavLink defaults with ThemeProvider's themeDefaults. Style the active state by targeting the emitted data-active attribute in your own CSS: extraClasses keys apply only to category props, so an active key there has no effect.

<ThemeProvider themeDefaults={{ navLink: { root: { md: true } } }}>
<NavLink href="/docs" active>Docs</NavLink>
</ThemeProvider>
.vane-nav-link[data-active] {
background: #eff6ff;
color: #1d4ed8;
font-weight: 600;
}

Frequently asked questions

How do I set active from the current route in Next.js?

Read the path with usePathname() in a client component and compare it with the link's href: active={pathname === '/docs'}. For an item whose section has child pages, pathname.startsWith('/docs/') keeps it active on those pages too. The active link gets aria-current="page" and a data-active attribute, and tag={Link} keeps the navigation on the client.

Should a sidebar item be a NavLink or a Button?

Use NavLink. It renders an <a> with href, marks the current page with active and aria-current="page", fills the column with wFull, aligns its text left and truncates a long label. Button is built for actions: it centers its label, fits its content and has no active state. A NavLink without href renders a <button> with type="button" for an item that runs an action.

Why do my NavLink theme defaults have no effect?

NavLink's theme has two parts, root for the link and label for its text span, so defaults go under root: themeDefaults={{ navLink: { root: { md: true } } }}. The flat form { navLink: { md: true } } matches no theme node, and a development build warns about it. The active state is not a theme prop, so style it through the data-active attribute, as Customizing shows.