Skip to main content
0 reactions

How to Style the Active Link in TanStack Start

Card titled Style the Active Link, showing a browser at localhost:3000/books with Home, Books and About links, where Books is filled and underlined.
Style the active link in TanStack Start.

In a menu, visitors want to see which page they’re on. TanStack Start’s Link knows when its page is open, and gives you three ways to style the active link: activeProps, inactiveProps and the data-status attribute.

This post styles the header of a real app, the Bookshop, with each one, then shows how exact stops a link from staying active on the pages under it.

Syntax

SyntaxTypeScript
<Link
	to="<address>"
	activeProps={{ className: "<classes while the page is open>" }}
	inactiveProps={{ className: "<classes while it isn’t>" }}
	activeOptions={{ exact: <true or false> }}
>
	<text>
</Link>
  • activeProps: settings the link gets while its page is open. A className here is added to the link’s own classes.
  • inactiveProps: settings the link gets while its page isn’t open.
  • activeOptions={{ exact: true }}: makes the link active only on its own address, not on the addresses under it. It’s off by default.
  • to and <text>: the address and the text, as for any Link.

Each option is described in the navigation guide (opens in a new tab) and the ActiveLinkOptions reference (opens in a new tab).

Give each link in the header an activeProps with the Tailwind CSS class font-bold:

src/routes/__root.tsx (excerpt)TypeScript
<nav className="mx-auto flex max-w-2xl gap-6 px-8 py-4 text-lg">
	<Link to="/" activeProps={{ className: "font-bold" }}>
		Home
	</Link>
	<Link to="/books" activeProps={{ className: "font-bold" }}>
		Books
	</Link>
	<Link to="/about" activeProps={{ className: "font-bold" }}>
		About
	</Link>
</nav>

On the Books page, its link is bold:

The Bookshop header on the Books page with Books in bold and Home and About in normal text.
activeProps makes the open page’s link bold.

Add inactiveProps to style the links of the pages that aren’t open. Here they turn gray:

src/routes/__root.tsx (excerpt)TypeScript
<nav className="mx-auto flex max-w-2xl gap-6 px-8 py-4 text-lg">
	<Link
		to="/"
		activeProps={{ className: "font-bold" }}
		inactiveProps={{ className: "text-gray-500" }}
	>
		Home
	</Link>
	<Link
		to="/books"
		activeProps={{ className: "font-bold" }}
		inactiveProps={{ className: "text-gray-500" }}
	>
		Books
	</Link>
	<Link
		to="/about"
		activeProps={{ className: "font-bold" }}
		inactiveProps={{ className: "text-gray-500" }}
	>
		About
	</Link>
</nav>
The header on the Books page with Books in bold black and the other links, Home and About, in gray.
inactiveProps dims the links of the other pages.

Step 3: Or Style the data-status Attribute

Link also marks the link of the open page with data-status="active". Tailwind CSS can style that attribute with the data-[status=active]: variant, so you don’t need activeProps at all:

src/routes/__root.tsx (excerpt)TypeScript
<nav className="mx-auto flex max-w-2xl gap-6 px-8 py-4 text-lg">
	<Link to="/" className="data-[status=active]:font-bold">
		Home
	</Link>
	<Link to="/books" className="data-[status=active]:font-bold">
		Books
	</Link>
	<Link to="/about" className="data-[status=active]:font-bold">
		About
	</Link>
</nav>

The header looks exactly as in Step 1. Use whichever style reads better to you.

A link is active on its own address and on every address under it. With a page at /books/new, the Books link stays bold there too:

A page called New arrivals at the address /books/new, with Books in bold in the header above it.
Books stays active on a page under /books.

Add activeOptions={{ exact: true }} to the Books link to make it active only on /books itself:

src/routes/__root.tsx (excerpt)TypeScript
<Link
	to="/books"
	activeProps={{ className: "font-bold" }}
	activeOptions={{ exact: true }}
>
	Books
</Link>
The New arrivals page at /books/new with no header link in bold, after adding exact to the Books link.
With exact, Books is active only on /books itself.

Most menus want the default, so a section stays highlighted on all of its pages. Add exact only where you don’t.

Common Mistakes

MistakeWhat happensDo this instead
Putting the active class in classNameEvery link gets it, open or notPut it in activeProps, or use data-[status=active]:
Expecting a link to be active only on its own addressBooks stays bold on /books/newAdd activeOptions={{ exact: true }}

FAQ

What does Link add to the open page’s link? data-status="active", aria-current="page" and, when you don’t set activeProps, the class active (see link.tsx (opens in a new tab) in TanStack Router’s repository). aria-current="page" tells screen readers which link is the current page (WAI-ARIA (opens in a new tab)).

Is a link to the home page active on every page? No. A link to / is active only on the home page, so it doesn’t need exact.

Conclusion

activeProps styles the open page’s link, inactiveProps styles the rest, data-[status=active]: does the same from CSS, and exact keeps a parent link from matching the pages under it.

Additional Resources

TanStack, React and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by TanStack.

0 comments