Creating a dynamic Vuetify app bar that adapts to different screen sizes

I have been working on setting up a responsive Vuetify app bar for my Vue.js project. When the website is viewed on a mobile screen, the navigation links are displayed in a 3-dot drop down menu.

<v-app>
    <v-app-bar color="indigo" dark fixed app>
      <v-toolbar-title>Toolbar Mobile Menu</v-toolbar-title>
      <v-spacer></v-spacer>
      <v-toolbar-items class="hidden-sm-and-down">
        <v-btn text to="create">
          <span class="mr-2" v-if="activeUser">Create Post</span>
        </v-btn>
        <v-btn text to="/">
          <span class="mr-2" v-if="activeUser">All Posts</span>
        </v-btn>
      </v-toolbar-items>

      <v-menu class="hidden-md-and-up">
        <template v-slot:activator="{ on }">
          <v-btn icon v-on="on">
            <v-icon>mdi-dots-vertical</v-icon>
          </v-btn>
        </template>

        <v-list>
          <v-list-item v-if="activeUser" to="create">
            <v-list-item-title>Create Post</v-list-item-title>
          </v-list-item>
          <v-list-item v-if="activeUser" to="/">
            <v-list-item-title>All Posts</v-list-item-title>
          </v-list-item>
        </v-list>
      </v-menu>
    </v-app-bar>
    <v-content>
      <router-view></router-view>
    </v-content>
  </v-app>

My issue is that the 3-dot button appears in both full screen and mobile views. I specifically want it to show only in the mobile view. I tried wrapping the button inside the v-toolbar-items tags, but that didn't solve the problem. Does anyone have any suggestions on how to configure the app bar so that the 3-dot button is visible only in the mobile view? Thank you!

Answer №1

My usual approach is to create a small helper function

computed: {
   isMobile() {
      return this.$vuetify.breakpoint.xsOnly;
   }
}

You can find the list of different available breakpoints here.
Then, you can use it in your template like this:

<v-menu v-if="isMobile">
      
</v-menu>

Alternatively, you can directly utilize $vuetify in your markup.

<v-menu v-if="$vuetify.breakpoint.xsOnly">
          
</v-menu>

Answer №2

When it comes to Vue3 and Vuetify3, the recommended approach is to utilize this.$vuetify.display.xs

Answer №3

Building upon the insights shared earlier by @discolor and @Jaysanf:

If you are working with Vuetify3 in conjunction with the Vue3 Composition API, you can take advantage of the "useDisplay" hook.

<script setup>
    import { computed, unref } from 'vue'
    import { useDisplay } from 'vuetify'

    const display = useDisplay()

    const isMobile = computed(() => {
      return unref(display.mobile)
    })
</script>

Subsequently, you can utilize isMobile within the template to conditionally display content specifically for mobile views:

<template>
    <v-menu v-if="isMobile">
          // This menu will only be visible on mobile devices
    </v-menu>
</template>

The rationale behind using unref(display.mobile) is because display.mobile already functions as a Ref. Neglecting to incorporate unref() could result in unexpected behavior when utilizing isMobile in both the template and script sections.

By opting for unref(), you avoid dealing with a

ComputedRef<Ref<boolean>>
, which would necessitate referencing isMobile.value in the template – a less intuitive approach. Instead, employing unref() generates a straightforward ComputedRef<boolean>.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are some ways to conceal or deactivate the JSON response in the browser when using the $http.post method in AngularJS

