1import {
2 LoaderCircleIcon,
3 LoaderIcon,
4 LoaderPinwheelIcon,
5 type LucideProps,
6} from "lucide-react";
7import { cn } from "@/lib/utils";
8
9type SpinnerVariantProps = Omit<SpinnerProps, "variant">;
10
11const Throbber = ({ className, ...props }: SpinnerVariantProps) => (
12 <LoaderIcon className={cn("animate-spin", className)} {...props} />
13);
14
15const Pinwheel = ({ className, ...props }: SpinnerVariantProps) => (
16 <LoaderPinwheelIcon className={cn("animate-spin", className)} {...props} />
17);
18
19const CircleFilled = ({
20 className,
21 size = 24,
22 ...props
23}: SpinnerVariantProps) => (
24 <div className="relative" style={{ width: size, height: size }}>
25 <div className="absolute inset-0 rotate-180">
26 <LoaderCircleIcon
27 className={cn("animate-spin", className, "text-foreground opacity-20")}
28 size={size}
29 {...props}
30 />
31 </div>
32 <LoaderCircleIcon
33 className={cn("relative animate-spin", className)}
34 size={size}
35 {...props}
36 />
37 </div>
38);
39
40const Ellipsis = ({ size = 24, ...props }: SpinnerVariantProps) => {
41 return (
42 <svg
43 height={size}
44 viewBox="0 0 24 24"
45 width={size}
46 xmlns="http://www.w3.org/2000/svg"
47 {...props}
48 >
49 <title>Loading...</title>
50 <circle cx="4" cy="12" fill="currentColor" r="2">
51 <animate
52 attributeName="cy"
53 begin="0;ellipsis3.end+0.25s"
54 calcMode="spline"
55 dur="0.6s"
56 id="ellipsis1"
57 keySplines=".33,.66,.66,1;.33,0,.66,.33"
58 values="12;6;12"
59 />
60 </circle>
61 <circle cx="12" cy="12" fill="currentColor" r="2">
62 <animate
63 attributeName="cy"
64 begin="ellipsis1.begin+0.1s"
65 calcMode="spline"
66 dur="0.6s"
67 keySplines=".33,.66,.66,1;.33,0,.66,.33"
68 values="12;6;12"
69 />
70 </circle>
71 <circle cx="20" cy="12" fill="currentColor" r="2">
72 <animate
73 attributeName="cy"
74 begin="ellipsis1.begin+0.2s"
75 calcMode="spline"
76 dur="0.6s"
77 id="ellipsis3"
78 keySplines=".33,.66,.66,1;.33,0,.66,.33"
79 values="12;6;12"
80 />
81 </circle>
82 </svg>
83 );
84};
85
86const Ring = ({ size = 24, ...props }: SpinnerVariantProps) => (
87 <svg
88 height={size}
89 stroke="currentColor"
90 viewBox="0 0 44 44"
91 width={size}
92 xmlns="http://www.w3.org/2000/svg"
93 {...props}
94 >
95 <title>Loading...</title>
96 <g fill="none" fillRule="evenodd" strokeWidth="2">
97 <circle cx="22" cy="22" r="1">
98 <animate
99 attributeName="r"
100 begin="0s"
101 calcMode="spline"
102 dur="1.8s"
103 keySplines="0.165, 0.84, 0.44, 1"
104 keyTimes="0; 1"
105 repeatCount="indefinite"
106 values="1; 20"
107 />
108 <animate
109 attributeName="stroke-opacity"
110 begin="0s"
111 calcMode="spline"
112 dur="1.8s"
113 keySplines="0.3, 0.61, 0.355, 1"
114 keyTimes="0; 1"
115 repeatCount="indefinite"
116 values="1; 0"
117 />
118 </circle>
119 <circle cx="22" cy="22" r="1">
120 <animate
121 attributeName="r"
122 begin="-0.9s"
123 calcMode="spline"
124 dur="1.8s"
125 keySplines="0.165, 0.84, 0.44, 1"
126 keyTimes="0; 1"
127 repeatCount="indefinite"
128 values="1; 20"
129 />
130 <animate
131 attributeName="stroke-opacity"
132 begin="-0.9s"
133 calcMode="spline"
134 dur="1.8s"
135 keySplines="0.3, 0.61, 0.355, 1"
136 keyTimes="0; 1"
137 repeatCount="indefinite"
138 values="1; 0"
139 />
140 </circle>
141 </g>
142 </svg>
143);
144
145const Bars = ({ size = 24, ...props }: SpinnerVariantProps) => (
146 <svg
147 height={size}
148 viewBox="0 0 24 24"
149 width={size}
150 xmlns="http://www.w3.org/2000/svg"
151 {...props}
152 >
153 <title>Loading...</title>
154 <style>{`
155 .spinner-bar {
156 animation: spinner-bars-animation .8s linear infinite;
157 animation-delay: -.8s;
158 }
159 .spinner-bars-2 {
160 animation-delay: -.65s;
161 }
162 .spinner-bars-3 {
163 animation-delay: -0.5s;
164 }
165 @keyframes spinner-bars-animation {
166 0% {
167 y: 1px;
168 height: 22px;
169 }
170 93.75% {
171 y: 5px;
172 height: 14px;
173 opacity: 0.2;
174 }
175 }
176 `}</style>
177 <rect
178 className="spinner-bar"
179 fill="currentColor"
180 height="22"
181 width="6"
182 x="1"
183 y="1"
184 />
185 <rect
186 className="spinner-bar spinner-bars-2"
187 fill="currentColor"
188 height="22"
189 width="6"
190 x="9"
191 y="1"
192 />
193 <rect
194 className="spinner-bar spinner-bars-3"
195 fill="currentColor"
196 height="22"
197 width="6"
198 x="17"
199 y="1"
200 />
201 </svg>
202);
203
204const Infinite = ({ size = 24, ...props }: SpinnerVariantProps) => (
205 <svg
206 height={size}
207 preserveAspectRatio="xMidYMid"
208 viewBox="0 0 100 100"
209 width={size}
210 xmlns="http://www.w3.org/2000/svg"
211 {...props}
212 >
213 <title>Loading...</title>
214 <path
215 d="M24.3 30C11.4 30 5 43.3 5 50s6.4 20 19.3 20c19.3 0 32.1-40 51.4-40 C88.6 30 95 43.3 95 50s-6.4 20-19.3 20C56.4 70 43.6 30 24.3 30z"
216 fill="none"
217 stroke="currentColor"
218 strokeDasharray="205.271142578125 51.317785644531256"
219 strokeLinecap="round"
220 strokeWidth="10"
221 style={{
222 transform: "scale(0.8)",
223 transformOrigin: "50px 50px",
224 }}
225 >
226 <animate
227 attributeName="stroke-dashoffset"
228 dur="2s"
229 keyTimes="0;1"
230 repeatCount="indefinite"
231 values="0;256.58892822265625"
232 />
233 </path>
234 </svg>
235);
236
237export type SpinnerProps = LucideProps & {
238 variant?:
239 | "default"
240 | "throbber"
241 | "pinwheel"
242 | "circle-filled"
243 | "ellipsis"
244 | "ring"
245 | "bars"
246 | "infinite";
247};
248
249export const Spinner = ({ variant, ...props }: SpinnerProps) => {
250 switch (variant) {
251 case "throbber":
252 return <Throbber {...props} />;
253 case "pinwheel":
254 return <Pinwheel {...props} />;
255 case "circle-filled":
256 return <CircleFilled {...props} />;
257 case "ellipsis":
258 return <Ellipsis {...props} />;
259 case "ring":
260 return <Ring {...props} />;
261 case "bars":
262 return <Bars {...props} />;
263 case "infinite":
264 return <Infinite {...props} />;
265 default:
266 return (
267 <CircleFilled className={cn("size-6", props.className)} {...props} />
268 );
269 }
270};
271