Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-content/themes/twentytwelve/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function twentytwelve_scripts_styles() {
}

// Adds JavaScript for handling the navigation menu hide-and-show behavior.
wp_enqueue_script( 'twentytwelve-navigation', get_template_directory_uri() . '/js/navigation.js', array( 'jquery' ), '20141205', true );
wp_enqueue_script( 'twentytwelve-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20220217', true );

$font_url = twentytwelve_get_font_url();
if ( ! empty( $font_url ) ) {
Expand Down
94 changes: 69 additions & 25 deletions src/wp-content/themes/twentytwelve/js/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
* accessibility for submenu items.
*/
( function() {
function getParentElements( element, selector ) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these useful enough to be moved into a package for re-use? thinking if we are encouraging the community to transition away from jQuery, covering the top use cases in packages (either distinct - or perhaps bundled into a single wp-dom package for efficiency - would make sense vs repeating the same boilerplate code in each theme.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it would be great to have a core-provided script with useful utility functions.

Most of what jQuery offers is nowadays not necessary, but it still has a few nice functions which replicating all the time is painful. Yet, loading jQuery for those is overkill. @mtias Is a simple DOM utility functions package (e.g. wp-dom) something we could consider moving forward through Gutenberg?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The risk of a DOM utility functions package is that it ends up growing large itself, which is a concern when the current library approach isn't modular.

In the case of themes, given that generally only one of them is ever in use for a single page view, I'd argue that we should keep these functions inline, as currently done in this PR.

var parentElements = [];

while ( element.parentElement !== null ) {
if ( ! selector || element.parentElement.matches( selector ) ) {
parentElements.push( element.parentElement );
}

element = element.parentElement;
}

return parentElements;
}

function getSiblingElements( element, selector ) {
var siblingElements = [];

element.parentElement.children.forEach( function( sibling ) {
if ( sibling !== element && ( ! selector || sibling.matches( selector ) ) ) {
siblingElements.push( sibling );
}
} );

return siblingElements;
}

var nav = document.getElementById( 'site-navigation' ), button, menu;
if ( ! nav ) {
return;
Expand All @@ -21,35 +47,53 @@
}

button.onclick = function() {
if ( -1 === menu.className.indexOf( 'nav-menu' ) ) {
if ( ! menu.classList.contains( 'nav-menu' ) ) {
menu.className = 'nav-menu';
Comment thread
felixarntz marked this conversation as resolved.
}

if ( -1 !== button.className.indexOf( 'toggled-on' ) ) {
button.className = button.className.replace( ' toggled-on', '' );
menu.className = menu.className.replace( ' toggled-on', '' );
} else {
button.className += ' toggled-on';
menu.className += ' toggled-on';
}
button.classList.toggle( 'toggled-on' );
menu.classList.toggle( 'toggled-on' );
};
} )();

// Better focus for hidden submenu items for accessibility.
( function( $ ) {
$( '.main-navigation' ).find( 'a' ).on( 'focus.twentytwelve blur.twentytwelve', function() {
$( this ).parents( '.menu-item, .page_item' ).toggleClass( 'focus' );
// Better focus for hidden submenu items for accessibility.
function toggleParentsFocusClass() {
getParentElements( this, '.menu-item, .page_item' ).forEach( function( parentElement ) {
parentElement.classList.toggle( 'focus' );
} );
}
document.querySelector( '.main-navigation' ).getElementsByTagName( 'a' ).forEach( function( menuLink ) {
menuLink.addEventListener( 'focus', toggleParentsFocusClass );
menuLink.addEventListener( 'blur', toggleParentsFocusClass );
} );

if ( 'ontouchstart' in window ) {
$('body').on( 'touchstart.twentytwelve', '.menu-item-has-children > a, .page_item_has_children > a', function( e ) {
var el = $( this ).parent( 'li' );

if ( ! el.hasClass( 'focus' ) ) {
e.preventDefault();
el.toggleClass( 'focus' );
el.siblings( '.focus').removeClass( 'focus' );
}
} );
}
} )( jQuery );
if ( 'ontouchstart' in window ) {
document.body.addEventListener( 'touchstart', function( e ) {
for ( var target = e.target; target && target != this; target = target.parentNode ) {
if ( target.matches( '.menu-item-has-children > a, .page_item_has_children > a' ) ) {
var el = getParentElements( target, 'li' )[0];
if ( el.classList.contains( 'focus' ) ) {
e.preventDefault();
el.classList.add( 'focus' );
getSiblingElements( el, '.focus' ).forEach( function( siblingElement ) {
siblingElement.classList.remove( 'focus' );
} );
}
break;
}
}
} );
document.querySelectorAll( '.menu-item-has-children > a, .page_item_has_children > a' ).forEach( function( menuLink ) {
menuLink.addEventListener( 'touchstart', function( e ) {
var el = getParentElements( this, 'li' )[0];

if ( el.classList.contains( 'focus' ) ) {
e.preventDefault();
el.classList.add( 'focus' );
getSiblingElements( el, '.focus' ).forEach( function( siblingElement ) {
siblingElement.classList.remove( 'focus' );
} );
}
} );
} );
}
} )();