A stateful-button component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import React from "react";
4import { motion, AnimatePresence, useAnimate } from "motion/react";
5
6interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
7 className?: string;
8 children: React.ReactNode;
9}
10
11export const Button = ({ className, children, ...props }: ButtonProps) => {
12 const [scope, animate] = useAnimate();
13
14 const animateLoading = async () => {
15 await animate(
16 ".loader",
17 {
18 width: "20px",
19 scale: 1,
20 display: "block",
21 },
22 {
23 duration: 0.2,
24 },
25 );
26 };
27
28 const animateSuccess = async () => {
29 await animate(
30 ".loader",
31 {
32 width: "0px",
33 scale: 0,
34 display: "none",
35 },
36 {
37 duration: 0.2,
38 },
39 );
40 await animate(
41 ".check",
42 {
43 width: "20px",
44 scale: 1,
45 display: "block",
46 },
47 {
48 duration: 0.2,
49 },
50 );
51
52 await animate(
53 ".check",
54 {
55 width: "0px",
56 scale: 0,
57 display: "none",
58 },
59 {
60 delay: 2,
61 duration: 0.2,
62 },
63 );
64 };
65
66 const handleClick = async (event: React.MouseEvent<HTMLButtonElement>) => {
67 await animateLoading();
68 await props.onClick?.(event);
69 await animateSuccess();
70 };
71
72 const {
73 onClick,
74 onDrag,
75 onDragStart,
76 onDragEnd,
77 onAnimationStart,
78 onAnimationEnd,
79 ...buttonProps
80 } = props;
81
82 return (
83 <motion.button
84 layout
85 layoutId="button"
86 ref={scope}
87 className={cn(
88 "flex min-w-[120px] cursor-pointer items-center justify-center gap-2 rounded-full bg-green-500 px-4 py-2 font-medium text-white ring-offset-2 transition duration-200 hover:ring-2 hover:ring-green-500 dark:ring-offset-black",
89 className,
90 )}
91 {...buttonProps}
92 onClick={handleClick}
93 >
94 <motion.div layout className="flex items-center gap-2">
95 <Loader />
96 <CheckIcon />
97 <motion.span layout>{children}</motion.span>
98 </motion.div>
99 </motion.button>
100 );
101};
102
103const Loader = () => {
104 return (
105 <motion.svg
106 animate={{
107 rotate: [0, 360],
108 }}
109 initial={{
110 scale: 0,
111 width: 0,
112 display: "none",
113 }}
114 style={{
115 scale: 0.5,
116 display: "none",
117 }}
118 transition={{
119 duration: 0.3,
120 repeat: Infinity,
121 ease: "linear",
122 }}
123 xmlns="http://www.w3.org/2000/svg"
124 width="24"
125 height="24"
126 viewBox="0 0 24 24"
127 fill="none"
128 stroke="currentColor"
129 strokeWidth="2"
130 strokeLinecap="round"
131 strokeLinejoin="round"
132 className="loader text-white"
133 >
134 <path stroke="none" d="M0 0h24v24H0z" fill="none" />
135 <path d="M12 3a9 9 0 1 0 9 9" />
136 </motion.svg>
137 );
138};
139
140const CheckIcon = () => {
141 return (
142 <motion.svg
143 initial={{
144 scale: 0,
145 width: 0,
146 display: "none",
147 }}
148 style={{
149 scale: 0.5,
150 display: "none",
151 }}
152 xmlns="http://www.w3.org/2000/svg"
153 width="24"
154 height="24"
155 viewBox="0 0 24 24"
156 fill="none"
157 stroke="currentColor"
158 strokeWidth="2"
159 strokeLinecap="round"
160 strokeLinejoin="round"
161 className="check text-white"
162 >
163 <path stroke="none" d="M0 0h24v24H0z" fill="none" />
164 <path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0" />
165 <path d="M9 12l2 2l4 -4" />
166 </motion.svg>
167 );
168};
169