Bootstrap 5 Navbar not working with either Menu or Nav action

I’m really struggling to get a nav menu to work in my WP theme. I’ve added the navbar (BS5) and the static nav works fine so there are no JS issues. But when I add the action to the

    tag (I’ve tried both Menu - Smart and Nav Menu) it keeps showing the sub-pages instead of putting them in the dropdown.

    Screenshot 2022-11-18 at 09.41.11

    This is really holding everything up so I need to get it working. I’ve watched Adam’s live video here and it seems to just work first time. I have exactly the same setup but it just isn’t working.

    Can someone please help me?

I’ve created a small javascript file which replaces everything so that the Menu looks fine again, but it’s very basic. @matjaz mentioned that there will be a new Pinegrow Version coming out this (or probably more likely next) week which includes the optimized nav-walker, where you can select the class of your dropdown and it get’s recognized. It also Includes support for Bootstrap 5 as far as i know.

So probably the best option is to wait for the next version, but if it’s really urgent I can send you the javascript-code!

1 Like

Fantastic. Thanks @Wolfgang.Hartl. I’m tempted to just wait until the next PG version but if you’re able to send me your javascript that would be really helpful as a stopgap. I’m hoping to show the client the build but without the nav working it will only cause more confusion than help.

@sitestreet Just had a look at it. I just needed it once, so I’m after reading it not quite sure if there was any additonal setup needed in pinegrow, but I don’t think so!

What the script does is turning every nav-item into a bootstrap dropdown button (only 1 level deep). As I mentioned, I think it should work out of the box, let me know if not please then I’m gonna have a look into the project in Pinegrow what I did there! :wink:

Ah and one important thing to mention, it removes the “HREF” attribute from the created dropdown-button element (parent element of any child).



document.addEventListener("DOMContentLoaded", () => {
  const subparents = document.querySelectorAll(".menu-item-has-children");

  subparents.forEach((link) => {
    var navItem = link.parentElement;
    navItem.classList.add("d-flex", "align-items-center", "position-relative");

    var newLink = document.createElement("button");
    newLink.innerHTML = link.innerHTML;

    // Copy the attributes
    for (index = link.attributes.length - 1; index >= 0; --index) {
      newLink.attributes.setNamedItem(link.attributes[index].cloneNode());
    }
    newLink.removeAttribute("href");
    newLink.classList.add("dropdown-toggle", "btn", "btn-link");
    newLink.setAttribute("data-bs-toggle", "dropdown");

    link.after(newLink);
    link.remove();

    // parent.classList.add("dropdown-toggle dropdown-toggle-split");

    var sub = newLink.nextElementSibling;
    sub.classList.add("dropdown-menu");

    subChilds = sub.querySelectorAll(".nav-item");

    subChilds.forEach((subEntry) => {
      subEntry.classList.add("dropdown-item");
    });
  });
  console.log(subparents);
  console.log("LOADED!");
});
2 Likes

Genius. That works perfectly. Removing the HREF is fine and actually what I would rather happen anyway.

Thanks for that. I’ve put it into a separate .js file and included it on the page so I can easily remove it again when the next PG is released.

Cheers again, you’re a star.

2 Likes

Awesome, glad that it worked… It’s been a while since I’ve used it! :smiley:

Cheers

3 Likes

Thanks @Wolfgang.Hartl! We are having some delays due to updating to the latest NWJS (including Mac arm build) and the resulting changes to the install packages build process. But PG7 is due next week.

4 Likes

That’s very exciting news! M1 build is also exciting.

1 Like

No Worries, take your time - I’m sure it will be a cool release when it’s finished! I’m very excited about it & looking forward to it, but one or two weeks more or less doesn’t really matter! :wink:

1 Like

Can I just check one thing? Will the new version support multiple depth navmenus or just one level? I need it to support multiple.

I’m not sure what the production version will look like on the desktop, but I just checked the latest WP Plugin build, and it’s only single-level for Bootstrap 5.

From a usability point of view, not everything deserves to be presented in a menu.
1 level is ideal. Your users will thank you :slight_smile:

I do agree with you but there are times when just one more level is needed.

Moreover, beyond the possible limitations of Pinegrow’s walker, Bootstrap no longer supports multi-level menus since version 3. There are certainly hacks as we published HERE for Bootstrap 4 but it is not planned that we propose a new tutorial for version 5 (and following versions).

In December 2012, in a statement posted on the Bootstrap 3 Development Github, Mark Otto (the creator of Bootstrap) said “We haven’t seen anyone using submenus in meaningful ways and the code necessary to make them work well is just too much overhead. Submenus just don’t have much of a place on the web right now, especially the mobile web. They will be removed with 3.0.”.

