I am trying to create a Navbar using headlessui’s Menu component. Since hovering is not supported by the component itself, I went through the GitHub issue thread and tried some solutions there. Yet, none of them seems to work without glitching: https://github.com/tailwindlabs/headlessui/issues/239
I build a minimal demo of my navbar (containing nested and untested (direct) links): https://codesandbox.io/s/headlessui-menu-forked-y5q9hm?file=/src/App.js
In here I added onMouseEnter suggested from this SO: Can't make the headlessui dropdown to open on mouse hover and not on click
Yet, it fails to close the menus...
Is there a solution out there?
UPDATE:
Naivly, I added an onMouseLeave block and swapped the ternary condition. This seem to work in the first place, yet, the page crashes when switching between two menu items being nested:
https://codesandbox.io/s/headlessui-menu-forked-ctfvjm?file=/src/App.js
It looks like you are nearly there - the only problem remaining in the updated codesandbox seems to be an exception that is raised when
onMouseLeaveis triggered on the SVG graphics element inside the menu item, which can not react toclickevents.The easiest way to fix it is to make the
clickcall null-safe by using use the optional chaining operator: