How to hide sidebar nav menu when user clicks outside of sidebar nav menu using Vue js?


Ryan Sacks

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 smaller than 991px, the navigation bar disappears and you see a hamburger icon that you can click to toggle (show/hide) the sidebar menu. Now the menu is only hidden when the little hamburger menu icon is clicked a second time. What I want is to close/hide the menu if the user clicks anywhere outside the menu. I am using Bootstrap-Vue for my UI.

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

What I want to do is something like this, but I know I'm mixing Javascript and Vue js, but not sure how to set it 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 the data for each page, so wherever they click this hideSidebarNavMenufunction should fire.

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

That's the idea. Thank you.

Ryan Sacks

I just realized now that in the Free Admin theme I use, when the user clicks the hamburger button to toggle the sidebar, all it does is add and remove a class called active on the navigation element. element:

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

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');
    });
}

Related


Accordion SideBar menu using Nav component with react-bootstrap

Sergey Bespalov I'm starting to build a user interface using react-bootstrap, and now, I have an important task to create a SideBar accordion menu using standard bootstrapcomponents . I found this example useful panel-groupand useful table, but I don't think i

Accordion SideBar menu using Nav component with react-bootstrap

Sergey Bespalov I'm starting to build a user interface using react-bootstrap, and now, I have an important task to create a SideBar accordion menu using standard bootstrapcomponents . I found this example useful panel-groupand useful table, but I don't think i

Accordion SideBar menu using Nav component with react-bootstrap

Sergey Bespalov I'm starting to build a user interface using react-bootstrap, and now, I have an important task to create a SideBar accordion menu using standard bootstrapcomponents . I found this example useful panel-groupand useful table, but I don't think i

Accordion SideBar menu using Nav component with react-bootstrap

Sergey Bespalov I'm starting to build a user interface using react-bootstrap, and now, I have an important task to create a SideBar accordion menu using standard bootstrapcomponents . I found this example useful panel-groupand useful table, but I don't think i

Accordion SideBar menu using Nav component with react-bootstrap

Sergey Bespalov I'm starting to build a user interface using react-bootstrap, and now, I have an important task to create a SideBar accordion menu using standard bootstrapcomponents . I found this example useful panel-groupand useful table, but I don't think i

Hide the menu sidebar when clicking outside the bar or button

Mander I'm trying to make a menu similar to Semantic UI, but I can only achieve clicking a menu button and opening the menu and vice versa. I use a toggle class to show the sidebar, but I don't know if this way is exactly right: <div class="menu-button" id="me

Hide the menu sidebar when clicking outside the bar or button

Mander I'm trying to make a menu similar to Semantic UI, but I can only achieve clicking a menu button and opening the menu and vice versa. I use a toggle class to show the sidebar, but I don't know if this way is exactly right: <div class="menu-button" id="me

Hide the menu sidebar when clicking outside the bar or button

Mander I'm trying to make a menu similar to Semantic UI, but I can only achieve clicking a menu button and opening the menu and vice versa. I use a toggle class to show the sidebar, but I don't know if this way is exactly right: <div class="menu-button" id="me

Hide the menu sidebar when clicking outside the bar or button

Mander I'm trying to make a menu similar to Semantic UI, but I can only achieve clicking a menu button and opening the menu and vice versa. I use a toggle class to show the sidebar, but I don't know if this way is exactly right: <div class="menu-button" id="me

Hide the menu sidebar when clicking outside the bar or button

Mander I'm trying to make a menu similar to Semantic UI, but I can only achieve clicking a menu button and opening the menu and vice versa. I use a toggle class to show the sidebar, but I don't know if this way is exactly right: <div class="menu-button" id="me

Hide the menu sidebar when clicking outside the bar or button

Mander I'm trying to make a menu similar to Semantic UI, but I can only achieve clicking a menu button and opening the menu and vice versa. I use a toggle class to show the sidebar, but I don't know if this way is exactly right: <div class="menu-button" id="me

How to hide the sidebar when clicking outside the sidebar?

allah jesus I have a sidebar that looks like this: Clicking the arrow will collapse the sidebar again. However, I would like to automatically close the sidebar if I click outside the sidebar. Is that possible? Here is my script to toggle the sidebar: <script t

How to hide the sidebar when clicking outside the sidebar?

allah jesus I have a sidebar that looks like this: Clicking the arrow will collapse the sidebar again. However, I would like to automatically close the sidebar if I click outside the sidebar. Is that possible? Here is my script to toggle the sidebar: <script t

How to hide the sidebar when clicking outside the sidebar?

allah jesus I have a sidebar that looks like this: Clicking the arrow will collapse the sidebar again. However, I would like to automatically close the sidebar if I click outside the sidebar. Is that possible? Here is my script to toggle the sidebar: <script t

How to hide the sidebar when clicking outside the sidebar?

allah jesus I have a sidebar that looks like this: Clicking the arrow will collapse the sidebar again. However, I would like to automatically close the sidebar if I click outside the sidebar. Is that possible? Here is my script to toggle the sidebar: <script t

How to hide the sidebar when clicking outside the sidebar?

allah jesus I have a sidebar that looks like this: Clicking the arrow will collapse the sidebar again. However, I would like to automatically close the sidebar if I click outside the sidebar. Is that possible? Here is my script to toggle the sidebar: <script t

How to set "Hide" and show on sidebar menu

More Murat I have a template but this js template has errors IDK what happens. I want to make this hide and show functional sidebar with another js. <div class="mdc-list-group sidebar">// this start sidebar <nav class="mdc-list mdc-drawer-menu "> <div cl

How to hide navbar when expanding/opening nav menu in javascript

Triple Beef I want to prevent the navbar from pulling down when the navmenu is open/expanded, I have a piece of javascript that pulls the navbar down when the user scrolls from the beginning of the page, when the menu is clicked the navbar also hides open, so

Multilevel sidebar menu using JS/jquery

small I need a responsive sidebar navigation (as part of making the site responsive) with a multi-level menu with a depth of 3. The html tag is as follows, <nav> <div data-level="1"> <ul> <li class="main-menu"> Level1