Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore using the Popover API in Twenty Fifteen theme #7899

Closed
Closed
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
37 changes: 37 additions & 0 deletions src/wp-content/themes/twentyfifteen/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,43 @@ function twentyfifteen_scripts() {
}
add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );

$_wp2015_id = null;
$_wp2015_i = 0;
add_filter(
'walker_nav_menu_start_el',
static function ( $output, $item, $depth, $args ) {
global $_wp2015_id, $_wp2015_i;

if ( isset( $item->classes ) && in_array( 'menu-item-has-children', $item->classes, true ) ) {
$_wp2015_i++;
$_wp2015_id = 'wp-submenu-' . $_wp2015_i;

$output .= '<button class="dropdown-toggle" popovertarget="' . esc_attr( $_wp2015_id ) . '" aria-expanded="false">';
$output .= '<span class="screen-reader-text">';
$output .= __( 'expand child menu', 'twentyfifteen' );
$output .= '</span>';
$output .= '</button>';
}
return $output;
},
10,
4
);

add_filter(
'nav_menu_submenu_attributes',
static function ( $attrs ) {
global $_wp2015_id;
if ( isset( $_wp2015_id ) ) {
$attrs['id'] = $_wp2015_id;
$attrs['popover'] = 'auto';

$_wp2015_id = null;
}
return $attrs;
}
);

/**
* Enqueue styles for the block-based editor.
*
Expand Down
27 changes: 12 additions & 15 deletions src/wp-content/themes/twentyfifteen/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,20 @@
secondary, button;

function initMainNavigation( container ) {
// Add dropdown toggle that display child menu items.
container.find( '.menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' );

// Toggle buttons and submenu items with active children menu items.
container.find( '.current-menu-ancestor > button' ).addClass( 'toggle-on' );
container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );

container.find( '.dropdown-toggle' ).on( 'click', function( e ) {
var _this = $( this );
e.preventDefault();
_this.toggleClass( 'toggle-on' );
_this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
_this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
_this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
container.querySelectorAll( '.sub-menu[popover]' ).forEach( function ( subMenu ) {
var toggleButton = container.querySelector( '.dropdown-toggle[popovertarget="' + subMenu.id + '"]' );
subMenu.addEventListener( 'toggle', function () {
toggleButton.classList.toggle( 'toggle-on' );
toggleButton.setAttribute( 'aria-expanded', toggleButton.getAttribute( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
toggleButton.innerHTML = toggleButton.innerHTML === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand;
} );
} );

container.querySelectorAll( '.current-menu-ancestor > .sub-menu[popover]' ).forEach( function ( subMenu ) {
subMenu.showPopover();
} );
}
initMainNavigation( $( '.main-navigation' ) );
initMainNavigation( document.querySelector( '.main-navigation' ) );

// Re-initialize the main navigation when it is updated, persisting any existing submenu expanded states.
$( document ).on( 'customize-preview-menu-refreshed', function( e, params ) {
Expand Down
13 changes: 11 additions & 2 deletions src/wp-content/themes/twentyfifteen/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -676,15 +676,23 @@ a:focus {
margin: 0;
}

.main-navigation ul ul {
.main-navigation ul ul:not([popover]) {
display: none;
margin-left: 0.8em;
}

.main-navigation ul .toggled-on {
.main-navigation ul ul:not([popover]).toggled-on {
display: block;
}

.main-navigation ul ul[popover] {
margin-left: 0.8em;
position-anchor: --parent-link;
top: anchor(bottom);
left: anchor(center);
justify-self: anchor-center;
}

.main-navigation li {
border-top: 1px solid #eaeaea;
border-top: 1px solid rgba(51, 51, 51, 0.1);
Expand All @@ -702,6 +710,7 @@ a:focus {
}

.main-navigation .menu-item-has-children > a {
anchor-name: --parent-link;
padding-right: 48px;
}

Expand Down
Loading