Rework menu navigation: these changes build off pull request #239 for issue #200

* Only set button and menu if container is not null.
 * Break up the "return early" conditional into multiple parts.
 * Shorten the name of the toggled class from `toggled-on` to `toggled`.
 * Restore the "Hide menu toggle button if menu is empty." documentation.
 * Remove the `nav-menu` class. The previous code would replace any custom custom classes applied via `wp_nav_menu()` or template file.
 * Replace the `nav-menu` selector is style.css with selectors that will target both a custom nav menu and a page menu. These changes could use critical feedback. Do you know of an edge case where they might not work as expected?
This commit is contained in:
Michael Fields 2013-06-16 20:29:17 -07:00
parent 75f612d365
commit 4c396f6f9b
2 changed files with 23 additions and 21 deletions

View File

@ -5,28 +5,28 @@
*/ */
( function() { ( function() {
var container = document.getElementById( 'site-navigation' ), var container = document.getElementById( 'site-navigation' ),
button = container.getElementsByTagName( 'h1' )[0], button,
menu;
if ( ! container )
return;
button = container.getElementsByTagName( 'h1' )[0];
if ( 'undefined' == typeof button )
return;
menu = container.getElementsByTagName( 'ul' )[0]; menu = container.getElementsByTagName( 'ul' )[0];
if ( undefined == button || undefined == menu ) // Hide menu toggle button if menu is empty and return early.
return false; if ( 'undefined' == typeof menu ) {
button.style.display = 'none';
return;
}
button.onclick = function() { button.onclick = function() {
if ( -1 == menu.className.indexOf( 'nav-menu' ) ) if ( -1 != container.className.indexOf( 'toggled' ) )
menu.className = 'nav-menu'; container.className = container.className.replace( ' toggled', '' );
else
if ( -1 != button.className.indexOf( 'toggled-on' ) ) { container.className += ' toggled';
button.className = button.className.replace( ' toggled-on', '' );
menu.className = menu.className.replace( ' toggled-on', '' );
container.className = container.className.replace( 'main-small-navigation', 'navigation-main' );
} else {
button.className += ' toggled-on';
menu.className += ' toggled-on';
container.className = container.className.replace( 'navigation-main', 'main-small-navigation' );
}
}; };
// Hide menu toggle button if menu is empty.
if ( ! menu.childNodes.length )
button.style.display = 'none';
} )(); } )();

View File

@ -447,7 +447,8 @@ a:active {
@media screen and (max-width: 600px) { @media screen and (max-width: 600px) {
.menu-toggle, .menu-toggle,
.main-small-navigation ul.nav-menu.toggled-on { .navigation-main.toggled ul.menu,
.navigation-main.toggled div.menu > ul {
display: block; display: block;
} }
@ -456,6 +457,7 @@ a:active {
} }
} }
/* =Content /* =Content
----------------------------------------------- */ ----------------------------------------------- */