Variable Font Hover by Letter
Variable Font Hover by Letter is a React and Tailwind CSS text component from Fancy Components, licensed MIT. Copy it and paste it into your project.
Fancy Components
MIT
text
What it is
Variable Font Hover by Letter is a React and Tailwind CSS text component from Fancy Components, licensed MIT. Copy it and paste it into your project.
How to use it
Open it in the catalogue, press copy, and paste it into your project. The prompt you copy carries the code and the rules that tell your agent to reproduce it without changing anything.
Five free copies a month. No card.
Needs
npm i lodash motion
Licence
This component comes from Fancy Components and is redistributed under the MIT licence, which permits commercial use. The copyright notice travels with the code.
Variable Font Hover by Letter
"use client"
import { useState } from "react"
import { debounce } from "lodash"
import { AnimationOptions, motion, stagger, useAnimate } from "motion/react"
interface TextProps {
label: string
fromFontVariationSettings: string
toFontVariationSettings: string
transition?: AnimationOptions
staggerDuration?: number
staggerFrom?: "first" | "last" | "center" | number
className?: string
onClick?: () => void
}
const VariableFontHoverByLetter = ({
label,
fromFontVariationSettings = "'wght' 400, 'slnt' 0",
toFontVariationSettings = "'wght' 900, 'slnt' -10",
transition = {
type: "spring",
duration: 0.7,
},
staggerDuration = 0.03,
staggerFrom = "first",
className,
onClick,
...props
}: TextProps) => {
const [scope, animate] = useAnimate()
const [isHovered, setIsHovered] = useState(false)
const mergeTransition = (baseTransition: AnimationOptions) => ({
...baseTransition,
delay: stagger(staggerDuration, {
from: staggerFrom,
}),
})
const hoverStart = debounce(
() => {
if (isHovered) return
setIsHovered(true)
animate(
".letter",
{ fontVariationSettings: toFontVariationSettings },
mergeTransition(transition)
)
},
100,
{ leading: true, trailing: true }
)
const hoverEnd = debounce(
() => {
setIsHovered(false)
animate(
".letter",
{ fontVariationSettings: fromFontVariationSettings },
mergeTransition(transition)
)
},
100,
{ leading: true, trailing: true }
)
return (
<motion.span
className={`${className}`}
onHoverStart={hoverStart}
onHoverEnd={hoverEnd}
onClick={onClick}
ref={scope}
{...props}
>
<span className="sr-only">{label}</span>
{label.split("").map((letter: string, i: number) => {
return (
<motion.span
key={i}
className="inline-block whitespace-pre letter"
aria-hidden="true"
>
{letter}
</motion.span>
)
})}
</motion.span>
)
}
export default VariableFontHoverByLetter