Vue animation :: warning issue #10650
Answered
by
Yordan-Ramchev
petr-hybler
asked this question in
Help/Questions
-
I have a working example of VUE JS menu animation ... however the problem is I have two root elements inside the menu component... 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> However, the problem is when I wrap the transitions in Menu.vue into a template the animation doesn't work at all... if I wrap it in a div, it only works one-way (slide-in, no slide-out) Any idea how to fix it? Playground: here |
Beta Was this translation helpful? Give feedback.
Answered by
Yordan-Ramchev
Apr 7, 2024
Replies: 1 comment 1 reply
-
Playground: here |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
petr-hybler
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Playground: here