{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "split-text",
  "type": "registry:ui",
  "title": "Split Text",
  "description": "A headline or short line that arrives a word, or a letter, at a time the first time it scrolls into view. It gives one line on a page a moment of attention; use it once or twice per page, on text short enough to read while it arrives.",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "decorative",
    "effect"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json",
    "https://ds.interlace.tools/r/motion-token.json",
    "https://ds.interlace.tools/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/effects/split-text.tsx",
      "target": "components/ui/effects/split-text.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\n// @interlace/split-text v1.0.1 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/split-text\n// What changed since: https://ds.interlace.tools/c/split-text#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — SplitText\n *\n * A headline or short line that arrives a word, or a letter, at a time the\n * first time it scrolls into view. It gives one line on a page a moment of\n * attention; use it once or twice per page, on text short enough to read\n * while it arrives.\n *\n * ## Content first\n *\n * The whole string is in the server HTML. Assistive tech reads it once, from\n * a visually hidden copy; the split pieces beside it are `aria-hidden`, so a\n * screen reader never spells a word out letter by letter. Pieces are inline\n * spans in reading order, so right-to-left text arrives from the right, and\n * a word split into letters never breaks across lines.\n *\n * ## Motion\n *\n * Before the first paint the pieces are set to transparent; when the line\n * enters the viewport each one fades in, `step` milliseconds after the one\n * before, rising a little (`rise`), coming into focus (`blur`) or neither\n * (`fade`). It runs on the Web Animations API on `opacity`, `transform` and\n * `filter`. Under `prefers-reduced-motion: reduce`, or without\n * IntersectionObserver or `element.animate`, the line is simply there.\n * Letters are cut with `Intl.Segmenter` where it exists, so an emoji or a\n * letter with combining marks stays whole.\n *\n * ## Composition\n *\n *   SplitText                           (as — data-slot=\"split-text\", data-by)\n *     ├─ span.sr-only                   (the whole string, read once)\n *     └─ span[aria-hidden]              (data-slot=\"split-text-visual\")\n *          └─ span × n                  (data-slot=\"split-text-piece\")\n *\n * | Rule | Concept                 | Where in this file                                   |\n * | ---- | ----------------------- | ---------------------------------------------------- |\n * | R4   | Extends native el       | `as` element props                                    |\n * | R6   | data-slot per part      | split-text / -visual / -piece                         |\n * | R7   | cn + ...rest            | `cn(className)` + `{...props}`                        |\n * | R8   | Enums, not booleans     | `by`, `effect`, `as`                                  |\n * | R25  | Client component        | the arrival runs in the browser                       |\n * | R26  | A11y from native el     | the heading keeps its role; text read once            |\n */\n\nimport { cn } from \"@/lib/utils\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { motionEase } from \"@/lib/motion-token\";\n\nconst FROM = {\n  fade: { opacity: 0 },\n  rise: { opacity: 0, transform: \"translateY(0.35em)\" },\n  blur: { opacity: 0, filter: \"blur(0.25em)\" },\n} as const satisfies Record<string, Keyframe>;\n\nconst TO = {\n  fade: { opacity: 1 },\n  rise: { opacity: 1, transform: \"none\" },\n  blur: { opacity: 1, filter: \"none\" },\n} as const satisfies Record<keyof typeof FROM, Keyframe>;\n\nexport type SplitTextEffect = keyof typeof FROM;\n\ntype SplitTextElement = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"p\" | \"span\";\n\nexport interface SplitTextProps extends Omit<\n  React.HTMLAttributes<HTMLElement>,\n  \"children\"\n> {\n  /** The text. A plain string, so it can be split and read whole. */\n  children: string;\n  /** The element to render; a heading keeps its heading role. @default 'span' */\n  as?: SplitTextElement;\n  /** Arrive a word at a time, or a letter at a time. @default 'word' */\n  by?: \"word\" | \"letter\";\n  /** How each piece arrives. Every effect fades; `rise` also lifts, `blur` sharpens. @default 'rise' */\n  effect?: SplitTextEffect;\n  /** Milliseconds between one piece and the next. @default by === 'word' ? 80 : 30 */\n  step?: number;\n  /** Milliseconds each piece takes to arrive. @default 500 */\n  duration?: number;\n  /** Milliseconds to wait after the line enters the viewport. @default 0 */\n  delay?: number;\n}\n\n/** One grapheme segmenter for the module, where the platform has one. */\nconst GRAPHEMES =\n  typeof Intl !== \"undefined\" && \"Segmenter\" in Intl\n    ? new Intl.Segmenter(undefined, { granularity: \"grapheme\" })\n    : null;\n\n/** Letters, keeping emoji and combining marks whole where the platform can. */\nfunction letters(word: string): string[] {\n  if (GRAPHEMES) return Array.from(GRAPHEMES.segment(word), (s) => s.segment);\n  return Array.from(word);\n}\n\nconst PIECE = \"inline-block whitespace-pre\";\n\nexport function SplitText({\n  children,\n  as: Tag = \"span\",\n  by = \"word\",\n  effect = \"rise\",\n  step,\n  duration = 500,\n  delay = 0,\n  className,\n  ...props\n}: SplitTextProps) {\n  const reduced = useReducedMotion();\n  const ref = React.useRef<HTMLElement>(null);\n  const gap = step ?? (by === \"word\" ? 80 : 30);\n  const words = children.split(/(\\s+)/);\n\n  // Before paint: hide the pieces, so they can arrive instead of flashing.\n  React.useLayoutEffect(() => {\n    const el = ref.current;\n    if (!el || reduced || typeof el.animate !== \"function\") return;\n    if (typeof IntersectionObserver === \"undefined\") return;\n    // Once played, a prop change (or a Fast Refresh) never plays it again.\n    if (el.dataset.revealed) return;\n    const pieces = Array.from(\n      el.querySelectorAll<HTMLElement>('[data-slot=\"split-text-piece\"]'),\n    );\n    for (const piece of pieces) piece.style.opacity = \"0\";\n\n    const io = new IntersectionObserver(\n      (entries) => {\n        if (!entries.some((e) => e.isIntersecting)) return;\n        io.disconnect();\n        el.dataset.revealed = \"true\";\n        pieces.forEach((piece, i) => {\n          piece.style.opacity = \"\";\n          piece.animate([FROM[effect], TO[effect]], {\n            duration,\n            delay: delay + i * gap,\n            easing: motionEase(piece, 'enter'),\n            fill: \"backwards\",\n          });\n        });\n      },\n      { threshold: 0.5 },\n    );\n    io.observe(el);\n\n    return () => {\n      io.disconnect();\n      for (const piece of pieces) {\n        piece.style.opacity = \"\";\n        for (const a of piece.getAnimations?.() ?? []) a.cancel();\n      }\n    };\n  }, [reduced, effect, duration, delay, gap, by, children]);\n\n  return (\n    <Tag\n      ref={ref as React.Ref<never>}\n      data-slot=\"split-text\"\n      data-by={by}\n      className={cn(className)}\n      {...props}\n    >\n      <span className=\"sr-only\">{children}</span>\n      <span data-slot=\"split-text-visual\" aria-hidden=\"true\">\n        {words.map((word, w) => {\n          if (word === \"\") return null;\n          if (/^\\s+$/.test(word)) return word;\n          if (by === \"word\") {\n            return (\n              <span key={w} data-slot=\"split-text-piece\" className={PIECE}>\n                {word}\n              </span>\n            );\n          }\n          // A word's letters stay together, so it never breaks mid-word.\n          return (\n            <span key={w} className=\"inline-block whitespace-nowrap\">\n              {letters(word).map((letter, l) => (\n                <span key={l} data-slot=\"split-text-piece\" className={PIECE}>\n                  {letter}\n                </span>\n              ))}\n            </span>\n          );\n        })}\n      </span>\n    </Tag>\n  );\n}\n"
    }
  ],
  "meta": {
    "tier": "effect",
    "client": true,
    "minViewport": null,
    "loading": false,
    "version": "1.0.1",
    "since": "2.0.0"
  },
  "docs": "## @interlace/split-text\n\nInstalled to `components/ui/effects/split-text.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/effects/split-text';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/split-text\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
