UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
feat: add hyper text component (#235)
SwayambhuPrasad08464b40ffab
diff --git a/config/docs.ts b/config/docs.tsindex fb02e60d..ec50ab85 100644--- a/config/docs.ts+++ b/config/docs.ts@@ -256,6 +256,12 @@ export const docsConfig: DocsConfig = {items: [],label: "",},+ {+ title: "Hyper Text",+ href: `/docs/components/hyper-text`,+ items: [],+ label: "New",+ },{title: "Word Rotate",href: `/docs/components/word-rotate`,diff --git a/content/docs/components/hyper-text.mdx b/content/docs/components/hyper-text.mdxnew file mode 100644index 00000000..68ceb837--- /dev/null+++ b/content/docs/components/hyper-text.mdx@@ -0,0 +1,57 @@+---+title: Hyper Text+date: 2024-08-03+description: A hyper changing text animation as you hover.+author: SwayambhuPrasad+published: true+---++<ComponentPreview name="hyper-text-demo" />++## Installation++<Tabs defaultValue="cli">++<TabsList>+ <TabsTrigger value="cli">CLI</TabsTrigger>+ <TabsTrigger value="manual">Manual</TabsTrigger>+</TabsList>+<TabsContent value="cli">++```bash+npx magicui-cli add hyper-text+```++</TabsContent>++<TabsContent value="manual">++<Steps>++<Step>Install the following dependencies:</Step>++```bash+npm install framer-motion+```++<Step>Copy and paste the following code into your project.</Step>++<ComponentSource name="hyper-text" />++<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 | |+| 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 |diff --git a/registry/components/example/hyper-text-demo.tsx b/registry/components/example/hyper-text-demo.tsxnew file mode 100644index 00000000..b24a00f7--- /dev/null+++ b/registry/components/example/hyper-text-demo.tsx@@ -0,0 +1,10 @@+import HyperText from "@/components/magicui/hyper-text";++export default function HyperTextDemo() {+ return (+ <HyperText+ className="text-4xl font-bold text-black dark:text-white"+ text="Hyper Text"+ />+ );+}diff --git a/registry/components/magicui/hyper-text.tsx b/registry/components/magicui/hyper-text.tsxnew file mode 100644index 00000000..e50bc43d--- /dev/null+++ b/registry/components/magicui/hyper-text.tsx@@ -0,0 +1,89 @@+"use client";++import { useEffect, useRef, useState } from "react";+import { AnimatePresence, motion, Variants } from "framer-motion";++import { cn } from "@/lib/utils";++interface HyperTextProps {+ text: string;+ duration?: number;+ framerProps?: Variants;+ className?: string;+ animateOnLoad?: boolean;+}++const alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");++const getRandomInt = (max: 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 },+ },+ className,+ animateOnLoad = true,+}: 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);+ };++ useEffect(() => {+ 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);+ }+ },+ duration / (text.length * 10),+ );+ // Clean up interval on unmount+ return () => clearInterval(interval);+ }, [text, duration, trigger, animateOnLoad]);++ return (+ <div+ className="overflow-hidden py-2 flex cursor-default scale-100"+ onMouseEnter={triggerAnimation}+ >+ <AnimatePresence mode="wait">+ {displayText.map((letter, i) => (+ <motion.h1+ key={i}+ className={cn("font-mono", letter === " " ? "w-3" : "", className)}+ {...framerProps}+ >+ {letter.toUpperCase()}+ </motion.h1>+ ))}+ </AnimatePresence>+ </div>+ );+}diff --git a/registry/index.tsx b/registry/index.tsxindex a50d3bdd..41bfd666 100644--- a/registry/index.tsx+++ b/registry/index.tsx@@ -113,6 +113,11 @@ const ui: Registry = {type: "components:magicui",files: ["registry/components/magicui/text-reveal.tsx"],},+ "hyper-text": {+ name: "hyper-text",+ type: "components:magicui",+ files: ["registry/components/magicui/hyper-text.tsx"],+ },"animated-gradient-text": {name: "animated-gradient-text",type: "components:magicui",@@ -613,6 +618,15 @@ const example: Registry = {() => import("@/registry/components/example/word-rotate-demo"),),},+ "hyper-text-demo": {+ name: "hyper-text-demo",+ type: "components:example",+ registryDependencies: ["hyper-text"],+ files: ["registry/components/example/hyper-text-demo.tsx"],+ component: React.lazy(+ () => import("@/registry/components/example/hyper-text-demo"),+ ),+ },"avatar-circles-demo": {name: "avatar-circles-demo",type: "components:example",