Random Letter Swap Forward Anim
fancy/random-letter-swap-forward-animFreeComponent
A ui component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/random-letter-swap-forward-anim.jsonSource
1"use client"23import { useState } from "react"4import { debounce } from "lodash"5import { AnimationOptions, motion, useAnimate } from "motion/react"67interface TextProps {8 label: string9 reverse?: boolean10 transition?: AnimationOptions11 staggerDuration?: number12 className?: string13 onClick?: () => void14}1516const RandomLetterSwapForward = ({17 label,18 reverse = true,19 transition = {20 type: "spring",21 duration: 0.8,22 },23 staggerDuration = 0.02,24 className,25 onClick,26 ...props27}: TextProps) => {28 const [scope, animate] = useAnimate()29 const [blocked, setBlocked] = useState(false)3031 const mergeTransition = (transition: AnimationOptions, i: number) => ({32 ...transition,33 delay: i * staggerDuration,34 })3536 const shuffledIndices = Array.from(37 { length: label.length },38 (_, i) => i39 ).sort(() => Math.random() - 0.5)4041 const hoverStart = debounce(42 () => {43 if (blocked) return44 setBlocked(true)4546 for (let i = 0; i < label.length; i++) {47 const randomIndex = shuffledIndices[i]48 animate(49 ".letter-" + randomIndex,50 {51 y: reverse ? "100%" : "-100%",52 },53 mergeTransition(transition, i)54 ).then(() => {55 animate(56 ".letter-" + randomIndex,57 {58 y: 0,59 },60 {61 duration: 0,62 }63 )64 })6566 animate(67 ".letter-secondary-" + randomIndex,68 {69 top: "0%",70 },71 mergeTransition(transition, i)72 )73 .then(() => {74 animate(75 ".letter-secondary-" + randomIndex,76 {77 top: reverse ? "-100%" : "100%",78 },79 {80 duration: 0,81 }82 )83 })84 .then(() => {85 if (i === label.length - 1) {86 setBlocked(false)87 }88 })89 }90 },91 100,92 { leading: true, trailing: true }93 )9495 return (96 <motion.span97 className={`flex justify-center items-center relative overflow-hidden ${className}`}98 onHoverStart={hoverStart}99 onClick={onClick}100 ref={scope}101 {...props}102 >103 <span className="sr-only">{label}</span>104105 {label.split("").map((letter: string, i: number) => {106 return (107 <span108 className="whitespace-pre relative flex"109 key={i}110 aria-hidden={true}111// … 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 |
