Underline To Background
fancy/underline-to-backgroundFreeComponent
A ui component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/underline-to-background.jsonSource
1"use client"23import { ElementType, useEffect, useMemo, useRef } from "react"4import { motion, ValueAnimationTransition } from "motion/react"56import { cn } from "@/lib/utils"78interface UnderlineProps {9 /**10 * The content to be displayed and animated11 */12 children: React.ReactNode1314 /**15 * HTML Tag to render the component as16 * @default span17 */18 as?: ElementType1920 /**21 * Optional class name for styling22 */23 className?: string2425 /**26 * Animation transition configuration27 * @default { type: "spring", damping: 30, stiffness: 300 }28 */29 transition?: ValueAnimationTransition3031 /**32 * The color that the text will animate to on hover33 */34 targetTextColor: string3536 /**37 * Height of the underline as a ratio of font size38 * @default 0.139 */40 underlineHeightRatio?: number4142 /**43 * Padding of the underline as a ratio of font size44 * @default 0.0145 */46 underlinePaddingRatio?: number47}4849const UnderlineToBackground = ({50 children,51 as,52 className,53 transition = { type: "spring", damping: 30, stiffness: 300 },54 underlineHeightRatio = 0.1, // Default to 10% of font size55 underlinePaddingRatio = 0.01, // Default to 1% of font size56 targetTextColor = "#fef",57 ...props58}: UnderlineProps) => {59 const textRef = useRef<HTMLSpanElement>(null)6061 // Create custom motion component based on the 'as' prop62 const MotionComponent = useMemo(() => motion.create(as ?? "span"), [as])6364 // Update CSS custom properties based on font size65 useEffect(() => {66 const updateUnderlineStyles = () => {67 if (textRef.current) {68 const fontSize = parseFloat(getComputedStyle(textRef.current).fontSize)69 const underlineHeight = fontSize * underlineHeightRatio70 const underlinePadding = fontSize * underlinePaddingRatio71 textRef.current.style.setProperty(72 "--underline-height",73 `${underlineHeight}px`74 )75 textRef.current.style.setProperty(76 "--underline-padding",77 `${underlinePadding}px`78 )79 }80 }8182 updateUnderlineStyles()83 window.addEventListener("resize", updateUnderlineStyles)8485 return () => window.removeEventListener("resize", updateUnderlineStyles)86 }, [underlineHeightRatio, underlinePaddingRatio])8788 // Animation variants for the underline background89 const underlineVariants = {90 initial: {91 height: "var(--underline-height)",92 },93 target: {94 height: "100%",95 transition: transition,96 },97 }9899// … 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 |
