JAVASCRIPT

Vue 3 TransitionGroup for Animating List Item Changes

Animate the addition, removal, and reordering of list items in Vue 3 using the `<TransitionGroup>` component, creating smooth and engaging user interface interactions.

<template>
  <div class="list-animation-container">
    <h2>My Todo List</h2>
    <input v-model="newItemText" @keyup.enter="addItem" placeholder="Add new todo item" class="todo-input">
    <button @click="addItem" class="add-button">Add Item</button>

    <TransitionGroup name="todo-list" tag="ul" class="todo-list">
      <li v-for="(item, index) in todoItems" :key="item.id" class="todo-item">
        <span>{{ item.text }}</span>
        <button @click="removeItem(index)" class="remove-button">Remove</button>
      </li>
    </TransitionGroup>

    <p v-if="todoItems.length === 0" class="empty-message">No items yet. Add some!</p>
  </div>
</template>

<script setup>
import { ref } from 'vue';

let id = 0;
const newItemText = ref('');
const todoItems = ref([
  { id: id++, text: 'Learn Vue 3' },
  { id: id++, text: 'Build a great app' },
  { id: id++, text: 'Deploy to production' },
]);

const addItem = () => {
  if (newItemText.value.trim() !== '') {
    todoItems.value.unshift({ id: id++, text: newItemText.value.trim() });
    newItemText.value = '';
  }
};

const removeItem = (index) => {
  todoItems.value.splice(index, 1);
};
</script>

<style>
.list-animation-container {
  font-family: 'Arial', sans-serif;
  max-width: 500px;
  margin: 30px auto;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background-color: #fff;
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 25px;
}

.todo-input {
  width: calc(100% - 100px);
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 5px;
  font-size: 16px;
  margin-right: 10px;
  box-sizing: border-box;
}

.add-button {
  padding: 12px 18px;
  background-color: #28a745;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.2s ease;
}

.add-button:hover {
  background-color: #218838;
}

.todo-list {
  list-style: none;
  padding: 0;
  margin-top: 25px;
}

.todo-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  margin-bottom: 10px;
  background-color: #f9f9f9;
  border: 1px solid #eee;
  border-radius: 5px;
  transition: all 0.5s ease; /* For move transitions */
}

.todo-item span {
  flex-grow: 1;
  font-size: 17px;
  color: #555;
}

.remove-button {
  background-color: #dc3545;
  color: white;
  border: none;
  padding: 8px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.2s ease;
}

.remove-button:hover {
  background-color: #c82333;
}

.empty-message {
  text-align: center;
  color: #666;
  margin-top: 30px;
  font-style: italic;
}

/* Transition classes for todo-list */
.todo-list-enter-active,
.todo-list-leave-active {
  transition: all 0.5s ease;
}

.todo-list-enter-from,
.todo-list-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}

/* ensure leaving items are taken out of layout flow so that moving
   animations can be calculated correctly. */
.todo-list-leave-active {
  position: absolute;
}

.todo-list-move {
  transition: transform 0.5s ease;
}
</style>
How it works: This snippet uses Vue 3's `<TransitionGroup>` component to add engaging animations when list items are added, removed, or reordered. By wrapping a `v-for` loop with `TransitionGroup` and applying specific CSS transition classes (`.todo-list-enter-from`, `.todo-list-enter-active`, etc.), elements smoothly slide in and out of view. The `tag="ul"` ensures the component renders as a `ul` element, maintaining semantic HTML for the list.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs