The difference between transition and transition-group components in Vue
Frontend Engineer who loves building tools.
Search for a command to run...
Frontend Engineer who loves building tools.
No comments yet. Be the first to comment.
Performance is a touchy subject in software engineering. It’s one thing to write code and ship it; it’s another thing entirely to make it faster and more enjoyable for your users — and the next developers after you. In this article, we will look at h...
Arrays have a lot of helpful higher-order functions that make working with them easier. Our focus in this article will be the sort() method, this method has both easy and complex use cases which we'll be looking at in this article. First look at the ...
Gridsome is a static site generator used for building powerful frontend applications, it was built on the Vue.js framework. One of the beauties of Gridsome apart from its ability to generate server-side rendered applications is its compatibility with...
Build hooks are unique URLs used to trigger a build on the Netlify. They can be used to refresh the content on your website after you’ve published or unpublished content in your Storyblok space. Getting the hook URL from Netlify To create one, open y...

Animations seem to be a necessity these days in frontend development, improving the user experience in apps by a significant margin. Thankfully vue.js shipped two built-in components that abstract away the complexities of animating our apps; transition and transition-group.
In this article, we'll be looking at a brief overview of the similarities and differences between these two components, how to use them and possible gotchas when working with them.
First off, we look at the similarities:
These two components as I mentioned earlier are used for animating between elements and components in our vue.js app.
name attribute indicating the name of the animation to be applied.Here's a high-level refresher on how to use these components:
<transition name="fade">
<!-- elements to be animated -->
</transition>
<transition-group name="fade">
<!-- elements to be animated -->
</transition-group>
That's just about how similar these components are, now its time to look at their differences.
The major difference between the transition and transition-group component is how they are used.
transition componentThe transition component is used to animate elements or components that fall under these categories:
v-show)v-if)router-view componentsBelow is an example of how you would use the transition component with dynamic components.
<template>
<div>
<transition name='fade'>
<component :is="currentComponent"></component>
</transition>
</div>
</template>
<script>
import UserOne from '~/components/users/One.vue'
import UserTwo from '~/components/users/Two.vue'
export default{
props: {
userId: {
type: Number,
required: true
}
}
components:{
UserOne,
UserTwo
},
computed:{
currentComponent(){
return this.userId === 1? 'UserOne' : 'UserTwo'
}
}
}
</script>
In the snippet above, two components will be toggled based on the value of the userId prop, we use the transition component to animate the process.
<template>
<div>
<button @click="showDescription = !showDecription">toggle description</button>
<transition name='bounce-up'>
<p v-show="showDescription">
This is a decription.
</p>
</transition>
</div>
</template>
<script>
export default{
data(){
return {
showDescription: false
}
}
}
</script>
The example above uses the conditional display directive (v-show) to toggle the display of the element and we can now animate when the element appears and disappears.
router-view componentIf our app had one base component that was responsible for rendering all route components, usually (App.vue), we could wrap the router-view component with a transition component to animate when routes are being moved to and from.
<template>
<div>
<nav>
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>
<router-link to="/contact">Contact us</router-link>
</nav>
<transition :name="transitionName">
<router-view></router-view>
</transiton>
</div>
</template>
<script>
export default {
data(){
return {
transitionName: 'fade-up'
}
}
watch: {
'$route'(to, from){
const toDepth = to.path.split('/').length
const fromDepth = from.path.split('/').length
this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
}
}
}
</script>
transition-group componentThe transition-group component has a different use-case when dealing with animations.
transition-group is used when dealing with the entry and exit of multiple components or elements in the DOM (eg. rendering lists).
Before we get into get code examples, let's look at how transition-group differs from transition
span but it can be changed with the tag attribute.transition-group component must have a key attribute with unique values (using the indices of the element does not work, it is not unique).<template>
<div>
<h2>Todos:</h2>
<transition-group name="fade-x" tag="ol">
<li v-for="todo in notDone" :key="todo.id">
<label>
<input type="checkbox"
v-on:change="toggle(todo)"
v-bind:checked="todo.done">
<del v-if="todo.done">
{{ todo.text }}
</del>
<span v-else>
{{ todo.text }}
</span>
</label>
</li>
</transition-group>
</div>
</template>
<script>
export default {
data(){
return{
todos: [
{ id: 1, text: "Learn JavaScript", done: false },
{ id: 2, text: "Learn Vue", done: false },
{ id: 3, text: "Play around in JSFiddle", done: true },
{ id: 4, text: "Build something awesome", done: false }
]
}
},
methods: {
toggle: function(todo){
todo.done = !todo.done
}
},
computed: {
notDone(){
return this.todos.filter(todo=> !todo.done)
}
}
}
</script>
<style>
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
.fade-x-enter-active, .fade-x-leave-active{
transition: all .3s;
}
.fade-x-enter, .fade-x-leave-to{
opacity: 0;
transform: translateX(20px)
}
</style>
Now if a task is checked off, you can notice the animation before the element leaves the DOM.
You can create a new fiddle here and paste the snippet above to test it out.
If you didn't have time to stick around long enough to read the entire article, here's a summary:
transition when only one element or component will be rendered at a time, and transiton-group for multiple.transition-group elements must have unique key attributes.tag attribute on the transition-group component to change the kind of element that wraps all elements within.