# Scroll Word Swap

> A pinned sentence whose key word changes as you scroll, with the space around it resizing smoothly.

Category: Text · Free · Updated 2026-10-20 · Live demo: https://www.motionkitui.com/components/scroll-word-swap

## When to use it

Story sections ("Built for [speed / teams / scale]") and long landing pages where the reader controls the pace.

## How it works

The section is tall and the sentence inside is position: sticky, so it stays pinned while you scroll. useScroll maps progress through the section to which word is showing, and the slot around the word resizes smoothly, so the words beside it glide instead of jumping.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/scroll-word-swap.json
```

Or copy `components/motionkit/scroll-word-swap.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `before` | `string` |  | Text before the changing word. |
| `words` | `string[]` |  | Words that take turns, in scroll order. |
| `after` | `string` | `""` | Text after the changing word. |
| `color` | `string` | `"#5FAEB4"` | Colour of the changing word. |
| `step` | `string` | `"60vh"` | Scroll distance per word. |
| `height` | `string` | `"100vh"` | Height of the pinned area. |
| `container` | `RefObject<HTMLElement>` |  | A scrolling element to follow instead of the page. |

## Accessibility

- **Keyboard: Pass.** Driven by scroll position, so arrow keys, Page Down and the space bar move it too.
- **Screen readers: Pass.** Screen readers get the whole sentence with every word, once.
- **Reduced motion: Pass.** With reduced motion on, words cross-fade in place instead of sliding.
- **Colour contrast: Pass.** The default teal word measures 7.5:1 on the dark background.

## Source

```tsx
"use client";

import { useRef, useState, type RefObject } from "react";
import { AnimatePresence, motion, useMotionValueEvent, useReducedMotion, useScroll } from "motion/react";

type ScrollWordSwapProps = {
  /** Text before the changing word. */
  before: string;
  /** The words that take turns, in scroll order. */
  words: string[];
  /** Text after the changing word. */
  after?: string;
  /** Colour of the changing word. */
  color?: string;
  /** Scroll distance per word, as a CSS length. */
  step?: string;
  /** Height of the pinned area, as a CSS length. Usually the screen. */
  height?: string;
  /** A scrolling element to follow instead of the page, e.g. a panel with overflow. */
  container?: RefObject<HTMLElement | null>;
  className?: string;
};

export function ScrollWordSwap({
  before,
  words,
  after = "",
  color = "#5FAEB4",
  step = "60vh",
  height = "100vh",
  container,
  className = "",
}: ScrollWordSwapProps) {
  const ref = useRef<HTMLDivElement>(null);
  const reduceMotion = useReducedMotion();
  const [index, setIndex] = useState(0);
  // 0 when the section's top reaches the top of the view, 1 when its end does
  const { scrollYProgress } = useScroll({ target: ref, container, offset: ["start start", "end end"] });

  useMotionValueEvent(scrollYProgress, "change", (progress) => {
    const next = Math.min(words.length - 1, Math.floor(progress * words.length));
    if (next !== index) setIndex(next);
  });

  return (
    // the section is tall so there is room to scroll; the sentence stays pinned while it does
    <div ref={ref} className="relative" style={{ height: `calc(${height} + ${step} * ${Math.max(words.length - 1, 0)})` }}>
      <div className={`sticky top-0 flex items-center justify-center ${className}`} style={{ height }}>
        <p>
          <span className="sr-only">{`${before} ${words.join(", ")} ${after}`}</span>
          <span aria-hidden>
            {before}{" "}
            {/* the slot resizes to each word, so the words around it glide instead of jumping */}
            <motion.span
              layout={!reduceMotion}
              transition={{ type: "spring", bounce: 0.15, duration: 0.5 }}
              className="relative inline-flex overflow-hidden whitespace-nowrap pb-[0.1em] align-bottom"
            >
              <AnimatePresence mode="popLayout" initial={false}>
                <motion.span
                  key={words[index]}
                  style={{ color }}
                  initial={reduceMotion ? { opacity: 0 } : { y: "100%", opacity: 0, filter: "blur(6px)" }}
                  animate={{ y: 0, opacity: 1, filter: "blur(0px)" }}
                  exit={reduceMotion ? { opacity: 0 } : { y: "-100%", opacity: 0, filter: "blur(6px)" }}
                  transition={{ duration: 0.45, ease: [0.22, 1, 0.36, 1] }}
                  className="inline-block"
                >
                  {words[index]}
                </motion.span>
              </AnimatePresence>
            </motion.span>
            {after && " "}
            {after}
          </span>
        </p>
      </div>
    </div>
  );
}
```
