← Components

Text block effect

A coloured block sweeps each line in.

Text · FreeInstall ↓
Open preview ↗
Loading the demoNew tab ↗

Customize

#DDFC3E
0.45 s
0.30 s
Your code
<TextBlockEffect children={children} />

Changes show live and stay in the address, so a link keeps them.

Code

"use client";

import { createElement, useRef } from "react";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { SplitText } from "gsap/SplitText";
import { useGSAP } from "@gsap/react";

// Text that is revealed behind a coloured block. Each line gets a block that slides across it,
// the words appear while the block covers them, and the block slides away to show the line.
// The text stays real text: selectable, and read as one sentence by screen readers.
// It starts by itself when it scrolls into view, so it does not need a section around it.

if (typeof window !== "undefined") gsap.registerPlugin(useGSAP, ScrollTrigger, SplitText);

export type TextBlockEffectProps = {
  /** The text to show. Plain text only (the lines are cut from it). */
  children: string;
  /** The tag to render: a page title, a section title or a paragraph. */
  as?: "h1" | "h2" | "h3" | "p";
  /** The colour of the sliding blocks. */
  blockColor?: string;
  /** The colour of the text. Empty keeps whatever colour the surrounding page gives it. */
  textColor?: string;
  /** The way the blocks travel across a line before they leave again. */
  direction?: "right" | "left" | "down" | "up";
  /** "line" reveals one line after another. "cover" covers every line first, then uncovers them all. */
  mode?: "line" | "cover";
  /** How long one block takes to slide across (or off) a line, in seconds. */
  duration?: number;
  /** The wait between one line starting and the next, in seconds. */
  stagger?: number;
  /** Play the reveal again each time the text scrolls back into view from above. */
  replay?: boolean;
  /** When the reveal starts: "<element edge> <viewport edge>", e.g. "top 80%". */
  start?: string;
  /** The scrolling element. Leave it out to use the page; give it a selector or an element for an inner scroller. */
  scroller?: string | HTMLElement;
  /** Extra classes: put the font size, weight, alignment and line height here. */
  className?: string;
};

export function TextBlockEffect({
  children,
  as = "p",
  blockColor = "#DDFC3E",
  textColor = "",
  direction = "right",
  mode = "line",
  duration = 0.45,
  stagger = 0.3,
  replay = false,
  start = "top 80%",
  scroller,
  className = "",
}: TextBlockEffectProps) {
  const textRef = useRef<HTMLElement>(null);

  useGSAP(
    () => {
      const element = textRef.current;
      if (!element) return;

      // The text is hidden until it is split, so you never see it jump. Show it again either way.
      element.style.visibility = "visible";

      // Reduced motion: leave the text exactly as written, fully readable
      if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;

      // Sideways directions use scaleX, up and down use scaleY.
      // A block grows from the side it comes from, then shrinks towards the opposite side.
      const sideways = direction === "right" || direction === "left";
      const axis = sideways ? "scaleX" : "scaleY";
      const origins = { right: ["left center", "right center"], left: ["right center", "left center"], down: ["center top", "center bottom"], up: ["center bottom", "center top"] } as const;
      const [growFrom, shrinkTo] = origins[direction];

      const split = SplitText.create(element, {
        type: "lines",
        linesClass: "text-block-line",
        // Cut the lines again when the width changes or the web font arrives
        autoSplit: true,
        onSplit(self) {
          const align = getComputedStyle(element).textAlign;
          const pieces: { text: HTMLElement; block: HTMLElement }[] = [];

          self.lines.forEach((line) => {
            // The line shrinks to the width of its words, so the block is exactly as wide as the text
            const lineBox = line as HTMLElement;
            lineBox.style.display = "block";
            lineBox.style.position = "relative";
            lineBox.style.width = "fit-content";
            lineBox.style.maxWidth = "100%";
            lineBox.style.textAlign = "start";
            if (align === "center") lineBox.style.marginInline = "auto";
            else if (align === "right" || align === "end") lineBox.style.marginLeft = "auto";

            // The words go in their own wrapper, so they can be hidden without hiding the block
            const text = document.createElement("span");
            while (lineBox.firstChild) text.appendChild(lineBox.firstChild);
            lineBox.appendChild(text);

            // A touch bigger than the line, so no sliver of text shows around the edges
            const block = document.createElement("span");
            block.setAttribute("aria-hidden", "true");
            block.style.cssText = `position:absolute;left:-1%;top:0;width:102%;height:102%;pointer-events:none;background:${blockColor};`;
            lineBox.appendChild(block);

            pieces.push({ text, block });
          });

          gsap.set(pieces.map((p) => p.text), { opacity: 0 });
          gsap.set(pieces.map((p) => p.block), { [axis]: 0, transformOrigin: growFrom });

          const timeline = gsap.timeline({
            scrollTrigger: { trigger: element, scroller, start, toggleActions: replay ? "play none none reset" : "play none none none" },
          });

          const grow = (block: HTMLElement, at: number) => {
            timeline.set(block, { transformOrigin: growFrom }, at);
            timeline.to(block, { [axis]: 1, duration, ease: "power2.inOut" }, at);
          };
          const shrink = (block: HTMLElement, text: HTMLElement, at: number) => {
            // The words are shown at the moment the block is fully over them
            timeline.set(text, { opacity: 1 }, at);
            timeline.set(block, { transformOrigin: shrinkTo }, at);
            timeline.to(block, { [axis]: 0, duration, ease: "power2.inOut" }, at);
          };

          if (mode === "cover") {
            // Every line is covered first, one after another, then they are all uncovered
            pieces.forEach((piece, i) => grow(piece.block, i * stagger));
            const covered = (pieces.length - 1) * stagger + duration;
            pieces.forEach((piece, i) => shrink(piece.block, piece.text, covered + i * stagger));
          } else {
            // Each line is covered and uncovered in turn, the next one starting a moment later
            pieces.forEach((piece, i) => {
              grow(piece.block, i * stagger);
              shrink(piece.block, piece.text, i * stagger + duration);
            });
          }

          // Returning the timeline lets SplitText keep it in step when it re-splits
          return timeline;
        },
      });

      return () => split.revert();
    },
    { scope: textRef, dependencies: [children, as, blockColor, direction, mode, duration, stagger, replay, start, scroller], revertOnUpdate: true },
  );

  // The key makes React build a fresh element when the text or tag changes, because SplitText
  // rewrites the inside of the old one.
  return createElement(as, { ref: textRef, key: `${as}:${children}`, className, style: { visibility: "hidden", ...(textColor ? { color: textColor } : null) } }, children);
}

