UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
feat: add typing animation component (#17)
Manu Bhardwaj8175aecd27b9
diff --git a/.gitignore b/.gitignoreindex 93ec1c23..4e9ac84d 100644--- a/.gitignore+++ b/.gitignore@@ -19,6 +19,9 @@.DS_Store*.pem+# IDE Files+.idea+# debugnpm-debug.log*yarn-debug.log*diff --git a/config/docs.ts b/config/docs.tsindex 9c943f99..4f867e64 100644--- a/config/docs.ts+++ b/config/docs.ts@@ -151,6 +151,12 @@ export const docsConfig: DocsConfig = {items: [],label: "New",},+ {+ title: "Typing Animation",+ href: `/docs/components/typing-animation`,+ items: [],+ label: "New",+ },],},{diff --git a/content/docs/components/typing-animation.mdx b/content/docs/components/typing-animation.mdxnew file mode 100644index 00000000..b0fb8658--- /dev/null+++ b/content/docs/components/typing-animation.mdx@@ -0,0 +1,31 @@+---+title: Typing Animation+date: 2024-05-22+description: Characters appearing in typed animation+author: imanubhardwaj+published: true+---++<ComponentPreview name="typing-animation-demo" />++<Steps>++### Installation++Copy and paste the following code into your project.++```text+components/magicui/typing-animation.tsx+```++<ComponentSource name="typing-animation" />++</Steps>++## Props++| Prop | Type | Description | Default |+| ----------- | --------------- | -------------------------------------------------- | ------- |+| className | string | The class name to be applied to the component | |+| duration | number | Duration to wait in between each char type. | 200 |+| text | string | Text to animate | "" |diff --git a/registry/components/example/typing-animation-demo.tsx b/registry/components/example/typing-animation-demo.tsxnew file mode 100644index 00000000..7a22669b--- /dev/null+++ b/registry/components/example/typing-animation-demo.tsx@@ -0,0 +1,10 @@+import TypingAnimation from "@/registry/components/magicui/typing-animation";++export default async function TextRevealDemo() {+ return (+ <TypingAnimation+ className="text-4xl font-bold text-black dark:text-white"+ text="Building Magic"+ />+ );+}diff --git a/registry/components/magicui/typing-animation.tsx b/registry/components/magicui/typing-animation.tsxnew file mode 100644index 00000000..4b8b289f--- /dev/null+++ b/registry/components/magicui/typing-animation.tsx@@ -0,0 +1,45 @@+"use client";++import { cn } from "@/lib/utils";+import { useEffect, useState } from "react";++interface TypingAnimationProps {+ text: string;+ duration?: number;+ className?: string;+}++export default function TypingAnimation({+ text,+ duration = 200,+ className,+}: TypingAnimationProps) {+ const [displayedText, setDisplayedText] = useState<string>("");+ const [i, setI] = useState<number>(0);++ useEffect(() => {+ const typingEffect = setInterval(() => {+ if (i < text.length) {+ setDisplayedText((prevState) => prevState + text.charAt(i));+ setI(i + 1);+ } else {+ clearInterval(typingEffect);+ }+ }, duration);++ return () => {+ clearInterval(typingEffect);+ };+ }, [duration, i]);++ return (+ <h1+ className={cn(+ 'text-center font-display text-4xl font-bold tracking-[-0.02em] drop-shadow-sm md:text-7xl md:leading-[5rem]',+ className,+ )}+ >+ {displayedText ? displayedText : text}+ </h1>+ );+}diff --git a/registry/index.tsx b/registry/index.tsxindex 11087b1f..4dd9fece 100644--- a/registry/index.tsx+++ b/registry/index.tsx@@ -142,6 +142,11 @@ const ui: Registry = {type: "components:ui",files: ["registry/components/magicui/word-rotate.tsx"],},+ "typing-animation": {+ name: "typing-animation",+ type: "components:ui",+ files: ["registry/components/magicui/typing-animation.tsx"],+ },};const example: Registry = {@@ -565,6 +570,14 @@ const example: Registry = {() => import("@/registry/components/example/word-rotate-demo"),),},+ "typing-animation-demo": {+ name: "typing-animation-demo",+ type: "components:example",+ files: ["registry/components/example/typing-animation-demo.tsx"],+ component: React.lazy(+ () => import("@/registry/components/example/typing-animation-demo"),+ ),+ },};export const registry: Registry = {