Variable Font Hover By Random Letter
fancy/variable-font-hover-by-random-letterFreeComponent
A ui component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/variable-font-hover-by-random-letter.jsonSource
1"use client"23import { useMemo } from "react"4import { motion, Transition } from "motion/react"56// Function to shuffle an array7function shuffleArray(array: number[]) {8 for (let i = array.length - 1; i > 0; i--) {9 const j = Math.floor(Math.random() * (i + 1))10 ;[array[i], array[j]] = [array[j], array[i]]11 }12}1314interface TextProps {15 label: string16 fromFontVariationSettings: string17 toFontVariationSettings: string18 transition?: Transition19 staggerDuration?: number20 className?: string21 onClick?: () => void22}2324const VariableFontHoverByRandomLetter = ({25 label,26 fromFontVariationSettings = "'wght' 400, 'slnt' 0",27 toFontVariationSettings = "'wght' 900, 'slnt' -10",28 transition = {29 type: "spring",30 duration: 0.7,31 },32 staggerDuration = 0.03,33 className,34 onClick,35 ...props36}: TextProps) => {37 const shuffledIndices = useMemo(() => {38 const indices = Array.from({ length: label.length }, (_, i) => i)39 shuffleArray(indices)40 return indices41 }, [label])4243 const letterVariants = {44 hover: (index: number) => ({45 fontVariationSettings: toFontVariationSettings,46 transition: {47 ...transition,48 delay: staggerDuration * index,49 },50 }),51 initial: (index: number) => ({52 fontVariationSettings: fromFontVariationSettings,53 transition: {54 ...transition,55 delay: staggerDuration * index,56 },57 }),58 }5960 return (61 <motion.span62 className={`${className}`}63 onClick={onClick}64 whileHover="hover"65 initial="initial"66 {...props}67 >68 <span className="sr-only">{label}</span>6970 {label.split("").map((letter: string, i: number) => {71 const index = shuffledIndices[i]72 return (73 <motion.span74 key={i}75 className="inline-block whitespace-pre"76 aria-hidden="true"77 variants={letterVariants}78 custom={index}79 >80 {letter}81 </motion.span>82 )83 })}84 </motion.span>85 )86}8788export default VariableFontHoverByRandomLetter
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 |
