Skip to content

Mini drawer in Quasar that expands when focused and on mouse over

Create slim drawers that expand on focus and on mouse over and stays open while you navigate its menus.

quasar
by
PDan

Problem description

You want to free screen estate and use amini/slim drawer, but you want it to expand when it is focused and on mouse over.

Solution

Nothing very complex, you just need:

  • a flag variable for the mini state of the drawer
  • a flag variable for when a menu from the drawer is opened.
  • only display the mini menu when it should be mini and there is no menu open
vue
<template>
  <div class="q-pa-md">
    <q-layout view="hHh Lpr lff" container style="height: 400px" class="shadow-2 rounded-borders">
      <q-header elevated class="bg-black">
        <q-toolbar>
          <q-btn flat @click="drawer = !drawer" round dense icon="menu" />
          <q-toolbar-title>Header</q-toolbar-title>
          <q-btn flat autofocus label="Focus helper - tab from here to get in the drawer" />
        </q-toolbar>
      </q-header>

      <q-drawer
        v-model="drawer"
        show-if-above

        :mini="miniState === true && miniMenuOpen !== true"
        @mouseover="miniState = false"
        @mouseout="miniState = true"
        @focusin="miniState = false"
      	@focusout="miniState = true"

        :width="200"
        :breakpoint="500"
        bordered
        content-class="bg-grey-3"
      >
        <q-scroll-area class="fit" :horizontal-thumb-style="{ opacity: 0 }">
          <q-list padding>
            <q-item class="q-pr-xs" clickable dense v-ripple>
              <q-item-section avatar>
                <q-icon class="q-py-sm" name="inbox" />
              </q-item-section>

              <q-item-section>
                <q-item-label>
                  Menu1
                </q-item-label>
              </q-item-section>
              <q-item-section side>
                <q-btn icon="more_vert" color="grey" dense flat round @click.stop>
                  <q-menu
                          anchor="top left"
                          self="top left"
                          auto-close
                          @before-show="miniMenuOpen = true"
                          @hide="miniMenuOpen = false"
                  >
                    <q-list dense>
                      <q-item clickable>
                        <q-item-section>Open in a new Tab</q-item-section>
                        <q-item-section avatar>
                          <q-icon name="star" color="yellow" />
                        </q-item-section>
                      </q-item>
                      <q-item clickable>
                        <q-item-section>Add to Favorite</q-item-section>
                        <q-item-section avatar>
                          <q-icon name="open_in_new" color="primary" />
                        </q-item-section>
                      </q-item>
                    </q-list>
                  </q-menu>
                </q-btn>
              </q-item-section>
            </q-item>
          </q-list>
        </q-scroll-area>
      </q-drawer>

      <q-page-container>
        <q-page padding>
          <p v-for="n in 15" :key="n">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Fugit nihil praesentium molestias a adipisci, dolore vitae odit, quidem consequatur optio voluptates asperiores pariatur eos numquam rerum delectus commodi perferendis voluptate?
          </p>
        </q-page>
      </q-page-container>
    </q-layout>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const drawer = ref(false);
const miniState = ref(true);
const miniMenuOpen = ref(false);
</script>

Demo

Last updated: