"use client"
import React, { useEffect, useState } from "react"
import { AnimatePresence, motion, type HTMLMotionProps } from "motion/react"
import { cn } from "@/lib/utils"
interface AnimatedSubscribeButtonProps extends Omit<
HTMLMotionProps<"button">,
"ref"
> {
subscribeStatus?: boolean
children: React.ReactNode
className?: string
}
export const AnimatedSubscribeButton = React.forwardRef<
HTMLButtonElement,
AnimatedSubscribeButtonProps
>(({ subscribeStatus, onClick, className, children, ...props }, ref) => {
const isControlled = subscribeStatus !== undefined // controlled vs uncontrolled check
const [isSubscribed, setIsSubscribed] = useState<boolean>(
subscribeStatus ?? false
)
useEffect(() => {
if (isControlled) {
setIsSubscribed(subscribeStatus!)
}
}, [subscribeStatus, isControlled])
if (
React.Children.count(children) !== 2 ||
!React.Children.toArray(children).every(
(child) => React.isValidElement(child) && child.type === "span"
)
) {
throw new Error(
"AnimatedSubscribeButton expects two children, both of which must be <span> elements."
)
}
const childrenArray = React.Children.toArray(children)
const initialChild = childrenArray[0]
const changeChild = childrenArray[1]
return (
<AnimatePresence mode="wait">
{isSubscribed ? (
<motion.button
ref={ref}
className={cn(
"bg-primary text-primary-foreground relative flex h-10 w-fit items-center justify-center overflow-hidden rounded-lg px-6",
className
)}
onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
if (!isControlled) {
setIsSubscribed(false) // Only toggle manually if uncontrolled
}
onClick?.(e)
}}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
{...props}
>
<motion.span
key="action"
className="relative flex h-full w-full items-center justify-center font-semibold"
initial={{ y: -50 }}
animate={{ y: 0 }}
>
{changeChild} {/* Use children for subscribed state */}
</motion.span>
</motion.button>
) : (
<motion.button
ref={ref}
className={cn(
"bg-primary text-primary-foreground relative flex h-10 w-fit cursor-pointer items-center justify-center rounded-lg border-none px-6",
className
)}
onClick={(e) => {
if (!isControlled) {
setIsSubscribed(true) // Only toggle manually if uncontrolled
}
onClick?.(e)
}}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
{...props}
>
<motion.span
key="reaction"
className="relative flex items-center justify-center font-semibold"
initial={{ x: 0 }}
exit={{ x: 50, transition: { duration: 0.1 } }}
>
{initialChild} {/* Use children for unsubscribed state */}
</motion.span>
</motion.button>
)}
</AnimatePresence>
)
})
AnimatedSubscribeButton.displayName = "AnimatedSubscribeButton"
Animated Subscribe Button
Try the interaction, copy the React source files, or download the complete example.
About Animated Subscribe Button
Animated Subscribe Button is an animated React and Tailwind CSS button by Dillion Verma. Try the interaction, copy the React source, or download the example with creator credit.
- Effects and categories
- Hover effects, Animated
- Using this example
- Copy a TSX file or download the original source and standalone demo. Test keyboard focus, motion and contrast in your own project.
Usage, Tailwind and licensing questions
Read the source files
demo.tsx
import {AnimatedSubscribeButton} from "@/registry/magicui/animated-subscribe-button"; export default function Demo(){return <AnimatedSubscribeButton><span>Subscribe</span><span>Subscribed</span></AnimatedSubscribeButton>}registry/magicui/animated-subscribe-button.tsx
"use client"
import React, { useEffect, useState } from "react"
import { AnimatePresence, motion, type HTMLMotionProps } from "motion/react"
import { cn } from "@/lib/utils"
interface AnimatedSubscribeButtonProps extends Omit<
HTMLMotionProps<"button">,
"ref"
> {
subscribeStatus?: boolean
children: React.ReactNode
className?: string
}
export const AnimatedSubscribeButton = React.forwardRef<
HTMLButtonElement,
AnimatedSubscribeButtonProps
>(({ subscribeStatus, onClick, className, children, ...props }, ref) => {
const isControlled = subscribeStatus !== undefined // controlled vs uncontrolled check
const [isSubscribed, setIsSubscribed] = useState<boolean>(
subscribeStatus ?? false
)
useEffect(() => {
if (isControlled) {
setIsSubscribed(subscribeStatus!)
}
}, [subscribeStatus, isControlled])
if (
React.Children.count(children) !== 2 ||
!React.Children.toArray(children).every(
(child) => React.isValidElement(child) && child.type === "span"
)
) {
throw new Error(
"AnimatedSubscribeButton expects two children, both of which must be <span> elements."
)
}
const childrenArray = React.Children.toArray(children)
const initialChild = childrenArray[0]
const changeChild = childrenArray[1]
return (
<AnimatePresence mode="wait">
{isSubscribed ? (
<motion.button
ref={ref}
className={cn(
"bg-primary text-primary-foreground relative flex h-10 w-fit items-center justify-center overflow-hidden rounded-lg px-6",
className
)}
onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
if (!isControlled) {
setIsSubscribed(false) // Only toggle manually if uncontrolled
}
onClick?.(e)
}}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
{...props}
>
<motion.span
key="action"
className="relative flex h-full w-full items-center justify-center font-semibold"
initial={{ y: -50 }}
animate={{ y: 0 }}
>
{changeChild} {/* Use children for subscribed state */}
</motion.span>
</motion.button>
) : (
<motion.button
ref={ref}
className={cn(
"bg-primary text-primary-foreground relative flex h-10 w-fit cursor-pointer items-center justify-center rounded-lg border-none px-6",
className
)}
onClick={(e) => {
if (!isControlled) {
setIsSubscribed(true) // Only toggle manually if uncontrolled
}
onClick?.(e)
}}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
{...props}
>
<motion.span
key="reaction"
className="relative flex items-center justify-center font-semibold"
initial={{ x: 0 }}
exit={{ x: 50, transition: { duration: 0.1 } }}
>
{initialChild} {/* Use children for unsubscribed state */}
</motion.span>
</motion.button>
)}
</AnimatePresence>
)
})
AnimatedSubscribeButton.displayName = "AnimatedSubscribeButton"
lib/utils.ts
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function formatNumber(value: number) {
return value.toLocaleString("en-US");
}
CSS
/*! tailwindcss v4.3.3 | MIT License | https://tailwindcss.com */
@layer properties;
@layer theme, base, components, utilities;
@layer theme {
:root, :host {
--font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
"Noto Sans", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
"Segoe UI Symbol", "Noto Color Emoji";
--font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono",
"Courier New", monospace;
--spacing: 0.25rem;
--font-weight-semibold: 600;
--radius-lg: 0.5rem;
--default-transition-duration: 150ms;
--default-transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
--default-font-family: var(--font-sans);
--default-mono-font-family: var(--font-mono);
}
}
@layer base {
*, ::after, ::before, ::backdrop, ::file-selector-button {
box-sizing: border-box;
margin: 0;
padding: 0;
border: 0 solid;
}
html, :host {
line-height: 1.5;
-webkit-text-size-adjust: 100%;
tab-size: 4;
font-family: var(--default-font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji");
font-feature-settings: var(--default-font-feature-settings, normal);
font-variation-settings: var(--default-font-variation-settings, normal);
-webkit-tap-highlight-color: transparent;
}
hr {
height: 0;
color: inherit;
border-top-width: 1px;
}
abbr:where([title]) {
-webkit-text-decoration: underline dotted;
text-decoration: underline dotted;
}
h1, h2, h3, h4, h5, h6 {
font-size: inherit;
font-weight: inherit;
}
a {
color: inherit;
-webkit-text-decoration: inherit;
text-decoration: inherit;
}
b, strong {
font-weight: bolder;
}
code, kbd, samp, pre {
font-family: var(--default-mono-font-family, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace);
font-feature-settings: var(--default-mono-font-feature-settings, normal);
font-variation-settings: var(--default-mono-font-variation-settings, normal);
font-size: 1em;
}
small {
font-size: 80%;
}
sub, sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
table {
text-indent: 0;
border-color: inherit;
border-collapse: collapse;
}
:-moz-focusring:where(:not(iframe)) {
outline: auto;
}
progress {
vertical-align: baseline;
}
summary {
display: list-item;
}
ol, ul, menu {
list-style: none;
}
img, svg, video, canvas, audio, iframe, embed, object {
display: block;
vertical-align: middle;
}
img, video {
max-width: 100%;
height: auto;
}
button, input, select, optgroup, textarea, ::file-selector-button {
font: inherit;
font-feature-settings: inherit;
font-variation-settings: inherit;
letter-spacing: inherit;
color: inherit;
border-radius: 0;
background-color: transparent;
opacity: 1;
}
:where(select:is([multiple], [size])) optgroup {
font-weight: bolder;
}
:where(select:is([multiple], [size])) optgroup option {
padding-inline-start: 20px;
}
::file-selector-button {
margin-inline-end: 4px;
}
::placeholder {
opacity: 1;
}
@supports (not (-webkit-appearance: -apple-pay-button)) or (contain-intrinsic-size: 1px) {
::placeholder {
color: currentcolor;
@supports (color: color-mix(in lab, red, red)) {
color: color-mix(in oklab, currentcolor 50%, transparent);
}
}
}
textarea {
resize: vertical;
}
::-webkit-search-decoration {
-webkit-appearance: none;
}
::-webkit-date-and-time-value {
min-height: 1lh;
text-align: inherit;
}
::-webkit-datetime-edit {
display: inline-flex;
}
::-webkit-datetime-edit-fields-wrapper {
padding: 0;
}
::-webkit-datetime-edit, ::-webkit-datetime-edit-year-field, ::-webkit-datetime-edit-month-field, ::-webkit-datetime-edit-day-field, ::-webkit-datetime-edit-hour-field, ::-webkit-datetime-edit-minute-field, ::-webkit-datetime-edit-second-field, ::-webkit-datetime-edit-millisecond-field, ::-webkit-datetime-edit-meridiem-field {
padding-block: 0;
}
::-webkit-calendar-picker-indicator {
line-height: 1;
}
:-moz-ui-invalid {
box-shadow: none;
}
button, input:where([type="button"], [type="reset"], [type="submit"]), ::file-selector-button {
appearance: button;
}
::-webkit-inner-spin-button, ::-webkit-outer-spin-button {
height: auto;
}
[hidden]:where(:not([hidden="until-found"])) {
display: none !important;
}
}
@layer utilities {
.relative {
position: relative;
}
.flex {
display: flex;
}
.h-10 {
height: calc(var(--spacing) * 10);
}
.h-full {
height: 100%;
}
.w-fit {
width: fit-content;
}
.w-full {
width: 100%;
}
.cursor-pointer {
cursor: pointer;
}
.items-center {
align-items: center;
}
.justify-center {
justify-content: center;
}
.overflow-hidden {
overflow: hidden;
}
.rounded-lg {
border-radius: var(--radius-lg);
}
.border-none {
--tw-border-style: none;
border-style: none;
}
.bg-primary {
background-color: var(--primary);
}
.px-6 {
padding-inline: calc(var(--spacing) * 6);
}
.font-semibold {
--tw-font-weight: var(--font-weight-semibold);
font-weight: var(--font-weight-semibold);
}
.text-primary-foreground {
color: var(--primary-foreground);
}
.transition {
transition-property: color, background-color, border-color, outline-color, text-decoration-color, fill, stroke, --tw-gradient-from, --tw-gradient-via, --tw-gradient-to, opacity, box-shadow, transform, translate, scale, rotate, filter, -webkit-backdrop-filter, backdrop-filter, display, content-visibility, overlay, pointer-events;
transition-timing-function: var(--tw-ease, var(--default-transition-timing-function));
transition-duration: var(--tw-duration, var(--default-transition-duration));
}
}
:root {
--background: #fff;
--foreground: #111;
--primary: #111;
--primary-foreground: #fff;
--border: #ddd;
--color-1: #ff7676;
--color-2: #f6ff76;
--color-3: #76ffcf;
--color-4: #76c2ff;
--color-5: #c276ff;
}
.dark {
--background: #0a0a0a;
--foreground: #fff;
--primary: #fff;
--primary-foreground: #111;
--border: #333;
}
@property --tw-font-weight {
syntax: "*";
inherits: false;
}
@layer properties {
@supports ((-webkit-hyphens: none) and (not (margin-trim: inline))) or ((-moz-orient: inline) and (not (color:rgb(from red r g b)))) {
*, ::before, ::after, ::backdrop {
--tw-font-weight: initial;
}
}
}