Underline Center
fancy/underline-centerFreeComponent
A ui component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/underline-center.jsonSource
1"use client"23import { ElementType, useEffect, useRef, useMemo } from "react"4import { motion, ValueAnimationTransition } from "motion/react"5import { cn } from "@/lib/utils"67interface UnderlineProps {8 /**9 * The content to be displayed and animated10 */11 children: React.ReactNode1213 /**14 * HTML Tag to render the component as15 * @default span16 */17 as?: ElementType1819 /**20 * Optional class name for styling21 */22 className?: string2324 /**25 * Animation transition configuration26 * @default { duration: 0.25, ease: "easeInOut" }27 */28 transition?: ValueAnimationTransition2930 /**31 * Height of the underline as a ratio of font size32 * @default 0.133 */34 underlineHeightRatio?: number3536 /**37 * Padding of the underline as a ratio of font size38 * @default 0.0139 */40 underlinePaddingRatio?: number41}4243const CenterUnderline = ({44 children,45 as,46 className,47 transition = { duration: 0.25, ease: "easeInOut" },48 underlineHeightRatio = 0.1,49 underlinePaddingRatio = 0.01,50 ...props51}: UnderlineProps) => {52 const textRef = useRef<HTMLSpanElement>(null)53 const MotionComponent = useMemo(() => motion.create(as ?? "span"), [as])5455 useEffect(() => {56 const updateUnderlineStyles = () => {57 if (textRef.current) {58 const fontSize = parseFloat(getComputedStyle(textRef.current).fontSize)59 const underlineHeight = fontSize * underlineHeightRatio60 const underlinePadding = fontSize * underlinePaddingRatio61 textRef.current.style.setProperty(62 "--underline-height",63 `${underlineHeight}px`64 )65 textRef.current.style.setProperty(66 "--underline-padding",67 `${underlinePadding}px`68 )69 }70 }7172 updateUnderlineStyles()73 window.addEventListener("resize", updateUnderlineStyles)7475 return () => window.removeEventListener("resize", updateUnderlineStyles)76 }, [underlineHeightRatio, underlinePaddingRatio])7778 const underlineVariants = {79 hidden: {80 width: 0,81 originX: 0.5,82 },83 visible: {84 width: "100%",85 transition: transition,86 },87 }8889 return (90 <MotionComponent91 className={cn("relative inline-block cursor-pointer", className)}92 whileHover="visible"93 ref={textRef}94 {...props}95 >96 <span>{children}</span>97 <motion.div98 className="absolute left-1/2 bg-current -translate-x-1/2"99 style={{100 height: "var(--underline-height)",101// … 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 |
