0

Does anyone know if there is a simple method of closing the Boostrap-VueJS toggle navbar when clicking outside?

I've tried multiple directive codes, tried the vue-click-outside plugin, and many different examples but without luck. It seems that when I try to bypass the vue bootstrap component, the hamburger toggle button stops working.

Here is my code:

<b-navbar toggleable="lg" fixed="top">
    <b-navbar-brand class="header-name" :to="{name: 'homeLink'}">Test</b-navbar-brand>
    <b-navbar-toggle class="custom-toggler" target="nav-collapse"></b-navbar-toggle>
    <b-collapse id="nav-collapse" is-nav>
      <!-- Right aligned nav items -->
      <b-navbar-nav class="ml-auto" >
        <b-nav-form>
        </b-nav-form>
         <b-nav-item :to="{name: 'homeLink'}">Home</b-nav-item>
          <b-nav-item :to="{name: 'test1'}">test</b-nav-item>
         <b-nav-item :to="{name: 'test2'}" >test</b-nav-item>
         <b-nav-item :to="{name: 'test3'}" >test</b-nav-item>
         <b-nav-item :to="{name: 'test4'}" >test</b-nav-item>
      </b-navbar-nav>
    </b-collapse>
  </b-navbar>
singamnv
  • 91
  • 2
  • 10

4 Answers4

1

I finally got it to work via using the following mechanism:

  1. Installed the vue-click-outside package
  2. imported and followed the directions from the package page
  3. installed jquery and imported it
  4. toggled the collapse event using a root event
<template>
<div>
  <b-navbar toggleable="lg" fixed="top">
    <b-navbar-brand class="header-name" :to="{name: 'homeLink'}">test</b-navbar-brand>
    <b-navbar-toggle class="custom-toggler" target="nav-collapse" v-click-outside="hide"></b-navbar-toggle>
    <b-collapse id="nav-collapse" is-nav>
      <!-- Right aligned nav items -->
      <b-navbar-nav class="ml-auto" >
        <b-nav-form>
        </b-nav-form>
         <b-nav-item :to="{name: 'homeLink'}">Home</b-nav-item>
      </b-navbar-nav>
    </b-collapse>
  </b-navbar>
</div> 
</template>

<script>
import ClickOutside from 'vue-click-outside'
import * as $ from 'jquery';


export default {
    name: 'appHeader',
    data() {

    },
    mounted () {
    // prevent click outside event with popupItem.
    this.popupItem = this.$el
  },
    methods:{

        hide(){
            console.log('hiding')
            this.$root.$emit('bv::toggle::collapse', 'nav-collapse')

        }

    },
    directives: {
         ClickOutside
     },

}
</script>
singamnv
  • 91
  • 2
  • 10
  • 1
    I ended up throwing the root event into an if statement so it doesn't toggle when navbar is already collapsed: ```if ($('.navbar-toggler').attr('aria-expanded') === "true") { this.$root.$emit('bv::toggle::collapse', 'nav-collapse') }``` – singamnv May 06 '20 at 04:06
0

One workaround is to think of it like a modal. Whenever the navbar is toggled, create an invisible scrim behind the navbar, with a click handler to close the navbar.

<div 
  v-if="navOpen" 
  @click="navOpen = false" 
  style="position:fixed;left:0;top:0;right:0;bottom:0;z-index:0">
</div>
Erich
  • 2,408
  • 18
  • 40
  • I can't seem to get the bootstrap elements to respond as modals. This is the example I tried with no luck: https://www.npmjs.com/package/vue-click-outside – singamnv May 06 '20 at 02:56
0

Here's a working example of what Erich suggested.

This will create a <div>that covers the entire screen, just under your navbar. Which we can then bind a @click event to, to collapse the navbar.

It's bound to the b-collapse v-model, so that it only shows when the collapse is open.

You can even add a <transition> element around the backdrop if you want some sort of fade for the backdrop. (Couldn't get this working in a Stackoverflow Snippet, but you can see an example here

new Vue({
  el: "#app",
  data() {
    return {
      isNavbarCollapseOpen: false
    };
  }
});
.navbar-backdrop {
  z-index: 1029;
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
<link href="https://unpkg.com/bootstrap@4.4.1/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://unpkg.com/bootstrap-vue@2.13.0/dist/bootstrap-vue.css" rel="stylesheet" />

<script src="https://unpkg.com/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script>
<script src="https://unpkg.com/bootstrap-vue@2.13.0/dist/bootstrap-vue.js"></script>

<div id="app">
  <b-navbar toggleable="xl" fixed="top" variant="dark" type="dark">
    <b-navbar-brand class="header-name" :to="{name: 'homeLink'}">Test</b-navbar-brand>
    <b-navbar-toggle class="custom-toggler" target="nav-collapse"></b-navbar-toggle>
    <b-collapse v-model="isNavbarCollapseOpen" id="nav-collapse" is-nav>
      <!-- Right aligned nav items -->
      <b-navbar-nav class="ml-auto">
        <b-nav-form>
        </b-nav-form>
        <b-nav-item :to="{name: 'homeLink'}">Home</b-nav-item>
        <b-nav-item :to="{name: 'test1'}">test</b-nav-item>
        <b-nav-item :to="{name: 'test2'}">test</b-nav-item>
        <b-nav-item :to="{name: 'test3'}">test</b-nav-item>
        <b-nav-item :to="{name: 'test4'}">test</b-nav-item>
      </b-navbar-nav>
    </b-collapse>
  </b-navbar>
  <div v-if="isNavbarCollapseOpen" @click="isNavbarCollapseOpen = false" class="navbar-backdrop">
  </div>
  <p v-for="i in 50">Some content</p>
</div>
Hiws
  • 8,230
  • 1
  • 19
  • 36
-2

I figureout using this method: https://stackoverflow.com/a/50174966/12647927

import * as $ from 'jquery'

watch: {
'$route' () {
  $('#navbarCallejeritos').collapse('hide')
  },
},