Refactor: Add useActiveUrl composable #223
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi!
I ran into a couple of navigation issues while working with persistent layouts and thought I'd fix them up. Also noticed the active URL checks between the header and sidebar layouts weren't consistent, so this cleans that up too.
Issues Fixed
Navigation state not updating with persistent layouts
When using persistent layouts, the sidebar navigation doesn't react to page changes - it remains stuck showing the previous active state.
Active state lost when URL contains query parameters
Navigation links lose their active state when the URL includes query parameters, even though the route is still active.
Inconsistent active URL checks
The header and sidebar layouts were checking active URLs differently.
AppHeader.vuewas using a computed function to check active state, whileNavMain.vuewas using theurlIsActivefunction directly.vue-starter-kit/resources/js/components/AppHeader.vue
Lines 51 to 54 in 4317f62
vue-starter-kit/resources/js/components/NavMain.vue
Lines 25 to 29 in 4317f62
Changes
useActiveUrlcomposable inresources/js/composables/useActiveUrl.tsfor centralized navigation state managementusePage().urlto ensure navigation updates with Inertia page changesAppHeader.vue,NavFooter.vue,NavMain.vue, andLayout.vueto use the new composablelib/utils.tsThe
urlIsActivefunction checks against the current page by default, but can be overridden by passing a custom URL as a second parameter.Future Enhancements
I also built a version with different matching modes (
startsWith,includes, etc.) but kept this PR simple. Happy to open a follow-up PR if that would be useful!