Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

[component] Text animate component (#482)

Dillion Verma9da78b49d5e0
diff --git a/__registry__/index.tsx b/__registry__/index.tsx
index 4eaa113d..3c6b0a91 100644
--- a/__registry__/index.tsx
+++ b/__registry__/index.tsx
@@ -564,13 +564,13 @@ export const Index: Record<string, any> = {
subcategory: "undefined",
chunks: [],
},
- "word-fade-in": {
- name: "word-fade-in",
+ "text-animate": {
+ name: "text-animate",
type: "registry:ui",
registryDependencies: undefined,
- files: ["registry/default/magicui/word-fade-in.tsx"],
+ files: ["registry/default/magicui/text-animate.tsx"],
component: React.lazy(
- () => import("@/registry/default/magicui/word-fade-in.tsx"),
+ () => import("@/registry/default/magicui/text-animate.tsx"),
),
source: "",
category: "undefined",
@@ -1687,13 +1687,65 @@ export const Index: Record<string, any> = {
subcategory: "undefined",
chunks: [],
},
- "word-fade-in-demo": {
- name: "word-fade-in-demo",
+ "text-animate-demo": {
+ name: "text-animate-demo",
type: "registry:example",
registryDependencies: undefined,
- files: ["registry/default/example/word-fade-in-demo.tsx"],
+ files: ["registry/default/example/text-animate-demo.tsx"],
component: React.lazy(
- () => import("@/registry/default/example/word-fade-in-demo.tsx"),
+ () => import("@/registry/default/example/text-animate-demo.tsx"),
+ ),
+ source: "",
+ category: "undefined",
+ subcategory: "undefined",
+ chunks: [],
+ },
+ "text-animate-demo-2": {
+ name: "text-animate-demo-2",
+ type: "registry:example",
+ registryDependencies: undefined,
+ files: ["registry/default/example/text-animate-demo-2.tsx"],
+ component: React.lazy(
+ () => import("@/registry/default/example/text-animate-demo-2.tsx"),
+ ),
+ source: "",
+ category: "undefined",
+ subcategory: "undefined",
+ chunks: [],
+ },
+ "text-animate-demo-3": {
+ name: "text-animate-demo-3",
+ type: "registry:example",
+ registryDependencies: undefined,
+ files: ["registry/default/example/text-animate-demo-3.tsx"],
+ component: React.lazy(
+ () => import("@/registry/default/example/text-animate-demo-3.tsx"),
+ ),
+ source: "",
+ category: "undefined",
+ subcategory: "undefined",
+ chunks: [],
+ },
+ "text-animate-demo-4": {
+ name: "text-animate-demo-4",
+ type: "registry:example",
+ registryDependencies: undefined,
+ files: ["registry/default/example/text-animate-demo-4.tsx"],
+ component: React.lazy(
+ () => import("@/registry/default/example/text-animate-demo-4.tsx"),
+ ),
+ source: "",
+ category: "undefined",
+ subcategory: "undefined",
+ chunks: [],
+ },
+ "text-animate-demo-5": {
+ name: "text-animate-demo-5",
+ type: "registry:example",
+ registryDependencies: undefined,
+ files: ["registry/default/example/text-animate-demo-5.tsx"],
+ component: React.lazy(
+ () => import("@/registry/default/example/text-animate-demo-5.tsx"),
),
source: "",
category: "undefined",
diff --git a/components/component-wrapper.tsx b/components/component-wrapper.tsx
index 8d9315e3..c02e156b 100644
--- a/components/component-wrapper.tsx
+++ b/components/component-wrapper.tsx
@@ -15,7 +15,7 @@ const ComponentWrapper = ({ className, children }: ComponentWrapperProps) => {
<div
className={cn(
`absolute inset-0 size-full`,
- `bg-[radial-gradient(#00000055_1px,transparent_1px)] dark:bg-[radial-gradient(#ffffff22_1px,transparent_1px)]`,
+ `bg-[radial-gradient(#00000022_1px,transparent_1px)] dark:bg-[radial-gradient(#ffffff22_1px,transparent_1px)]`,
"lab-bg pointer-events-none [background-size:16px_16px]",
)}
/>
diff --git a/config/docs.ts b/config/docs.ts
index 9b0b9077..7868b29e 100644
--- a/config/docs.ts
+++ b/config/docs.ts
@@ -313,6 +313,12 @@ export const docsConfig: DocsConfig = {
{
title: "Text Animations",
items: [
+ {
+ title: "Text Animate",
+ href: `/docs/components/text-animate`,
+ items: [],
+ label: "New",
+ },
{
title: "Number Ticker",
href: `/docs/components/number-ticker`,
@@ -389,12 +395,6 @@ export const docsConfig: DocsConfig = {
items: [],
label: "",
},
- {
- title: "Word Fade In",
- href: `/docs/components/word-fade-in`,
- items: [],
- label: "",
- },
{
title: "Fade Text",
href: `/docs/components/fade-text`,
diff --git a/content/docs/components/text-animate.mdx b/content/docs/components/text-animate.mdx
new file mode 100644
index 00000000..85fef5fc
--- /dev/null
+++ b/content/docs/components/text-animate.mdx
@@ -0,0 +1,73 @@
+---
+title: Text Animate
+date: 2025-01-01
+description: A text animation component that animates text using a variety of different animations.
+author: dillionverma
+published: true
+---
+
+<ComponentPreview name="text-animate-demo" />
+
+## Installation
+
+<Tabs defaultValue="cli">
+
+<TabsList>
+ <TabsTrigger value="cli">CLI</TabsTrigger>
+ <TabsTrigger value="manual">Manual</TabsTrigger>
+</TabsList>
+<TabsContent value="cli">
+
+```bash
+npx shadcn@latest add "https://magicui.design/r/text-animate"
+```
+
+</TabsContent>
+
+<TabsContent value="manual">
+
+<Steps>
+
+<Step>Copy and paste the following code into your project.</Step>
+
+<ComponentSource name="text-animate" />
+
+<Step>Update the import paths to match your project setup.</Step>
+
+</Steps>
+
+</TabsContent>
+
+</Tabs>
+
+## Examples
+
+### Blur In by Text
+
+<ComponentPreview name="text-animate-demo-2" />
+
+### Slide Up by Word
+
+<ComponentPreview name="text-animate-demo-3" />
+
+### Scale Up by Text
+
+<ComponentPreview name="text-animate-demo-4" />
+
+### Fade In by Line
+
+<ComponentPreview name="text-animate-demo-5" />
+
+## Props
+
+| Prop | Type | Description | Default |
+| ----------- | ----------------------------------------- | --------------------------------------------------------- | -------- |
+| children | string | The text content to animate | |
+| className | string | The class name to be applied to the component | |
+| delay | number | Delay before animation starts | 0 |
+| duration | number | Duration of the animation | 0.6 |
+| variants | Variants | Custom motion variants for the animation | |
+| as | ElementType | The element type to render | "div" |
+| by | "text" \| "word" \| "character" \| "line" | How to split the text ("text", "word", "character") | "word" |
+| startOnView | boolean | Whether to start animation when component enters viewport | false |
+| animation | AnimationVariant | The animation preset to use | "fadeIn" |
diff --git a/content/docs/components/word-fade-in.mdx b/content/docs/components/word-fade-in.mdx
deleted file mode 100644
index e33b25f2..00000000
--- a/content/docs/components/word-fade-in.mdx
+++ /dev/null
@@ -1,50 +0,0 @@
----
-title: Word Fade In
-date: 2024-05-24
-description: Text animation which fades in word by word
-author: shrix1
-published: true
----
-
-<ComponentPreview name="word-fade-in-demo" />
-
-## Installation
-
-<Tabs defaultValue="cli">
-
-<TabsList>
- <TabsTrigger value="cli">CLI</TabsTrigger>
- <TabsTrigger value="manual">Manual</TabsTrigger>
-</TabsList>
-<TabsContent value="cli">
-
-```bash
-npx shadcn@latest add "https://magicui.design/r/word-fade-in"
-```
-
-</TabsContent>
-
-<TabsContent value="manual">
-
-<Steps>
-
-<Step>Copy and paste the following code into your project.</Step>
-
-<ComponentSource name="word-fade-in" />
-
-<Step>Update the import paths to match your project setup.</Step>
-
-</Steps>
-
-</TabsContent>
-
-</Tabs>
-
-## Props
-
-| Prop | Type | Description | Default |
-| --------- | -------- | --------------------------------------------- | -------------- |
-| className | string | The class name to be applied to the component | |
-| delay | number | The Delay between each word animation | 0.15 |
-| words | string | A Text that is animated word by word | "word fade in" |
-| variants | Variants | An object containing motion animation props | {} |
diff --git a/public/r/index.json b/public/r/index.json
index 75133afa..423e75f2 100644
--- a/public/r/index.json
+++ b/public/r/index.json
@@ -749,14 +749,14 @@
]
},
{
- "name": "word-fade-in",
+ "name": "text-animate",
"type": "registry:ui",
"dependencies": [
"motion"
],
"files": [
{
- "path": "magicui/word-fade-in.tsx",
+ "path": "magicui/text-animate.tsx",
"type": "registry:ui"
}
]
diff --git a/public/r/styles/default/scratch-to-reveal.json b/public/r/styles/default/scratch-to-reveal.json
index b7a2a764..9eda8484 100644
--- a/public/r/styles/default/scratch-to-reveal.json
+++ b/public/r/styles/default/scratch-to-reveal.json
@@ -1,7 +1,9 @@
{
"name": "scratch-to-reveal",
"type": "registry:ui",
- "dependencies": ["motion"],
+ "dependencies": [
+ "motion"
+ ],
"files": [
{
"path": "magicui/scratch-to-reveal.tsx",
@@ -10,4 +12,4 @@
"target": ""
}
]
-}
+}
\ No newline at end of file
diff --git a/public/r/styles/default/text-animate.json b/public/r/styles/default/text-animate.json
new file mode 100644
index 00000000..61fab3cb
--- /dev/null
+++ b/public/r/styles/default/text-animate.json
@@ -0,0 +1,15 @@
+{
+ "name": "text-animate",
+ "type": "registry:ui",
+ "dependencies": [
+ "motion"
+ ],
+ "files": [
+ {
+ "path": "magicui/text-animate.tsx",
+ "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n AnimatePresence,\n motion,\n MotionProps,\n useInView,\n Variants,\n} from \"motion/react\";\nimport { ElementType, useRef } from \"react\";\n\ntype AnimationType = \"text\" | \"word\" | \"character\" | \"line\";\ntype AnimationVariant =\n | \"fadeIn\"\n | \"fadeUp\"\n | \"fadeDown\"\n | \"fadeLeft\"\n | \"fadeRight\"\n | \"blurIn\"\n | \"blurFade\"\n | \"slideUp\"\n | \"slideDown\"\n | \"slideLeft\"\n | \"slideRight\"\n | \"scaleUp\"\n | \"scaleDown\";\n\ninterface TextAnimateProps extends MotionProps {\n children: string;\n className?: string;\n delay?: number;\n duration?: number;\n variants?: Variants;\n as?: ElementType;\n by?: AnimationType;\n startOnView?: boolean;\n animation?: AnimationVariant;\n}\n\nconst staggerTimings: Record<AnimationType, number> = {\n text: 0.06,\n word: 0.05,\n character: 0.03,\n line: 0.02,\n};\n\nconst defaultContainerVariants = {\n hidden: { opacity: 1 },\n show: {\n opacity: 1,\n transition: {\n staggerChildren: 0.05,\n },\n },\n exit: {\n opacity: 0,\n transition: {\n staggerChildren: 0.05,\n staggerDirection: -1,\n },\n },\n};\n\nconst defaultItemVariants: Variants = {\n hidden: { opacity: 0 },\n show: {\n opacity: 1,\n },\n exit: {\n opacity: 0,\n },\n};\n\nconst defaultItemAnimationVariants: Record<\n AnimationVariant,\n { container: Variants; item: Variants }\n> = {\n fadeIn: {\n container: defaultContainerVariants,\n item: {\n hidden: { opacity: 0, y: 20 },\n show: (delay: number) => ({\n opacity: 1,\n y: 0,\n transition: {\n delay,\n duration: 0.3,\n },\n }),\n exit: {\n opacity: 0,\n y: 20,\n transition: { duration: 0.3 },\n },\n },\n },\n fadeUp: {\n container: defaultContainerVariants,\n item: {\n hidden: { opacity: 0, y: -20 },\n show: (delay: number) => ({\n opacity: 1,\n y: 0,\n transition: {\n delay,\n duration: 0.3,\n },\n }),\n },\n },\n fadeDown: {\n container: defaultContainerVariants,\n item: {\n hidden: { opacity: 0, y: 20 },\n show: (delay: number) => ({\n opacity: 1,\n y: 0,\n transition: {\n delay,\n duration: 0.3,\n },\n }),\n },\n },\n fadeLeft: {\n container: defaultContainerVariants,\n item: {\n hidden: { opacity: 0, x: -20 },\n show: (delay: number) => ({\n opacity: 1,\n x: 0,\n transition: {\n delay,\n duration: 0.3,\n },\n }),\n exit: {\n opacity: 0,\n x: -20,\n transition: { duration: 0.3 },\n },\n },\n },\n fadeRight: {\n container: defaultContainerVariants,\n item: {\n hidden: { opacity: 0, x: 20 },\n show: (delay: number) => ({\n opacity: 1,\n x: 0,\n transition: {\n delay,\n duration: 0.3,\n },\n }),\n exit: {\n opacity: 0,\n x: 20,\n transition: { duration: 0.3 },\n },\n },\n },\n blurIn: {\n container: defaultContainerVariants,\n item: {\n hidden: { opacity: 0, filter: \"blur(10px)\" },\n show: (i: number) => ({\n opacity: 1,\n filter: \"blur(0px)\",\n transition: {\n delay: i * 0.1,\n duration: 0.3,\n },\n }),\n exit: {\n opacity: 0,\n filter: \"blur(10px)\",\n transition: { duration: 0.3 },\n },\n },\n },\n blurFade: {\n container: defaultContainerVariants,\n item: {\n hidden: { opacity: 0, filter: \"blur(10px)\", y: 20 },\n show: (delay: number) => ({\n opacity: 1,\n filter: \"blur(0px)\",\n y: 0,\n transition: {\n y: { duration: 0.3 },\n opacity: { duration: 0.4 },\n filter: { duration: 0.3 },\n },\n }),\n exit: {\n opacity: 0,\n filter: \"blur(10px)\",\n y: 20,\n transition: {\n y: { duration: 0.3 },\n opacity: { duration: 0.4 },\n filter: { duration: 0.3 },\n },\n },\n },\n },\n slideUp: {\n container: defaultContainerVariants,\n item: {\n hidden: { y: 20, opacity: 0 },\n show: (delay: number) => ({\n y: 0,\n opacity: 1,\n transition: {\n delay,\n duration: 0.3,\n },\n }),\n exit: {\n y: -20,\n opacity: 0,\n transition: { duration: 0.3 },\n },\n },\n },\n slideDown: {\n container: defaultContainerVariants,\n item: {\n hidden: { y: -20, opacity: 0 },\n show: {\n y: 0,\n opacity: 1,\n transition: { duration: 0.3 },\n },\n exit: {\n y: 20,\n opacity: 0,\n transition: { duration: 0.3 },\n },\n },\n },\n slideLeft: {\n container: defaultContainerVariants,\n item: {\n hidden: { x: 20, opacity: 0 },\n show: {\n x: 0,\n opacity: 1,\n transition: { duration: 0.3 },\n },\n exit: {\n x: -20,\n opacity: 0,\n transition: { duration: 0.3 },\n },\n },\n },\n slideRight: {\n container: defaultContainerVariants,\n item: {\n hidden: { x: -20, opacity: 0 },\n show: {\n x: 0,\n opacity: 1,\n transition: { duration: 0.3 },\n },\n exit: {\n x: 20,\n opacity: 0,\n transition: { duration: 0.3 },\n },\n },\n },\n scaleUp: {\n container: defaultContainerVariants,\n item: {\n hidden: { scale: 0.5, opacity: 0 },\n show: {\n scale: 1,\n opacity: 1,\n transition: {\n duration: 0.3,\n scale: {\n type: \"spring\",\n damping: 15,\n stiffness: 300,\n },\n },\n },\n exit: {\n scale: 0.5,\n opacity: 0,\n transition: { duration: 0.3 },\n },\n },\n },\n scaleDown: {\n container: defaultContainerVariants,\n item: {\n hidden: { scale: 1.5, opacity: 0 },\n show: (delay: number) => ({\n scale: 1,\n opacity: 1,\n transition: {\n delay,\n duration: 0.3,\n scale: {\n type: \"spring\",\n damping: 15,\n stiffness: 300,\n },\n },\n }),\n exit: {\n scale: 1.5,\n opacity: 0,\n transition: { duration: 0.3 },\n },\n },\n },\n};\n\nexport function TextAnimate({\n children,\n delay = 0,\n duration = 0.3,\n variants,\n className,\n as: Component = \"div\",\n startOnView = false,\n by = \"word\",\n animation = \"fadeIn\",\n ...props\n}: TextAnimateProps) {\n const MotionComponent = motion.create(Component);\n\n // Use provided variants or default variants based on animation type\n const finalVariants = animation\n ? {\n container: {\n ...defaultItemAnimationVariants[animation].container,\n show: {\n ...defaultItemAnimationVariants[animation].container.show,\n transition: {\n staggerChildren: staggerTimings[by],\n },\n },\n exit: {\n ...defaultItemAnimationVariants[animation].container.exit,\n transition: {\n staggerChildren: staggerTimings[by],\n staggerDirection: -1,\n },\n },\n },\n item: defaultItemAnimationVariants[animation].item,\n }\n : { container: defaultContainerVariants, item: defaultItemVariants };\n\n const ref = useRef<HTMLElement>(null);\n const inView = useInView(ref, { once: true });\n const shouldAnimate = startOnView ? inView : true;\n\n let segments: string[] = [];\n switch (by) {\n case \"word\":\n segments = children.split(/(\\s+)/);\n break;\n case \"character\":\n segments = children.split(\"\");\n break;\n case \"line\":\n segments = children.split(\"\\n\");\n break;\n case \"text\":\n default:\n segments = [children];\n break;\n }\n\n return (\n <AnimatePresence mode=\"popLayout\">\n {shouldAnimate && (\n <MotionComponent\n ref={ref}\n variants={finalVariants.container}\n initial=\"hidden\"\n animate=\"show\"\n exit=\"exit\"\n className={cn(\"whitespace-pre-wrap\", className)}\n {...props}\n >\n {segments.map((segment, i) => (\n <motion.span\n key={`${by}-${segment}-${i}`}\n variants={finalVariants.item}\n custom={i * staggerTimings[by]}\n className=\"inline-block whitespace-pre\"\n >\n {segment}\n </motion.span>\n ))}\n </MotionComponent>\n )}\n </AnimatePresence>\n );\n}\n",
+ "type": "registry:ui",
+ "target": ""
+ }
+ ]
+}
\ No newline at end of file
diff --git a/public/r/styles/default/word-fade-in.json b/public/r/styles/default/word-fade-in.json
deleted file mode 100644
index 2600d5be..00000000
--- a/public/r/styles/default/word-fade-in.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "name": "word-fade-in",
- "type": "registry:ui",
- "dependencies": [
- "motion"
- ],
- "files": [
- {
- "path": "magicui/word-fade-in.tsx",
- "content": "\"use client\";\n\nimport { motion, Variants } from \"motion/react\";\n\nimport { cn } from \"@/lib/utils\";\n\ninterface WordFadeInProps {\n words: string;\n className?: string;\n delay?: number;\n variants?: Variants;\n}\n\nexport default function WordFadeIn({\n words,\n delay = 0.15,\n variants = {\n hidden: { opacity: 0 },\n visible: (i: number) => ({\n y: 0,\n opacity: 1,\n transition: { delay: i * delay },\n }),\n },\n className,\n}: WordFadeInProps) {\n const _words = words.split(\" \");\n\n return (\n <motion.h1\n variants={variants}\n initial=\"hidden\"\n animate=\"visible\"\n className={cn(\n \"font-display text-center text-4xl font-bold tracking-[-0.02em] text-black drop-shadow-sm dark:text-white md:text-7xl md:leading-[5rem]\",\n className,\n )}\n >\n {_words.map((word, i) => (\n <motion.span key={word} variants={variants} custom={i}>\n {word}{\" \"}\n </motion.span>\n ))}\n </motion.h1>\n );\n}\n",
- "type": "registry:ui",
- "target": ""
- }
- ]
-}
\ No newline at end of file
diff --git a/registry/default/example/text-animate-demo-2.tsx b/registry/default/example/text-animate-demo-2.tsx
new file mode 100644
index 00000000..b014075b
--- /dev/null
+++ b/registry/default/example/text-animate-demo-2.tsx
@@ -0,0 +1,9 @@
+import { TextAnimate } from "@/registry/default/magicui/text-animate";
+
+export default function TextAnimateDemo2() {
+ return (
+ <TextAnimate animation="blurIn" as="h1">
+ Blur in text
+ </TextAnimate>
+ );
+}
diff --git a/registry/default/example/text-animate-demo-3.tsx b/registry/default/example/text-animate-demo-3.tsx
new file mode 100644
index 00000000..91e83384
--- /dev/null
+++ b/registry/default/example/text-animate-demo-3.tsx
@@ -0,0 +1,9 @@
+import { TextAnimate } from "@/registry/default/magicui/text-animate";
+
+export default function TextAnimateDemo3() {
+ return (
+ <TextAnimate animation="slideUp" by="word">
+ Slide up by word
+ </TextAnimate>
+ );
+}
diff --git a/registry/default/example/text-animate-demo-4.tsx b/registry/default/example/text-animate-demo-4.tsx
new file mode 100644
index 00000000..fde4dc24
--- /dev/null
+++ b/registry/default/example/text-animate-demo-4.tsx
@@ -0,0 +1,9 @@
+import { TextAnimate } from "@/registry/default/magicui/text-animate";
+
+export default function TextAnimateDemo4() {
+ return (
+ <TextAnimate animation="scaleUp" by="text">
+ Scale up by text
+ </TextAnimate>
+ );
+}
diff --git a/registry/default/example/text-animate-demo-5.tsx b/registry/default/example/text-animate-demo-5.tsx
new file mode 100644
index 00000000..62ab155a
--- /dev/null
+++ b/registry/default/example/text-animate-demo-5.tsx
@@ -0,0 +1,9 @@
+import { TextAnimate } from "@/registry/default/magicui/text-animate";
+
+export default function TextAnimateDemo5() {
+ return (
+ <TextAnimate animation="fadeIn" by="line" as="p">
+ {`Fade in by line as paragraph\n\nFade in by line as paragraph\n\nFade in by line as paragraph`}
+ </TextAnimate>
+ );
+}
diff --git a/registry/default/example/text-animate-demo.tsx b/registry/default/example/text-animate-demo.tsx
new file mode 100644
index 00000000..61feb605
--- /dev/null
+++ b/registry/default/example/text-animate-demo.tsx
@@ -0,0 +1,9 @@
+import { TextAnimate } from "@/registry/default/magicui/text-animate";
+
+export default function TextAnimateDemo() {
+ return (
+ <TextAnimate animation="blurInUp" by="character">
+ Blur in by character
+ </TextAnimate>
+ );
+}
diff --git a/registry/default/example/word-fade-in-demo.tsx b/registry/default/example/word-fade-in-demo.tsx
deleted file mode 100644
index c2ec713a..00000000
--- a/registry/default/example/word-fade-in-demo.tsx
+++ /dev/null
@@ -1,5 +0,0 @@
-import WordFadeIn from "@/registry/default/magicui/word-fade-in";
-
-export default function WordFadeInDemo() {
- return <WordFadeIn words="Word Fade In" />;
-}
diff --git a/registry/default/magicui/text-animate.tsx b/registry/default/magicui/text-animate.tsx
new file mode 100644
index 00000000..4fe17cd4
--- /dev/null
+++ b/registry/default/magicui/text-animate.tsx
@@ -0,0 +1,386 @@
+"use client";
+
+import { cn } from "@/lib/utils";
+import { AnimatePresence, motion, MotionProps, Variants } from "motion/react";
+import { ElementType } from "react";
+
+type AnimationType = "text" | "word" | "character" | "line";
+type AnimationVariant =
+ | "fadeIn"
+ | "blurIn"
+ | "blurInUp"
+ | "blurInDown"
+ | "slideUp"
+ | "slideDown"
+ | "slideLeft"
+ | "slideRight"
+ | "scaleUp"
+ | "scaleDown";
+
+interface TextAnimateProps extends MotionProps {
+ /**
+ * The text content to animate
+ */
+ children: string;
+ /**
+ * The class name to be applied to the component
+ */
+ className?: string;
+ /**
+ * The class name to be applied to each segment
+ */
+ segmentClassName?: string;
+ /**
+ * The delay before the animation starts
+ */
+ delay?: number;
+ /**
+ * The duration of the animation
+ */
+ duration?: number;
+ /**
+ * Custom motion variants for the animation
+ */
+ variants?: Variants;
+ /**
+ * The element type to render
+ */
+ as?: ElementType;
+ /**
+ * How to split the text ("text", "word", "character")
+ */
+ by?: AnimationType;
+ /**
+ * Whether to start animation when component enters viewport
+ */
+ startOnView?: boolean;
+ /**
+ * Whether to animate only once
+ */
+ once?: boolean;
+ /**
+ * The animation preset to use
+ */
+ animation?: AnimationVariant;
+}
+
+const staggerTimings: Record<AnimationType, number> = {
+ text: 0.06,
+ word: 0.05,
+ character: 0.03,
+ line: 0.06,
+};
+
+const defaultContainerVariants = {
+ hidden: { opacity: 1 },
+ show: {
+ opacity: 1,
+ transition: {
+ staggerChildren: 0.05,
+ },
+ },
+ exit: {
+ opacity: 0,
+ transition: {
+ staggerChildren: 0.05,
+ staggerDirection: -1,
+ },
+ },
+};
+
+const defaultItemVariants: Variants = {
+ hidden: { opacity: 0 },
+ show: {
+ opacity: 1,
+ },
+ exit: {
+ opacity: 0,
+ },
+};
+
+const defaultItemAnimationVariants: Record<
+ AnimationVariant,
+ { container: Variants; item: Variants }
+> = {
+ fadeIn: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { opacity: 0, y: 20 },
+ show: (delay: number) => ({
+ opacity: 1,
+ y: 0,
+ transition: {
+ delay,
+ duration: 0.3,
+ },
+ }),
+ exit: {
+ opacity: 0,
+ y: 20,
+ transition: { duration: 0.3 },
+ },
+ },
+ },
+ blurIn: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { opacity: 0, filter: "blur(10px)" },
+ show: (i: number) => ({
+ opacity: 1,
+ filter: "blur(0px)",
+ transition: {
+ delay: i * 0.1,
+ duration: 0.3,
+ },
+ }),
+ exit: {
+ opacity: 0,
+ filter: "blur(10px)",
+ transition: { duration: 0.3 },
+ },
+ },
+ },
+ blurInUp: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { opacity: 0, filter: "blur(10px)", y: 20 },
+ show: (delay: number) => ({
+ opacity: 1,
+ filter: "blur(0px)",
+ y: 0,
+ transition: {
+ y: { duration: 0.3 },
+ opacity: { duration: 0.4 },
+ filter: { duration: 0.3 },
+ },
+ }),
+ exit: {
+ opacity: 0,
+ filter: "blur(10px)",
+ y: 20,
+ transition: {
+ y: { duration: 0.3 },
+ opacity: { duration: 0.4 },
+ filter: { duration: 0.3 },
+ },
+ },
+ },
+ },
+ blurInDown: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { opacity: 0, filter: "blur(10px)", y: -20 },
+ show: (delay: number) => ({
+ opacity: 1,
+ filter: "blur(0px)",
+ y: 0,
+ transition: {
+ y: { duration: 0.3 },
+ opacity: { duration: 0.4 },
+ filter: { duration: 0.3 },
+ },
+ }),
+ },
+ },
+ slideUp: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { y: 20, opacity: 0 },
+ show: (delay: number) => ({
+ y: 0,
+ opacity: 1,
+ transition: {
+ delay,
+ duration: 0.3,
+ },
+ }),
+ exit: {
+ y: -20,
+ opacity: 0,
+ transition: {
+ duration: 0.3,
+ },
+ },
+ },
+ },
+ slideDown: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { y: -20, opacity: 0 },
+ show: {
+ y: 0,
+ opacity: 1,
+ transition: { duration: 0.3 },
+ },
+ exit: {
+ y: 20,
+ opacity: 0,
+ transition: { duration: 0.3 },
+ },
+ },
+ },
+ slideLeft: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { x: 20, opacity: 0 },
+ show: {
+ x: 0,
+ opacity: 1,
+ transition: { duration: 0.3 },
+ },
+ exit: {
+ x: -20,
+ opacity: 0,
+ transition: { duration: 0.3 },
+ },
+ },
+ },
+ slideRight: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { x: -20, opacity: 0 },
+ show: {
+ x: 0,
+ opacity: 1,
+ transition: { duration: 0.3 },
+ },
+ exit: {
+ x: 20,
+ opacity: 0,
+ transition: { duration: 0.3 },
+ },
+ },
+ },
+ scaleUp: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { scale: 0.5, opacity: 0 },
+ show: {
+ scale: 1,
+ opacity: 1,
+ transition: {
+ duration: 0.3,
+ scale: {
+ type: "spring",
+ damping: 15,
+ stiffness: 300,
+ },
+ },
+ },
+ exit: {
+ scale: 0.5,
+ opacity: 0,
+ transition: { duration: 0.3 },
+ },
+ },
+ },
+ scaleDown: {
+ container: defaultContainerVariants,
+ item: {
+ hidden: { scale: 1.5, opacity: 0 },
+ show: (delay: number) => ({
+ scale: 1,
+ opacity: 1,
+ transition: {
+ delay,
+ duration: 0.3,
+ scale: {
+ type: "spring",
+ damping: 15,
+ stiffness: 300,
+ },
+ },
+ }),
+ exit: {
+ scale: 1.5,
+ opacity: 0,
+ transition: { duration: 0.3 },
+ },
+ },
+ },
+};
+
+export function TextAnimate({
+ children,
+ delay = 0,
+ duration = 0.3,
+ variants,
+ className,
+ segmentClassName,
+ as: Component = "p",
+ startOnView = true,
+ once = false,
+ by = "word",
+ animation = "fadeIn",
+ ...props
+}: TextAnimateProps) {
+ const MotionComponent = motion.create(Component);
+
+ // Use provided variants or default variants based on animation type
+ const finalVariants = animation
+ ? {
+ container: {
+ ...defaultItemAnimationVariants[animation].container,
+ show: {
+ ...defaultItemAnimationVariants[animation].container.show,
+ transition: {
+ staggerChildren: staggerTimings[by],
+ },
+ },
+ exit: {
+ ...defaultItemAnimationVariants[animation].container.exit,
+ transition: {
+ staggerChildren: staggerTimings[by],
+ staggerDirection: -1,
+ },
+ },
+ },
+ item: defaultItemAnimationVariants[animation].item,
+ }
+ : { container: defaultContainerVariants, item: defaultItemVariants };
+
+ let segments: string[] = [];
+ switch (by) {
+ case "word":
+ segments = children.split(/(\s+)/);
+ break;
+ case "character":
+ segments = children.split("");
+ break;
+ case "line":
+ segments = children.split("\n");
+ break;
+ case "text":
+ default:
+ segments = [children];
+ break;
+ }
+
+ return (
+ <AnimatePresence mode="popLayout">
+ <MotionComponent
+ variants={finalVariants.container}
+ initial="hidden"
+ whileInView={startOnView ? "show" : undefined}
+ animate={startOnView ? undefined : "show"}
+ exit="exit"
+ className={cn("whitespace-pre-wrap", className)}
+ {...props}
+ >
+ {segments.map((segment, i) => (
+ <motion.span
+ key={`${by}-${segment}-${i}`}
+ variants={finalVariants.item}
+ custom={i * staggerTimings[by]}
+ className={cn(
+ by === "line" ? "block" : "inline-block whitespace-pre",
+ segmentClassName,
+ )}
+ >
+ {segment}
+ </motion.span>
+ ))}
+ </MotionComponent>
+ </AnimatePresence>
+ );
+}
diff --git a/registry/default/magicui/word-fade-in.tsx b/registry/default/magicui/word-fade-in.tsx
deleted file mode 100644
index 9364f599..00000000
--- a/registry/default/magicui/word-fade-in.tsx
+++ /dev/null
@@ -1,46 +0,0 @@
-"use client";
-
-import { motion, Variants } from "motion/react";
-
-import { cn } from "@/lib/utils";
-
-interface WordFadeInProps {
- words: string;
- className?: string;
- delay?: number;
- variants?: Variants;
-}
-
-export default function WordFadeIn({
- words,
- delay = 0.15,
- variants = {
- hidden: { opacity: 0 },
- visible: (i: number) => ({
- y: 0,
- opacity: 1,
- transition: { delay: i * delay },
- }),
- },
- className,
-}: WordFadeInProps) {
- const _words = words.split(" ");
-
- return (
- <motion.h1
- variants={variants}
- initial="hidden"
- animate="visible"
- className={cn(
- "font-display text-center text-4xl font-bold tracking-[-0.02em] text-black drop-shadow-sm dark:text-white md:text-7xl md:leading-[5rem]",
- className,
- )}
- >
- {_words.map((word, i) => (
- <motion.span key={word} variants={variants} custom={i}>
- {word}{" "}
- </motion.span>
- ))}
- </motion.h1>
- );
-}
diff --git a/registry/registry-examples.ts b/registry/registry-examples.ts
index cb291e1b..022de88a 100644
--- a/registry/registry-examples.ts
+++ b/registry/registry-examples.ts
@@ -396,9 +396,29 @@ export const examples: Registry = [
files: ["example/gradual-spacing-demo.tsx"],
},
{
- name: "word-fade-in-demo",
+ name: "text-animate-demo",
type: "registry:example",
- files: ["example/word-fade-in-demo.tsx"],
+ files: ["example/text-animate-demo.tsx"],
+ },
+ {
+ name: "text-animate-demo-2",
+ type: "registry:example",
+ files: ["example/text-animate-demo-2.tsx"],
+ },
+ {
+ name: "text-animate-demo-3",
+ type: "registry:example",
+ files: ["example/text-animate-demo-3.tsx"],
+ },
+ {
+ name: "text-animate-demo-4",
+ type: "registry:example",
+ files: ["example/text-animate-demo-4.tsx"],
+ },
+ {
+ name: "text-animate-demo-5",
+ type: "registry:example",
+ files: ["example/text-animate-demo-5.tsx"],
},
{
name: "fade-text-demo",
diff --git a/registry/registry-ui.ts b/registry/registry-ui.ts
index 81607055..d0aa5d67 100644
--- a/registry/registry-ui.ts
+++ b/registry/registry-ui.ts
@@ -465,10 +465,10 @@ export const ui: Registry = [
files: ["magicui/gradual-spacing.tsx"],
},
{
- name: "word-fade-in",
+ name: "text-animate",
type: "registry:ui",
dependencies: ["motion"],
- files: ["magicui/word-fade-in.tsx"],
+ files: ["magicui/text-animate.tsx"],
},
{
name: "scroll-based-velocity",