-
Notifications
You must be signed in to change notification settings - Fork 821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NavigationMenu] Option to open navigation menu on click instead of pointer enter #1630
Comments
Thanks for raising @hannahcancode, this is possible by preventing the relevant pointer events on https://codesandbox.io/s/navigation-menu-on-click-duwvgn?file=/App.js however, this is not perfect as it relies on understanding the implementation details (which may change in the future). I'll mark this as an improvement to explore. |
Oh that's great information, thanks @andy-hook. I did dive a little into whether I could "hack" it in such a way but as you say, I didn't have the knowledge of the implementation and failed. I'll see if that helps! Would love to see it as an enhancement 😁 |
@andy-hook thank you so much for that code sample. My use case: Solved with something like: export const preventHover = (event: any) => {
const e = event as Event
if (window.innerWidth < 1024) e.preventDefault()
}
…
<NavigationMenu.Trigger
onPointerMove={preventHover}
onPointerLeave={preventHover}
>
…
<NavigationMenu.Content
onPointerEnter={preventHover}
onPointerLeave={preventHover}
> |
Based on my testing, only <NavigationMenu.Content
onPointerLeave={preventHover}
> |
Hi @andy-hook! I tried using |
Hey @justkahdri, I don't think anything has changed https://codesandbox.io/p/sandbox/pensive-shadow-j8m3lm Can you provide a sandbox? |
You're right, nothing has changed. I was using |
Just a note, on vercel.com (which was mentioned in the OP), the "Features" menu is now opened both on hover and on click. I'm wondering if they did any sort of use study and determined this was better than opening the menu only on click. |
I've found that a lot of mouse users will instinctively click the menu triggers to open them, which will close the menu as their "hover" state has already triggered the menu to open. Here is Theo exhibiting this behaviour a few months ago. theo_navigationmenu.mp4A hack is to create mutation observers which listen to the https://codesandbox.io/p/sandbox/peaceful-nobel-gmlbyy?file=%2FApp.tsx While it's a bit messy this should give the best of both click and hover worlds. If anyone can foresee any problems with this let me know! Cheers |
I find myself constantly doing that with this library. |
Hi all, how do you manually control the open and close state of the navigation menu in react? Do we just set the |
oh nvm, i found the |
|
- radix-ui/primitives#1630 (comment) - add `disableOutsidePointerEvents` to prevent interaction with elements outside the nav content
@jpizzle34 @benoitgrelard can you provide an example to force an open state on a navigation item? |
<NavigationMenu.Root defaultValue="some-value-item">
...
</NavigationMenu.Root>
// or
const [value, setValue] = React.useState('some-value-item')
<NavigationMenu.Root value={value} onValueChange={setValue}>
...
</NavigationMenu.Root> |
So there is no official way to make the menu open on the click instead of horrible hover? |
Thanks for the workarounds! I've also had to override |
Not sure why this still isn't an option without workarounds. I tried adding these props:
On my |
@jwmann by now I prevent default the following:
The |
@elbotho That definitely made the difference for me! |
I you as me are using a viewport element outside of the regular "flow", also add these handlers over there:
|
how? Can you point us to it?
What os the regular "flow"? |
Like this
|
Glad this got added in thanks for the help!! |
This worked for me, hope this gets added as a prop ( |
Can anyone explain this further? It seems like you should be able to pass in an isOpen prop, but the value prop only accepts strings...do I pass in "closed"/"open"? |
Hey @kdevay, It looks like you're trying to pass isOpen as a boolean to the value prop, correct? Unfortunately, this won't work with this component. The value prop requires a string because the NavigationMenu can contain multiple items, and each needs a unique string identifier to determine which item to display. I created a codesandbox demo hope this helps. |
When we getting the prop? <3 |
If anyone has gotten this far using Shadcn/ui: For me, the solution was to add I modified the component NavigationMenuTrigger in It looks like this: const NavigationMenuTrigger = React.forwardRef<
React.ElementRef<typeof NavigationMenuPrimitive.Trigger>,
React.ComponentPropsWithoutRef<typeof NavigationMenuPrimitive.Trigger> & {
triggerMode?: "click" | "hover";
}
>(({ className, children, triggerMode = "hover", ...props }, ref) => (
<NavigationMenuPrimitive.Trigger
ref={ref}
className={cn(navigationMenuTriggerStyle(), "group", className)}
{...props}
{...(triggerMode === "click" && {
onPointerEnter: (event) => event.preventDefault(),
onPointerMove: (event) => event.preventDefault(),
onPointerLeave: (event) => event.preventDefault(),
})}
>
{children}{" "}
<ChevronDown
className="relative top-[1px] ml-1 h-3 w-3 transition duration-200 group-data-[state=open]:rotate-180"
aria-hidden="true"
/>
</NavigationMenuPrimitive.Trigger>
));
NavigationMenuTrigger.displayName = NavigationMenuPrimitive.Trigger.displayName; Now just use it in the application composition:
Again, this solution works if you use shadcn/ui, my intention is not to propose a change to Radix, I just couldn't find an appropriate issue in the correct repository |
In my opinion there is still missing functionality along with this Tho not close an element if any of them was already open. |
I'm able to have the best of both solution by increasing the delayDuration, set |
Feature request
Overview
The ability to optionally have the navigation menu trigger on click instead of on hover.
Who does this impact? Who is this for?
Would love for this to be a beginner friendly boolean prop, that turns on/off click in preference to hover.
For users building more complicated navigation menus, the ability to click to open the menu and have the menu stay open can provide a better UX, as they can move their mouse into the menu more easily, without the possibility of the menu closing accidentally. In our testing it also seems that some users will automatically go to click on the trigger and close the menu without realising.
As the
button
primitive already has anonClick
prop being passed through it could be relatively straightforward to implement with a conditional.Additional context
For example, vercel.com (logged out) mega menus open on click.
Smashing magazine article on click vs hover: https://www.smashingmagazine.com/2021/05/frustrating-design-patterns-mega-dropdown-hover-menus/#designing-a-better-mega-dropdown-tap-click-menu
The text was updated successfully, but these errors were encountered: