We are working on new components <3
HextaUIHextaUI
AnimationMagic Text

Magic Text

Simple animated text component that reveals as we scroll down the page.

Preview

Scroll Down 👇

HiHithere!there!I'mI'mpreet,preet,creatorcreatorofofHextaUI.HextaUI.ThankThankyouyousosomuchmuchofofallallthethesupportsupportandandloveloveyou'veyou'veshownshownme.me.IIhopehopeyouyouenjoyenjoyusingusingHextaUIHextaUIasasmuchmuchasasIIenjoyedenjoyedcreatingcreatingit.it.

Code

MagicText.tsx
"use client";
 
import { motion, useScroll, useTransform } from "motion/react";
import React, { useRef } from "react";
 
export interface MagicTextProps {
  text: string;
}
 
interface WordProps {
  children: string;
  progress: any;
  range: number[];
}
 
const Word: React.FC<WordProps> = ({ children, progress, range }) => {
  const opacity = useTransform(progress, range, [0, 1]);
 
  return (
    <span className="relative mt-[12px] mr-1 text-3xl font-semibold">
      <span className="absolute opacity-20">{children}</span>
      <motion.span style={{ opacity: opacity }}>{children}</motion.span>
    </span>
  );
};
 
export const MagicText: React.FC<MagicTextProps> = ({ text }) => {
  const container = useRef(null);
 
  const { scrollYProgress } = useScroll({
    target: container,
 
    offset: ["start 0.9", "start 0.25"],
  });
  text;
  const words = text.split(" ");
 
  return (
    <p ref={container} className="flex flex-wrap leading-[0.5] p-4">
      {words.map((word, i) => {
        const start = i / words.length;
 
        const end = start + 1 / words.length;
 
        return (
          <Word key={i} progress={scrollYProgress} range={[start, end]}>
            {word}
          </Word>
        );
      })}
    </p>
  );
};

Usage

index.tsx
import { MagicText } from "@/components/library/animation/MagicText";
 
<div className="h-[70rem] relative flex items-center justify-center">
  <MagicText
    text={
      "Hi there! I'm preet, creator of HextaUI. Thank you so much of all the support and love you've shown me. I hope you enjoy using HextaUI as much as I enjoyed creating it."
    }
  />
</div>;

Props

PropTypeDefault
text
string
-
Edit on GitHub

Last updated on

On this page