-
Hey!
I have set Navigation Dropdown to “Click – menu item” and it works with keyboard navigation as well. Very good for accessibility.
However for mouse users it would be quite convenient to get sub menus to open on mouse “hover”.
Is it possible to keep both behaviours – click to open/close menu with mouse/keyboard and hover to open with mouseover?
Thanks!
-
Hi there,
It’s possible, but it will create conflicts.
When you click on a menu item, the submenu is shown, now you hover over another menu item, and a new submenu is shown, so both of the submenus will be shown at the same time.
To avoid this conflict, you will need custom JS which is out of the scope of the support forum, unfortunately.
You can give the below CSS a try:
@media(min-width: 769px) { .dropdown-click .main-navigation li.menu-item-has-children:hover > ul, .dropdown-click .main-navigation ul li.sfHover > ul { display: block; left: auto; right: auto; opacity: 1; visibility: visible; pointer-events: auto; height: auto; overflow: visible; float: none; } }
-
Thanks!
I tried your CSS – it works, partially.
Submenu will open on hover but clicking on main menu item now does nothing, only arrow changes its position (probably because JS is triggered) and it can be also confusing for users 🙂
I guess that’s something that can’t be fixed via CSS?And can you give me function/hook for adding Navigation Dropdown choices for menus in Generatepress? Or is it even possible in Generatepress to have multiple Navigation Dropdown coices in effect, despite of conflicts?
-
And can you give me function/hook for adding Navigation Dropdown choices for menus in Generatepress?
what do you mean? can you explain some more?
-
Right now I can choose one of three options in Customizer (Navigation Dropdown) – Hover, Click menu, Click arrow.
But is it possible to add one of the other options via code in functions.php? Or it will create JS conflicts and is not recommended? -
Hi there,
this is going to confuse users and be a UI/UX nightmare.
Consider; in the normal state the Click option and the hover option has 2 states, Open and Close.
Trying to combine the two gets weird. Consider this:1. default state Menu is not open
2. On Hover – the custom CSS opens the submenu
3. On Click – the submenu item is set to open state.
There is no visible change accept for the toggle icon, as the hover state still applies
4. On 2nd Click – the menu item is set to close state.
But again there is no visible change accent for the icon as the hover state still applies
5. On mouse out – the custom CSS no longer applies.
And the open/close state will depend on the last clicked state.So you can’t really have it as Either Hover or Click. It has to really be one or the other.
-
Yes, that makes sense, thanks for thinking along. Case closed! 🙂
-
No provlem – glad to be of help
-
domchocolate
Hi I want to do this – how do you set the dropdown to click from mouseover?
UPDATE – found it in Customize settings! Doh!
BUT is there away for the dropdown menu to collapse again when the keyboard focus moves away to an element outside of the .sub-menu and it’s associate .menu-item-has-children?
Thanks in advance
Dominic
-
Hi there,
that’s not a function of the menu script , but you could try something like this:
const menuItemsWithChildren = document.querySelectorAll('ul li.menu-item-has-children'); menuItemsWithChildren.forEach(menuItem => { menuItem.addEventListener('focusout', (event) => { setTimeout(() => { if (!menuItem.contains(event.relatedTarget)) { // Remove classes from parent and submenu menuItem.classList.remove('sfHover'); const submenu = menuItem.querySelector('.sub-menu.toggled-on'); if (submenu) { submenu.classList.remove('toggled-on'); } } }, 100); }); });
The script will listen for any out focusing on parent menu items and strip it and it the submenu of the open classes.
-
domchocolate
Hi David this works great. Just one thing – it doesn’t change the attribute to aria-expanded=”false” after the menu collapses when the focus shifts. Otherwise great – any chance of a small tweak for accessibility purposes?
-
domchocolate
Hi David I tweaked it to this and this works:
const menuItemsWithChildren = document.querySelectorAll('ul li.menu-item-has-children'); menuItemsWithChildren.forEach(menuItem => { menuItem.addEventListener('focusout', (event) => { setTimeout(() => { if (!menuItem.contains(event.relatedTarget)) { // Remove classes from parent and submenu menuItem.classList.remove('sfHover'); const submenu = menuItem.querySelector('.sub-menu.toggled-on'); if (submenu) { submenu.classList.remove('toggled-on'); } // Toggle aria-expanded attribute to false const dropdownToggle = menuItem.querySelector('.dropdown-menu-toggle'); if (dropdownToggle) { dropdownToggle.setAttribute('aria-expanded', 'false'); } } }, 100); }); });
-
Ah good spot and thanks for the fixed code!
- You must be logged in to reply to this topic.