{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "word-reveal",
  "title": "Word Reveal",
  "description": "Staggered word or character reveal text, driven by scroll progress, viewport entry, or a manual trigger, 4 animation presets, 3 stagger patterns, and word highlighting.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/word-reveal/word-reveal.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\ntype Token = { text: string; isSpace: boolean; index: number };\nexport type WordRevealStyle = \"words\" | \"chars\";\nexport type WordRevealAnimPreset = \"fade-up\" | \"blur-in\" | \"fade-only\" | \"spring-up\";\nexport type WordRevealTriggerMode = \"scroll\" | \"inview\" | \"manual\";\nexport type WordRevealAlign = \"left\" | \"center\" | \"right\";\nexport type WordRevealStaggerMode = \"linear\" | \"center-out\" | \"random\";\n\nfunction tokenize(text: string, mode: WordRevealStyle): Token[][] {\n  const paragraphs = text.split(\"\\n\").filter((p) => p.trim().length > 0);\n\n  return paragraphs.map((para) => {\n    if (mode === \"chars\") {\n      return para.split(\"\").map((ch, i) => ({ text: ch, isSpace: ch === \" \", index: i }));\n    }\n    const words = para.split(/(\\s+)/);\n    let idx = 0;\n    return words.map((w) => {\n      const isSpace = /^\\s+$/.test(w);\n      const token: Token = { text: w, isSpace, index: idx };\n      if (!isSpace) idx++;\n      return token;\n    });\n  });\n}\n\n// Deterministic pseudo-random in [0, 1) — a pure function of the index, so\n// \"random\" stagger order is stable across re-renders without needing\n// Math.random() (which isn't allowed during render) or cached ref state.\nfunction pseudoRandom(seed: number): number {\n  const x = Math.sin(seed * 12.9898) * 43758.5453;\n  return x - Math.floor(x);\n}\n\nfunction getDelay(tokenIndex: number, totalTokens: number, stagger: number, staggerMode: WordRevealStaggerMode): number {\n  if (staggerMode === \"center-out\") {\n    const center = (totalTokens - 1) / 2;\n    const dist = Math.abs(tokenIndex - center);\n    return dist * stagger * 0.001;\n  }\n  if (staggerMode === \"random\") {\n    return pseudoRandom(tokenIndex) * totalTokens * stagger * 0.001;\n  }\n  return tokenIndex * stagger * 0.001;\n}\n\nconst EASINGS: Record<WordRevealAnimPreset, string> = {\n  \"fade-up\": \"cubic-bezier(0.16, 1, 0.3, 1)\",\n  \"blur-in\": \"cubic-bezier(0.25, 0.46, 0.45, 0.94)\",\n  \"fade-only\": \"cubic-bezier(0.4, 0, 0.2, 1)\",\n  \"spring-up\": \"cubic-bezier(0.34, 1.56, 0.64, 1)\",\n};\n\nconst DURATIONS: Record<WordRevealAnimPreset, number> = {\n  \"fade-up\": 0.7,\n  \"blur-in\": 0.6,\n  \"fade-only\": 0.5,\n  \"spring-up\": 0.65,\n};\n\nfunction getHiddenStyle(preset: WordRevealAnimPreset, translateY: number): React.CSSProperties {\n  const base: React.CSSProperties = { opacity: 0 };\n  if (preset === \"fade-up\" || preset === \"spring-up\") {\n    base.transform = `translateY(${translateY}px)`;\n  }\n  if (preset === \"blur-in\") {\n    base.filter = \"blur(12px)\";\n    base.transform = `translateY(${translateY * 0.4}px)`;\n  }\n  return base;\n}\n\nfunction getVisibleStyle(preset: WordRevealAnimPreset): React.CSSProperties {\n  const base: React.CSSProperties = { opacity: 1 };\n  if (preset === \"fade-up\" || preset === \"spring-up\") {\n    base.transform = \"translateY(0px)\";\n  }\n  if (preset === \"blur-in\") {\n    base.filter = \"blur(0px)\";\n    base.transform = \"translateY(0px)\";\n  }\n  return base;\n}\n\ninterface TokenSpanProps {\n  token: Token;\n  visible: boolean;\n  delay: number;\n  preset: WordRevealAnimPreset;\n  translateY: number;\n  duration: number;\n  color: string;\n  highlightWords: string[];\n  highlightColor: string;\n  easing: string;\n}\n\nfunction TokenSpan({ token, visible, delay, preset, translateY, duration, color, highlightWords, highlightColor, easing }: TokenSpanProps) {\n  const isHighlighted = highlightWords.some((w) => w.trim().toLowerCase() === token.text.trim().toLowerCase());\n\n  const style: React.CSSProperties = {\n    display: \"inline-block\",\n    willChange: \"transform, opacity, filter\",\n    transition: visible ? `opacity ${duration}s ${easing} ${delay}s, transform ${duration}s ${easing} ${delay}s, filter ${duration}s ${easing} ${delay}s` : \"none\",\n    color: isHighlighted ? highlightColor : color,\n    ...(visible ? getVisibleStyle(preset) : getHiddenStyle(preset, translateY)),\n  };\n\n  return <span style={style}>{token.text}</span>;\n}\n\nexport interface WordRevealProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\" | \"content\" | \"translate\"> {\n  text?: string;\n  revealStyle?: WordRevealStyle;\n  animPreset?: WordRevealAnimPreset;\n  triggerMode?: WordRevealTriggerMode;\n  threshold?: number;\n  scrollStart?: number;\n  scrollEnd?: number;\n  stagger?: number;\n  staggerMode?: WordRevealStaggerMode;\n  translateY?: number;\n  durationOverride?: number;\n  fontSize?: number;\n  fontWeight?: string;\n  fontFamily?: string;\n  lineHeight?: number;\n  letterSpacing?: number;\n  color?: string;\n  align?: WordRevealAlign;\n  highlightWords?: string;\n  highlightColor?: string;\n  backgroundColor?: string;\n  padding?: number;\n  manualTrigger?: boolean;\n}\n\nexport function WordReveal({\n  text = \"Scroll to reveal each word\\none at a time.\",\n  revealStyle = \"words\",\n  animPreset = \"blur-in\",\n  triggerMode = \"scroll\",\n  threshold = 0.15,\n  scrollStart = 0,\n  scrollEnd = 100,\n  stagger = 60,\n  staggerMode = \"linear\",\n  translateY = 24,\n  durationOverride = 0,\n  fontSize = 48,\n  fontWeight = \"600\",\n  fontFamily = \"Inter, sans-serif\",\n  lineHeight = 1.2,\n  letterSpacing = -0.5,\n  color = \"#ffffff\",\n  align = \"center\",\n  highlightWords = \"\",\n  highlightColor = \"#87FFE3\",\n  backgroundColor = \"transparent\",\n  padding = 0,\n  manualTrigger = false,\n  className,\n  style,\n  ...props\n}: WordRevealProps) {\n  const containerRef = React.useRef<HTMLDivElement>(null);\n  const [visibleCount, setVisibleCount] = React.useState(0);\n  const [hasTriggered, setHasTriggered] = React.useState(false);\n\n  const paragraphs = tokenize(text, revealStyle);\n  const allTokens = paragraphs.flat();\n  const meaningfulTokens = allTokens.filter((t) => !t.isSpace);\n  const totalMeaningful = meaningfulTokens.length;\n\n  const duration = durationOverride > 0 ? durationOverride : DURATIONS[animPreset];\n  const easing = EASINGS[animPreset];\n  const parsedHighlights = highlightWords\n    .split(\",\")\n    .map((w) => w.trim())\n    .filter(Boolean);\n\n  const revealAll = React.useCallback(() => {\n    setVisibleCount(totalMeaningful);\n    setHasTriggered(true);\n  }, [totalMeaningful]);\n\n  React.useEffect(() => {\n    if (triggerMode !== \"inview\") return;\n    const el = containerRef.current;\n    if (!el) return;\n\n    const observer = new IntersectionObserver(\n      ([entry]) => {\n        if (entry.isIntersecting && !hasTriggered) {\n          revealAll();\n        }\n      },\n      { threshold: Math.max(0, Math.min(1, threshold)) }\n    );\n\n    observer.observe(el);\n    return () => observer.disconnect();\n  }, [triggerMode, threshold, hasTriggered, revealAll]);\n\n  React.useEffect(() => {\n    if (triggerMode !== \"scroll\") return;\n    const el = containerRef.current;\n    if (!el) return;\n\n    const handleScroll = () => {\n      const rect = el.getBoundingClientRect();\n      const vh = window.innerHeight;\n      const startPx = vh * (1 - scrollStart / 100);\n      const endPx = vh * (1 - scrollEnd / 100);\n      const progress = Math.max(0, Math.min(1, (startPx - rect.top) / (startPx - endPx)));\n      const newCount = Math.round(progress * totalMeaningful);\n      setVisibleCount(newCount);\n    };\n\n    window.addEventListener(\"scroll\", handleScroll, { passive: true });\n    handleScroll();\n    return () => window.removeEventListener(\"scroll\", handleScroll);\n  }, [triggerMode, scrollStart, scrollEnd, totalMeaningful]);\n\n  const effectiveVisibleCount = triggerMode === \"manual\" ? (manualTrigger ? totalMeaningful : 0) : visibleCount;\n\n  let meaningfulIdx = 0;\n\n  return (\n    <div\n      ref={containerRef}\n      className={cn(\"select-none\", className)}\n      style={{ backgroundColor, padding, textAlign: align, fontFamily, fontSize, fontWeight, lineHeight, letterSpacing, ...style }}\n      {...props}\n    >\n      {paragraphs.map((tokens, pIdx) => (\n        <div key={pIdx} className=\"block\" style={{ marginBottom: pIdx < paragraphs.length - 1 ? `${fontSize * 0.5}px` : 0 }}>\n          {tokens.map((token, tIdx) => {\n            const isMeaningful = !token.isSpace;\n            const myIdx = isMeaningful ? meaningfulIdx++ : -1;\n            const visible = isMeaningful ? myIdx < effectiveVisibleCount : false;\n            const delay = isMeaningful ? getDelay(myIdx, totalMeaningful, stagger, staggerMode) : 0;\n\n            if (token.isSpace) {\n              return <span key={tIdx} className=\"inline-block\" style={{ width: \"0.28em\" }} />;\n            }\n\n            return (\n              <TokenSpan\n                key={tIdx}\n                token={token}\n                visible={visible}\n                delay={delay}\n                preset={animPreset}\n                translateY={translateY}\n                duration={duration}\n                color={color}\n                highlightWords={parsedHighlights}\n                highlightColor={highlightColor}\n                easing={easing}\n              />\n            );\n          })}\n        </div>\n      ))}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}