Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@middlesister
Last active January 31, 2020 15:42
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 middlesister/f06a064a474bbf48eaa8 to your computer and use it in GitHub Desktop.
Save middlesister/f06a064a474bbf48eaa8 to your computer and use it in GitHub Desktop.
Accessible and translatable Genesis reponsive menu
<?php
/**
* Enqueue javascript for responsive menus
*
* Pass localized strings to the javascript. Replace "prefix-" with your
* child theme prefix and "textdomain" with your theme textdomain. The strings
* will be available for translation in your pot-file like normal
*
* @uses wp_enqueue_script
* @uses wp_localize_script
*/
function prefix_enqueue_scripts() {
// create an array containing the data you want to access in your js
$prefix_javascript_options = array(
'screenReaderNavHeading' => _x( 'Main navigation', 'Screen reader navigation heading', 'textdomain' ),
'responsiveMenubuttonText' => _x( 'Menu', 'Responsive menu button text', 'textdomain')
);
// enqueue the script
wp_enqueue_script( 'prefix-responsive-menu', get_stylesheet_directory_uri() . '/js/responsive-menu.js', array( 'jquery' ), 1.0.0, true );
// pass the data to the script - script handle, js object name, php data
wp_localize_script( 'prefix-responsive-menu', 'menuData', $prefix_javascript_options );
}
add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' );
jQuery(function( $ ){
// by using wp_localize_script, we now have a menuData object we can use to get all localized strings
$("header .genesis-nav-menu, .nav-primary .genesis-nav-menu").addClass("responsive-menu").before('<h2 class="screen-reader-text" id="rian-main-nav">menuData.screenReaderNavHeading</h2><button class="responsive-menu-icon" aria-controls="menu-primary-navigation" aria-expanded="false">menuData.responsiveMenubuttonText</button>');
$(".responsive-menu-icon").click(function(){
var _this = $( this );
_this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
$(this).next("header .genesis-nav-menu, .nav-primary .genesis-nav-menu").slideToggle();
});
$(window).resize(function(){
if(window.innerWidth > 768) {
$("header .genesis-nav-menu, .nav-primary .genesis-nav-menu, nav .sub-menu").removeAttr("style");
$(".responsive-menu > .menu-item").removeClass("menu-open");
}
});
$(".responsive-menu > .menu-item").click(function(event){
if (event.target !== this)
return;
$(this).find(".sub-menu:first").slideToggle(function() {
$(this).parent().toggleClass("menu-open");
});
});
});
@middlesister
Copy link
Author

This is a translatable version of https://gist.github.com/RRWD/e248ca84ef680c5341f1

@punlf
Copy link

punlf commented Oct 14, 2019

I have tested your code.
I have the following suggestions. I am not very good at coding, but would my best to explain.

  1. In accessible-genesis-menu.php, line 23, for the "1.0.0", the two full-stop are in different colors (one is blue and one is red), which suggest that the syntax is wrong. This can be fixed by enclosing 1.0.0 with ' ' .
// enqueue the script
wp_enqueue_script( 'prefix-responsive-menu', get_stylesheet_directory_uri() . '/js/responsive-menu.js', array( 'jquery' ), '1.0.0', true );
  1. In accessible-genesis-menu.php, line 18 and 19, please add a comma after line 19, that is
    'responsiveMenubuttonText' => _x( 'Menu', 'Responsive menu button text', 'textdomain'),
    Also, someone may use __( ) instead of _x( ) for translating string.
    That is
'screenReaderNavHeading'     => __( 'Main navigation', 'textdomain' ),
'responsiveMenubuttonText'   => __( 'Menu', 'textdomain'),
  1. In responsive-menu.js, line 5, the translatable variables menuData.screenReaderNavHeading and menuData.responsiveMenubuttonText are in the ' ' string, and this makes the translation fail to work.
    You should change it to something like ' + menuData.screenReaderNavHeading + ' and ' + menuData.responsiveMenubuttonText + '
    which close the previous string, add variable and then open the next string.
    That is to change line 5 to something like the following:
    $("header .genesis-nav-menu, .nav-primary .genesis-nav-menu").addClass("responsive-menu").before('<h2 class="screen-reader-text" id="rian-main-nav">' + menuData.screenReaderNavHeading + '</h2><button class="responsive-menu-icon" aria-controls="menu-primary-navigation" aria-expanded="false">' + menuData.responsiveMenubuttonText + '</button>');

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment