Search code examples
vuejs2vuetify.jsv-navigation-drawer

How to display two v-navigation-drawer next to each other and be able to hide the right one from the left one?


With Vuetify I would like to display two v-navigation-drawers next to each other and be able to hide the right one with a button within the left one. My current code (see below) displays the right drawer over the left one, but I want it to display right drawer next to the left one (see screens below).

I tried to find something useful in the official documentation and went through a few different sample templates but nothing was helpful in my case.

I was able to come up with the following code:

<template>
  <v-app dark>
    <v-navigation-drawer v-model="leftMenu" app clipped>
      <v-container ma-0 pa-0>
        <v-toolbar flat>
          <span>Some helper toolbar</span>
        </v-toolbar>
      </v-container>

      <v-container pa-1 mt-1>
        <v-layout row wrap>
          <v-list>
            <v-list-tile>
              <v-list-tile-action>
                <span><a @click.stop="toggleRightMenu"><v-icon>android</v-icon> Task Menu</a></span>
              </v-list-tile-action>
            </v-list-tile>
            <v-list-tile>
              <v-list-tile-action>
                <span><v-icon>android</v-icon> Menu Element #1</span>
              </v-list-tile-action>
            </v-list-tile>
          </v-list>
        </v-layout>
      </v-container>
    </v-navigation-drawer>

    <v-navigation-drawer v-model="rightMenu" app clipped>
      <v-container>
        <v-layout>
          <v-list>
            <v-list-tile>
              <v-list-tile-action>
                <span><v-icon>android</v-icon> Testing...</span>
              </v-list-tile-action>
            </v-list-tile>
          </v-list>
        </v-layout>
      </v-container>
    </v-navigation-drawer>

    <v-toolbar app clipped-left>
      <v-toolbar-side-icon @click="leftMenu = !leftMenu"></v-toolbar-side-icon>
      <v-toolbar-title class="headline">
        <span>Testing...</span>
      </v-toolbar-title>
      <v-spacer></v-spacer>
    </v-toolbar>

    <v-content>
      <router-view/>
    </v-content>

    <v-footer app>
      <span>Footer</span>
    </v-footer>
  </v-app>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      leftMenu: true,
      rightMenu: false
    }
  },
  methods: {
    toggleRightMenu() {
      this.rightMenu = !this.rightMenu;
    }
  }
}
</script>

Currently I have the following: https://i.ibb.co/txdN0X8/current-drawer.png

I am looking for something like that: https://i.ibb.co/b514yyC/target-drawer.png


Solution

  • The trick is to have a v-layout that fills the height and contains both of the navigation drawers.

    One more thing that you need to make sure to apply is to have a z-index on both navigation drawers. Otherwise, when you toggle the right nav it will go at the top of the left nav. And when you want to toggle the left nav you actually have to toggle both.

    I have created a Codepen using your data example.

    <v-toolbar flat>
      <v-toolbar-side-icon @click="toggleLeftMenu"></v-toolbar-side-icon>
      <v-toolbar-title class="headline">
        <span>Testing...</span>
      </v-toolbar-title>
      <v-spacer></v-spacer>
    </v-toolbar>
    <v-layout fill-height>
      <v-navigation-drawer
        style="z-index: 2"
        dark
        clipped
        stateless
        v-model="leftMenu"
      >
        <v-container ma-0 pa-0>
          <v-toolbar flat>
            <span>Some helper toolbar</span>
          </v-toolbar>
        </v-container>
        <v-list>
          <v-list-tile>
            <v-list-tile-action>
              <v-icon @click="rightMenu = !rightMenu">android</v-icon>
            </v-list-tile-action>
            <v-list-tile-content>
              <v-list-tile-title>Task Menu</v-list-tile-title>
            </v-list-tile-content>
          </v-list-tile>
          <v-list-tile>
            <v-list-tile-action>
              <v-icon>android</v-icon>
            </v-list-tile-action>
            <v-list-tile-content>
              <v-list-tile-title>Menu Element #1</v-list-tile-title>
            </v-list-tile-content>
          </v-list-tile>
        </v-list>
      </v-navigation-drawer>
      <v-navigation-drawer
        dark
        style="z-index: 1"
        v-model="rightMenu"
        class="pb-0"
        stateless
      >
        <v-list>
          <v-list-tile>
            <v-list-tile-action>
              <v-icon>android</v-icon>
            </v-list-tile-action>
            <v-list-tile-content>
              <v-list-tile-title>Testing right nav</v-list-tile-title>
            </v-list-tile-content>
          </v-list-tile>
        </v-list>
      </v-navigation-drawer>
    </v-layout>
    

    Hope it helps :)