vue.joedoe.dev

← Totes les receptesEl que porta Vue

Llistes que es reordenen amb animació

TransitionGroup amb la classe -move: cada element llisca al seu lloc nou. Respecta «reduir el moviment».

Demo.vue
<script setup lang="ts">
import { ref } from "vue";

let next = 6;
const items = ref([1, 2, 3, 4, 5]);

const shuffle = () => (items.value = [...items.value].sort(() => Math.random() - 0.5));
const add = () => items.value.splice(Math.floor(Math.random() * (items.value.length + 1)), 0, next++);
const remove = (n: number) => (items.value = items.value.filter((i) => i !== n));
</script>

<template>
	<p>
		<button @click="shuffle">Barajar</button>
		<button @click="add">Añadir</button>
		<span class="muted">· clic en una para quitarla</span>
	</p>
	<TransitionGroup name="list" tag="ul" class="chips">
		<li v-for="n in items" :key="n" @click="remove(n)">{{ n }}</li>
	</TransitionGroup>
</template>

<style scoped>
.chips {
	position: relative;
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
	padding: 0;
	list-style: none;
}
.chips li {
	display: grid;
	place-items: center;
	width: 2.75rem;
	height: 2.75rem;
	border-radius: 10px;
	background: #42b883;
	color: #10261b;
	font-weight: 700;
	cursor: pointer;
}
/* Moving items glide to their new place; leaving ones fade out of the flow. */
.list-move,
.list-enter-active,
.list-leave-active {
	transition: all 0.4s ease;
}
.list-enter-from,
.list-leave-to {
	opacity: 0;
	transform: scale(0.5);
}
.list-leave-active {
	position: absolute;
}
@media (prefers-reduced-motion: reduce) {
	.list-move,
	.list-enter-active,
	.list-leave-active {
		transition: none;
	}
}
</style>

· clic en una para quitarla

  • 1
  • 2
  • 3
  • 4
  • 5

↑↓ moure's · ↵ obrir · esc tancar