Scroll And Swap Text
fancy/scroll-and-swap-textFreeComponent
A ui component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/scroll-and-swap-text.jsonSource
1"use client"23import React, { ElementType, useMemo, useRef } from "react"4import { motion, useScroll, useTransform, useSpring } from "motion/react"5import { cn } from "@/lib/utils"67// handy function to extract text from children8const extractTextFromChildren = (children: React.ReactNode): string | undefined => {9 // Handle null/undefined10 if (children == null) return ""1112 // Handle string13 if (typeof children === "string") return children1415 // Handle number16 if (typeof children === "number") return String(children)1718 // Handle arrays (including fragments)19 if (Array.isArray(children)) {20 return children.map(extractTextFromChildren).join("")21 }2223 // Handle React elements24 if (React.isValidElement(children)) {25 const props = (children as React.ReactElement).props26 const childText = (props as any).children as React.ReactNode2728 // Recursively extract text from children29 if (childText != null) {30 return extractTextFromChildren(childText)31 }3233 return ""34 }35}3637interface ScrollAndSwapTextProps {38 /**39 * The content to be displayed and animated40 */41 children: React.ReactNode4243 /**44 * HTML Tag to render the component as45 * @default "span"46 */47 as?: ElementType4849 /**50 * Reference to the container element for scroll tracking51 */52 containerRef: React.RefObject<HTMLElement | null>5354 /**55 * Offset configuration for when the animation should start and end relative to the scroll container. Check motion documentation for more details.56 * @default ["0 0", "0 1"]57 */58 offset?: [string, string]5960 /**61 * Additional CSS classes for styling the component62 */63 className?: string6465 /**66 * Spring animation configuration for smoothing the scroll-based animation67 * @default { stiffness: 200, damping: 30 }68 */69 springConfig?: {70 stiffness?: number71 damping?: number72 mass?: number73 }74}7576/**77 * ScrollAndSwapText creates a scroll-triggered text animation where text slides vertically78 * based on scroll progress.79 */80const ScrollAndSwapText = ({81 children,82 as = "span",83 offset = ["0 0", "0 1"],84 className,85 containerRef,86 springConfig = { stiffness: 200, damping: 30 },87 ...props88}: ScrollAndSwapTextProps) => {89 const ref = useRef<HTMLElement>(null)9091 // Convert children to string for processing with error handling92 const text = useMemo(() => {93 try {94 return extractTextFromChildren(children)95 } catch (error) {96 console.error(error)97 return ""98 }99 }, [children])100101// … truncated
What it pulls in
npm packages
Files it writes
More from fancy
All 158 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Gradient With Svganimated-gradient-with-svg | fancy | Components | Free | no deps | |
| Basic Number Tickerbasic-number-ticker | fancy | Components | Free | 1 dep | |
| Box Carouselbox-carousel | fancy | Components | Free | 1 dep | |
| Breathing Textbreathing-text | fancy | Components | Free | 1 dep | |
| Circling Elementscircling-elements | fancy | Components | Free | 1 dep | |
| Css Boxcss-box | fancy | Components | Free | 1 dep | |
| Cursor Attractor And Gravitycursor-attractor-and-gravity | fancy | Components | Free | 5 deps | |
| Drag Elementsdrag-elements | fancy | Components | Free | 1 dep |