In 2016, when asked again about the support of submenus as part of the development of Bootstrap 4, he said “Nested dropdowns aren’t supported and haven’t been since v3. I made the decision over three years ago, and it hasn’t stopped anyone from building anything :).”.

For accessibility issues, and particularly on mobile touch devices, the use of such menus is nowadays widely discussed and it is generally recommended not to use them.

2 Likes

OK, I shall make sure I stick to one level. I do sites for schools and they seem to always need multiple levels in their menus but I know it would be better to avoid that… so I will :slightly_smiling_face:

@Wolfgang.Hartl, I’m hoping you can help with this. Your script works perfectly and it’s populating the nav bar as it should. However, the dropdown colors are changed to white text on a white background and light grey background on hover. If I remove the Menu action, the styling is correct. And it’s also correct in PG. So it must be to do with the script but I just can’t see where.

I was hoping to hold out for PG7 when the native solution will be included but it’s been delayed until next week and I need to show an update to my client before then.

Are you able to offer any possible thoughts on this?

Hey, I’m gonna have a look at it tomorrow. Could be that a wrong class is assigned or so, gonna have to look through it :slight_smile:

1 Like

@sitestreet just checked the documentation of bootstrap again. Didn’t find any indicator that the class assigned to the child-elements is responsible for that hover-effect.

Maybe there are some other styles you have set in your theme/plugin to those child-elements which are responsible for that effect? As far as I know I think the sub-elements get the same classes as the main-menu items?

The Only class that is added to the sub-items is the “dropdown-item” class located at the end of the script:

    subChilds.forEach((subEntry) => {
      subEntry.classList.add("dropdown-item");
    });

Maybe you can share a URL of your site so I can inspect what’s going on there?

1 Like

Thanks again @Wolfgang.Hartl for all your help. Unfortunately I’m working on localhost so can’t share anything but I will continue to dig.

Worst case, you can just do CSS dropdowns for the next few days until PG7 is released.

/* Menu Styling */
.menulist__nav--mainmenu .menu-item {
    position: relative;
    display: flex;
    flex-direction: column;
    transition-duration: 0.2s;
    font-size: var(--font-size-1);
    flex-grow: 1;
    color: white;
    padding: var(--padding-0) var(--padding-1) var(--padding-0) var(--padding-1);
}

.menulist__nav--mainmenu .menu-item:hover,
.menulist__nav--mainmenu .menu-item:focus,
.menulist__nav--mainmenu .menu-item:focus-within {
    background-color: rgba(255, 255, 255, 0.8);
    color: var(--asc-blue);
    cursor: pointer;
}

.menulist__nav--mainmenu .menu-item > a {
    cursor: default;
    display: flex;
    font-weight: 400;
    text-decoration: none;
    color: white;
    align-items: center;
}

.menulist__nav--mainmenu .menu-item > a::before {
    cursor: default;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    position: fixed;
    display: none;
}

.menulist__nav--mainmenu .menu-item:hover > a,
.menulist__nav--mainmenu .menu-item:focus > a,
.menulist__nav--mainmenu .menu-item:focus-within > a {
    color: var(--asc-blue);
    cursor: pointer;
}

.menulist__nav--mainmenu .sub-menu {
    position: absolute;
    background-color: rgba(255, 255, 255, 0.8);
    display: flex;
    flex-direction: column;
    transition-duration: 0.2s;
    left: 0;
    top: 0;
    margin-top: var(--padding-3);
    padding-bottom: 0.5rem;
    visibility: hidden;
    opacity: 0;
    font-size: var(--body-font);
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: 4rem;
    white-space: nowrap;
    min-width: 100%;
    box-shadow: 5px 12px 20px rgb(0 0 0 / 20%);
}

.menulist__nav--mainmenu .menu-item:nth-last-of-type(1) .sub-menu {
    left: unset;
    right: 0;
}

.menulist__nav--mainmenu .sub-menu > li {
    padding-top: unset;
    padding-bottom: unset;
}

.menulist__nav--mainmenu .sub-menu > li > a {
    text-decoration-line: none;
    color: var(--asc-blue);
    font-weight: 400;
    font-size: var(--body-font);
    text-align: left;
    transition-duration: 0.2s;
    padding-top: var(--padding-0);
    padding-bottom: var(--padding-0);
    width: 100%;
}

.menulist__nav--mainmenu .sub-menu > li > a:hover,
.menulist__nav--mainmenu .sub-menu > li > a:focus {
    color: var(--asc-red);
}
2 Likes