UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
Add isView prop to AnimatedSpan and TypingAnimation for on-view anima… (#703)
Corede813be3ba3658
diff --git a/public/r/typing-animation-demo.json b/public/r/typing-animation-demo.jsonindex 7e93aee4..708555f8 100644--- a/public/r/typing-animation-demo.json+++ b/public/r/typing-animation-demo.json@@ -10,7 +10,7 @@"files": [{"path": "registry/example/typing-animation-demo.tsx",- "content": "import { TypingAnimation } from \"@/registry/magicui/typing-animation\";\n\nexport default function TypingAnimationDemo() {\n return <TypingAnimation>Typing Animation 👋</TypingAnimation>;\n}\n",+ "content": "import { TypingAnimation } from \"@/registry/magicui/typing-animation\";\n\nexport default function TypingAnimationDemo() {\n return (\n <TypingAnimation startOnView={true}>Typing Animation 👋</TypingAnimation>\n );\n}\n","type": "registry:example","target": "components/typing-animation-demo.tsx"}diff --git a/public/r/typing-animation.json b/public/r/typing-animation.jsonindex 04d65d81..9cfee0a7 100644--- a/public/r/typing-animation.json+++ b/public/r/typing-animation.json@@ -10,7 +10,7 @@"files": [{"path": "registry/magicui/typing-animation.tsx",- "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport { motion, MotionProps } from \"motion/react\";\nimport { useEffect, useRef, useState } from \"react\";\n\ninterface TypingAnimationProps extends MotionProps {\n children: string;\n className?: string;\n duration?: number;\n delay?: number;\n as?: React.ElementType;\n startOnView?: boolean;\n}\n\nexport function TypingAnimation({\n children,\n className,\n duration = 100,\n delay = 0,\n as: Component = \"div\",\n startOnView = false,\n ...props\n}: TypingAnimationProps) {\n const MotionComponent = motion.create(Component, {\n forwardMotionProps: true,\n });\n\n const [displayedText, setDisplayedText] = useState<string>(\"\");\n const [started, setStarted] = useState(false);\n const elementRef = useRef<HTMLElement | null>(null);\n\n useEffect(() => {\n if (!startOnView) {\n const startTimeout = setTimeout(() => {\n setStarted(true);\n }, delay);\n return () => clearTimeout(startTimeout);\n }\n\n const observer = new IntersectionObserver(\n ([entry]) => {\n if (entry.isIntersecting) {\n setTimeout(() => {\n setStarted(true);\n }, delay);\n observer.disconnect();\n }\n },\n { threshold: 0.1 },\n );\n\n if (elementRef.current) {\n observer.observe(elementRef.current);\n }\n\n return () => observer.disconnect();\n }, [delay, startOnView]);\n\n useEffect(() => {\n if (!started) return;\n\n const graphemes = Array.from(children);\n let i = 0;\n const typingEffect = setInterval(() => {\n if (i < graphemes.length) {\n setDisplayedText(graphemes.slice(0, i + 1).join(\"\"));\n i++;\n } else {\n clearInterval(typingEffect);\n }\n }, duration);\n\n return () => {\n clearInterval(typingEffect);\n };\n }, [children, duration, started]);\n\n return (\n <MotionComponent\n ref={elementRef}\n className={cn(\n \"text-4xl font-bold leading-[5rem] tracking-[-0.02em]\",\n className,\n )}\n {...props}\n >\n {displayedText}\n </MotionComponent>\n );\n}\n",+ "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport { motion, MotionProps, useInView } from \"motion/react\";\nimport { useEffect, useRef, useState } from \"react\";\n\ninterface TypingAnimationProps extends MotionProps {\n children: string;\n className?: string;\n duration?: number;\n delay?: number;\n as?: React.ElementType;\n startOnView?: boolean;\n}\n\nexport function TypingAnimation({\n children,\n className,\n duration = 100,\n delay = 0,\n as: Component = \"div\",\n startOnView = false,\n ...props\n}: TypingAnimationProps) {\n const MotionComponent = motion.create(Component, {\n forwardMotionProps: true,\n });\n\n const [displayedText, setDisplayedText] = useState<string>(\"\");\n const [started, setStarted] = useState(false);\n const elementRef = useRef<HTMLElement | null>(null);\n const isInView = useInView(elementRef as React.RefObject<Element>, {\n amount: 0.3,\n once: true,\n });\n\n useEffect(() => {\n if (!startOnView) {\n const startTimeout = setTimeout(() => {\n setStarted(true);\n }, delay);\n return () => clearTimeout(startTimeout);\n }\n\n if (!isInView) return;\n\n const startTimeout = setTimeout(() => {\n setStarted(true);\n }, delay);\n\n return () => clearTimeout(startTimeout);\n }, [delay, startOnView, isInView]);\n\n useEffect(() => {\n if (!started) return;\n\n const graphemes = Array.from(children);\n let i = 0;\n const typingEffect = setInterval(() => {\n if (i < graphemes.length) {\n setDisplayedText(graphemes.slice(0, i + 1).join(\"\"));\n i++;\n } else {\n clearInterval(typingEffect);\n }\n }, duration);\n\n return () => {\n clearInterval(typingEffect);\n };\n }, [children, duration, started]);\n\n return (\n <MotionComponent\n ref={elementRef}\n className={cn(\n \"text-4xl font-bold leading-[5rem] tracking-[-0.02em]\",\n className,\n )}\n {...props}\n >\n {displayedText}\n </MotionComponent>\n );\n}\n","type": "registry:ui","target": "components/magicui/typing-animation.tsx"}diff --git a/registry/example/typing-animation-demo.tsx b/registry/example/typing-animation-demo.tsxindex 8aa29905..ce8a9d77 100644--- a/registry/example/typing-animation-demo.tsx+++ b/registry/example/typing-animation-demo.tsx@@ -1,5 +1,7 @@import { TypingAnimation } from "@/registry/magicui/typing-animation";export default function TypingAnimationDemo() {- return <TypingAnimation>Typing Animation 👋</TypingAnimation>;+ return (+ <TypingAnimation startOnView={true}>Typing Animation 👋</TypingAnimation>+ );}diff --git a/registry/magicui/terminal.tsx b/registry/magicui/terminal.tsxindex a32083db..dd8b2c64 100644--- a/registry/magicui/terminal.tsx+++ b/registry/magicui/terminal.tsx@@ -8,24 +8,55 @@ interface AnimatedSpanProps extends MotionProps {children: React.ReactNode;delay?: number;className?: string;+ isView?: boolean; // Prop to control animation trigger on visibility}export const AnimatedSpan = ({children,delay = 0,className,+ isView = false, // Default: animation waits for visibility...props-}: AnimatedSpanProps) => (- <motion.div- initial={{ opacity: 0, y: -5 }}- animate={{ opacity: 1, y: 0 }}- transition={{ duration: 0.3, delay: delay / 1000 }}- className={cn("grid text-sm font-normal tracking-tight", className)}- {...props}- >- {children}- </motion.div>-);+}: AnimatedSpanProps) => {+ const [isVisible, setIsVisible] = useState(!isView); // Start with false if isView=false+ const elementRef = useRef<HTMLDivElement | null>(null);++ useEffect(() => {+ if (!isView) {+ // Use IntersectionObserver to trigger animation when component is visible+ const observer = new IntersectionObserver(+ ([entry]) => {+ if (entry.isIntersecting) {+ setIsVisible(true);+ observer.disconnect(); // Disconnect observer after triggering+ }+ },+ { threshold: 0.1 } // Trigger when 10% of the component is visible+ );++ if (elementRef.current) {+ observer.observe(elementRef.current);+ }++ return () => {+ observer.disconnect();+ };+ }+ }, [isView]);++ return (+ <motion.div+ ref={elementRef}+ initial={{ opacity: 0, y: -5 }}+ animate={isVisible ? { opacity: 1, y: 0 } : { opacity: 0, y: -5 }}+ transition={{ duration: 0.3, delay: delay / 1000 }}+ className={cn("grid text-sm font-normal tracking-tight", className)}+ {...props}+ >+ {children}+ </motion.div>+ );+};interface TypingAnimationProps extends MotionProps {children: string;@@ -33,6 +64,7 @@ interface TypingAnimationProps extends MotionProps {duration?: number;delay?: number;as?: React.ElementType;+ isView?: boolean; // Prop to control animation trigger on visibility}export const TypingAnimation = ({@@ -41,6 +73,7 @@ export const TypingAnimation = ({duration = 60,delay = 0,as: Component = "span",+ isView = false, // Default: animation waits for visibility...props}: TypingAnimationProps) => {if (typeof children !== "string") {@@ -56,15 +89,42 @@ export const TypingAnimation = ({const elementRef = useRef<HTMLElement | null>(null);useEffect(() => {- const startTimeout = setTimeout(() => {- setStarted(true);- }, delay);- return () => clearTimeout(startTimeout);- }, [delay]);+ if (!isView) {+ // Use IntersectionObserver to start typing animation when component is visible+ const observer = new IntersectionObserver(+ ([entry]) => {+ if (entry.isIntersecting) {+ // Start animation with specified delay after visibility+ const startTimeout = setTimeout(() => {+ setStarted(true);+ }, delay);+ observer.disconnect(); // Disconnect observer after triggering+ return () => clearTimeout(startTimeout);+ }+ },+ { threshold: 0.1 } // Trigger when 10% of the component is visible+ );++ if (elementRef.current) {+ observer.observe(elementRef.current);+ }++ return () => {+ observer.disconnect();+ };+ } else {+ // Start animation with delay if isView=true+ const startTimeout = setTimeout(() => {+ setStarted(true);+ }, delay);+ return () => clearTimeout(startTimeout);+ }+ }, [delay, isView]);useEffect(() => {if (!started) return;+ // Handle typing animationlet i = 0;const typingEffect = setInterval(() => {if (i < children.length) {@@ -116,4 +176,4 @@ export const Terminal = ({ children, className }: TerminalProps) => {</pre></div>);-};+};\ No newline at end of filediff --git a/registry/magicui/typing-animation.tsx b/registry/magicui/typing-animation.tsxindex 813f70b5..1ad55c5e 100644--- a/registry/magicui/typing-animation.tsx+++ b/registry/magicui/typing-animation.tsx@@ -1,7 +1,7 @@"use client";import { cn } from "@/lib/utils";-import { motion, MotionProps } from "motion/react";+import { motion, MotionProps, useInView } from "motion/react";import { useEffect, useRef, useState } from "react";interface TypingAnimationProps extends MotionProps {@@ -29,6 +29,10 @@ export function TypingAnimation({const [displayedText, setDisplayedText] = useState<string>("");const [started, setStarted] = useState(false);const elementRef = useRef<HTMLElement | null>(null);+ const isInView = useInView(elementRef as React.RefObject<Element>, {+ amount: 0.3,+ once: true,+ });useEffect(() => {if (!startOnView) {@@ -38,24 +42,14 @@ export function TypingAnimation({return () => clearTimeout(startTimeout);}- const observer = new IntersectionObserver(- ([entry]) => {- if (entry.isIntersecting) {- setTimeout(() => {- setStarted(true);- }, delay);- observer.disconnect();- }- },- { threshold: 0.1 },- );+ if (!isInView) return;- if (elementRef.current) {- observer.observe(elementRef.current);- }+ const startTimeout = setTimeout(() => {+ setStarted(true);+ }, delay);- return () => observer.disconnect();- }, [delay, startOnView]);+ return () => clearTimeout(startTimeout);+ }, [delay, startOnView, isInView]);useEffect(() => {if (!started) return;