1

My SPA has an Admin Dashboard with a Sidebar Navigation Menu that is visible by default when the viewport is larger than 991px. When the viewport is less than 991px, the Navbar disappears and you have a hamburger icon that you can click to Toggle(show/hide) the Sidebar Menu. Right now the menu will only hide if you click on the little hamburger menu icon a second time. What I want is to close/hide the menu if a User clicks anywhere outside of it. I'm using Bootstrap-Vue for my UI.

So I have a <b-navbar toggleable="md" type="dark" variant="info"></b-navbar>.

What I'd like to do is something like this, but I know I'm mixing Javascript and Vue js and not sure exactly how to set this up.

data() {
    return {
        sidebarMenu: true
    }
}
<section class="app-sidebar" v-if="sidebarMenu">
<div class="main-panel" @click="hideSidebarNavMenu">
methods: {
    hideSidebarNavMenu() {
        this.sidebarMenu = false;
    },

    const navbarTogglerButton = document.querySelector(".navbar-toggler");

    navbarTogglerButton.addEventListener('click', function(event) {
        this.sidebarMenu = true;
    })
}

This main-panel div will contain all of the data for every page, so no matter where they click this hideSidebarNavMenu function should get fired.

I need this code to work only when the viewport is less than 991px. Again, I'm getting some syntax errors in my methods, I believe because I'm trying to write Javascript in my methods attribute.

This is the idea. Thank you.

2 Answers 2

2

I just realized now that in the Free Admin theme that I'm using, when a user clicks on the Hamburger button to toggle the Sidebar, all it does is just add and remove a class called active on the nav element. Element:

<nav id="sidebar" class="sidebar sidebar-offcanvas active">

And so this is how I solved it:

mounted() {
    var mainPanelDiv = document.querySelector(".main-panel");
    var navbar = document.querySelector("#sidebar");
    mainPanelDiv.addEventListener('click', function(event) {
        navbar.classList.remove('active');
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that methods must contains… methods and not code like const navbarTogglerButton … This is probably the error you have

attach your event to .navbar-toggler

<div class="navbar-toggler" @click="showSidebarNavMenu">
methods: {
    hideSidebarNavMenu() {
        this.sidebarMenu = false;
    },

    showSidebarNavMenu() {
        this.sidebarMenu = true;
    },

}

4 Comments

The problem is that div doesn't exist in my HTML, because as mentioned above it's just Bootstrap-Vue's <b-navbar> component that automatically generates the code inside of it. So I'm not sure how to access the div inside dynamically.
I see, then you need to add your code const navbarTogglerButton = document.querySelector(".navbar-toggler"); navbarTogglerButton.addEventListener('click', function(event) { this.sidebarMenu = true; }) once your component or view is mounted. Have a look to the lifecyle diagram for more details
Okay and is there a way to only call this function when the viewport or window is less than 991px?
Also, the sidebar disappears when I click outside of it, but when I click on the navbar-toggler icon I get this error: TypeError: Cannot read property 'classList' of null and I think it might be because my code/function is in Admin.vue but the <b-navbar> is inside my AppHeader.vue component. How can I fix this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.