T
TanStack10mo ago
absent-sapphire

Active link highlighting not working.

When the link is clicked, the route changes but the link styling doesn't. This is my full code below: import { Link, useRouter } from "@tanstack/react-router"; import * as Icons from "solar-icon-set"; import cn from "classnames"; const Sidebar = () => { const links = [ { title: "Home", href: "/dashboard", icon: Icons.Home2, }, { title: "Collections", href: "/dashboard/collections", icon: Icons.Folder2, }, ]; const router = useRouter(); return ( <div className="py-3 px-4 w-full h-screen bg-[#f5f5f3]"> <h1>Logo</h1> <div className="space-y-3 mt-10"> {links.map((link, index) => ( <Link key={index} to={link.href} className={cn( "flex items-center gap-x-2 bg-transparent px-4 py-3 rounded-full text-gray-600 transition-colors", { "!bg-accent/15 !text-accent font-medium": router.latestLocation.pathname === link.href, "hover:bg-accent/10 hover:text-accent": router.latestLocation.pathname !== link.href, } )} > <link.icon iconStyle={ router.latestLocation.pathname === link.href ? "Bold" : "Linear" } size={20} /> <span>{link.title}</span> </Link> ))} </div> </div> ); }; export default Sidebar;
No description
5 Replies
foreign-sapphire
foreign-sapphire10mo ago
can you please provide a complete minimal example based on the existing stackblitz examples?
absent-sapphire
absent-sapphireOP10mo ago
Here: https://github.com/akinloluwami/linkdrive (Sorry, I couldn't get Stackblitz to work.)
GitHub
GitHub - akinloluwami/linkdrive
Contribute to akinloluwami/linkdrive development by creating an account on GitHub.
vicious-gold
vicious-gold10mo ago
Link takes an activeProps property where you can define classes for when it's active
sensitive-blue
sensitive-blue10mo ago
We already set and active class for you and also add attrs and aria attrs too.
absent-sapphire
absent-sapphireOP10mo ago
I get that but why isn't my implementation working? I need to use the active route to change the style of the icon, that prop is not available on the level that I need it in.

Did you find this page helpful?