# Decrypting headline

> The letters of a headline start as noise and lock in one by one, left to right, without shifting the layout.

- Category: [Text & scroll](https://library.aimran.es/components/text-scroll)
- Built with: React + Motion
- Live editor: https://library.aimran.es/blocks/scramble-decrypt-text
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

A text scramble effect suits brands that live close to code or secrets: a cybersecurity firm, a developer portfolio, the launch page of an API, a crypto wallet. The headline decodes itself when it scrolls into view, and because every letter keeps its place, nothing around it jumps while the noise settles.

Short headlines work best, around three to six words. Keep the duration under two seconds or people wait for it instead of reading. Tapping replays it. Screen readers hear the real sentence straight away, not the random characters.

## Code

### React (Motion)

```tsx
import { Fragment, useCallback, useEffect, useRef, useState } from "react";
import { useInView, useReducedMotion } from "framer-motion";
import { RotateCcw } from "lucide-react";

interface ScrambleTextProps {
  text?: string;
  color?: string;
  /** Milisegundos hasta que se descifra entero. */
  duration?: number;
}

const NARROW = "il1!|:;/";
const UPPER = "ABDEFHKLNPRSTUXZ";
const LOWER = "abcdeghknopqsuvxyz#*";
const glyphSet = (c: string) => (/[ilíjtfr.,;:!¡'|]/.test(c) ? NARROW : /[A-ZÁÉÍÓÚÑ]/.test(c) ? UPPER : LOWER);
const glyph = (c: string, n: number) => {
  const set = glyphSet(c);
  return set.charAt(Math.floor(n * set.length) % set.length);
};
const noise = (text: string) => [...text].map((c, i) => (c === " " ? null : glyph(c, ((i * 7 + 3) % 10) / 10)));

export default function ScrambleText({
  text = "Less noise. More signal.",
  color = "#ff375f",
  duration = 1600,
}: ScrambleTextProps) {
  const ref = useRef<HTMLButtonElement>(null);
  const seen = useInView(ref, { once: true, amount: 0.5 });
  const reduce = useReducedMotion();
  const raf = useRef(0);
  const [glyphs, setGlyphs] = useState<(string | null)[]>(() => noise(text));

  const run = useCallback(() => {
    cancelAnimationFrame(raf.current);
    const chars = [...text];
    const locks = chars.map((_, i) => (i / chars.length) * 0.75 + Math.random() * 0.25);
    const start = performance.now();
    let last = 0;
    const step = (now: number) => {
      const p = reduce ? 1 : Math.min(1, (now - start) / duration);
      if (now - last > 50 || p === 1) {
        last = now;
        setGlyphs(
          chars.map((c, i) =>
            c === " " || p >= (locks[i] ?? 0) ? null : glyph(c, Math.random()),
          ),
        );
      }
      if (p < 1) raf.current = requestAnimationFrame(step);
    };
    raf.current = requestAnimationFrame(step);
  }, [text, duration, reduce]);

  useEffect(() => {
    if (seen) run();
    return () => cancelAnimationFrame(raf.current);
  }, [seen, run]);

  const words = text.split(" ");
  let offset = 0;

  return (
    <div className="w-full max-w-md">
      <button
        ref={ref}
        type="button"
        onClick={run}
        aria-label={text}
        className="block w-full cursor-pointer text-left text-balance text-[44px] font-semibold leading-[1.02] tracking-[-0.045em] text-[#1d1d1f] dark:text-[#f5f5f7] sm:text-[50px]"
      >
        <span aria-hidden="true">
          {words.map((w, wi) => {
            const start = offset;
            offset += w.length + 1;
            return (
              <Fragment key={wi}>
                <span className="inline-block whitespace-nowrap">
                  {[...w].map((ch, ci) => {
                    const g = glyphs[start + ci];
                    return (
                      <span key={ci} className="relative inline-block">
                        <span className="transition-opacity duration-300" style={{ opacity: g ? 0 : 1 }}>
                          {ch}
                        </span>
                        {g ? (
                          <span className="absolute inset-0 flex justify-center" style={{ color }}>
                            {g}
                          </span>
                        ) : null}
                      </span>
                    );
                  })}
                </span>
                {wi < words.length - 1 ? " " : null}
              </Fragment>
            );
          })}
        </span>
      </button>
      <p className="mt-5 flex items-center gap-1.5 text-[13px] text-[#86868b]">
        <RotateCcw strokeWidth={1.5} className="h-3.5 w-3.5" />
        Tap the headline to decode it again
      </p>
    </div>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Decrypting headline” block (Text & scroll) from AIMRAN Library to my project.

## What it is
The letters of a headline start as noise and lock in one by one, left to right, without shifting the layout.

## Chosen settings
- Text: "Less noise. More signal."
- Color del ruido: #ff375f
- Duration: 1600ms (from 400 to 3000ms)

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with React + TypeScript, with framer-motion for the animation and lucide-react for the icons. If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
import { Fragment, useCallback, useEffect, useRef, useState } from "react";
import { useInView, useReducedMotion } from "framer-motion";
import { RotateCcw } from "lucide-react";

interface ScrambleTextProps {
  text?: string;
  color?: string;
  /** Milisegundos hasta que se descifra entero. */
  duration?: number;
}

const NARROW = "il1!|:;/";
const UPPER = "ABDEFHKLNPRSTUXZ";
const LOWER = "abcdeghknopqsuvxyz#*";
const glyphSet = (c: string) => (/[ilíjtfr.,;:!¡'|]/.test(c) ? NARROW : /[A-ZÁÉÍÓÚÑ]/.test(c) ? UPPER : LOWER);
const glyph = (c: string, n: number) => {
  const set = glyphSet(c);
  return set.charAt(Math.floor(n * set.length) % set.length);
};
const noise = (text: string) => [...text].map((c, i) => (c === " " ? null : glyph(c, ((i * 7 + 3) % 10) / 10)));

export default function ScrambleText({
  text = "Less noise. More signal.",
  color = "#ff375f",
  duration = 1600,
}: ScrambleTextProps) {
  const ref = useRef<HTMLButtonElement>(null);
  const seen = useInView(ref, { once: true, amount: 0.5 });
  const reduce = useReducedMotion();
  const raf = useRef(0);
  const [glyphs, setGlyphs] = useState<(string | null)[]>(() => noise(text));

  const run = useCallback(() => {
    cancelAnimationFrame(raf.current);
    const chars = [...text];
    const locks = chars.map((_, i) => (i / chars.length) * 0.75 + Math.random() * 0.25);
    const start = performance.now();
    let last = 0;
    const step = (now: number) => {
      const p = reduce ? 1 : Math.min(1, (now - start) / duration);
      if (now - last > 50 || p === 1) {
        last = now;
        setGlyphs(
          chars.map((c, i) =>
            c === " " || p >= (locks[i] ?? 0) ? null : glyph(c, Math.random()),
          ),
        );
      }
      if (p < 1) raf.current = requestAnimationFrame(step);
    };
    raf.current = requestAnimationFrame(step);
  }, [text, duration, reduce]);

  useEffect(() => {
    if (seen) run();
    return () => cancelAnimationFrame(raf.current);
  }, [seen, run]);

  const words = text.split(" ");
  let offset = 0;

  return (
    <div className="w-full max-w-md">
      <button
        ref={ref}
        type="button"
        onClick={run}
        aria-label={text}
        className="block w-full cursor-pointer text-left text-balance text-[44px] font-semibold leading-[1.02] tracking-[-0.045em] text-[#1d1d1f] dark:text-[#f5f5f7] sm:text-[50px]"
      >
        <span aria-hidden="true">
          {words.map((w, wi) => {
            const start = offset;
            offset += w.length + 1;
            return (
              <Fragment key={wi}>
                <span className="inline-block whitespace-nowrap">
                  {[...w].map((ch, ci) => {
                    const g = glyphs[start + ci];
                    return (
                      <span key={ci} className="relative inline-block">
                        <span className="transition-opacity duration-300" style={{ opacity: g ? 0 : 1 }}>
                          {ch}
                        </span>
                        {g ? (
                          <span className="absolute inset-0 flex justify-center" style={{ color }}>
                            {g}
                          </span>
                        ) : null}
                      </span>
                    );
                  })}
                </span>
                {wi < words.length - 1 ? " " : null}
              </Fragment>
            );
          })}
        </span>
      </button>
      <p className="mt-5 flex items-center gap-1.5 text-[13px] text-[#86868b]">
        <RotateCcw strokeWidth={1.5} className="h-3.5 w-3.5" />
        Tap the headline to decode it again
      </p>
    </div>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
