Back to TextsYou are on Sliding Text.

Sliding Text

Text slides out of its box while a copy slides in.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks
PrequelUI

Libraries

PrequelUI

Motion

Spring + short tween

Reduced motion

Fade in place
sliding-text.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/sliding-text.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/sliding-text.json
npmthe default on every machine

First component? Set up your project first

Frameworks

The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.

5 runtimes
sliding-text.tsx
"use client";

import * as React from "react";
import {
  animate,
  motion,
  useMotionValue,
  useTransform,
  type AnimationPlaybackControls,
  type HTMLMotionProps,
  type Transition,
} from "motion/react";
import { cn } from "@/lib/utils";
import { useReduceMotion, useTempoTransition } from "./motion";

export type SlidingTextDirection = "up" | "down";
export type SlidingTextTrigger = "hover" | "click";

const HOST = 'a, button, [role="button"], [role="link"]';
const BLEED = "0.25em";
const GAP = "0.6em";

export type SlidingTextProps = Omit<HTMLMotionProps<"span">, "children"> & {
  text: string;
  direction?: SlidingTextDirection;
  trigger?: SlidingTextTrigger;
  transition?: Transition;
};

export function SlidingText({
  text,
  direction = "down",
  trigger = "hover",
  transition,
  className,
  style,
  ...props
}: SlidingTextProps) {
  const reduce = useReduceMotion();
  const tween = useTempoTransition(
    transition ?? { duration: 0.25, ease: [0.4, 0, 0.2, 1] },
  );
  const sign = direction === "down" ? 1 : -1;
  const progress = useMotionValue(0);
  const y = useTransform(progress, (value) => {
    const lap = sign * (value - Math.floor(value));
    return lap === 0 ? "0%" : `calc(${lap * 100}% + ${lap} * ${GAP})`;
  });
  const target = React.useRef(0);
  const controls = React.useRef<AnimationPlaybackControls | null>(null);

  React.useEffect(() => () => controls.current?.stop(), []);

  const slideTo = React.useCallback(
    (next: number) => {
      target.current = next;
      controls.current?.stop();
      if (reduce) {
        progress.jump(next);
        return;
      }
      controls.current = animate(progress, next, tween);
    },
    [progress, reduce, tween],
  );

  const elementRef = React.useRef<HTMLSpanElement | null>(null);
  const [host, setHost] = React.useState<Element | null>(null);

  React.useLayoutEffect(() => {
    setHost(elementRef.current?.parentElement?.closest(HOST) ?? null);
  }, []);

  React.useEffect(() => {
    if (!host) return;
    const enter = (event: Event) => {
      if (trigger === "hover" && (event as PointerEvent).pointerType !== "touch") {
        slideTo(1);
      }
    };
    const leave = (event: Event) => {
      if (trigger === "hover" && (event as PointerEvent).pointerType !== "touch") {
        slideTo(0);
      }
    };
    const click = () => {
      if (trigger === "click") slideTo(Math.round(target.current) + 1);
    };
    host.addEventListener("pointerenter", enter);
    host.addEventListener("pointerleave", leave);
    host.addEventListener("click", click);
    return () => {
      host.removeEventListener("pointerenter", enter);
      host.removeEventListener("pointerleave", leave);
      host.removeEventListener("click", click);
    };
  }, [host, slideTo, trigger]);

  return (
    <motion.span
      ref={(node) => {
        elementRef.current = node;
      }}
      data-slot="sliding-text"
      data-direction={direction}
      data-trigger={trigger}
      onHoverStart={() => {
        if (trigger === "hover" && !host) slideTo(1);
      }}
      onHoverEnd={() => {
        if (trigger === "hover" && !host) slideTo(0);
      }}
      onClick={() => {
        if (trigger === "click" && !host) slideTo(Math.round(target.current) + 1);
      }}
      className={cn("relative inline-block overflow-clip", className)}
      style={{
        paddingBlock: BLEED,
        marginBlock: `calc(-1 * ${BLEED})`,
        ...style,
      }}
      {...props}
    >
      <motion.span
        data-slot="sliding-text-track"
        className="relative inline-block"
        style={{ y }}
      >
        {text}
        <span
          data-slot="sliding-text-copy"
          aria-hidden="true"
          className="absolute left-0 w-full"
          style={{ top: `calc(${-sign * 100}% - ${sign} * ${GAP})` }}
        >
          {text}
        </span>
      </motion.span>
    </motion.span>
  );
}

Install

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/sliding-text.json
npmthe default on every machine

Dependencies

motion lucide-react

The registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.

02

Usage

Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.

sliding-text.tsx
import { SlidingText } from "@/components/prequelui/sliding-text";

export default function Demo() {
  return (
    <button className="rounded-lg bg-accent px-5 py-2 text-[15px] font-medium">
      <SlidingText text="Read the story" />
    </button>
  );
}
import { SlidingText } from "@/components/prequelui/sliding-text";

export function Variant() {
  return (
    <SlidingText tempo={1.2}>
      …
    </SlidingText>
  );
}
03

Animation

The label and an identical copy sit in a clipped box. Hovering slides both by exactly one line plus a 0.6 em gap, so the copy lands where the original was; the motion is a single continuous value that can be interrupted at any time.

  • Slide: 250 ms with a fill curve, on a retargetable motion value.
  • Gap between the two copies: 0.6 em; bleed: 0.25 em.
  • Inside a link or a button, the whole host drives the slide.
  • Reduced motion jumps between the two positions.
250 ms fillgap 0.6 emcontinuous valuehost aware
04

API Reference

Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.

SlidingText

PropTypeDefault
textstring—
direction"up" | "down""down"
trigger"hover" | "click""hover"
transitionTransitioncomponent spring or tween

Next component

Splitting Text

Text arrives piece by piece with a staggered blur.