-
Notifications
You must be signed in to change notification settings - Fork 281
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
Improve Mobile Navigation #697
Comments
This is definitely an issue. It looks like it should work, but it doesn't. I guessing that someone is working on this based on the information given in the navigation.js? |
I got through this issue today. It still needs some final tweaking but it is functional. I made changes to global.css and navigation.js. All of the changes I made are marked with "TODO: FIXME:". I also left the original code commented above my changes so they can be seen. I'm not sure how to get added to this project as a contributor. @robruiz can you take a look at this since it seems you are the only contributor these days? What happened to @mor10 and the rest of the crew? global.css changes
navigation.js changes
|
I tried your solution from the navigation.js first it seems to work good but if you have two or more submenus ( I do not speak of sub submenus ) it's not working properly due to the mouseleave event. I then put up another eventListener to listen on the touchstart event and set a helper variable to true. In the mouseleave event it first checks if there was a touch event triggered and if not than fire the toggleSubMenu. Like so (see the GOT ADDED and GOT CHANGED sections): /**
* Initiate the script to process submenu
* navigation toggle for a specific navigation menu.
* @param {Object} nav Navigation element.
*/
function initEachNavToggleSubmenu( nav ) {
// GOT ADDED
// Helper var for storing if touch event happened.
let isTouched;
// Get the submenus.
const SUBMENUS = nav.querySelectorAll( '.menu ul' );
// No point if no submenus.
if ( ! SUBMENUS.length ) {
return;
}
// Create the dropdown button.
const dropdownButton = getDropdownButton();
for ( let i = 0; i < SUBMENUS.length; i++ ) {
const parentMenuItem = SUBMENUS[ i ].parentNode;
let dropdown = parentMenuItem.querySelector( '.dropdown' );
// If no dropdown, create one.
if ( ! dropdown ) {
// Create dropdown.
dropdown = document.createElement( 'span' );
dropdown.classList.add( 'dropdown' );
const dropdownSymbol = document.createElement( 'i' );
dropdownSymbol.classList.add( 'dropdown-symbol' );
dropdown.appendChild( dropdownSymbol );
// Add before submenu.
SUBMENUS[ i ].parentNode.insertBefore( dropdown, SUBMENUS[ i ] );
}
// Convert dropdown to button.
const thisDropdownButton = dropdownButton.cloneNode( true );
// Copy contents of dropdown into button.
thisDropdownButton.innerHTML = dropdown.innerHTML;
// Replace dropdown with toggle button.
dropdown.parentNode.replaceChild( thisDropdownButton, dropdown );
// GOT CHANGED FROM robruiz
// Toggle the submenu when we click the dropdown button.
thisDropdownButton.addEventListener( 'click', ( e ) => {
if ( e.target.classList.contains( 'dropdown-symbol' ) ) {
toggleSubMenu( e.target.parentNode.parentNode );
} else {
toggleSubMenu( e.target.parentNode );
}
} );
// GOT ADDED
// Check if there is a touch event so it does not trigger the mouseleave event.
parentMenuItem.addEventListener( 'touchstart', ( ) => {
isTouched = true;
} );
// GOT CHANGED
// Clean up the toggle if there was no touch event and a mouse takes over from keyboard.
parentMenuItem.addEventListener( 'mouseleave', ( e ) => {
if ( ! isTouched ) {
toggleSubMenu( e.target, false );
} else {
// putting back to false in case there is also a mouse on the device.
isTouched = false;
}
} );
// When we focus on a menu link, make sure all siblings are closed.
parentMenuItem.querySelector( 'a' ).addEventListener( 'focus', ( e ) => {
const parentMenuItemsToggled = e.target.parentNode.parentNode.querySelectorAll( 'li.menu-item--toggled-on' );
for ( let j = 0; j < parentMenuItemsToggled.length; j++ ) {
toggleSubMenu( parentMenuItemsToggled[ j ], false );
}
} );
// Handle keyboard accessibility for traversing menu.
SUBMENUS[ i ].addEventListener( 'keydown', ( e ) => {
// These specific selectors help us only select items that are visible.
const focusSelector = 'ul.toggle-show > li > a, ul.toggle-show > li > button';
if ( KEYMAP.TAB === e.keyCode ) {
if ( e.shiftKey ) {
// Means we're tabbing out of the beginning of the submenu.
if ( isfirstFocusableElement( e.target, document.activeElement, focusSelector ) ) {
toggleSubMenu( e.target.parentNode, false );
}
// Means we're tabbing out of the end of the submenu.
} else if ( islastFocusableElement( e.target, document.activeElement, focusSelector ) ) {
toggleSubMenu( e.target.parentNode, false );
}
}
} );
SUBMENUS[ i ].parentNode.classList.add( 'menu-item--has-toggle' );
}
} Maybe it'll help someone too... (of course the CSS needs to be changed as well) |
Issue Overview
Currently the mobile navigation is not very project ready IMO. The mobile navigation menu item with dropdowns does not hide the dropdown mobile but displays the full menu on mobile. It also removes the dropdown toggle as well.
Also the way it is set up on WPRig current site the menu is in the corner even when you select the menu button it opens in a corner on the right.
Describe your environment
Steps to Reproduce
Expected Behavior
Have an actual mobile menu where you can click the menu item or the dropdown toggle to then open the submenu.
Current Behavior
Possible Solution
Screenshots / Video
Related Issues and/or PRs
The text was updated successfully, but these errors were encountered: