Skip to content

Instantly share code, notes, and snippets.

@beags121
Forked from joshmcrty/navigation.js
Created January 12, 2023 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beags121/78008a7cba92dc6cb1f7ff38026d0dd4 to your computer and use it in GitHub Desktop.
Save beags121/78008a7cba92dc6cb1f7ff38026d0dd4 to your computer and use it in GitHub Desktop.
/**
* navigation.js
*
* Handles toggling the navigation menu for small screens and enabling tab support for dropdown menus.
*/
( function() {
var container, button, menu, links;
container = document.getElementById( 'site-navigation' );
if ( ! container )
return;
button = container.getElementsByTagName( 'button' )[0];
if ( 'undefined' === typeof button )
return;
menu = container.getElementsByTagName( 'ul' )[0];
// Hide menu toggle button if menu is empty and return early.
if ( 'undefined' === typeof menu ) {
button.style.display = 'none';
return;
}
if ( -1 === menu.className.indexOf( 'nav-menu' ) )
menu.className += ' nav-menu';
button.onclick = function() {
if ( -1 !== container.className.indexOf( 'toggled' ) )
container.className = container.className.replace( ' toggled', '' );
else
container.className += ' toggled';
};
// Get all the link elements within the menu. Return if none are found.
links = menu.getElementsByTagName( 'a' );
if ( 0 === links.length )
return;
// Each time a menu link is focused or blurred call the function toggle_focus
for ( i = 0, len = links.length; i < len; i++ ) {
links[i].onfocus = toggle_focus;
links[i].onblur = toggle_focus;
};
function toggle_focus() {
// Skipping the actual link itself as this can be styled with :focus
var current = this.parentElement;
var parents = []
// Build an array of the parent elements of the focused link until we hit the top level .nav-menu ul
while ( -1 === current.className.indexOf( 'nav-menu' ) ) {
parents.unshift( current );
current = current.parentElement;
}
// For each element in parents[] toggle the class .focus
for ( i = 0, len = parents.length; i < len; i++ ) {
if ( -1 !== parents[i].className.indexOf( 'focus' ) )
parents[i].className = parents[i].className.replace( ' focus', '' );
else
parents[i].className += ' focus';
}
}
} )();
/**
* I'm including this css here for reference as it's needed to make the javascript useful.
* If this code is used this should proably be combined with the :hover styles already in _s.
*/
.main-navigation ul li.focus > ul {
left: auto;
}
.main-navigation ul ul li.focus > ul {
left: 100%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment