{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dissolve-text",
  "title": "Dissolve Text",
  "description": "Words or characters surfacing at scattered moments, the same on server and client.",
  "registryDependencies": [
    "@corsair-ui/utils",
    "@corsair-ui/use-entrance"
  ],
  "files": [
    {
      "path": "registry/default/ui/dissolve-text.tsx",
      "content": "\"use client\";\n\nimport { Fragment, useMemo, type ComponentProps, type CSSProperties, type Ref } from \"react\";\n\nimport { useEntrance, type EntranceTrigger } from \"@/registry/default/hooks/use-entrance\";\nimport { cn } from \"@/registry/default/lib/utils\";\n\nfunction mergeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n  return (node: T | null) => {\n    for (const ref of refs) {\n      if (typeof ref === \"function\") ref(node);\n      else if (ref) ref.current = node;\n    }\n  };\n}\n\n/** Splits into user-perceived characters, so emoji and accents stay whole. */\nfunction graphemes(text: string) {\n  if (typeof Intl !== \"undefined\" && \"Segmenter\" in Intl) {\n    const segmenter = new Intl.Segmenter(undefined, { granularity: \"grapheme\" });\n    return Array.from(segmenter.segment(text), ({ segment }) => segment);\n  }\n  return Array.from(text);\n}\n\n/**\n * A small seeded generator (mulberry32). The same text always gets the same\n * timings, so the server and the browser agree and hydration stays clean.\n */\nfunction seededRandom(text: string) {\n  let seed = 2166136261;\n  for (let index = 0; index < text.length; index++) {\n    seed = Math.imul(seed ^ text.charCodeAt(index), 16777619);\n  }\n  return () => {\n    seed = (seed + 0x6d2b79f5) | 0;\n    let value = Math.imul(seed ^ (seed >>> 15), 1 | seed);\n    value = (value + Math.imul(value ^ (value >>> 7), 61 | value)) ^ value;\n    return ((value ^ (value >>> 14)) >>> 0) / 4294967296;\n  };\n}\n\ntype DissolveTextTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\" | \"p\" | \"span\" | \"div\";\n\ninterface DissolveTextProps extends Omit<ComponentProps<\"p\">, \"children\"> {\n  /** Plain text. */\n  children: string;\n  as?: DissolveTextTag;\n  /** Whether words or single characters fade in on their own. */\n  split?: \"words\" | \"characters\";\n  /** \"load\" plays on first paint with CSS alone; \"in-view\" when the text scrolls into view. */\n  trigger?: EntranceTrigger;\n  /** In-view: play the first time only. */\n  once?: boolean;\n  /** Takes over from `trigger`: `false` holds the text hidden, `true` plays it. */\n  play?: boolean;\n  /** Wait before anything appears, in ms. */\n  delay?: number;\n  /** The window, in ms, over which pieces pick their random start. */\n  spread?: number;\n  /** How long each piece takes to appear, in ms. */\n  duration?: number;\n}\n\n/**\n * Text that dissolves in: words (or characters) surface at scattered\n * moments inside a short window, like a chart coming out of the fog. The\n * scatter is seeded by the text, so it is the same on every render and the\n * server agrees with the browser. Screen readers get the text in one\n * piece, and with `prefers-reduced-motion` it is shown as is.\n *\n * @example\n * <DissolveText as=\"p\" split=\"characters\">Fog lifting over the harbour.</DissolveText>\n */\nfunction DissolveText({\n  children,\n  as: Tag = \"p\",\n  split = \"words\",\n  trigger = \"load\",\n  once = true,\n  play,\n  delay = 200,\n  spread = 230,\n  duration = 1000,\n  className,\n  ref,\n  ...props\n}: DissolveTextProps) {\n  const [observe, phase] = useEntrance<HTMLElement>({ trigger, once, play });\n  const mergedRef = useMemo(() => mergeRefs(ref, observe), [ref, observe]);\n\n  const words = useMemo(() => {\n    const random = seededRandom(`${split}:${children}`);\n    return children\n      .trim()\n      .split(/\\s+/)\n      .filter(Boolean)\n      .map((word) =>\n        (split === \"characters\" ? graphemes(word) : [word]).map((text) => ({\n          text,\n          // Two draws, so most pieces bunch in the middle of the window.\n          wait: (random() * 0.87 + random() * 0.13) * spread,\n        }))\n      );\n  }, [children, split, spread]);\n\n  const pieceStyle = (wait: number): CSSProperties =>\n    phase === \"static\"\n      ? {}\n      : {\n          animationDelay: `${Math.round(delay + wait)}ms`,\n          animationDuration: `${duration}ms`,\n          animationPlayState: phase === \"armed\" ? \"paused\" : undefined,\n        };\n\n  return (\n    <Tag\n      ref={mergedRef as Ref<never>}\n      data-slot=\"dissolve-text\"\n      data-state={phase}\n      className={className}\n      {...props}\n    >\n      <span className=\"sr-only\">{children}</span>\n      <span aria-hidden=\"true\">\n        {words.map((pieces, wordIndex) => (\n          <Fragment key={wordIndex}>\n            {wordIndex > 0 ? \" \" : null}\n            <span className=\"inline-block whitespace-nowrap\">\n              {pieces.map(({ text, wait }, index) => (\n                <span\n                  key={index}\n                  data-slot=\"dissolve-text-piece\"\n                  className={cn(\n                    \"inline-block\",\n                    phase !== \"static\" && \"motion-safe:animate-dissolve-text\"\n                  )}\n                  style={pieceStyle(wait)}\n                >\n                  {text}\n                </span>\n              ))}\n            </span>\n          </Fragment>\n        ))}\n      </span>\n    </Tag>\n  );\n}\n\nexport { DissolveText, type DissolveTextProps };\n",
      "type": "registry:ui"
    }
  ],
  "tailwind": {
    "config": {
      "theme": {
        "extend": {
          "keyframes": {
            "dissolve-text": {
              "from": {
                "opacity": "0"
              }
            }
          },
          "animation": {
            "dissolve-text": "dissolve-text 1000ms cubic-bezier(0.25, 0.8, 0.3, 1) both"
          }
        }
      }
    }
  },
  "cssVars": {
    "theme": {
      "animate-dissolve-text": "dissolve-text 1000ms cubic-bezier(0.25, 0.8, 0.3, 1) both"
    }
  },
  "css": {
    "@keyframes dissolve-text": {
      "from": {
        "opacity": "0"
      }
    }
  },
  "categories": [
    "text"
  ],
  "type": "registry:ui"
}