# 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.

Source: https://vaneui.com/docs/basic-components/navlink

Import: `import { NavLink, Badge, Card, Col, Divider, Row, Text, ThemeProvider } from "@vaneui/ui"`

NavLink is one entry in a sidebar or a tab bar and points at one destination. For a link inside a sentence, use [Link](/docs/typography-components/link); for a list of actions that opens from a trigger, use [Menu](/docs/overlay-components/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`.

```tsx demo
<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](/docs/basic-components/button#how-button-centers-its-label).

```tsx demo
<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).

```tsx demo
<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.

```tsx demo
<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.

```tsx demo
<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.

```tsx demo
<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.

```tsx demo
<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.

```tsx demo
<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.

```tsx demo
<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.

```tsx demo
<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.

```tsx demo
<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](/docs/layout-components/row) with `noGap` and `borderB`, so the bar sits on the row's bottom border.

```tsx demo
<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.

```tsx demo
<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.

```tsx
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.

```tsx demo
<ThemeProvider themeDefaults={{ navLink: { root: { md: true } } }}>
  <NavLink href="/docs" active>Docs</NavLink>
</ThemeProvider>
```

```css
.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](#customizing) shows.

## Props

| Prop | Category | Default | Description |
| --- | --- | --- | --- |
| `primary` | Appearance | yes | Primary color appearance (gray) |
| `accent` | Appearance |  | Accent color appearance (rose) |
| `secondary` | Appearance |  | Secondary color appearance (gray) |
| `tertiary` | Appearance |  | Tertiary color appearance |
| `success` | Appearance |  | Success color appearance (green) |
| `danger` | Appearance |  | Danger color appearance (red) |
| `warning` | Appearance |  | Warning color appearance (amber) |
| `info` | Appearance |  | Info color appearance (cyan) |
| `inheritAppearance` | Appearance |  | Inherit appearance from parent — suppresses own data-appearance/data-variant, uses parent's CSS variables |
| `disabled` | Disabled |  | Disable the component — reduces opacity, changes cursor to not-allowed, and prevents interaction |
| `flex1` | Flex |  | Take up remaining space (= `flex-1`, i.e. `flex: 1 1 0%`) |
| `flexAuto` | Flex |  | Grow but respect intrinsic size (= `flex-auto`, i.e. `flex: 1 1 auto`) |
| `flexNone` | Flex |  | Don't grow and don't shrink (= `flex-none`, i.e. `flex: none`) |
| `fontSans` | Font Family | yes | Sans-serif font family — emits font-sans |
| `fontSerif` | Font Family |  | Serif font family — emits font-serif |
| `fontMono` | Font Family |  | Monospace font family — emits font-mono |
| `fontHeading` | Font Family |  | Heading font family (defaults to sans, customizable via --font-heading) — emits font-heading |
| `italic` | Font Style |  | Italic font style |
| `notItalic` | Font Style |  | Not italic (normal) font style |
| `fontThin` | Font Weight |  | Thin font weight (100) — emits font-thin |
| `fontExtralight` | Font Weight |  | Extra light font weight (200) — emits font-extralight |
| `fontLight` | Font Weight |  | Light font weight (300) — emits font-light |
| `fontNormal` | Font Weight |  | Normal font weight (400) — emits font-normal |
| `fontMedium` | Font Weight |  | Medium font weight (500) — emits font-medium |
| `fontSemibold` | Font Weight |  | Semibold font weight (600) — emits font-semibold |
| `fontBold` | Font Weight |  | Bold font weight (700) — emits font-bold |
| `fontExtrabold` | Font Weight |  | Extra bold font weight (800) — emits font-extrabold |
| `fontBlack` | Font Weight |  | Black font weight (900) — emits font-black |
| `indicatorStart` | Indicator |  | 1px rail on the start edge that turns into a 2px accent mark when active or selected |
| `indicatorBottom` | Indicator |  | 1px accent bar on the bottom edge while active, for tab bars |
| `inheritSize` | Inherit Size |  | Inherit font-size and line-height from the nearest parent typography element |
| `noInheritSize` | Inherit Size |  | Keep own font-size; do not inherit from parent |
| `padding` | Padding | yes | Enable internal padding |
| `paddingX` | Padding |  | Enable only horizontal padding |
| `paddingY` | Padding |  | Enable only vertical padding |
| `noPadding` | Padding |  | Disable internal padding |
| `pill` | Shape |  | Fully rounded corners (circular) |
| `sharp` | Shape |  | No rounded corners (square) |
| `rounded` | Shape | yes | Medium rounded corners |
| `noShrink` | Shrink |  | Prevent the flex item from shrinking below its content size (= `shrink-0`) |
| `xs` | Size |  | Extra small size |
| `sm` | Size | yes | Small size |
| `md` | Size |  | Medium size |
| `lg` | Size |  | Large size |
| `xl` | Size |  | Extra large size |
| `skeleton` | Skeleton |  | Show the component as a loading placeholder in its own size and shape. On a container, every component inside it becomes a placeholder too |
| `textLeft` | Text Align | yes | Align text to left (physical side, does not flip under RTL) |
| `textCenter` | Text Align |  | Align text to center |
| `textRight` | Text Align |  | Align text to right (physical side, does not flip under RTL) |
| `textJustify` | Text Align |  | Justify text |
| `textStart` | Text Align |  | Align text to the reading-direction start (left in LTR, right in RTL) |
| `textEnd` | Text Align |  | Align text to the reading-direction end (right in LTR, left in RTL) |
| `underline` | Text Decoration |  | Add underline decoration below text |
| `lineThrough` | Text Decoration |  | Add strikethrough/line-through decoration across text |
| `noUnderline` | Text Decoration |  | Remove ALL text-decoration — underline, line-through, and overline (text-decoration: none) |
| `overline` | Text Decoration |  | Add overline decoration above text |
| `uppercase` | Text Transform |  | Transform text to uppercase |
| `lowercase` | Text Transform |  | Transform text to lowercase |
| `capitalize` | Text Transform |  | Capitalize first letter of each word |
| `normalCase` | Text Transform |  | Normal text case (no transformation) |
| `filled` | Variant |  | Filled variant - solid background with contrasting text color |
| `outline` | Variant | yes | Outline variant - transparent background with border and colored text |
| `ghost` | Variant |  | Ghost variant - transparent background, no border, appearance-colored text, tinted hover background |
| `breakNormal` | Word Break |  | Reset breaking - overflow-wrap and word-break normal |
| `breakWords` | Word Break |  | Break long words to prevent overflow - overflow-wrap: break-word |
| `breakAll` | Word Break |  | Break between any two characters - word-break: break-all |
| `breakKeep` | Word Break |  | Do not break CJK text - word-break: keep-all |

Layout and utility props (Border, Cursor, Display, Flex Direction, Focus Visible, Gap, Height, Hide, Items, Justify, Overflow, Position, Responsive Sizing, Responsive Width, Ring, Shadow, Show, Transition, Transparent, Whitespace, Width, Wrap) are listed on [Common Props](https://vaneui.com/docs/reference/common-props.md).
