UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
feat: add controlled version support to AnimatedSubscribeButton with async demo (#668)
Mohammed Hamza7afd945472b7
diff --git a/__registry__/index.tsx b/__registry__/index.tsxindex 6bd09263..7a596cf2 100644--- a/__registry__/index.tsx+++ b/__registry__/index.tsx@@ -4191,6 +4191,34 @@ export const Index: Record<string, any> = {}),meta: undefined,},+ "animated-subscribe-button-controlled-demo": {+ name: "animated-subscribe-button-controlled-demo",+ description:+ "Controlled example showing async behavior for AnimatedSubscribeButton.",+ type: "registry:example",+ registryDependencies: [+ "https://magicui.design/r/animated-subscribe-button",+ ],+ files: [+ {+ path: "registry/example/animated-subscribe-button-controlled-demo.tsx",+ type: "registry:example",+ target: "components/animated-subscribe-button-controlled-demo.tsx",+ },+ ],+ component: React.lazy(async () => {+ const mod = await import(+ "@/registry/example/animated-subscribe-button-controlled-demo.tsx"+ );+ const exportName =+ Object.keys(mod).find(+ (key) =>+ typeof mod[key] === "function" || typeof mod[key] === "object",+ ) || item.name;+ return { default: mod.default || mod[exportName] };+ }),+ meta: undefined,+ },"cool-mode-demo": {name: "cool-mode-demo",description: "Example showing cool mode effect for buttons and links.",diff --git a/content/docs/components/animated-subscribe-button.mdx b/content/docs/components/animated-subscribe-button.mdxindex d2da3d69..d95ff206 100644--- a/content/docs/components/animated-subscribe-button.mdx+++ b/content/docs/components/animated-subscribe-button.mdx@@ -53,6 +53,16 @@ import { AnimatedSubscribeButton } from "@/components/magicui/animated-subscribe</AnimatedSubscribeButton>```+## Examples++### Basic++<ComponentPreview name="animated-subscribe-button-demo" />++### Controlled (Async Behavior)++<ComponentPreview name="animated-subscribe-button-controlled-demo" />+## Props| Prop | Type | Default | Description |diff --git a/public/r/animated-subscribe-button-controlled-demo.json b/public/r/animated-subscribe-button-controlled-demo.jsonnew file mode 100644index 00000000..22dfafc2--- /dev/null+++ b/public/r/animated-subscribe-button-controlled-demo.json@@ -0,0 +1,18 @@+{+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",+ "name": "animated-subscribe-button-controlled-demo",+ "type": "registry:example",+ "title": "Animated Subscribe Button (Controlled)",+ "description": "Controlled example showing async behavior for AnimatedSubscribeButton.",+ "registryDependencies": [+ "https://magicui.design/r/animated-subscribe-button"+ ],+ "files": [+ {+ "path": "registry/example/animated-subscribe-button-controlled-demo.tsx",+ "content": "\"use client\";\n\nimport { AnimatedSubscribeButton } from \"@/registry/magicui/animated-subscribe-button\";\nimport { CheckIcon, ChevronRightIcon } from \"lucide-react\";\nimport { useState } from \"react\";\n\nexport default function AnimatedSubscribeButtonControlledDemo() {\n const [subscribed, setSubscribed] = useState(false);\n const [loading, setLoading] = useState(false);\n\n async function handleClick() {\n setLoading(true);\n await new Promise((res) => setTimeout(res, 1500));\n setSubscribed(true);\n setTimeout(() => setSubscribed(false), 3000);\n setLoading(false);\n }\n\n return (\n <AnimatedSubscribeButton\n className=\"w-36\"\n subscribeStatus={subscribed}\n onClick={() => !loading && handleClick()}\n >\n <span className=\"group inline-flex items-center\">\n {loading ? (\n \"Sending...\"\n ) : (\n <>\n Follow\n <ChevronRightIcon className=\"ml-1 size-4 transition-transform duration-300 group-hover:translate-x-1\" />\n </>\n )}\n </span>\n <span className=\"group inline-flex items-center\">\n <CheckIcon className=\"mr-2 size-4\" />\n Subscribed\n </span>\n </AnimatedSubscribeButton>\n );\n}\n",+ "type": "registry:example",+ "target": "components/animated-subscribe-button-controlled-demo.tsx"+ }+ ]+}\ No newline at end of filediff --git a/public/r/animated-subscribe-button.json b/public/r/animated-subscribe-button.jsonindex e49bc86e..fa2e7c9e 100644--- a/public/r/animated-subscribe-button.json+++ b/public/r/animated-subscribe-button.json@@ -10,7 +10,7 @@"files": [{"path": "registry/magicui/animated-subscribe-button.tsx",- "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport { HTMLMotionProps } from \"motion/react\";\nimport { AnimatePresence, motion } from \"motion/react\";\nimport React, { useState } from \"react\";\n\ninterface AnimatedSubscribeButtonProps\n extends Omit<HTMLMotionProps<\"button\">, \"ref\"> {\n subscribeStatus?: boolean;\n children: React.ReactNode;\n className?: string;\n}\n\nexport const AnimatedSubscribeButton = React.forwardRef<\n HTMLButtonElement,\n AnimatedSubscribeButtonProps\n>(\n (\n { subscribeStatus = false, onClick, className, children, ...props },\n ref,\n ) => {\n const [isSubscribed, setIsSubscribed] = useState<boolean>(subscribeStatus);\n\n if (\n React.Children.count(children) !== 2 ||\n !React.Children.toArray(children).every(\n (child) => React.isValidElement(child) && child.type === \"span\",\n )\n ) {\n throw new Error(\n \"AnimatedSubscribeButton expects two children, both of which must be <span> elements.\",\n );\n }\n\n const childrenArray = React.Children.toArray(children);\n const initialChild = childrenArray[0];\n const changeChild = childrenArray[1];\n\n return (\n <AnimatePresence mode=\"wait\">\n {isSubscribed ? (\n <motion.button\n ref={ref}\n className={cn(\n \"relative flex h-10 w-fit items-center justify-center overflow-hidden rounded-lg bg-primary px-6 text-primary-foreground\",\n className,\n )}\n onClick={(e: React.MouseEvent<HTMLButtonElement>) => {\n setIsSubscribed(false);\n onClick?.(e);\n }}\n initial={{ opacity: 0 }}\n animate={{ opacity: 1 }}\n exit={{ opacity: 0 }}\n {...props}\n >\n <motion.span\n key=\"action\"\n className=\"relative flex h-full w-full items-center justify-center font-semibold\"\n initial={{ y: -50 }}\n animate={{ y: 0 }}\n >\n {changeChild} {/* Use children for subscribed state */}\n </motion.span>\n </motion.button>\n ) : (\n <motion.button\n ref={ref}\n className={cn(\n \"relative flex h-10 w-fit cursor-pointer items-center justify-center rounded-lg border-none bg-primary px-6 text-primary-foreground\",\n className,\n )}\n onClick={(e) => {\n setIsSubscribed(true);\n onClick?.(e);\n }}\n initial={{ opacity: 0 }}\n animate={{ opacity: 1 }}\n exit={{ opacity: 0 }}\n {...props}\n >\n <motion.span\n key=\"reaction\"\n className=\"relative flex items-center justify-center font-semibold\"\n initial={{ x: 0 }}\n exit={{ x: 50, transition: { duration: 0.1 } }}\n >\n {initialChild} {/* Use children for unsubscribed state */}\n </motion.span>\n </motion.button>\n )}\n </AnimatePresence>\n );\n },\n);\n\nAnimatedSubscribeButton.displayName = \"AnimatedSubscribeButton\";\n",+ "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport { HTMLMotionProps } from \"motion/react\";\nimport { AnimatePresence, motion } from \"motion/react\";\nimport React, { useEffect, useState } from \"react\";\n\ninterface AnimatedSubscribeButtonProps\n extends Omit<HTMLMotionProps<\"button\">, \"ref\"> {\n subscribeStatus?: boolean;\n children: React.ReactNode;\n className?: string;\n}\n\nexport const AnimatedSubscribeButton = React.forwardRef<\n HTMLButtonElement,\n AnimatedSubscribeButtonProps\n>(({ subscribeStatus, onClick, className, children, ...props }, ref) => {\n const isControlled = subscribeStatus !== undefined; // controlled vs uncontrolled check\n const [isSubscribed, setIsSubscribed] = useState<boolean>(\n subscribeStatus ?? false,\n );\n\n useEffect(() => {\n if (isControlled) {\n setIsSubscribed(subscribeStatus!);\n }\n }, [subscribeStatus, isControlled]);\n\n if (\n React.Children.count(children) !== 2 ||\n !React.Children.toArray(children).every(\n (child) => React.isValidElement(child) && child.type === \"span\",\n )\n ) {\n throw new Error(\n \"AnimatedSubscribeButton expects two children, both of which must be <span> elements.\",\n );\n }\n\n const childrenArray = React.Children.toArray(children);\n const initialChild = childrenArray[0];\n const changeChild = childrenArray[1];\n\n return (\n <AnimatePresence mode=\"wait\">\n {isSubscribed ? (\n <motion.button\n ref={ref}\n className={cn(\n \"relative flex h-10 w-fit items-center justify-center overflow-hidden rounded-lg bg-primary px-6 text-primary-foreground\",\n className,\n )}\n onClick={(e: React.MouseEvent<HTMLButtonElement>) => {\n if (!isControlled) {\n setIsSubscribed(false); // Only toggle manually if uncontrolled\n }\n onClick?.(e);\n }}\n initial={{ opacity: 0 }}\n animate={{ opacity: 1 }}\n exit={{ opacity: 0 }}\n {...props}\n >\n <motion.span\n key=\"action\"\n className=\"relative flex h-full w-full items-center justify-center font-semibold\"\n initial={{ y: -50 }}\n animate={{ y: 0 }}\n >\n {changeChild} {/* Use children for subscribed state */}\n </motion.span>\n </motion.button>\n ) : (\n <motion.button\n ref={ref}\n className={cn(\n \"relative flex h-10 w-fit cursor-pointer items-center justify-center rounded-lg border-none bg-primary px-6 text-primary-foreground\",\n className,\n )}\n onClick={(e) => {\n if (!isControlled) {\n setIsSubscribed(true); // Only toggle manually if uncontrolled\n }\n onClick?.(e);\n }}\n initial={{ opacity: 0 }}\n animate={{ opacity: 1 }}\n exit={{ opacity: 0 }}\n {...props}\n >\n <motion.span\n key=\"reaction\"\n className=\"relative flex items-center justify-center font-semibold\"\n initial={{ x: 0 }}\n exit={{ x: 50, transition: { duration: 0.1 } }}\n >\n {initialChild} {/* Use children for unsubscribed state */}\n </motion.span>\n </motion.button>\n )}\n </AnimatePresence>\n );\n});\n\nAnimatedSubscribeButton.displayName = \"AnimatedSubscribeButton\";\n","type": "registry:ui","target": "components/magicui/animated-subscribe-button.tsx"}diff --git a/public/registry.json b/public/registry.jsonindex 6a34c010..a43d5f49 100644--- a/public/registry.json+++ b/public/registry.json@@ -3074,6 +3074,22 @@}]},+ {+ "name": "animated-subscribe-button-controlled-demo",+ "type": "registry:example",+ "title": "Animated Subscribe Button (Controlled)",+ "description": "Controlled example showing async behavior for AnimatedSubscribeButton.",+ "registryDependencies": [+ "https://magicui.design/r/animated-subscribe-button"+ ],+ "files": [+ {+ "path": "registry/example/animated-subscribe-button-controlled-demo.tsx",+ "type": "registry:example",+ "target": "components/animated-subscribe-button-controlled-demo.tsx"+ }+ ]+ },{"name": "cool-mode-demo","type": "registry:example",diff --git a/registry.json b/registry.jsonindex 6a34c010..a43d5f49 100644--- a/registry.json+++ b/registry.json@@ -3074,6 +3074,22 @@}]},+ {+ "name": "animated-subscribe-button-controlled-demo",+ "type": "registry:example",+ "title": "Animated Subscribe Button (Controlled)",+ "description": "Controlled example showing async behavior for AnimatedSubscribeButton.",+ "registryDependencies": [+ "https://magicui.design/r/animated-subscribe-button"+ ],+ "files": [+ {+ "path": "registry/example/animated-subscribe-button-controlled-demo.tsx",+ "type": "registry:example",+ "target": "components/animated-subscribe-button-controlled-demo.tsx"+ }+ ]+ },{"name": "cool-mode-demo","type": "registry:example",diff --git a/registry/example/animated-subscribe-button-controlled-demo.tsx b/registry/example/animated-subscribe-button-controlled-demo.tsxnew file mode 100644index 00000000..7c8879d6--- /dev/null+++ b/registry/example/animated-subscribe-button-controlled-demo.tsx@@ -0,0 +1,41 @@+"use client";++import { AnimatedSubscribeButton } from "@/registry/magicui/animated-subscribe-button";+import { CheckIcon, ChevronRightIcon } from "lucide-react";+import { useState } from "react";++export default function AnimatedSubscribeButtonControlledDemo() {+ const [subscribed, setSubscribed] = useState(false);+ const [loading, setLoading] = useState(false);++ async function handleClick() {+ setLoading(true);+ await new Promise((res) => setTimeout(res, 1500));+ setSubscribed(true);+ setTimeout(() => setSubscribed(false), 3000);+ setLoading(false);+ }++ return (+ <AnimatedSubscribeButton+ className="w-36"+ subscribeStatus={subscribed}+ onClick={() => !loading && handleClick()}+ >+ <span className="group inline-flex items-center">+ {loading ? (+ "Sending..."+ ) : (+ <>+ Follow+ <ChevronRightIcon className="ml-1 size-4 transition-transform duration-300 group-hover:translate-x-1" />+ </>+ )}+ </span>+ <span className="group inline-flex items-center">+ <CheckIcon className="mr-2 size-4" />+ Subscribed+ </span>+ </AnimatedSubscribeButton>+ );+}diff --git a/registry/magicui/animated-subscribe-button.tsx b/registry/magicui/animated-subscribe-button.tsxindex 80e15ecf..7f41a095 100644--- a/registry/magicui/animated-subscribe-button.tsx+++ b/registry/magicui/animated-subscribe-button.tsx@@ -3,7 +3,7 @@import { cn } from "@/lib/utils";import { HTMLMotionProps } from "motion/react";import { AnimatePresence, motion } from "motion/react";-import React, { useState } from "react";+import React, { useEffect, useState } from "react";interface AnimatedSubscribeButtonPropsextends Omit<HTMLMotionProps<"button">, "ref"> {@@ -15,84 +15,92 @@ interface AnimatedSubscribeButtonPropsexport const AnimatedSubscribeButton = React.forwardRef<HTMLButtonElement,AnimatedSubscribeButtonProps->(- (- { subscribeStatus = false, onClick, className, children, ...props },- ref,- ) => {- const [isSubscribed, setIsSubscribed] = useState<boolean>(subscribeStatus);+>(({ subscribeStatus, onClick, className, children, ...props }, ref) => {+ const isControlled = subscribeStatus !== undefined; // controlled vs uncontrolled check+ const [isSubscribed, setIsSubscribed] = useState<boolean>(+ subscribeStatus ?? false,+ );- 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.",- );+ useEffect(() => {+ if (isControlled) {+ setIsSubscribed(subscribeStatus!);}+ }, [subscribeStatus, isControlled]);- const childrenArray = React.Children.toArray(children);- const initialChild = childrenArray[0];- const changeChild = childrenArray[1];+ 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(- "relative flex h-10 w-fit items-center justify-center overflow-hidden rounded-lg bg-primary px-6 text-primary-foreground",- className,- )}- onClick={(e: React.MouseEvent<HTMLButtonElement>) => {- setIsSubscribed(false);- onClick?.(e);- }}- initial={{ opacity: 0 }}- animate={{ opacity: 1 }}- exit={{ opacity: 0 }}- {...props}+ return (+ <AnimatePresence mode="wait">+ {isSubscribed ? (+ <motion.button+ ref={ref}+ className={cn(+ "relative flex h-10 w-fit items-center justify-center overflow-hidden rounded-lg bg-primary px-6 text-primary-foreground",+ 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 }}>- <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(- "relative flex h-10 w-fit cursor-pointer items-center justify-center rounded-lg border-none bg-primary px-6 text-primary-foreground",- className,- )}- onClick={(e) => {- setIsSubscribed(true);- onClick?.(e);- }}- initial={{ opacity: 0 }}- animate={{ opacity: 1 }}- exit={{ opacity: 0 }}- {...props}+ {changeChild} {/* Use children for subscribed state */}+ </motion.span>+ </motion.button>+ ) : (+ <motion.button+ ref={ref}+ className={cn(+ "relative flex h-10 w-fit cursor-pointer items-center justify-center rounded-lg border-none bg-primary px-6 text-primary-foreground",+ 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 } }}>- <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>- );- },-);+ {initialChild} {/* Use children for unsubscribed state */}+ </motion.span>+ </motion.button>+ )}+ </AnimatePresence>+ );+});AnimatedSubscribeButton.displayName = "AnimatedSubscribeButton";diff --git a/registry/registry-examples.ts b/registry/registry-examples.tsindex 54a59c1e..a257dff6 100644--- a/registry/registry-examples.ts+++ b/registry/registry-examples.ts@@ -1499,6 +1499,23 @@ export const examples: Registry["items"] = [},],},+ {+ name: "animated-subscribe-button-controlled-demo",+ type: "registry:example",+ title: "Animated Subscribe Button (Controlled)",+ description:+ "Controlled example showing async behavior for AnimatedSubscribeButton.",+ registryDependencies: [+ "https://magicui.design/r/animated-subscribe-button",+ ],+ files: [+ {+ path: "registry/example/animated-subscribe-button-controlled-demo.tsx",+ type: "registry:example",+ target: "components/animated-subscribe-button-controlled-demo.tsx",+ },+ ],+ },{name: "cool-mode-demo",type: "registry:example",