The component. Installs to components/dimaac/text-block-effect.tsx.

Install

With the shadcn CLI

npx shadcn@latest add https://ui.dimaac.com/r/text-block-effect.json

It adds components/dimaac/text-block-effect.tsx and installs what it imports. With the @dimaac namespace in components.json (Docs) it's just npx shadcn@latest add @dimaac/text-block-effect.

By hand

npm install gsap @gsap/react
  1. Install the packages it uses: gsap, @gsap/react.
  2. Copy the component file below into components/dimaac/text-block-effect.tsx.
  3. It's styled with Tailwind v4 classes. If that folder sits outside the places Tailwind scans, add it with @source in your CSS.
  4. Import it with import { TextBlockEffect } from "@/components/dimaac/text-block-effect". Give its parent a size: it fills it.

Props

PropTypeDefaultDescription
children (required)string-The text to show. Plain text only (the lines are cut from it).
as"h1" | "h2" | "h3" | "p""p"The tag to render: a page title, a section title or a paragraph.
blockColorstring"#DDFC3E"The colour of the sliding blocks.
textColorstring""The colour of the text. Empty keeps whatever colour the surrounding page gives it.
direction"right" | "left" | "down" | "up""right"The way the blocks travel across a line before they leave again.
mode"line" | "cover""line""line" reveals one line after another. "cover" covers every line first, then uncovers them all.
durationnumber0.45How long one block takes to slide across (or off) a line, in seconds.
staggernumber0.3The wait between one line starting and the next, in seconds.
replaybooleanfalsePlay the reveal again each time the text scrolls back into view from above.
startstring"top 80%"When the reveal starts: "<element edge> <viewport edge>", e.g. "top 80%".
scrollerstring | HTMLElement-The scrolling element. Leave it out to use the page; give it a selector or an element for an inner scroller.
classNamestring""Extra classes: put the font size, weight, alignment and line height here.

Use with AI

Agent prompt

Paste it into Claude Code, Cursor or any coding agent, or open it straight in a chat. It carries your Customize settings.

Prompt
Add the Text block effect component (<TextBlockEffect />) from DiMaac UI to this project.

Docs: https://ui.dimaac.com/components/text-block-effect
Source and dependencies (shadcn registry JSON): https://ui.dimaac.com/r/text-block-effect.json

Install it with:
npx shadcn@latest add https://ui.dimaac.com/r/text-block-effect.json

Then use it like this:
import { TextBlockEffect } from "@/components/dimaac/text-block-effect";
<TextBlockEffect children={children} />

If the shadcn CLI isn't set up here, fetch the registry JSON above, write files[0].content to components/dimaac/text-block-effect.tsx and install gsap, @gsap/react.
It uses Tailwind v4 classes, so make sure Tailwind scans that folder. Give its parent a size: it fills it. Keep light and dark mode working and keep its prefers-reduced-motion fallback.
Full index for agents: https://ui.dimaac.com/llms.txt

Files agents read

llms.txt
Every component and how to install it
text-block-effect.md
This page as Markdown: install, props and the full source
text-block-effect.json
The shadcn registry item

About

A paragraph or heading cut into lines, where a block of colour slides across each line, the words appear underneath it and the block slides away. The blocks fit the words exactly, whether the text is left, centred or right aligned, and can travel in any of four directions, one line at a time or covering everything first. It uses GSAP SplitText with autoSplit, so the lines are cut again when the width changes or the web font loads, and everything is put back the way it was on unmount. It needs no section around it: the text starts by itself when it scrolls into view. It takes an optional scroller for inner scroll areas, can play again every time it comes back into view, and shows the text as it is when the visitor prefers reduced motion.

Installs to
components/dimaac/text-block-effect.tsx
Uses
gsap, @gsap/react
Tags
gsap, splittext, scrolltrigger, text, reveal, editorial

Background photo by stacy on Lummi (Lummi License).