_s Navigation dropdowns macOS Safari fix

Created Diff never expires
2 removals
100 lines
6 additions
102 lines
/**
/**
* File navigation.js.
* File navigation.js.
*
*
* Handles toggling the navigation menu for small screens and enables TAB key
* Handles toggling the navigation menu for small screens and enables TAB key
* navigation support for dropdown menus.
* navigation support for dropdown menus.
*/
*/
( function() {
( function() {
const siteNavigation = document.getElementById( 'site-navigation' );
const siteNavigation = document.getElementById( 'site-navigation' );


// Return early if the navigation doesn't exist.
// Return early if the navigation doesn't exist.
if ( ! siteNavigation ) {
if ( ! siteNavigation ) {
return;
return;
}
}


const button = siteNavigation.getElementsByTagName( 'button' )[ 0 ];
const button = siteNavigation.getElementsByTagName( 'button' )[ 0 ];


// Return early if the button doesn't exist.
// Return early if the button doesn't exist.
if ( 'undefined' === typeof button ) {
if ( 'undefined' === typeof button ) {
return;
return;
}
}


const menu = siteNavigation.getElementsByTagName( 'ul' )[ 0 ];
const menu = siteNavigation.getElementsByTagName( 'ul' )[ 0 ];


// Hide menu toggle button if menu is empty and return early.
// Hide menu toggle button if menu is empty and return early.
if ( 'undefined' === typeof menu ) {
if ( 'undefined' === typeof menu ) {
button.style.display = 'none';
button.style.display = 'none';
return;
return;
}
}


if ( ! menu.classList.contains( 'nav-menu' ) ) {
if ( ! menu.classList.contains( 'nav-menu' ) ) {
menu.classList.add( 'nav-menu' );
menu.classList.add( 'nav-menu' );
}
}


// Toggle the .toggled class and the aria-expanded value each time the button is clicked.
// Toggle the .toggled class and the aria-expanded value each time the button is clicked.
button.addEventListener( 'click', function() {
button.addEventListener( 'click', function() {
siteNavigation.classList.toggle( 'toggled' );
siteNavigation.classList.toggle( 'toggled' );


if ( button.getAttribute( 'aria-expanded' ) === 'true' ) {
if ( button.getAttribute( 'aria-expanded' ) === 'true' ) {
button.setAttribute( 'aria-expanded', 'false' );
button.setAttribute( 'aria-expanded', 'false' );
} else {
} else {
button.setAttribute( 'aria-expanded', 'true' );
button.setAttribute( 'aria-expanded', 'true' );
}
}
} );
} );


// Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation.
// Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation.
document.addEventListener( 'click', function( event ) {
document.addEventListener( 'click', function( event ) {
const isClickInside = siteNavigation.contains( event.target );
const isClickInside = siteNavigation.contains( event.target );


if ( ! isClickInside ) {
if ( ! isClickInside ) {
siteNavigation.classList.remove( 'toggled' );
siteNavigation.classList.remove( 'toggled' );
button.setAttribute( 'aria-expanded', 'false' );
button.setAttribute( 'aria-expanded', 'false' );
}
}
} );
} );


// Get all the link elements within the menu.
// Get all the link elements within the menu.
const links = menu.getElementsByTagName( 'a' );
const links = menu.getElementsByTagName( 'a' );


// Get all the link elements with children within the menu.
// Get all the link elements with children within the menu.
const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );
const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' );


// Toggle focus each time a menu link is focused or blurred.
// Toggle focus each time a menu link is focused or blurred.
for ( const link of links ) {
for ( const link of links ) {
link.addEventListener( 'focus', toggleFocus, true );
// Disabled to fix submenus on iOS/Android
link.addEventListener( 'blur', toggleFocus, true );
//link.addEventListener( 'focus', toggleFocus, true );
//link.addEventListener( 'blur', toggleFocus, true );
}
}


// Toggle focus each time a menu link with children receive a touch event.
// Toggle focus each time a menu link with children receive a touch event.
for ( const link of linksWithChildren ) {
for ( const link of linksWithChildren ) {
link.addEventListener( 'touchstart', toggleFocus, false );
link.addEventListener( 'touchstart', toggleFocus, false );
link.addEventListener( 'click', toggleFocus, true ); // macOS Safari fix
}
}


/**
/**
* Sets or removes .focus class on an element.
* Sets or removes .focus class on an element.
*/
*/
function toggleFocus() {
function toggleFocus() {
if ( event.type === 'focus' || event.type === 'blur' ) {
if ( event.type === 'focus' || event.type === 'blur' ) {
let self = this;
let self = this;
// Move up through the ancestors of the current link until we hit .nav-menu.
// Move up through the ancestors of the current link until we hit .nav-menu.
while ( ! self.classList.contains( 'nav-menu' ) ) {
while ( ! self.classList.contains( 'nav-menu' ) ) {
// On li elements toggle the class .focus.
// On li elements toggle the class .focus.
if ( 'li' === self.tagName.toLowerCase() ) {
if ( 'li' === self.tagName.toLowerCase() ) {
self.classList.toggle( 'focus' );
self.classList.toggle( 'focus' );
}
}
self = self.parentNode;
self = self.parentNode;
}
}
}
}


if ( event.type === 'touchstart' ) {
if ( event.type === 'click' || event.type === 'touchstart' ) { // macOS Safari fix
const menuItem = this.parentNode;
const menuItem = this.parentNode;
event.preventDefault();
event.preventDefault();
for ( const link of menuItem.parentNode.children ) {
for ( const link of menuItem.parentNode.children ) {
if ( menuItem !== link ) {
if ( menuItem !== link ) {
link.classList.remove( 'focus' );
link.classList.remove( 'focus' );
}
}
}
}
menuItem.classList.toggle( 'focus' );
menuItem.classList.toggle( 'focus' );
}
}
}
}
}() );
}() );