In this episode, we tackle a super common UI problem: how to highlight the current page in your navigation so users know exactly where they are. Right now, none of our navigation items show which one is active, and that's not ideal.
First, we try a basic approach: conditionally adding a class to a nav item when the current URL matches, for example, /account
or /security
. This works okay, but if your URLs change or get more dynamic, this method can get messy and break.
To make things more reliable, we tap into Ziggy to globally expose the current route name. By outputting the current route name (like account.index
or security.index
), we can match our nav items against these stable identifiers instead of fragile URLs. This is way better since route names rarely change compared to URLs.
We update both our desktop and mobile navigation to use this new approach, adding visual cues (like borders or background colors) whenever a nav item matches the current route name. We also make sure to handle styles, so when an item is active, hover effects don’t override it.
By the end of this episode, we've got a solid, DRY way to highlight the current page using route names, making our navigation much more robust and user-friendly!