Site icon JD Bots

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

Syntax

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

Each option is described in the navigation guide and the ActiveLinkOptions reference.

Step 1: Make the Open Page’s Link Bold

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

src/routes/__root.tsx (excerpt)

<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:

activeProps makes the open page’s link bold.

Step 2: Dim the Other Links

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

src/routes/__root.tsx (excerpt)

<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>
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)

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

Step 4: Keep a Parent Link from Matching the Pages Under It

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:

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)

<Link
	to="/books"
	activeProps={{ className: "font-bold" }}
	activeOptions={{ exact: true }}
>
	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 in TanStack Router’s repository). aria-current="page" tells screen readers which link is the current page (WAI-ARIA).

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

Related

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

Exit mobile version