{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"text-reveal-card","type":"registry:ui","title":"TextRevealCard","description":"Card that reveals text on horizontal mouse drag with animated star particles","author":"fancy-ui <https://fancy-ui.rama.app>","categories":["cards"],"registryDependencies":["https://fancy-ui.rama.app/r/utils.json"],"files":[{"path":"react/src/components/text-reveal-card/TextRevealCard.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState, type MouseEvent, type ReactNode } from \"react\";\nimport { cn } from \"../../utils\";\nimport { TextRevealStars } from \"./TextRevealStars\";\n\n/**\n * TextRevealCard - A card whose hidden text is wiped into view by the\n * pointer. A clipped reveal layer and a tilted reveal line follow the\n * pointer's horizontal position; leaving the card eases everything back.\n */\nexport interface TextRevealCardProps {\n\tclassName?: string;\n\t/** Number of stars rendered behind the hidden text */\n\tstarsCount?: number;\n\t/** Additional CSS classes applied to each star */\n\tstarsClass?: string;\n\t/** Seed forwarded to the star field (see TextRevealStars) */\n\tstarsSeed?: number;\n\tchildren?: ReactNode;\n\t/** Content of the reveal layer (shown where the pointer has wiped) */\n\ttext?: ReactNode;\n\t/** Content of the background layer (visible before the wipe) */\n\trevealText?: ReactNode;\n}\n\nexport function TextRevealCard({\n\tclassName = \"\",\n\tstarsCount = 130,\n\tstarsClass = \"\",\n\tstarsSeed,\n\tchildren,\n\ttext,\n\trevealText,\n}: TextRevealCardProps) {\n\tconst cardRef = useRef<HTMLDivElement | null>(null);\n\tconst [widthPercentage, setWidthPercentage] = useState(0);\n\tconst [isMouseOver, setIsMouseOver] = useState(false);\n\t// Mirror for the leave-handler's timeout: its closure must read the\n\t// CURRENT hover state 100ms later, not the render it was created in.\n\tconst isMouseOverRef = useRef(false);\n\n\tconst rotateDeg = (widthPercentage - 50) * 0.1;\n\n\tfunction mouseMoveHandler(event: MouseEvent) {\n\t\tevent.preventDefault();\n\t\tconst card = cardRef.current;\n\t\tif (card) {\n\t\t\tconst rect = card.getBoundingClientRect();\n\t\t\tconst relativeX = event.clientX - rect.left;\n\t\t\tsetWidthPercentage((relativeX / rect.width) * 100);\n\t\t}\n\t}\n\n\tfunction mouseLeaveHandler() {\n\t\tisMouseOverRef.current = false;\n\t\tsetIsMouseOver(false);\n\t\tsetTimeout(() => {\n\t\t\tif (!isMouseOverRef.current) {\n\t\t\t\tsetWidthPercentage(0);\n\t\t\t}\n\t\t}, 100);\n\t}\n\n\tfunction mouseEnterHandler() {\n\t\tisMouseOverRef.current = true;\n\t\tsetIsMouseOver(true);\n\t}\n\n\t// touchmove is attached natively and passive: the card only tracks the\n\t// finger, it never cancels the gesture, so the page keeps scrolling\n\t// vertically (`touch-pan-y`).\n\tuseEffect(() => {\n\t\tconst card = cardRef.current;\n\t\tif (!card) return;\n\t\tconst touchMoveHandler = (event: TouchEvent) => {\n\t\t\tconst rect = card.getBoundingClientRect();\n\t\t\tconst relativeX = event.touches[0]!.clientX - rect.left;\n\t\t\tsetWidthPercentage((relativeX / rect.width) * 100);\n\t\t};\n\t\tcard.addEventListener(\"touchmove\", touchMoveHandler, { passive: true });\n\t\treturn () => card.removeEventListener(\"touchmove\", touchMoveHandler);\n\t}, []);\n\n\treturn (\n\t\t<div\n\t\t\tref={cardRef}\n\t\t\tclassName={cn(\n\t\t\t\t\"relative w-full max-w-[40rem] touch-pan-y touch-pinch-zoom overflow-hidden rounded-lg border border-white/[0.08] bg-[#1d1c20] p-4 sm:p-6 md:p-8\",\n\t\t\t\tclassName\n\t\t\t)}\n\t\t\trole=\"presentation\"\n\t\t\tonMouseEnter={mouseEnterHandler}\n\t\t\tonMouseLeave={mouseLeaveHandler}\n\t\t\tonMouseMove={mouseMoveHandler}\n\t\t\tonTouchStart={mouseEnterHandler}\n\t\t\tonTouchEnd={mouseLeaveHandler}\n\t\t>\n\t\t\t{children}\n\n\t\t\t<div className=\"relative flex h-40 items-center overflow-hidden\">\n\t\t\t\t{/* Reveal layer */}\n\t\t\t\t<div\n\t\t\t\t\tstyle={{\n\t\t\t\t\t\twidth: \"100%\",\n\t\t\t\t\t\topacity: widthPercentage > 0 ? 1 : 0,\n\t\t\t\t\t\tclipPath: `inset(0 ${100 - widthPercentage}% 0 0)`,\n\t\t\t\t\t\ttransition: isMouseOver ? \"none\" : \"all 0.4s ease-out\",\n\t\t\t\t\t}}\n\t\t\t\t\tclassName=\"absolute z-20 bg-[#1d1c20] will-change-transform\"\n\t\t\t\t>\n\t\t\t\t\t{text}\n\t\t\t\t</div>\n\n\t\t\t\t{/* Reveal line */}\n\t\t\t\t<div\n\t\t\t\t\tstyle={{\n\t\t\t\t\t\tleft: `${widthPercentage}%`,\n\t\t\t\t\t\ttransform: `rotate(${rotateDeg}deg)`,\n\t\t\t\t\t\topacity: widthPercentage > 0 ? 1 : 0,\n\t\t\t\t\t\ttransition: isMouseOver ? \"none\" : \"all 0.4s ease-out\",\n\t\t\t\t\t}}\n\t\t\t\t\tclassName=\"absolute z-50 h-40 w-[8px] bg-gradient-to-b from-transparent via-neutral-800 to-transparent will-change-transform\"\n\t\t\t\t></div>\n\n\t\t\t\t{/* Background text + stars */}\n\t\t\t\t<div className=\"overflow-hidden [mask-image:linear-gradient(to_bottom,transparent,white,transparent)]\">\n\t\t\t\t\t{revealText}\n\t\t\t\t\t<TextRevealStars starsCount={starsCount} className={starsClass} seed={starsSeed} />\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</div>\n\t);\n}\n","type":"registry:ui","target":"@ui/fancy/components/text-reveal-card/TextRevealCard.tsx"},{"path":"react/src/components/text-reveal-card/TextRevealStars.tsx","content":"\"use client\";\n\nimport { memo, useMemo, type CSSProperties } from \"react\";\nimport { cn } from \"../../utils\";\nimport \"./text-reveal-stars.css\";\n\n/**\n * TextRevealStars - Drifting star field rendered behind the hidden text of\n * TextRevealCard. Each star fades in, scales up and drifts toward a random\n * target position on an infinite loop.\n */\nexport interface TextRevealStarsProps {\n\t/** Number of stars to render */\n\tstarsCount?: number;\n\t/** Additional CSS classes applied to each star */\n\tclassName?: string;\n\t/**\n\t * Seed for the star layout. The same seed always produces the same field,\n\t * which is what keeps a server render and its hydration identical. Change\n\t * it to give two fields on one page different layouts — the default is\n\t * shared, so two `<TextRevealStars />` with no seed match.\n\t */\n\tseed?: number;\n}\n\ninterface StarData {\n\ttop: string;\n\tleft: string;\n\ttargetTop: string;\n\ttargetLeft: string;\n\topacity: number;\n\tduration: number;\n}\n\n/**\n * mulberry32 — a tiny deterministic PRNG. `Math.random()` cannot be used\n * here: the layout is computed in the render path, which runs once on the\n * server and again during client hydration, and the two would disagree on\n * every value — a hydration mismatch React may resolve by keeping the SERVER\n * attributes. A seed keeps both renders identical AND keeps the stars in the\n * server markup.\n */\nfunction mulberry32(seed: number): () => number {\n\tlet state = seed >>> 0;\n\treturn () => {\n\t\tstate = (state + 0x6d2b79f5) >>> 0;\n\t\tlet t = state;\n\t\tt = Math.imul(t ^ (t >>> 15), t | 1);\n\t\tt ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n\t\treturn ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n\t};\n}\n\nfunction createStars(starsCount: number, seed: number): StarData[] {\n\tconst random = mulberry32(seed);\n\tconst randomMove = () => random() * 4 - 2;\n\treturn Array.from({ length: starsCount }, () => ({\n\t\ttop: `calc(${random() * 100}% + ${randomMove()}px)`,\n\t\tleft: `calc(${random() * 100}% + ${randomMove()}px)`,\n\t\ttargetTop: `calc(${random() * 100}% + ${randomMove()}px)`,\n\t\ttargetLeft: `calc(${random() * 100}% + ${randomMove()}px)`,\n\t\topacity: random(),\n\t\tduration: random() * 10 + 20,\n\t}));\n}\n\n/**\n * Memoised so a pointer scrub over TextRevealCard — which re-renders the card on\n * every mouse/touch move — leaves the star field alone. Without it the whole\n * field (130 spans by default) is re-created and diffed on every pointer event;\n * the star markup only ever depends on `starsCount`, `className` and `seed`.\n */\nexport const TextRevealStars = memo(function TextRevealStars({\n\tstarsCount = 130,\n\tclassName,\n\tseed = 1,\n}: TextRevealStarsProps) {\n\t// Recomputed only when `starsCount` or `seed` changes — the same dependency\n\t// the Svelte source's `$derived` tracks. A plain render-time call would\n\t// reshuffle the field on every unrelated re-render.\n\tconst stars = useMemo(() => createStars(starsCount, seed), [starsCount, seed]);\n\n\treturn (\n\t\t// `text-reveal-stars` is port-added: the CSS anchor class for the\n\t\t// compiler-scoped `.star-animate` rule (see text-reveal-stars.css).\n\t\t<div className=\"text-reveal-stars absolute inset-0\">\n\t\t\t{stars.map((star, i) => (\n\t\t\t\t<span\n\t\t\t\t\tkey={i}\n\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\"star-animate absolute z-[1] inline-block h-0.5 w-0.5 rounded-full bg-white\",\n\t\t\t\t\t\tclassName\n\t\t\t\t\t)}\n\t\t\t\t\tstyle={\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttop: star.top,\n\t\t\t\t\t\t\tleft: star.left,\n\t\t\t\t\t\t\t\"--target-top\": star.targetTop,\n\t\t\t\t\t\t\t\"--target-left\": star.targetLeft,\n\t\t\t\t\t\t\t\"--star-opacity\": star.opacity,\n\t\t\t\t\t\t\tanimationDuration: `${star.duration}s`,\n\t\t\t\t\t\t} as CSSProperties\n\t\t\t\t\t}\n\t\t\t\t></span>\n\t\t\t))}\n\t\t</div>\n\t);\n});\n","type":"registry:ui","target":"@ui/fancy/components/text-reveal-card/TextRevealStars.tsx"},{"path":"react/src/components/text-reveal-card/index.ts","content":"\"use client\";\n\nexport { TextRevealCard } from \"./TextRevealCard\";\nexport type { TextRevealCardProps } from \"./TextRevealCard\";\nexport { TextRevealStars } from \"./TextRevealStars\";\nexport type { TextRevealStarsProps } from \"./TextRevealStars\";\n","type":"registry:ui","target":"@ui/fancy/components/text-reveal-card/index.ts"},{"path":"react/src/components/text-reveal-card/text-reveal-stars.css","content":"/* The keyframes were declared `:global` in the Svelte source; the emitted\n   name `star-drift` is the visual contract and is kept identical. */\n@keyframes star-drift {\n\t0% {\n\t\topacity: 0;\n\t\ttransform: scale(1);\n\t}\n\t50% {\n\t\topacity: var(--star-opacity, 0.5);\n\t\ttransform: scale(1.2);\n\t}\n\t100% {\n\t\topacity: 0;\n\t\ttransform: scale(0);\n\t\ttop: var(--target-top);\n\t\tleft: var(--target-left);\n\t}\n}\n\n/* `.text-reveal-stars` is a port-added anchor: the Svelte compiler scoped\n   this rule and the source has no root class to anchor on, so the slug class\n   keeps the generic `.star-animate` name from leaking globally. */\n.text-reveal-stars .star-animate {\n\tanimation: star-drift linear infinite;\n}\n","type":"registry:ui","target":"@ui/fancy/components/text-reveal-card/text-reveal-stars.css"}],"meta":{"license":"MIT","package":"fancy-ui-react","version":"0.7.0","source":"https://fancy-ui.rama.app/docs/components/text-reveal-card?framework=react"}}