1/** 2 * File navigation.js. 3 * 4 * Handles toggling the navigation menu for small screens and enables TAB key 5 * navigation support for dropdown menus. 6 */ 7( function() { 8 const siteNavigation = document.getElementById( 'site-navigation' ); 9 10 // Return early if the navigation doesn't exist. 11 if ( ! siteNavigation ) { 12 return; 13 } 14 15 const burgerButton = siteNavigation.getElementsByClassName( 'menu-toggle' )[ 0 ]; 16 const closeButton = siteNavigation.getElementsByClassName( 'menu-toggle' )[ 1 ]; 17 18 // Return early if the button doesn't exist. 19 if ( 'undefined' === typeof burgerButton ) { 20 return; 21 } 22 23 const menu = siteNavigation.getElementsByClassName('primary-menu')[0]; 24 25 // // Hide menu toggle button if menu is empty and return early. 26 // if ( 'undefined' === typeof menu ) { 27 // burgerButton.style.display = 'none'; 28 // return; 29 // } 30 31 if ( ! menu.classList.contains( 'nav-menu' ) ) { 32 menu.classList.add( 'nav-menu' ); 33 } 34 35 // Toggle the .toggled class and the aria-expanded value each time the button is clicked. 36 burgerButton.addEventListener( 'click', function() { 37 siteNavigation.classList.toggle( 'toggled' ); 38 39 if ( burgerButton.getAttribute( 'aria-expanded' ) === 'true') { 40 burgerButton.setAttribute( 'aria-expanded', 'false' ); 41 } else { 42 burgerButton.setAttribute( 'aria-expanded', 'true' ); 43 } 44 } ); 45 46 closeButton.addEventListener( 'click', function( event ) { 47 siteNavigation.classList.remove( 'toggled' ); 48 burgerButton.setAttribute( 'aria-expanded', 'false' ); 49 window.scrollTo(0,0); 50 } ); 51 52 53 // Remove the .toggled class and set aria-expanded to false when the user clicks outside the navigation. 54 // Unlikely this will happen because it's a full screen menu 55 document.addEventListener( 'click', function( event ) { 56 const isClickInside = siteNavigation.contains( event.target ); 57 58 if ( ! isClickInside ) { 59 siteNavigation.classList.remove( 'toggled' ); 60 burgerButton.setAttribute( 'aria-expanded', 'false' ); 61 } 62 } ); 63 64 //------------ Accessibility below ---------// 65 66 // Get all the link elements within the menu. 67 const links = menu.getElementsByTagName( 'a' ); 68 69 // Get all the link elements with children within the menu. 70 const linksWithChildren = menu.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ); 71 72 // Toggle focus each time a menu link is focused or blurred. 73 for ( const link of links ) { 74 link.addEventListener( 'focus', toggleFocus, true ); 75 link.addEventListener( 'blur', toggleFocus, true ); 76 } 77 78 // Toggle focus each time a menu link with children receive a touch event. 79 for ( const link of linksWithChildren ) { 80 link.addEventListener( 'touchstart', toggleFocus, false ); 81 } 82 83 /** 84 * Sets or removes .focus class on an element. 85 */ 86 function toggleFocus() { 87 if ( event.type === 'focus' || event.type === 'blur' ) { 88 let self = this; 89 // Move up through the ancestors of the current link until we hit .nav-menu. 90 while ( ! self.classList.contains( 'nav-menu' ) ) { 91 // On li elements toggle the class .focus. 92 if ( 'li' === self.tagName.toLowerCase() ) { 93 self.classList.toggle( 'focus' ); 94 } 95 self = self.parentNode; 96 } 97 } 98 99 if ( event.type === 'touchstart' ) { 100 const menuItem = this.parentNode; 101 event.preventDefault(); 102 for ( const link of menuItem.parentNode.children ) { 103 if ( menuItem !== link ) { 104 link.classList.remove( 'focus' ); 105 } 106 } 107 menuItem.classList.toggle( 'focus' ); 108 } 109 } 110}() );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.