Text Highlighter
fancy/text-highlighterFreeComponent
A ui component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/text-highlighter.jsonSource
1"use client"23import {4 ElementType,5 forwardRef,6 useEffect,7 useImperativeHandle,8 useMemo,9 useRef,10 useState,11} from "react"12import { motion, Transition, useInView, UseInViewOptions } from "motion/react"1314import { cn } from "@/lib/utils"1516type HighlightDirection = "ltr" | "rtl" | "ttb" | "btt"1718type TextHighlighterProps = {19 /**20 * The text content to be highlighted21 */22 children: React.ReactNode2324 /**25 * HTML element to render as26 * @default "p"27 */28 as?: ElementType2930 /**31 * How to trigger the animation32 * @default "inView"33 */34 triggerType?: "hover" | "ref" | "inView" | "auto"3536 /**37 * Animation transition configuration38 * @default { duration: 0.4, type: "spring", bounce: 0 }39 */40 transition?: Transition4142 /**43 * Options for useInView hook when triggerType is "inView"44 */45 useInViewOptions?: UseInViewOptions4647 /**48 * Class name for the container element49 */50 className?: string5152 /**53 * Highlight color (CSS color string). Also can be a function that returns a color string, eg:54 * @default 'hsl(60, 90%, 68%)' (yellow)55 */56 highlightColor?: string5758 /**59 * Direction of the highlight animation60 * @default "ltr" (left to right)61 */62 direction?: HighlightDirection63} & React.HTMLAttributes<HTMLElement>6465export type TextHighlighterRef = {66 /**67 * Trigger the highlight animation68 * @param direction - Optional direction override for this animation69 */70 animate: (direction?: HighlightDirection) => void7172 /**73 * Reset the highlight animation74 */75 reset: () => void76}7778export const TextHighlighter = forwardRef<79 TextHighlighterRef,80 TextHighlighterProps81>(82 (83 {84 children,85 as = "span",86 triggerType = "inView",87 transition = { type: "spring", duration: 1, delay: 0, bounce: 0 },88 useInViewOptions = {89 once: true,90 initial: false,91 amount: 0.1,92 },93 className,94 highlightColor = "hsl(25, 90%, 80%)",95 direction = "ltr",96 ...props97 },98 ref99 ) => {100 const componentRef = useRef<HTMLDivElement>(null)101 const [isAnimating, setIsAnimating] = useState(false)102 const [isHovered, setIsHovered] = useState(false)103 const [currentDirection, setCurrentDirection] =104 useState<HighlightDirection>(direction)105106 // this allows us to change the direction whenever the direction prop changes107 useEffect(() => {108 setCurrentDirection(direction)109 }, [direction])110111 const isInView =112// … 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 |
