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

Fix placement of the .active class for dropdown items & remove dropdown when item has no visible children #530

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 33 additions & 6 deletions class-wp-bootstrap-navwalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,30 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
// Join any icon classes plucked from $classes into a string.
$icon_class_string = join( ' ', $icon_classes );

// Whether the current item is a dropdown.
$is_dropdown = false;
if ( $this->has_children && 1 !== $args->depth ) {
$is_dropdown = true;
if ( $depth >= $args->depth - 1 && 0 !== $args->depth ) {
$is_dropdown = false;
}
}

// Whether the current item is a dropdown item.
$is_dropdown_item = false;
if ( ! ( $this->has_children && 0 === $depth ) && $depth > 0 ) {
$is_dropdown_item = true;
}

// Whether the current item is active or the item is an ancestor of
// an active item.
$is_active = false;
if ( $item->current || $item->current_item_ancestor ) {
if ( ! ( $item->current_item_ancestor && 1 === $args->depth ) ) {
$is_active = true;
}
}

/**
* Filters the arguments for a single nav menu item.
*
Expand All @@ -165,11 +189,12 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
*/
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );

// Add .dropdown or .active classes where they are needed.
if ( $this->has_children ) {
if ( $is_dropdown ) {
$classes[] = 'dropdown';
}
if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-parent', $classes, true ) ) {

if ( $is_active && ! $is_dropdown_item ) {
// For dropdown items the .active class is set on the a tag.
$classes[] = 'active';
}

Expand Down Expand Up @@ -211,7 +236,7 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
}

// If the item has_children add atts to <a>.
if ( $this->has_children && 0 === $depth ) {
if ( $is_dropdown ) {
$atts['href'] = '#';
$atts['data-toggle'] = 'dropdown';
$atts['aria-expanded'] = 'false';
Expand All @@ -223,9 +248,11 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
}

$atts['href'] = ! empty( $item->url ) ? $item->url : '#';

// For items in dropdowns use .dropdown-item instead of .nav-link.
if ( $depth > 0 ) {
$atts['class'] = 'dropdown-item';
if ( $is_dropdown_item ) {
$atts['class'] = 'dropdown-item';
$atts['class'] .= $is_active ? ' active' : '';
} else {
$atts['class'] = 'nav-link';
}
Expand Down