Issue alert with Vue animations

I have created a functional demonstration of VUE JS menu animation. However, I am encountering an issue due to having two root elements inside the menu component, resulting in the following warning:

[Vue warn]: Runtime directive used on component with non-element root node. The directives will not function as intended.

App.vue

<script setup>
import { ref } from 'vue'
import Menu from './Menu.vue'

const menuVisible = ref(false)
const toggle = () => {
  menuVisible.value = !menuVisible.value
}
</script>

<template>
  <h1>Side bar animation</h1>
  <button @click="toggle">Toggle menu</button>
  <Menu :visible="menuVisible" @update:visible="toggle" v-show="menuVisible"/>
</template>

Menu.vue

<script setup lang="ts">
import {  toRefs, watchEffect, onBeforeUnmount, onMounted } from 'vue';

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false
  }
});

const { visible } = toRefs(props);

const emit = defineEmits(['update:visible']);

const hidemenuBar = () => {
  emit('update:visible', false);
};

watchEffect(() => {
  document.body.style.overflow = visible.value ? 'hidden' : 'auto';
  document.body.style.paddingRight = visible.value ? '15px' : '0px';
});

onBeforeUnmount(() => {
  document.body.style.overflow = 'auto';
  document.body.style.paddingRight = '0px';
});

</script>

<template>
  <transition name="fade">
    <div v-if="visible" class="menubar-overlay" @click="hidemenuBar"></div>
  </transition>
  <transition name="slide">
    <div class="menubar-container" v-if="visible">
      <div class="menubar__header">
        <h2 class="menu-headline">Menu</h2>
        <div>
          <i @click="hidemenuBar" class="cursor-pointer pi pi-times p-2"></i>
        </div>
      </div>

       <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
       </ul>

       <button @click="hidemenuBar">Close</button>
    </div>
  </transition>
</template>

<style scoped>
.slide-enter-from,
.slide-leave-to {
  transform: translateX(100%);
}

.slide-enter-to,
.slide-leave-from {
  transform: translateX(0);
}

.slide-enter-active,
.slide-leave-active {
  transition: transform 1s;
}

/* menubar */
.menubar-overlay {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.7);
  cursor: pointer;
}
.menubar-container {
  position: fixed;
  display: grid;
  align-items: start;
  top: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  max-width: 200px;
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  margin-left: auto;
  background-color: white;
  z-index: 100;
}

.menubar__header {
  z-index: 2;
  display: flex;
  justify-content: space-between;
  margin: 20px;
  border-bottom: 2px solid black;
}
</style>

Unfortunately, when I enclose the transitions in a template within Menu.vue, the animation ceases to work entirely... while wrapping it in a div only results in one-way functionality (slide-in but no slide-out).

Do you have any suggestions on how to resolve this issue?

Test it out here: here

Answer №1

The cautionary signal indicates a confusion over which primary element to assign the v-show directive. It is recommended to manage this within the Menu component rather than at the parent level... even though you are already doing so. Both of your primary elements have the v-if="visible" condition:

 <transition name="fade">
    <div v-if="visible" class="menubar-overlay" @click="hidemenuBar"></div>
  </transition>
  <transition name="slide">
    <div class="menubar-container" v-if="visible">
    ...
  </transition>

You can safely remove v-show="menuVisible" from the parent component as it serves no purpose.

explore updated Playground

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

Accessing the current state outside of a component using React Context

As I delve into creating a React application, I find myself in uncharted territory with hooks and the new context API. Typically, I rely on Redux for my projects, but this time I wanted to explore the context API and hooks. However, I'm encountering s ...

Error: The Node Express Server is unable to locate the requested resource at "/

const http = require("http"); const myApp= require('./myApp'); const portNumber = 8080; const myServer = http.createServer(myApp); myServer.listen(portNumber) ...

Retrieving the URL of an image from a webpage's photo gallery utilizing the Facebook Graph API with the help of Javascript

