I don’t know if this is an issue with something I’m doing within Roots or whether it’s a lack of general understanding so I apologise in advance if I’m asking for help in the wrong place.
I’m using Bootstrap v4.1.3 and Roots v9.0.5.
I’ve got a navbar that should default to collapsed until the screen size is large. However, the menu is still showing on small screens (vertical menu) and the navbar toggler is showing too. When I click on the navbar toggler the menu collapses but when I refresh the page or move to other pages the navbar always defaults to open. On large screens the menu is visible (horizontal menu) and the navbar toggler is not showing.
I’ve looked at the site’s HTML and the navbar has the ‘show’ class in the ‘navbar-collapse.collapse’ div by default which I don’t think it should do.
I have got the following code in app/Controllers/App.php
public function primarymenu()
{
$args = [
'theme_location' => 'primary_navigation',
'menu_class' => 'navbar-nav mr-auto',
'walker' => new \App\wp_bootstrap4_navwalker()
];
return $args;
}
I haven’t added any script in my common.js file because I didn’t think it was necessary but when I have tried adding $(’.collapse’).collapse(); it didn’t make any difference.
I have got other code in common.js:
lozad
ekko lightbox
I’ve tried replacing the html within with the first example code on https://getbootstrap.com/docs/4.0/components/navbar/ and the same thing happens on small screens - so, the navbar toggler is visible and the vertical menu is shown. Clicking on the navbar toggler collapses the menu successfully.
I hope I’m explaining this clearly! Is anyone able to help?
This is more of a Bootstrap question than a Sage question, but if you can share the actual HTML output of your code I’ll bet we could find the problem.
Even if I replace the entire navbar with the sample code from the Bootstrap 4 site, it still happens. Somehow the ‘show’ class gets added in for small screens.
This one’s got me stumped. I would try eliminating other factors: remove other JavaScript, try updating Bootstrap to the latest version, check that you’re not using the same collapse ID elsewhere on your page, etc. see if you can remove things until it works.
It’s so strange isn’t it. I’ve tried removing all the JavaScript I added to common.js (I haven’t got anything in home.js or about.js) and it makes no difference. I don’t think I’ve added any JavaScript elsewhere.
I’m not using the same collapse ID elsewhere on the page.
I’ve updated Bootrap from v4.1.3 to v4.2.1 and that hasn’t fixed it either.