Docs
Text
Scroll and Swap Text

Scroll and Swap Text

A text component that swaps the letters vertically on scroll.

Loading...

Installation


1pnpm dlx shadcn add @fancy/scroll-and-swap-text

Understanding the component


The trick here is similar to the Letter Swap Hover component—duplicate the text, then wrapping the them in a container with relative position, then stack the elements vertically. We use useScroll hook from motion to track the scroll position of the container, and use the scrollYProgress value to offset the vertical position of the elements (by setting the y property of the element).

Notes


  • In order to achieve a nice effect, you likely have to play with the container (where to track the scroll) and its offset. Please refer to motion's documentation for more details.

  • Make sure that the container has a non-static position, like relative, fixed, or absolute to ensure scroll offset is calculated correctly.

Props


PropTypeDefaultDescription
children*React.ReactNode-The content to be displayed and animated
asElementType"span"HTML Tag to render the component as
containerRef*React.RefObject<HTMLElement>-Reference to the container element for scroll tracking
offset[string, string]["0 0", "0 1"]Offset configuration for when the animation should start and end relative to the scroll container
classNamestring-Additional CSS classes for styling the component
springConfig{ stiffness?: number, damping?: number, mass?: number }{ stiffness: 200, damping: 30 }Spring animation configuration for smoothing the scroll-based animation