UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
fix: update hyper text component (#450)
Dillion Verma25bb649f48a3
diff --git a/content/docs/components/hyper-text.mdx b/content/docs/components/hyper-text.mdxindex 876de791..4194325c 100644--- a/content/docs/components/hyper-text.mdx+++ b/content/docs/components/hyper-text.mdx@@ -1,7 +1,7 @@---title: Hyper Textdate: 2024-08-03-description: A hyper changing text animation as you hover.+description: A text animation that scrambles letters before revealing the final text.author: SwayambhuPrasadpublished: true---@@ -48,10 +48,13 @@ npm install framer-motion## Props-| Prop | Type | Description | Default |-| ------------- | -------- | -------------------------------------------------- | ------- |-| className | string | The class name to be applied to the component | |-| duration | number | The duration for which the animation runs. | 80 |-| text | string | Text to animate | "" |-| framerProps | Variants | An object containing framer-motion animation props | {} |-| animateOnLoad | boolean | Play animation on load | true |+| Prop | Type | Description | Default |+| -------------- | ----------------- | --------------------------------------------- | ------- |+| children | string | Text content to animate | |+| className | string | The class name to be applied to the component | |+| duration | number | Duration of the animation in milliseconds | 800 |+| 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 |+| animateOnHover | boolean | Enable hover animation | true |+| characterSet | string[] | Custom character set for scramble effect | A-Z |diff --git a/registry/default/example/hyper-text-demo.tsx b/registry/default/example/hyper-text-demo.tsxindex b974ead2..fe5727b5 100644--- a/registry/default/example/hyper-text-demo.tsx+++ b/registry/default/example/hyper-text-demo.tsx@@ -1,10 +1,5 @@import HyperText from "@/registry/default/magicui/hyper-text";export default function HyperTextDemo() {- return (- <HyperText- className="text-4xl font-bold text-black dark:text-white"- text="Hyper Text"- />- );+ return <HyperText>Hover Me!</HyperText>;}diff --git a/registry/default/magicui/hyper-text.tsx b/registry/default/magicui/hyper-text.tsxindex e99ed406..2bb935a2 100644--- a/registry/default/magicui/hyper-text.tsx+++ b/registry/default/magicui/hyper-text.tsx@@ -1,89 +1,138 @@"use client";-import { AnimatePresence, motion, Variants } from "framer-motion";+import { cn } from "@/lib/utils";+import { AnimatePresence, motion, MotionProps } from "motion/react";import { useEffect, useRef, useState } from "react";-import { cn } from "@/lib/utils";+type CharacterSet = string[] | readonly string[];-interface HyperTextProps {- text: string;- duration?: number;- framerProps?: Variants;+interface HyperTextProps extends MotionProps {+ /** The text content to be animated */+ children: string;+ /** Optional className for styling */className?: string;- animateOnLoad?: boolean;+ /** Duration of the animation in milliseconds */+ duration?: number;+ /** Delay before animation starts in milliseconds */+ delay?: number;+ /** Component to render as - defaults to div */+ as?: React.ElementType;+ /** Whether to start animation when element comes into view */+ startOnView?: boolean;+ /** Whether to trigger animation on hover */+ animateOnHover?: boolean;+ /** Custom character set for scramble effect. Defaults to uppercase alphabet */+ characterSet?: CharacterSet;}-const alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");+const DEFAULT_CHARACTER_SET = Object.freeze(+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),+) as readonly string[];-const getRandomInt = (max: number) => Math.floor(Math.random() * max);+const getRandomInt = (max: number): number => Math.floor(Math.random() * max);export default function HyperText({- text,- duration = 800,- framerProps = {- initial: { opacity: 0, y: -10 },- animate: { opacity: 1, y: 0 },- exit: { opacity: 0, y: 3 },- },+ children,className,- animateOnLoad = true,+ duration = 800,+ delay = 0,+ as: Component = "div",+ startOnView = false,+ animateOnHover = true,+ characterSet = DEFAULT_CHARACTER_SET,+ ...props}: HyperTextProps) {- const [displayText, setDisplayText] = useState(text.split(""));- const [trigger, setTrigger] = useState(false);- const interations = useRef(0);- const isFirstRender = useRef(true);-- const triggerAnimation = () => {- interations.current = 0;- setTrigger(true);+ const MotionComponent = motion.create(Component, {+ forwardMotionProps: true,+ });++ const [displayText, setDisplayText] = useState<string[]>(() =>+ children.split(""),+ );+ const [isAnimating, setIsAnimating] = useState(false);+ const iterationCount = useRef(0);+ const elementRef = useRef<HTMLElement>(null);++ const handleAnimationTrigger = () => {+ if (animateOnHover) {+ iterationCount.current = 0;+ setIsAnimating(true);+ }};+ // Handle animation start based on view or delayuseEffect(() => {- const interval = setInterval(- () => {- if (!animateOnLoad && isFirstRender.current) {- clearInterval(interval);- isFirstRender.current = false;- return;- }- if (interations.current < text.length) {- setDisplayText((t) =>- t.map((l, i) =>- l === " "- ? l- : i <= interations.current- ? text[i]- : alphabets[getRandomInt(26)],- ),- );- interations.current = interations.current + 0.1;- } else {- setTrigger(false);- clearInterval(interval);+ if (!startOnView) {+ const startTimeout = setTimeout(() => {+ setIsAnimating(true);+ }, delay);+ return () => clearTimeout(startTimeout);+ }++ const observer = new IntersectionObserver(+ ([entry]) => {+ if (entry.isIntersecting) {+ setTimeout(() => {+ setIsAnimating(true);+ }, delay);+ observer.disconnect();}},- duration / (text.length * 10),+ { threshold: 0.1 },);- // Clean up interval on unmount++ if (elementRef.current) {+ observer.observe(elementRef.current);+ }++ return () => observer.disconnect();+ }, [delay, startOnView]);++ // Handle scramble animation+ useEffect(() => {+ if (!isAnimating) return;++ const intervalDuration = duration / (children.length * 10);+ const maxIterations = children.length;++ const interval = setInterval(() => {+ if (iterationCount.current < maxIterations) {+ setDisplayText((currentText) =>+ currentText.map((letter, index) =>+ letter === " "+ ? letter+ : index <= iterationCount.current+ ? children[index]+ : characterSet[getRandomInt(characterSet.length)],+ ),+ );+ iterationCount.current = iterationCount.current + 0.1;+ } else {+ setIsAnimating(false);+ clearInterval(interval);+ }+ }, intervalDuration);+return () => clearInterval(interval);- }, [text, duration, trigger, animateOnLoad]);+ }, [children, duration, isAnimating, characterSet]);return (- <div- className="flex scale-100 cursor-default overflow-hidden py-2"- onMouseEnter={triggerAnimation}+ <MotionComponent+ ref={elementRef}+ className={cn("overflow-hidden py-2 text-4xl font-bold", className)}+ onMouseEnter={handleAnimationTrigger}+ {...props}><AnimatePresence mode="wait">- {displayText.map((letter, i) => (+ {displayText.map((letter, index) => (<motion.span- key={i}- className={cn("font-mono", letter === " " ? "w-3" : "", className)}- {...framerProps}+ key={index}+ className={cn("font-mono", letter === " " ? "w-3" : "")}>{letter.toUpperCase()}</motion.span>))}</AnimatePresence>- </div>+ </MotionComponent>);}diff --git a/registry/registry-ui.ts b/registry/registry-ui.tsindex 00ff39dd..df307a5b 100644--- a/registry/registry-ui.ts+++ b/registry/registry-ui.ts@@ -316,7 +316,7 @@ export const ui: Registry = [{name: "hyper-text",type: "registry:ui",- dependencies: ["framer-motion"],+ dependencies: ["motion"],files: ["magicui/hyper-text.tsx"],},{