UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
🐛 fix: update animated list component
Dillion Verma0e8375a73b78
diff --git a/registry/components/example/animated-list-demo.tsx b/registry/components/example/animated-list-demo.tsxindex 7ac73c3e..8d7500e3 100644--- a/registry/components/example/animated-list-demo.tsx+++ b/registry/components/example/animated-list-demo.tsx@@ -10,31 +10,7 @@ interface Item {color: string;}-const notifications = [- {- name: "New task added",- description: "Magic UI · 15m ago",- icon: "📝",- color: "#1E86FF",- },- {- name: "New message",- description: "Magic UI · 10m ago",- icon: "💬",- color: "#FF3D71",- },- {- name: "User signed up",- description: "Magic UI · 5m ago",- icon: "👤",- color: "#FFB800",- },- {- name: "Payment received",- description: "Magic UI · 2m ago",- icon: "💸",- color: "#00C9A7",- },+let notifications = [{name: "New task added",description: "Magic UI · 15m ago",@@ -61,6 +37,8 @@ const notifications = [},];+notifications = Array.from({ length: 10 }, () => notifications).flat();+const AnimatedListItem = ({ name, description, icon, color }: Item) => {return (<figure@@ -76,7 +54,7 @@ const AnimatedListItem = ({ name, description, icon, color }: Item) => {><div className="flex flex-row items-center gap-4"><div- className="font-lg flex min-h-[3rem] min-w-[3rem] items-center justify-center rounded-2xl"+ className="font-lg flex min-h-[2.5rem] min-w-[2.5rem] items-center justify-center rounded-2xl"style={{backgroundColor: color,}}@@ -99,10 +77,6 @@ const AnimatedListItem = ({ name, description, icon, color }: Item) => {export default function AnimatedListDemo() {return (<div className="relative flex max-h-[500px] min-h-[500px] w-full flex-col overflow-hidden rounded-lg border bg-background p-6 shadow-lg">- {/* <span className="pointer-events-none hidden whitespace-pre-wrap bg-gradient-to-b from-black to-gray-300/80 bg-clip-text text-center font-semibold leading-none text-transparent dark:from-white dark:to-slate-900/10 sm:block sm:text-7xl">- Animated List- </span> */}-<AnimatedList>{notifications.map((item, idx) => (<AnimatedListItem {...item} key={idx}></AnimatedListItem>diff --git a/registry/components/magicui/animated-list.tsx b/registry/components/magicui/animated-list.tsxindex 75354258..ac69de20 100644--- a/registry/components/magicui/animated-list.tsx+++ b/registry/components/magicui/animated-list.tsx@@ -1,76 +1,52 @@"use client";-import { cn } from "@/lib/utils";-import {- AnimatePresence,- MotionProps,- motion,- useIsPresent,-} from "framer-motion";-import React, { ReactElement, useEffect, useRef, useState } from "react";--export function AnimatedList({- className,- children,-}: {- className?: string;- children: React.ReactNode;-}) {- const [items, setItems] = useState<React.ReactNode[]>([]);- const childQueue = useRef(React.Children.toArray(children));- const intervalRef = useRef<NodeJS.Timeout | null>(null);-- useEffect(() => {- // Update the queue when the children prop changes- childQueue.current = React.Children.toArray(children);-- // Start or restart the interval to add children every 1 second- clearInterval(intervalRef.current!); // Clear any existing interval- intervalRef.current = setInterval(() => {- setItems((prevItems) => {- if (childQueue.current.length > 0) {- // Dequeue the next child and add it to the list of items- const nextChild = childQueue.current.shift();- return [nextChild, ...prevItems];- } else {- // When no children are left in the queue, clear the interval- clearInterval(intervalRef.current!);- }- return prevItems;- });- }, 1300);-- // Cleanup the interval on component unmount- return () => {- if (intervalRef.current) {- clearInterval(intervalRef.current);- }- };- }, [children]); // Depend on children, so it restarts when children change-- return (- <div className={cn("flex flex-col items-center gap-4", className)}>- <AnimatePresence>- {items.map((item, index) => (- <AnimatedListItem key={(item as ReactElement)?.key || index}>- {item}- </AnimatedListItem>- ))}- </AnimatePresence>- </div>- );-}+import { AnimatePresence, motion } from "framer-motion";+import React, { ReactElement, useEffect, useMemo, useState } from "react";++export const AnimatedList = React.memo(+ ({+ className,+ children,+ delay = 1000,+ }: {+ className?: string;+ children: React.ReactNode;+ delay?: number;+ }) => {+ const [index, setIndex] = useState(0);+ const childrenArray = React.Children.toArray(children);++ useEffect(() => {+ const interval = setInterval(() => {+ setIndex((prevIndex) => (prevIndex + 1) % childrenArray.length);+ }, delay);++ return () => clearInterval(interval);+ }, [childrenArray.length, delay]);++ const itemsToShow = useMemo(+ () => childrenArray.slice(0, index + 1).reverse(),+ [index, childrenArray],+ );++ return (+ <div className={`flex flex-col items-center gap-4 ${className}`}>+ <AnimatePresence>+ {itemsToShow.map((item) => (+ <AnimatedListItem key={(item as ReactElement).key}>+ {item}+ </AnimatedListItem>+ ))}+ </AnimatePresence>+ </div>+ );+ },+);AnimatedList.displayName = "AnimatedList";export function AnimatedListItem({ children }: { children: React.ReactNode }) {- const isPresent = useIsPresent();- const animations: MotionProps = {- style: {- position: isPresent ? "static" : "absolute",- width: "100%",- margin: "0 auto",- },+ const animations = {initial: { scale: 0, opacity: 0 },animate: { scale: 1, opacity: 1, originY: 0 },exit: { scale: 0, opacity: 0 },@@ -78,7 +54,7 @@ export function AnimatedListItem({ children }: { children: React.ReactNode }) {};return (- <motion.div {...animations} layout>+ <motion.div {...animations} layout className="mx-auto w-full">{children}</motion.div>);