I am looking for a way to protect my json response data using angularjs $http.post("./rest/getMethodBack", parameter).then(function(response){ console.log(response); }); When viewed in the browser console, the output will appear as: {"name": ...

Is there a way to broadcast a message to all the Discord servers where my bot is currently active using v14 of discord.js?

Can someone assist me in creating code that allows me to send a message to all servers at my discretion? I have not found any resources on this topic for discord.py or the newer versions of discord.js ...

The functionality of GET_POST is not functioning properly

I've encountered an issue with my PHP webpage and JavaScript function: function send_answer(){ $.ajax({ type: 'POST', url: '../path2file/file_mine.php', data: {dbf1:1, us:1, re:1}, success: relo ...

What is the best way to connect tags with their corresponding tag synonyms?

I'm currently developing a system where users can link tags to posts, similar to how it's done on SO. I'm facing some challenges when it comes to implementing tag synonyms. Let's take a look at the Tags table: | TagName | |-------- ...

Changing the value of a ref in Vue 3 within a function

I need to change the value of 'reset' from true to false when a file is uploaded. Even though 'reset' is externally set to true, it should be changed to false during file upload process. How can this be achieved? <template> rese ...

Is it possible to include a class in a .json object for use with CSS?

I am working with a .json object that is displayed using Angular's ng-bind directive: <p ng-bind-html="paragraph" ng-repeat="paragraph in content.sections[0].pages[1].paragraphs"></p> Here is the structure of the .json data: { "header ...

Is there a way to save the values of all input fields in a single container using local storage simultaneously?

Let's say I have the following structure: <div id="main"> <div id="wrapper"> <input/> <input/> <input/> </div> </div> My goal is to use localstorage to store the entire wrappe ...

When attempting to make a GET request from Django, the infamous Error 404 NOT FOUND rears

I encountered a 404 error message stating: "Failed to load resource: the server responded with a status of 404 (Not Found) at http://127.0.0.1:8000/api/v1/products/$%7Bcategory_slug%7D/$%7Bproduct_slug%7D:1" Within my code for product/models.py: From impo ...

Pass the outcome back to the calling function

I've been struggling with this issue for a while. I am working with ion-autocomplete and trying to retrieve data using a factory. The Factory I'm using is as follows: myApp.factory('items', function($http){ return { list: fun ...

I recently created a "dist" folder through Babel. Is it possible to integrate the files within this folder directly into a fresh React project?

I have a React library that is available on npm. My process for publishing it involves the following steps: To begin, I create a folder called dist using Babel by executing this command- babel ./src -d ./dist Next, I upload the resulting dist directory ...

Performing a JSON AJAX call that sends a null object to a Java class

Within my JavaScript file, I am utilizing the following getJSON method like so: $.getJSON('do/ajax-convertlocaldatetime', { timestamp : localdatetime, The localdatetime variable is an instance of a LocalDateTime object. This ajax call trigg ...

JavaScript Canvas Vanishes When Width is Altered

Following the execution of this code snippet: let canvasElement = document.getElementById("inventoryCanvas"); canvasElement.width = width2; The code runs successfully, but I encounter an issue where the canvas disappears afterwards. I am unable to deter ...

What drawbacks come with developing an Express.js application using TypeScript?

Curious about the potential drawbacks of using TypeScript to write Express.js applications or APIs instead of JavaScript. ...

Ajax updates previous text to new text upon successfully completing the task

I have a question regarding changing text using AJAX after success. I have written this AJAX code which is functioning properly. However, I aim to replace the old text with new text in the .chnged div. For instance: <input type="text" name="text" va ...

What is the method to allocate the smallest available number that has not yet been assigned?

As I'm dynamically generating elements and adding them to the page, each element needs a unique numerical id. However, due to non-linear removal of elements, there can be gaps in the assigned ids. For example... In one scenario: 1, 3, 4, 5, 16, 22. ...

Guide to update the source of several images simultaneously based on the chosen list item

My website includes a variety of images and thumbnails showcasing a car in different colors. There are four color options available, along with a selection list to choose a color. I would like the ability to switch between colors in the list and have 4 out ...

Organize routes into distinct modules in Angular 6

Currently grappling with routing in my Angular 6 application. Wondering if the structure I have in mind is feasible. Here's what it looks like: The App module contains the main routing with a parent route defining the layout: const routes: Routes = ...

Preserving the initial input values in a secure manner for future reference, utilizing either an object or a

Recently, I've started revisiting a script I created a while back that checks for changes in a form to prompt a message asking 'Would you like to save your changes?'... One thing that's been on my mind is whether I should store the ori ...

Express.js Issue: Error in JSON Object Returned When Date Parameter is Empty

Currently tackling the challenges of the FreeCodeCamp Timestamp Microservice project and facing a roadblock in the implementation. While most requirements are met successfully, there's an issue with handling an empty date parameter test case. Let me b ...

The term "Spark" has not been formally established

I recently started integrating Spark into my Laravel project with Vue.js. However, when I added it to the home.blade.php file, I encountered the following error: Uncaught ReferenceError: Spark is not defined This is what I have already tried: Ran the co ...