Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A component for creating animated lists with smooth transitions between items.
motion-sv
components
<script lang="ts"> import { AnimatedList } from "#lib/components/magic/animated-list/index.js"; import { cn } from "#lib/utils.js"; import Notification from "./notification.svelte";
interface NotificationItem { name: string; description: string; icon: string; color: string; time: string; id: number; }
interface AnimatedListDemoProps { class?: string; }
let { class: className }: AnimatedListDemoProps = $props();
let baseNotifications = [ { name: "Payment received", description: "Magic UI", time: "15m ago", icon: "💸", color: "#00C9A7", }, { name: "User signed up", description: "Magic UI", time: "10m ago", icon: "👤", color: "#FFB800", }, { name: "New message", description: "Magic UI", time: "5m ago", icon: "💬", color: "#FF3D71", }, { name: "New event", description: "Magic UI", time: "2m ago", icon: "🗞️", color: "#1E86FF", }, ];
// Create 10 copies of the notifications with unique IDs let notifications: NotificationItem[] = Array.from({ length: 10 }, (_, i) => baseNotifications.map((notif, j) => ({ ...notif, id: i * baseNotifications.length + j, })) ).flat();</script>
<div class={cn("relative flex h-125 w-full flex-col overflow-hidden p-2", className)}> <AnimatedList items={notifications}> {#snippet children(item, i)} <Notification {...item} /> {/snippet} </AnimatedList>
<div class="from-background pointer-events-none absolute inset-x-0 bottom-0 h-1/4 bg-gradient-to-t" ></div></div>Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/animated-list.json
Install dependencies
Install the required packages for this component:
pnpm add motion-sv
Copy the Source Code
Copy and paste the following code into your project:
animated-list.svelte
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { onMount } from "svelte"; import { motion, AnimatePresence, createLayoutMotion } from "motion-sv"; import { cn } from "#lib/utils.js"; import type { Snippet } from "svelte";
interface AnimatedListProps<T> { items: T[]; class?: ClassValue; delay?: number; children: Snippet<[T, number]>; }
type AnimatedListPropsGeneric = AnimatedListProps<any>;
let { items, class: className, delay = 1000, children }: AnimatedListPropsGeneric = $props();
// Create layout motion for smooth layout animations const layout = createLayoutMotion(motion); const update = layout.update;
// Use a mutable state array instead of derived let itemsToShow = $state<any[]>([]);
onMount(() => { // Initialize with first item update.with(() => { itemsToShow = [items[0]]; })();
let currentIndex = 0;
const interval = setInterval(() => { if (currentIndex < items.length - 1) { currentIndex++; // Use update.with to track layout changes update.with(() => { // Add new item at the beginning (top) itemsToShow = [items[currentIndex], ...itemsToShow]; })(); } }, delay);
return () => clearInterval(interval); });</script>
<div class={cn("flex flex-col items-center gap-4", className)}> <AnimatePresence mode="popLayout"> {#each itemsToShow as item, i (item.id)} <layout.div initial={{ scale: 0, opacity: 0, y: -8 }} animate={{ scale: 1, opacity: 1, y: 0, transition: { type: "spring", stiffness: 500, damping: 30 }, }} exit={{ scale: 0, opacity: 0, y: 8, transition: { duration: 0.18 }, }} layout layoutDependency={itemsToShow.length} class="mx-auto w-full" > {@render children(item, i)} </layout.div> {/each} </AnimatePresence></div> index.ts
import AnimatedList from "./animated-list.svelte";export { AnimatedList };Folder Structure
Folder Structure
src/ └── lib/ └── components/ └── magic/ └── animated-list/ ├── animated-list.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/animated-list/llms.txt
Follow the guide's installation and usage instructions.Match the project's existing structure, imports, and styles.Keep changes scoped to the component and verify it works.Reply with a short bullet list of completed changes.Usage
<script lang="ts"> import { AnimatedList } from "#lib/components/magic/animated-list/index.js";
const items = [ { id: 1, label: "First item" }, { id: 2, label: "Second item" }, { id: 3, label: "Third item" }, ];</script>
<AnimatedList {items}> {#snippet children(item)} <div class="rounded-md border p-3">{item.label}</div> {/snippet}</AnimatedList>Props
A component for .
| Name | Type | Default | Description |
|---|---|---|---|
class | ClassValue | "" | Additional CSS classes to apply |
delay | number | 0 | Delay in milliseconds before the animation starts |
items | T[] | [] | An array of items to be rendered in the animated list |
children | Snippet<[T, number]> | | A render prop function that receives each item and its index, returning a Svelte component or HTML to be rendered for that item. |