Text Cursor Proximity
fancy/text-cursor-proximityFreeComponent
A ui component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/text-cursor-proximity.jsonSource
1"use client"23import React, { CSSProperties, ElementType, forwardRef, useRef, useMemo } from "react"4import {5 motion,6 useAnimationFrame,7 useMotionValue,8 useTransform,9} from "motion/react"1011import { useMousePositionRef } from "@/hooks/use-mouse-position-ref"12import { cn } from "@/lib/utils"1314// Helper type that makes all properties of CSSProperties accept number | string15type CSSPropertiesWithValues = {16 [K in keyof CSSProperties]: string | number17}1819interface StyleValue<T extends keyof CSSPropertiesWithValues> {20 from: CSSPropertiesWithValues[T]21 to: CSSPropertiesWithValues[T]22}2324interface TextProps extends React.HTMLAttributes<HTMLSpanElement> {25 /**26 * The content to be displayed and animated27 */28 children: React.ReactNode2930 /**31 * HTML Tag to render the component as32 * @default span33 */34 as?: ElementType3536 /**37 * Object containing style properties to animate38 * Each property should have 'from' and 'to' values39 */40 styles: Partial<{41 [K in keyof CSSPropertiesWithValues]: StyleValue<K>42 }>4344 /**45 * Reference to the container element for mouse position calculations46 */47 containerRef: React.RefObject<HTMLDivElement | null>4849 /**50 * Radius of the proximity effect in pixels51 * @default 5052 */53 radius?: number5455 /**56 * Type of falloff function to use for the proximity effect57 * @default "linear"58 */59 falloff?: "linear" | "exponential" | "gaussian"60}6162const TextCursorProximity = forwardRef<HTMLSpanElement, TextProps>(63 (64 {65 children,66 as,67 styles,68 containerRef,69 radius = 50,70 falloff = "linear",71 className,72 ...props73 },74 ref75 ) => {76 const MotionComponent = useMemo(() => motion.create(as ?? "span"), [as])77 const letterRefs = useRef<(HTMLSpanElement | null)[]>([])78 const mousePositionRef = useMousePositionRef(containerRef)7980 // Convert children to string for letter processing81 const text = React.Children.toArray(children).join("")8283 // Create a motion value for each letter's proximity84 const letterProximities = useRef(85 Array(text.replace(/\s/g, "").length)86 .fill(0)87 .map(() => useMotionValue(0))88 )8990 const calculateDistance = (91 x1: number,92 y1: number,93 x2: number,94 y2: number95 ): number => {96 return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))97 }9899 const calculateFalloff = (distance: number): number => {100// … truncated
What it pulls in
npm packages
Other registry items
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 |
