Also posted on GitHub Discussion
How to use click handlers instead of 'to' in UNavigationMenu items
Description
I'm using UNavigationMenu with a list of NavigationMenuItem[] items. Currently, to make an item clickable, it needs a to: "/path" property which changes the route. However, for my application, I want to change views without changing routes.
What I've tried:
- Adding
click: () => { /* handler */ }to my navigation items - Adding
@select="handleSelect"to the UNavigationMenu component - Using a select handler that calls item.click()
Current behavior:
When I click on navigation items, nothing happens. It seems the UNavigationMenu component does not properly recognize or trigger the click handlers in the items.
// My current setup
const handleSelect = (item: NavigationMenuItem) => {
console.log("selected item", item.label) // This logs correctly
if (item.click) {
item.click() // This doesn't seem to execute
}
}
const items: NavigationMenuItem[][] = [
[
{
label: "Home",
icon: "i-lucide-house",
// to: "/" - I want to avoid this
click: () => {
console.log("Home clicked!") // This never logs
tab.value = "home"
}
},
// Other items...
]
]
// my template
<UNavigationMenu
:collapsed="collapsed"
:items="items[0]"
orientation="vertical"
@select="handleSelect"
/>
Version
Nuxt = 3.17.2
@nuxt/ui==3.1.1
``