UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
fix: update typing animation component (#449)
Dillion Vermafd9224df1276
diff --git a/content/docs/components/typing-animation.mdx b/content/docs/components/typing-animation.mdxindex 360f418f..90e731b9 100644--- a/content/docs/components/typing-animation.mdx+++ b/content/docs/components/typing-animation.mdx@@ -42,8 +42,11 @@ npx shadcn@latest add "https://magicui.design/r/typing-animation"## 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 | "" |+| Prop | Type | Description | Default |+| ----------- | ----------------- | --------------------------------------------- | ------- |+| children | string | Text content to animate | |+| className | string | The class name to be applied to the component | |+| duration | number | Duration to wait in between each char type | 100 |+| delay | number | Delay before animation starts (in ms) | 0 |+| as | React.ElementType | Component to render as | "div" |+| startOnView | boolean | Start animation when component is in view | false |diff --git a/package.json b/package.jsonindex fde85841..8e16a1cb 100644--- a/package.json+++ b/package.json@@ -43,7 +43,6 @@"@radix-ui/react-toast": "^1.2.1","@radix-ui/react-tooltip": "^1.1.2","@t3-oss/env-nextjs": "^0.7.1",- "concurrently": "^9.0.1","@types/lodash.template": "^4.5.3","autoprefixer": "10.4.14","canvas-confetti": "^1.9.3",@@ -51,6 +50,7 @@"clsx": "^2.1.1","cmdk": "^1.0.0","cobe": "^0.6.3",+ "concurrently": "^9.0.1","crisp-sdk-web": "^1.0.25","critters": "^0.0.24","date-fns": "^2.30.0",@@ -60,6 +60,7 @@"jotai": "^2.10.0","lodash.template": "^4.5.0","lucide-react": "^0.401.0",+ "motion": "^11.15.0","next": "14.2.13","next-themes": "^0.3.0","octokit": "^4.0.2",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 74bc0e13..2e7bffff 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -116,6 +116,9 @@ importers:lucide-react:specifier: ^0.401.0version: 0.401.0([email protected])+ motion:+ specifier: ^11.15.0+ version: 11.15.0(@emotion/[email protected])([email protected]([email protected]))([email protected])next:specifier: 14.2.13version: 14.2.13(@babel/[email protected])(@opentelemetry/[email protected])([email protected]([email protected]))([email protected])@@ -2917,6 +2920,20 @@ packages:resolution: {integrity: sha512-ZsDfxO51wGAXREY55a7la9LScWpwv9RxIrYABrlvOFBlH/ShPnrtsXeuUIfXKKOVicNxQ+o8JTbJvjS4M89yew==}+ resolution: {integrity: sha512-MLk8IvZntxOMg7lDBLw2qgTHHv664bYoYmnFTmE0Gm/FW67aOJk0WM3ctMcG+Xhcv+vh5uyyXwxvxhSeJzSe+w==}+ peerDependencies:+ '@emotion/is-prop-valid': '*'+ react: ^18.0.0 || ^19.0.0+ react-dom: ^18.0.0 || ^19.0.0+ peerDependenciesMeta:+ '@emotion/is-prop-valid':+ optional: true+ react:+ optional: true+ react-dom:+ optional: true+resolution: {integrity: sha512-Xmxs08WBXnc2tNzNZbFSpquI33lvleJg4Y+hmZ+vFkn+laN9ZnR3gbZnNGKDtuz7c/x3u8dLg05OU3EhLobCsg==}peerDependencies:@@ -3860,6 +3877,26 @@ packages:engines: {node: '>=10'}hasBin: true+ resolution: {integrity: sha512-lW+D2wBy5vxLJi6aCP0xyxTxlTfiu+b+zcpVbGVFUxotwThqhdpPRSmX8xztAgtZMPMeU0WGVn/k1w4I+TbPqA==}++ resolution: {integrity: sha512-Xg+8xnqIJTpr0L/cidfTTBFkvRw26ZtGGuIhA94J9PQ2p4mEa06Xx7QVYZH0BP+EpMSaDlu+q0I0mmvwADPsaQ==}++ resolution: {integrity: sha512-iZ7dwADQJWGsqsSkBhNHdI2LyYWU+hA1Nhy357wCLZq1yHxGImgt3l7Yv0HT/WOskcYDq9nxdedyl4zUv7UFFw==}+ peerDependencies:+ '@emotion/is-prop-valid': '*'+ react: ^18.0.0 || ^19.0.0+ react-dom: ^18.0.0 || ^19.0.0+ peerDependenciesMeta:+ '@emotion/is-prop-valid':+ optional: true+ react:+ optional: true+ react-dom:+ optional: true+resolution: {integrity: sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==}engines: {node: '>=4'}@@ -7794,7 +7831,7 @@ snapshots:eslint: 8.57.1eslint-import-resolver-node: 0.3.9eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]([email protected]))([email protected])- eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])+ eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]([email protected]))([email protected]))([email protected])eslint-plugin-jsx-a11y: 6.8.0([email protected])eslint-plugin-react: 7.33.2([email protected])eslint-plugin-react-hooks: 4.6.0([email protected])@@ -7822,7 +7859,7 @@ snapshots:enhanced-resolve: 5.15.1eslint: 8.57.1eslint-module-utils: 2.8.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]([email protected]))([email protected]))([email protected])- eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])+ eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]([email protected]))([email protected]))([email protected])fast-glob: 3.3.2get-tsconfig: 4.7.5is-core-module: 2.13.1@@ -7844,7 +7881,7 @@ snapshots:transitivePeerDependencies:- supports-color- [email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]):+ [email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]([email protected]))([email protected]))([email protected]):dependencies:array-includes: 3.1.7array.prototype.findlastindex: 1.2.4@@ -8122,6 +8159,16 @@ snapshots:+ [email protected](@emotion/[email protected])([email protected]([email protected]))([email protected]):+ dependencies:+ motion-dom: 11.14.3+ motion-utils: 11.14.3+ tslib: 2.6.2+ optionalDependencies:+ '@emotion/is-prop-valid': 0.8.8+ react: 18.3.1+ react-dom: 18.3.1([email protected])+[email protected](@emotion/[email protected])([email protected]([email protected]))([email protected]):dependencies:tslib: 2.6.2@@ -9464,6 +9511,19 @@ snapshots:+ [email protected]: {}++ [email protected]: {}++ [email protected](@emotion/[email protected])([email protected]([email protected]))([email protected]):+ dependencies:+ framer-motion: 11.15.0(@emotion/[email protected])([email protected]([email protected]))([email protected])+ tslib: 2.6.2+ optionalDependencies:+ '@emotion/is-prop-valid': 0.8.8+ react: 18.3.1+ react-dom: 18.3.1([email protected])+diff --git a/registry/default/example/typing-animation-demo.tsx b/registry/default/example/typing-animation-demo.tsxindex dc41a7b5..dae36ac9 100644--- a/registry/default/example/typing-animation-demo.tsx+++ b/registry/default/example/typing-animation-demo.tsx@@ -1,10 +1,5 @@import TypingAnimation from "@/registry/default/magicui/typing-animation";export default function TypingAnimationDemo() {- return (- <TypingAnimation- className="text-4xl font-bold text-black dark:text-white"- text="Typing Animation"- />- );+ return <TypingAnimation>Typing Animation</TypingAnimation>;}diff --git a/registry/default/magicui/typing-animation.tsx b/registry/default/magicui/typing-animation.tsxindex 637890d6..506813fa 100644--- a/registry/default/magicui/typing-animation.tsx+++ b/registry/default/magicui/typing-animation.tsx@@ -1,28 +1,70 @@"use client";-import { useEffect, useState } from "react";-import { cn } from "@/lib/utils";+import { motion, MotionProps } from "motion/react";+import { useEffect, useRef, useState } from "react";-interface TypingAnimationProps {- text: string;- duration?: number;+interface TypingAnimationProps extends MotionProps {+ children: string;className?: string;+ duration?: number;+ delay?: number;+ as?: React.ElementType;+ startOnView?: boolean;}export default function TypingAnimation({- text,- duration = 200,+ children,className,+ duration = 100,+ delay = 0,+ as: Component = "div",+ startOnView = false,+ ...props}: TypingAnimationProps) {+ const MotionComponent = motion.create(Component, {+ forwardMotionProps: true,+ });+const [displayedText, setDisplayedText] = useState<string>("");- const [i, setI] = useState<number>(0);+ const [started, setStarted] = useState(false);+ const elementRef = useRef<HTMLElement | null>(null);++ useEffect(() => {+ if (!startOnView) {+ const startTimeout = setTimeout(() => {+ setStarted(true);+ }, delay);+ return () => clearTimeout(startTimeout);+ }++ const observer = new IntersectionObserver(+ ([entry]) => {+ if (entry.isIntersecting) {+ setTimeout(() => {+ setStarted(true);+ }, delay);+ observer.disconnect();+ }+ },+ { threshold: 0.1 },+ );++ if (elementRef.current) {+ observer.observe(elementRef.current);+ }++ return () => observer.disconnect();+ }, [delay, startOnView]);useEffect(() => {+ if (!started) return;++ let i = 0;const typingEffect = setInterval(() => {- if (i < text.length) {- setDisplayedText(text.substring(0, i + 1));- setI(i + 1);+ if (i < children.length) {+ setDisplayedText(children.substring(0, i + 1));+ i++;} else {clearInterval(typingEffect);}@@ -31,16 +73,18 @@ export default function TypingAnimation({return () => {clearInterval(typingEffect);};- }, [duration, i]);+ }, [children, duration, started]);return (- <h1+ <MotionComponent+ ref={elementRef}className={cn(- "font-display text-center text-4xl font-bold leading-[5rem] tracking-[-0.02em] drop-shadow-sm",+ "text-4xl font-bold leading-[5rem] tracking-[-0.02em]",className,)}+ {...props}>- {displayedText ? displayedText : text}- </h1>+ {displayedText}+ </MotionComponent>);}diff --git a/registry/registry-ui.ts b/registry/registry-ui.tsindex 5eacbb5e..00ff39dd 100644--- a/registry/registry-ui.ts+++ b/registry/registry-ui.ts@@ -396,6 +396,7 @@ export const ui: Registry = [{name: "typing-animation",type: "registry:ui",+ dependencies: ["motion"],files: ["magicui/typing-animation.tsx"],},{