A multi-step-loader component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import { AnimatePresence, motion } from "motion/react";
4import { useState, useEffect } from "react";
5
6const CheckIcon = ({ className }: { className?: string }) => {
7 return (
8 <svg
9 xmlns="http://www.w3.org/2000/svg"
10 fill="none"
11 viewBox="0 0 24 24"
12 strokeWidth={1.5}
13 stroke="currentColor"
14 className={cn("w-6 h-6 ", className)}
15 >
16 <path d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
17 </svg>
18 );
19};
20
21const CheckFilled = ({ className }: { className?: string }) => {
22 return (
23 <svg
24 xmlns="http://www.w3.org/2000/svg"
25 viewBox="0 0 24 24"
26 fill="currentColor"
27 className={cn("w-6 h-6 ", className)}
28 >
29 <path
30 fillRule="evenodd"
31 d="M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12Zm13.36-1.814a.75.75 0 1 0-1.22-.872l-3.236 4.53L9.53 12.22a.75.75 0 0 0-1.06 1.06l2.25 2.25a.75.75 0 0 0 1.14-.094l3.75-5.25Z"
32 clipRule="evenodd"
33 />
34 </svg>
35 );
36};
37
38type LoadingState = {
39 text: string;
40};
41
42const LoaderCore = ({
43 loadingStates,
44 value = 0,
45}: {
46 loadingStates: LoadingState[];
47 value?: number;
48}) => {
49 return (
50 <div className="flex relative justify-start max-w-xl mx-auto flex-col mt-40">
51 {loadingStates.map((loadingState, index) => {
52 const distance = Math.abs(index - value);
53 const opacity = Math.max(1 - distance * 0.2, 0); // Minimum opacity is 0, keep it 0.2 if you're sane.
54
55 return (
56 <motion.div
57 key={index}
58 className={cn("text-left flex gap-2 mb-4")}
59 initial={{ opacity: 0, y: -(value * 40) }}
60 animate={{ opacity: opacity, y: -(value * 40) }}
61 transition={{ duration: 0.5 }}
62 >
63 <div>
64 {index > value && (
65 <CheckIcon className="text-black dark:text-white" />
66 )}
67 {index <= value && (
68 <CheckFilled
69 className={cn(
70 "text-black dark:text-white",
71 value === index &&
72 "text-black dark:text-lime-500 opacity-100"
73 )}
74 />
75 )}
76 </div>
77 <span
78 className={cn(
79 "text-black dark:text-white",
80 value === index && "text-black dark:text-lime-500 opacity-100"
81 )}
82 >
83 {loadingState.text}
84 </span>
85 </motion.div>
86 );
87 })}
88 </div>
89 );
90};
91
92export const MultiStepLoader = ({
93 loadingStates,
94 loading,
95 duration = 2000,
96 loop = true,
97}: {
98 loadingStates: LoadingState[];
99 loading?: boolean;
100 duration?: number;
101 loop?: boolean;
102}) => {
103 const [currentState, setCurrentState] = useState(0);
104
105 useEffect(() => {
106 if (!loading) {
107 setCurrentState(0);
108 return;
109 }
110 const timeout = setTimeout(() => {
111 setCurrentState((prevState) =>
112 loop
113 ? prevState === loadingStates.length - 1
114 ? 0
115 : prevState + 1
116 : Math.min(prevState + 1, loadingStates.length - 1)
117 );
118 }, duration);
119
120 return () => clearTimeout(timeout);
121 }, [currentState, loading, loop, loadingStates.length, duration]);
122 return (
123 <AnimatePresence mode="wait">
124 {loading && (
125 <motion.div
126 initial={{
127 opacity: 0,
128 }}
129 animate={{
130 opacity: 1,
131 }}
132 exit={{
133 opacity: 0,
134 }}
135 className="w-full h-full fixed inset-0 z-[100] flex items-center justify-center backdrop-blur-2xl"
136 >
137 <div className="h-96 relative">
138 <LoaderCore value={currentState} loadingStates={loadingStates} />
139 </div>
140
141 <div className="bg-gradient-to-t inset-x-0 z-20 bottom-0 bg-white dark:bg-black h-full absolute [mask-image:radial-gradient(900px_at_center,transparent_30%,white)]" />
142 </motion.div>
143 )}
144 </AnimatePresence>
145 );
146};
147