How to make nav links close the collapse menu? #36786
-
I'm using Bootstrap 5 and Next.js and I'm trying to collapse the Navbar menu when I click a link in mobile view. I've tried a few different methods but I'm hitting a few roadblocks and I can't find any documentation on preferred methods. I've tried using the classlist.remove('show') and this makes the toggle method very jumpy and effects the toggle process. I've tried using ( data-bs-toggle"collapse" data-bs-target="#navbarToggler" ) as properties on the links and this does collapse the menu but the links then blink in and out when the collapse menu is past its breakpoint and is not in the collapse menu. I'm trying now to access the toggle method for that collapse by using the onClick property on the link, and I can't seem to get that to work at all. my method is currently: onClick={() => { Not sure what to do but wondering if anyone here has some experience with this and a preferred go-to method, Thanks for taking the time to read this |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 11 replies
-
Probably you are searching something like const tog = document.getElementById("navbarToggler")
if ( window.innerWidth < 768 && tog.classList.contains("show") ) {
const instance= bootstrap.collapse.getOrCreateInstance(tog)
instance.hide()
} else {
console.log("no");
} |
Beta Was this translation helpful? Give feedback.
-
I've managed to do it, it's probably not the soundest way to do it in react but it's working I ended up doing something along the lines of this: useEffect(() => {
}, []); const handleClick = (e) => { |
Beta Was this translation helpful? Give feedback.
-
You are the best.
…On Wednesday, August 17, 2022, arturhonores ***@***.***> wrote:
Dear @DeanParkin <https://github.com/DeanParkin>
I have the same problem, but I don't know how I can add your solution to
my script, could you provide me with the code, I can't find a solution in
other forums.
I only found this, but it doesn't work for me:
const navLinks = document.querySelectorAll(".nav-item"); const menuToggle
= document.getElementById("navbarSupportedContent"); const bsCollapse =
new bootstrap.Collapse(menuToggle); navLinks.forEach((l) => {
l.addEventListener("click", () => { bsCollapse.toggle(); }); });
—
Reply to this email directly, view it on GitHub
<#36786 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHORFC7DOXUOS4H44T4CW7DVZUP43ANCNFSM54DEZNAQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Thanks for help. |
Beta Was this translation helpful? Give feedback.
I've managed to do it, it's probably not the soundest way to do it in react but it's working I ended up doing something along the lines of this:
useEffect(() => {
let size = window.innerWidth;
let navLinks = document.querySelectorAll(".nav-link");