Randomized Text Effect
Randomized Text Effect is a React and Tailwind CSS text component from UI Layouts, licensed MIT. Copy it and paste it into your project.
UI Layouts
MIT
text
What it is
Randomized Text Effect is a React and Tailwind CSS text component from UI Layouts, licensed MIT. Copy it and paste it into your project.
How to use it
Open it in the catalogue, press copy, and paste it into your project. The prompt you copy carries the code and the rules that tell your agent to reproduce it without changing anything.
Five free copies a month. No card.
Needs
npm i motion @motionone/utils
Licence
This component comes from UI Layouts and is redistributed under the MIT licence, which permits commercial use. The copyright notice travels with the code.
Randomized Text Effect
'use client';
import React, { useCallback, useEffect, useState } from 'react';
const lettersAndSymbols = 'abcdefghijklmnopqrstuvwxyz!@#$%^&*-_+=;:<>,';
interface AnimatedTextProps {
text: string;
}
export function RandomizedTextEffect({ text }: AnimatedTextProps) {
const [animatedText, setAnimatedText] = useState('');
const getRandomChar = useCallback(
() => lettersAndSymbols[Math.floor(Math.random() * lettersAndSymbols.length)],
[]
);
const animateText = useCallback(async () => {
const duration = 50;
const revealDuration = 80;
const initialRandomDuration = 300;
const generateRandomText = () =>
text
.split('')
.map(() => getRandomChar())
.join('');
setAnimatedText(generateRandomText());
const endTime = Date.now() + initialRandomDuration;
while (Date.now() < endTime) {
await new Promise((resolve) => setTimeout(resolve, duration));
setAnimatedText(generateRandomText());
}
for (let i = 0; i < text.length; i++) {
await new Promise((resolve) => setTimeout(resolve, revealDuration));
setAnimatedText(
(prevText) =>
text.slice(0, i + 1) +
prevText
.slice(i + 1)
.split('')
.map(() => getRandomChar())
.join('')
);
}
}, [text, getRandomChar]);
useEffect(() => {
animateText();
}, [text, animateText]);
return <div className='relative inline-block'>{animatedText}</div>;
}