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

[#765] Enable dropdowns on desktop for all sections #36

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
6 changes: 4 additions & 2 deletions assets/src/sass/components/_dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
}

[data-dropdown-backdrop="active"] {
opacity: 0.6;
pointer-events: auto;
@media (max-width: $primary-nav-breakpoint) {
opacity: 0.6;
pointer-events: auto;
}
}

[data-dropdown-toggle][disabled] {
Expand Down
14 changes: 14 additions & 0 deletions assets/src/sass/components/_primary-nav.scss
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@
flex-wrap: wrap;
}

// A toggleable sub-menu should be displayed over the open one.
&[data-toggleable="yes"][data-visible="yes"] .sub-menu {
background-color: $color-white;
padding: rem(6) rem(40);
z-index: 11; // Ensure this renders over the menu-item-bottom-line active item marker.

// When toggled, the relative parent is different, so this needs to be positioned differently.
[data-subnav-visible="yes"] & {
box-sizing: content-box;
margin-left: rem(-40);
margin-right: rem(-40);
}
}

a {
align-items: flex-start;
color: $wmui-color-base0;
Expand Down
35 changes: 30 additions & 5 deletions assets/src/scripts/modules/site-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ function createObserver() {
return new IntersectionObserver( handleIntersection );
}

/**
* Is a sub-menu be opened by default?
*
* Detects whether an subnav item is expanded by virtue of being the current
* menu item or current menu ancestor.
*
* @param {HTMLElement} subNav Sub-menu to check.
* @returns {boolean} True if the element should be expanded by default.
*/
function isSubNavExpanded( subNav ) {
return !! subNav.closest( '.current-menu-item, .current-menu-parent' );
}

/**
* Manipulate primary nav to have the viewport-correct behavior.
*
Expand All @@ -47,12 +60,14 @@ function processEntry( entry ) {
_primaryNav.dataset.visible = 'yes';
_primaryNav.dataset.toggleable = 'no';
_primaryNav.dataset.backdrop = 'inactive';
_primaryNav.dataset.trap = 'inactive';
_primaryNav.dataset.trap = 'active';

// Make subnavs not toggleable.
// Make subnavs toggleable, and set visibilty as expected.
_subNavMenus.forEach( _subNavMenu => {
_subNavMenu.dataset.toggleable = 'no';
_subNavMenu.dropdown.toggle.hidden = true;
const isExpanded = isSubNavExpanded( _subNavMenu );
_subNavMenu.dataset.visible = isExpanded ? 'yes' : 'no';
_subNavMenu.dataset.toggleable = isExpanded ? 'no' : 'yes';
_subNavMenu.dropdown.toggle.removeAttribute( 'hidden' );
} );
} else {
// We're on mobile
Expand All @@ -66,7 +81,17 @@ function processEntry( entry ) {
} );
}
} else if ( target.classList.contains( 'sub-menu' ) ) {
target.closest( '[data-dropdown]' ).dataset.trap = 'inactive';
const currentParentItem = target.closest( '[data-dropdown]' );

// If expanding one section, all others should close.
if ( currentParentItem.dataset.visible === 'yes' ) {
[ ..._subNavMenus ].filter( menu => menu !== currentParentItem )
.forEach( item => {
item.dataset.visible = isSubNavExpanded( item ) ? 'yes' : 'no';
} );
} else {
currentParentItem.dataset.trap = 'inactive';
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions inc/classes/walkers/class-walker-main-nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Walker_Main_Nav extends \Walker_Nav_Menu {

// Private variable for the current menu item.
private $currentItemID;

/**
* Starts the list before the elements are added.
*
Expand All @@ -32,7 +32,7 @@ function start_lvl( &$output, $depth = 0, $args = array() ) {
'nav-sub-menu-'. $this->currentItemID,
);
$class_names = implode( ' ', $classes );

// Build HTML for output.
$output .= "\n" . $indent . '<ul class="' . esc_attr( $class_names ) . '" data-dropdown-content="nav-sub-menu-'. $this->currentItemID . '">' . "\n";
}
Expand Down Expand Up @@ -70,16 +70,19 @@ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

// Build HTML.
$active_classes = [ 'current-menu-item', 'current-menu-ancestor' ];
$output .=

$is_open = count( array_intersect( $active_classes, $classes ) ) > 0;

$output .=
$indent
. '<li id="nav-menu-item-'. $item->ID . '"'
. ' class="' . $class_names . '"'
. ( in_array( 'menu-item-has-children', $classes, true ) ?
' data-dropdown="nav-sub-menu-' . $item->ID . '"'
. ' data-dropdown-content=".nav-sub-menu-'. $item->ID . '"'
. ' data-dropdown-toggle=".nav-sub-menu-button-'. $item->ID . '"'
. ' data-visible="' . ( count( array_intersect( $active_classes, $classes ) ) > 0 ? 'yes' : 'no' ) . '"'
. ' data-toggleable="no"'
. ' data-visible="' . ( $is_open ? 'yes' : 'no' ) . '"'
. ' data-toggleable="' . ( $is_open ? 'no' : 'yes' ) . '"'
: '' )
. '>';

Expand All @@ -103,11 +106,12 @@ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

if ( in_array( 'menu-item-has-children', $classes, true ) ) {
$label = __( 'Expand submenu for ', 'shiro' ) . apply_filters( 'the_title', $item->title, $item->ID );
$output .=
$output .=
'<button class="menu-item__expand nav-sub-menu-button-'. $item->ID . '"'
. ' hidden'
. ' aria-label="' . esc_attr( $label ) . '"'
. ' aria-expanded="' . ( count( array_intersect( $active_classes, $classes ) ) > 0 ? 'true' : 'false' ) . '"'
. ' aria-expanded="' . ( $is_open ? 'true' : 'false' ) . '"'
. ( $is_open ? ' disabled="disabled"' : '' )
. ' data-dropdown-toggle="nav-sub-menu-'. $item->ID . '">'
. '<span class="btn-label-a11y">'
. esc_html( $label )
Expand Down