{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "text-bloom",
  "title": "TextBloom",
  "description": "Reveals readable text with a gentle or softly overshooting stagger.",
  "dependencies": [
    "motion@^13.1.0"
  ],
  "files": [
    {
      "path": "packages/react/src/text-bloom.tsx",
      "content": "'use client'\n\nimport './text-bloom.css'\nimport { type TextBloomOptions, textBloom, textBloomInitialStyle } from './motion/text-bloom.js'\nimport type { CSSProperties, ElementType, HTMLAttributes } from 'react'\nimport { forwardRef, useRef } from 'react'\nimport { composeRefs } from './compose-refs.js'\nimport { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect.js'\n\nexport interface TextBloomProps\n  extends TextBloomOptions,\n    Omit<HTMLAttributes<HTMLElement>, 'children'> {\n  children: string\n  /** Segment by words or Unicode characters. @default 'word' */\n  by?: 'word' | 'character'\n  /** Element to render. @default 'span' */\n  as?: ElementType\n}\n\nfunction splitText(text: string, by: 'word' | 'character') {\n  return by === 'character' ? Array.from(text) : text.split(/(\\s+)/)\n}\n\nexport const TextBloom = forwardRef<HTMLElement, TextBloomProps>(function TextBloom(\n  {\n    as: Tag = 'span',\n    by = 'word',\n    voice = 'gentle',\n    stagger,\n    delay,\n    duration,\n    once,\n    amount,\n    children,\n    ...props\n  },\n  forwardedRef,\n) {\n  const ref = useRef<HTMLElement>(null)\n\n  useIsomorphicLayoutEffect(() => {\n    const element = ref.current\n    if (!element) return\n    return textBloom(element, { voice, stagger, delay, duration, once, amount })\n  }, [Tag, amount, by, children, delay, duration, once, stagger, voice])\n\n  let cursor = 0\n  return (\n    <Tag {...props} ref={composeRefs(ref, forwardedRef)} data-kida-text-bloom=\"\" data-state=\"idle\">\n      <span>\n        {splitText(children, by).map((segment) => {\n          const key = `${cursor}-${segment}`\n          cursor += segment.length\n          if (/^\\s+$/.test(segment)) return segment\n          return (\n            <span\n              key={key}\n              data-kida-text-bloom-segment=\"\"\n              style={textBloomInitialStyle(voice) as CSSProperties}\n            >\n              {segment}\n            </span>\n          )\n        })}\n      </span>\n    </Tag>\n  )\n})\n",
      "type": "registry:ui",
      "target": "@ui/kida/text-bloom.tsx"
    },
    {
      "path": "packages/react/src/compose-refs.ts",
      "content": "import type { Ref } from 'react'\n\n/** Feed one node to several refs — ours for measuring, Zag's for animation detection. */\nexport function composeRefs<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 as { current: T | null }).current = node\n    }\n  }\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/compose-refs.ts"
    },
    {
      "path": "packages/react/src/use-isomorphic-layout-effect.ts",
      "content": "'use client'\n\nimport { useEffect, useLayoutEffect } from 'react'\n\n/**\n * `useLayoutEffect` in the browser, `useEffect` on the server.\n *\n * Motion has to be applied before paint or the user sees an unstyled frame, but React\n * warns when `useLayoutEffect` runs during SSR — where it does nothing anyway.\n */\nexport const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect\n",
      "type": "registry:hook",
      "target": "@ui/kida/use-isomorphic-layout-effect.ts"
    },
    {
      "path": "packages/styles/components/text-bloom.css",
      "content": "[data-kida-text-bloom-segment] {\n  display: inline-block;\n}\n\n@media (prefers-reduced-motion: reduce) {\n  [data-kida-text-bloom-segment] {\n    /* biome-ignore lint/complexity/noImportantStyles: overrides the SSR inline initial state */\n    opacity: 1 !important;\n    /* biome-ignore lint/complexity/noImportantStyles: overrides the SSR inline initial state */\n    transform: none !important;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/kida/text-bloom.css"
    },
    {
      "path": "packages/motion/src/text-bloom.ts",
      "content": "import { animate, inView, stagger } from 'motion'\nimport { prefersReducedMotion } from './reduced-motion.js'\n\nexport type TextBloomVoice = 'gentle' | 'pop'\n\nexport interface TextBloomOptions {\n  /** Motion character. @default 'gentle' */\n  voice?: TextBloomVoice\n  /** Seconds between segments. @default 0.045 */\n  stagger?: number\n  /** Seconds before the first segment. @default 0 */\n  delay?: number\n  /** Seconds per segment. @default 0.55 */\n  duration?: number\n  /** Animate only on the first viewport entry. @default true */\n  once?: boolean\n  /** How much of the root must be visible. @default 0.4 */\n  amount?: 'some' | 'all' | number\n}\n\nconst DEFAULTS = {\n  voice: 'gentle',\n  stagger: 0.045,\n  delay: 0,\n  duration: 0.55,\n  once: true,\n  amount: 0.4,\n} as const\n\nexport function textBloomInitialStyle(voice: TextBloomVoice = 'gentle') {\n  return {\n    opacity: 0,\n    transform:\n      voice === 'pop'\n        ? 'translate3d(0, 18px, 0) scale(0.86)'\n        : 'translate3d(0, 10px, 0) scale(0.98)',\n    transformOrigin: '50% 80%',\n    willChange: 'transform, opacity',\n  } as const\n}\n\nexport function textBloom(element: HTMLElement, options: TextBloomOptions = {}): () => void {\n  const settings = {\n    voice: options.voice ?? DEFAULTS.voice,\n    stagger: options.stagger ?? DEFAULTS.stagger,\n    delay: options.delay ?? DEFAULTS.delay,\n    duration: options.duration ?? DEFAULTS.duration,\n    once: options.once ?? DEFAULTS.once,\n    amount: options.amount ?? DEFAULTS.amount,\n  }\n  const segments = [...element.querySelectorAll<HTMLElement>('[data-kida-text-bloom-segment]')]\n  if (segments.length === 0) return () => {}\n\n  const hide = () => {\n    for (const segment of segments)\n      Object.assign(segment.style, textBloomInitialStyle(settings.voice))\n    element.dataset.state = 'idle'\n  }\n  const settle = () => {\n    for (const segment of segments) {\n      segment.style.opacity = '1'\n      segment.style.transform = 'none'\n      segment.style.transformOrigin = ''\n      segment.style.willChange = 'auto'\n    }\n    element.dataset.state = 'settled'\n  }\n\n  if (prefersReducedMotion()) {\n    settle()\n    return () => {}\n  }\n\n  hide()\n  let controls: ReturnType<typeof animate> | undefined\n  let settleFrame = 0\n\n  const cancelSettle = () => {\n    if (!settleFrame) return\n    cancelAnimationFrame(settleFrame)\n    settleFrame = 0\n  }\n\n  const stop = inView(\n    element,\n    () => {\n      cancelSettle()\n      element.dataset.state = 'active'\n      const pop = settings.voice === 'pop'\n      controls = animate(\n        segments,\n        {\n          opacity: pop ? [0, 1, 1, 1] : [0, 1, 1],\n          transform: pop\n            ? [\n                'translate3d(0, 18px, 0) scale(0.86)',\n                'translate3d(0, -4px, 0) scale(1.08)',\n                'translate3d(0, 1px, 0) scale(0.985)',\n                'translate3d(0, 0, 0) scale(1)',\n              ]\n            : [\n                'translate3d(0, 10px, 0) scale(0.98)',\n                'translate3d(0, 0, 0) scale(1)',\n                'translate3d(0, 0, 0) scale(1)',\n              ],\n        },\n        {\n          duration: settings.duration,\n          delay: stagger(settings.stagger, { startDelay: settings.delay }),\n          // Pop reaches its visible overshoot early, then recoils once before settling.\n          ease: pop\n            ? [\n                [0.16, 1, 0.3, 1],\n                [0.4, 0, 0.6, 1],\n                [0.16, 1, 0.3, 1],\n              ]\n            : [0.16, 1, 0.3, 1],\n          times: pop ? [0, 0.45, 0.72, 1] : [0, 0.72, 1],\n        },\n      )\n\n      const currentControls = controls\n      void currentControls.finished\n        .then(() => {\n          // `animate()` creates one animation per property and segment. Wait for the whole\n          // group, then move one frame past Motion's final style commits before cleaning up.\n          if (controls !== currentControls) return\n          settleFrame = requestAnimationFrame(() => {\n            settleFrame = 0\n            if (controls === currentControls) settle()\n          })\n        })\n        .catch(() => {\n          // Cancelling a pass rejects its finished promise; the exit cleanup re-arms it.\n        })\n\n      if (settings.once) stop()\n      return settings.once\n        ? undefined\n        : () => {\n            cancelSettle()\n            controls?.cancel()\n            hide()\n          }\n    },\n    { amount: settings.amount },\n  )\n\n  return () => {\n    stop()\n    cancelSettle()\n    controls?.cancel()\n  }\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/motion/text-bloom.ts"
    },
    {
      "path": "packages/motion/src/reduced-motion.ts",
      "content": "const QUERY = '(prefers-reduced-motion: reduce)'\n\n/**\n * Whether the user has asked for reduced motion.\n *\n * SSR-safe: returns `false` when there is no `window`, matching the CSS default\n * (the `prefers-reduced-motion` media query in `@kida-ui/styles` is the real guard;\n * this is for the JS-driven paths that CSS cannot express).\n */\nexport function prefersReducedMotion(): boolean {\n  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return false\n  return window.matchMedia(QUERY).matches\n}\n\n/**\n * Subscribe to changes of the reduced-motion preference.\n * Returns an unsubscribe function. No-op (and immediately unsubscribable) on the server.\n */\nexport function watchReducedMotion(onChange: (reduced: boolean) => void): () => void {\n  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return () => {}\n\n  const list = window.matchMedia(QUERY)\n  const handler = (event: MediaQueryListEvent) => onChange(event.matches)\n  list.addEventListener('change', handler)\n  return () => list.removeEventListener('change', handler)\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/motion/reduced-motion.ts"
    }
  ],
  "meta": {
    "kida": {
      "package": {
        "name": "@kida-ui/react",
        "exportName": "TextBloom",
        "styleImports": [
          "@kida-ui/styles/kida.css"
        ]
      }
    }
  },
  "categories": [
    "text"
  ],
  "type": "registry:ui"
}
