Encrypted Text
aceternity/encrypted-textFreeComponent
aceternity publishes no description for this item.
Install it
npx shadcn@latest add https://ui.aceternity.com/registry/encrypted-text.jsonSource
1"use client";2import React, { useEffect, useRef, useState } from "react";3import { motion, useInView } from "motion/react";4import { cn } from "@/lib/utils";56type EncryptedTextProps = {7 text: string;8 className?: string;9 /**10 * Time in milliseconds between revealing each subsequent real character.11 * Lower is faster. Defaults to 50ms per character.12 */13 revealDelayMs?: number;14 /** Optional custom character set to use for the gibberish effect. */15 charset?: string;16 /**17 * Time in milliseconds between gibberish flips for unrevealed characters.18 * Lower is more jittery. Defaults to 50ms.19 */20 flipDelayMs?: number;21 /** CSS class for styling the encrypted/scrambled characters */22 encryptedClassName?: string;23 /** CSS class for styling the revealed characters */24 revealedClassName?: string;25};2627const DEFAULT_CHARSET =28 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-={}[];:,.<>/?";2930function generateRandomCharacter(charset: string): string {31 const index = Math.floor(Math.random() * charset.length);32 return charset.charAt(index);33}3435function generateGibberishPreservingSpaces(36 original: string,37 charset: string,38): string {39 if (!original) return "";40 let result = "";41 for (let i = 0; i < original.length; i += 1) {42 const ch = original[i];43 result += ch === " " ? " " : generateRandomCharacter(charset);44 }45 return result;46}4748export const EncryptedText: React.FC<EncryptedTextProps> = ({49 text,50 className,51 revealDelayMs = 50,52 charset = DEFAULT_CHARSET,53 flipDelayMs = 50,54 encryptedClassName,55 revealedClassName,56}) => {57 const ref = useRef<HTMLSpanElement>(null);58 const isInView = useInView(ref, { once: true });5960 const [revealCount, setRevealCount] = useState<number>(0);61 const animationFrameRef = useRef<number | null>(null);62 const startTimeRef = useRef<number>(0);63 const lastFlipTimeRef = useRef<number>(0);64 const scrambleCharsRef = useRef<string[]>(65 text ? generateGibberishPreservingSpaces(text, charset).split("") : [],66 );6768 useEffect(() => {69 if (!isInView) return;7071 // Reset state for a fresh animation whenever dependencies change72 const initial = text73 ? generateGibberishPreservingSpaces(text, charset)74 : "";75 scrambleCharsRef.current = initial.split("");76 startTimeRef.current = performance.now();77 lastFlipTimeRef.current = startTimeRef.current;78 setRevealCount(0);7980 let isCancelled = false;8182 const update = (now: number) => {83// … truncated
What it pulls in
npm packages
Files it writes
More from aceternity
All 89 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| 3d-card3d-card | aceternity | Components | Unverified | no deps | |
| 3d Globe3d-globe | aceternity | Components | Free | 3 deps | |
| 3d-marquee3d-marquee | aceternity | Components | Unverified | no deps | |
| 3d-pin3d-pin | aceternity | Components | Unverified | 1 dep | |
| animated-modalanimated-modal | aceternity | Components | Unverified | 1 dep | |
| animated-testimonialsanimated-testimonials | aceternity | Components | Unverified | 2 deps | |
| animated-tooltipanimated-tooltip | aceternity | Components | Unverified | 1 dep | |
| apple-cards-carouselapple-cards-carousel | aceternity | Components | Unverified | 2 deps |