After trying out multiple tutorials, it appears that the latest version of the Graph API has caused some issues with my code. Here is the snippet I am working with: function fetchAlbumImages(){ FB.api( '/760126260690750/photos&ap ...

Ways to serve HTML without relying on code generated by JQuery

I am currently utilizing the html() function in JQuery to extract the HTML content of a specific div on my web page. Nevertheless, the output includes all the code generated by JQuery as well: <ul><li id="fact_1" class="jstree-open"><ins cl ...

Comparing object keys in JavaScript for property values

I'm currently working on comparing the properties of objects by their keys. Here is some example data: const data = [{ "name": "John", "value": "30" }, { "name": "Cindy", "value": "50" }, { "name": "Mathew", "value": "80" }, { ...

Showing a generated content before element on a progress bar using the ::before pseudo-class

I'm currently attempting to showcase a ::before pseudo-element on a <progress> bar in the form of a label featuring the value attribute. My current approach successfully displays the ::before element in Brave (v1.61.109), but unfortunately, it d ...

Nuxt Apollo GraphQL encountered an issue where a mandatory variable was missing

Using Nuxt.js in conjunction with Apollo, I am attempting to access GraphQL data from WordPress. Successfully retrieving the data proves that everything is functioning as intended. In my file _slug.vue, I am aiming to display a post based on its URI. page ...

Verify if there are duplicate values present in a table

I have a Table that contains multiple rows, each row having input fields. I need to check for duplicate values within the table. Below is my code where I am currently checking for empty values, how can I modify it to also detect duplicate values? JavaScr ...

The Axios interceptor is failing to retrieve the user's current authentication token from the Vuex store

Currently, I am utilizing Axios to transmit user input to a DRF API, which then returns an authentication token. This token is being saved in the Vuex store. However, when I attempt to request another API endpoint using Axios with the most recent token in ...

Remove an object based on its unique identifier with the help of mongoose

I am working on developing an API to delete a document in MongoDB using Mongoose. Below is the route I have created: router .route("/tasks") .delete('/:id', function (res, err) { taskSchema.findByIdAndRemove(req.params.id, (err, ...

What is the best way to keep a header row in place while scrolling?

I am looking to keep the "top" row of the header fixed or stuck during page scrolling, while excluding the middle and bottom rows. I have already created a separate class for the top row in my header code: view image description ...

Exploring the Possibilities of Selenium: Interactive Buttons and Dynamic Class Selections

While experimenting with a test case on a website, Selenium was able to accurately register the events. Now, I am faced with a challenge - how can I search for a specific class and retrieve the innerHTML of that class, especially when using Java as my driv ...

Socket.io: sending signals without receiving any responses

I've been working on a real-time socket.io project that involves a collaborative whiteboard app. I'm facing some issues with emitting data. server.js const express = require('express') const app = express(); const http = require(&apos ...

Enhance your website's user experience with jQuery by implementing smooth scrolling alongside

I have a fixed header at the top of my page. When it scrolls, I want to ensure that it does not cover or hide any of my content, especially the top portion of the section. // This code enables smooth scrolling (source: css-tricks) $('a[href*="#"]:no ...

Ways to optimize angular performance by minimizing unnecessary data binding:NSUTF8B

When comparing the performance of rendering a list using Angular versus other libraries like Handlebars, I noticed about a 10x decrease in speed. In my case, the table does not need to update dynamically when the model changes. If necessary, I can simply ...

Attempting to modify/align JSON data structure

I seem to be struggling with a task that should be straightforward. I am attempting to adjust the structure and variables of a JSON object to fit specific parameters. Here is the current JSON structure I am dealing with: { "name":"BHPhotovideo", ...

Is JavaScript's setTimeout 0 feature delaying the rendering of the page?

Based on information from this StackOverflow post The process of changing the DOM occurs synchronously, while rendering the DOM actually takes place after the JavaScript stack has cleared. Furthermore, according to this document from Google, a screen r ...

Automatically collapse the Bootstrap4 Dropdown Menu upon clicking the select element

After tackling my previous issue on Stack Overflow, I stumbled upon a new question that has been bothering me. The problem arises when users click on the arrow to reveal advanced search options, as the form drops down as expected. However, when a user cli ...

Utilizing a flexbox container within a table container, implementing PRE tags for handling overflow

Currently, I am diligently working on an intricate DASHBOARD utilizing bootstrap-4. A peculiar issue arose with the implementation of <pre> within a Flexbox. However, upon closer inspection, it became evident that this was not a flexbox or <pre> ...

How does the behavior of instanceof change when used within JSON.stringify()?

I am utilizing the decimal.js library for conducting financial calculations within Node. In my code, I have crafted a custom JSON.stringify replacer function. However, I have noticed a discrepancy in the results of property type tests conducted using insta ...