{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "flip-text",
  "title": "Flip Text",
  "description": "Kinetic type in 3D: characters or words turning into place around their horizontal axis, one after another.",
  "registryDependencies": [
    "@corsair-ui/utils",
    "@corsair-ui/use-entrance"
  ],
  "files": [
    {
      "path": "registry/default/ui/flip-text.tsx",
      "content": "\"use client\";\n\nimport {\n  Fragment,\n  useMemo,\n  type AnimationEvent,\n  type ComponentProps,\n  type CSSProperties,\n  type Ref,\n} 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// One splitter for the module: user-perceived characters, so emoji and\n// accents stay whole wherever the runtime can tell them apart.\nconst characterSplitter =\n  typeof Intl === \"object\" && typeof Intl.Segmenter === \"function\"\n    ? new Intl.Segmenter(undefined, { granularity: \"grapheme\" })\n    : null;\n\nfunction graphemes(text: string) {\n  if (!characterSplitter) return [...text];\n  const pieces: string[] = [];\n  for (const { segment } of characterSplitter.segment(text)) pieces.push(segment);\n  return pieces;\n}\n\ntype FlipTextTag = \"h1\" | \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\" | \"p\" | \"span\" | \"div\";\n\ninterface FlipTextProps extends Omit<ComponentProps<\"p\">, \"children\" | \"onAnimationEnd\"> {\n  /** Plain text. */\n  children: string;\n  as?: FlipTextTag;\n  /** What turns on its own: each character or each word. */\n  split?: \"characters\" | \"words\";\n  /** Where the wave starts. */\n  from?: \"first\" | \"last\" | \"center\";\n  /**\n   * Where each piece starts, in degrees around its horizontal axis.\n   * Negative values tip the top away from the reader, positive towards them.\n   */\n  angle?: number;\n  /** Distance to the viewer, in px: lower values give a deeper 3D turn. */\n  perspective?: number;\n  /**\n   * The axis each piece turns around. \"center\" rolls it like a drum,\n   * \"bottom\" stands it up from its baseline, \"top\" lets it drop down.\n   */\n  origin?: \"center\" | \"bottom\" | \"top\";\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 turned away, `true` plays it. */\n  play?: boolean;\n  /** Wait before the first piece, in ms. */\n  delay?: number;\n  /** Time between pieces, in ms. */\n  stagger?: number;\n  /** How long each piece takes, in ms. */\n  duration?: number;\n  /** Called once the last piece is in place. */\n  onAnimationComplete?: () => void;\n}\n\n// Behind the face for \"center\", so the piece rolls like a drum instead of spinning on the spot.\nconst ORIGINS = {\n  center: \"50% 50% -0.5em\",\n  bottom: \"50% 100%\",\n  top: \"50% 0%\",\n} as const;\n\n/**\n * Kinetic type in 3D: characters (or words) turn into place around their\n * horizontal axis, one after another, with their own perspective. It is one\n * CSS animation of `rotateX` per piece, so it plays on first paint without\n * JavaScript. Screen readers get the text in one piece, and with\n * `prefers-reduced-motion` it is shown as is.\n *\n * @example\n * <FlipText as=\"h2\" from=\"center\">Hoist the colours</FlipText>\n */\nfunction FlipText({\n  children,\n  as: Tag = \"p\",\n  split = \"characters\",\n  from = \"first\",\n  angle = -90,\n  perspective = 600,\n  origin = \"center\",\n  trigger = \"load\",\n  once = true,\n  play,\n  delay = 0,\n  stagger = 40,\n  duration = 800,\n  onAnimationComplete,\n  className,\n  style,\n  ref,\n  ...props\n}: FlipTextProps) {\n  const [observe, phase] = useEntrance<HTMLElement>({ trigger, once, play });\n  const mergedRef = useMemo(() => mergeRefs(ref, observe), [ref, observe]);\n\n  // Words keep their characters on one line; each holds the pieces that turn.\n  const words = useMemo(() => {\n    let next = 0;\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          index: next++,\n        }))\n      );\n  }, [children, split]);\n  const count = words.reduce((sum, word) => sum + word.length, 0);\n\n  const order = (index: number) => {\n    if (from === \"last\") return count - 1 - index;\n    if (from === \"center\") return Math.abs(Math.floor((count - 1) / 2) - index);\n    return index;\n  };\n\n  const pieceStyle = (index: number): CSSProperties =>\n    phase === \"static\"\n      ? {}\n      : {\n          animationDelay: `${delay + order(index) * stagger}ms`,\n          animationDuration: `${duration}ms`,\n          animationPlayState: phase === \"armed\" ? \"paused\" : undefined,\n        };\n\n  // The piece that moves last: the far end of the wave.\n  const last = from === \"last\" ? 0 : Math.max(count - 1, 0);\n  const handleAnimationEnd = (event: AnimationEvent<HTMLElement>) => {\n    if ((event.target as HTMLElement).dataset.last !== undefined) onAnimationComplete?.();\n  };\n\n  return (\n    <Tag\n      ref={mergedRef as Ref<never>}\n      data-slot=\"flip-text\"\n      data-state={phase}\n      className={className}\n      style={\n        {\n          \"--flip-text-angle\": `${angle}deg`,\n          \"--flip-text-perspective\": `${perspective}px`,\n          ...style,\n        } as CSSProperties\n      }\n      onAnimationEnd={handleAnimationEnd}\n      {...props}\n    >\n      <span className=\"sr-only\">{children}</span>\n      {words.map((word, wordIndex) => (\n        <Fragment key={wordIndex}>\n          {wordIndex > 0 ? \" \" : null}\n          <span aria-hidden=\"true\" className=\"inline-block whitespace-nowrap\">\n            {word.map(({ text, index }) => (\n              <span\n                key={index}\n                data-slot=\"flip-text-piece\"\n                data-last={index === last ? \"\" : undefined}\n                className={cn(\n                  \"inline-block [backface-visibility:hidden]\",\n                  phase !== \"static\" && \"motion-safe:animate-flip-text\"\n                )}\n                style={{ transformOrigin: ORIGINS[origin], ...pieceStyle(index) }}\n              >\n                {text}\n              </span>\n            ))}\n          </span>\n        </Fragment>\n      ))}\n    </Tag>\n  );\n}\n\nexport { FlipText, type FlipTextProps };\n",
      "type": "registry:ui"
    }
  ],
  "tailwind": {
    "config": {
      "theme": {
        "extend": {
          "keyframes": {
            "flip-text": {
              "from": {
                "opacity": "0",
                "transform": "perspective(var(--flip-text-perspective, 600px)) rotateX(var(--flip-text-angle, -90deg))"
              },
              "to": {
                "transform": "perspective(var(--flip-text-perspective, 600px)) rotateX(0deg)"
              }
            }
          },
          "animation": {
            "flip-text": "flip-text 800ms cubic-bezier(0.2, 0.8, 0.2, 1) both"
          }
        }
      }
    }
  },
  "cssVars": {
    "theme": {
      "animate-flip-text": "flip-text 800ms cubic-bezier(0.2, 0.8, 0.2, 1) both"
    }
  },
  "css": {
    "@keyframes flip-text": {
      "from": {
        "opacity": "0",
        "transform": "perspective(var(--flip-text-perspective, 600px)) rotateX(var(--flip-text-angle, -90deg))"
      },
      "to": {
        "transform": "perspective(var(--flip-text-perspective, 600px)) rotateX(0deg)"
      }
    }
  },
  "categories": [
    "text"
  ],
  "type": "registry:ui"
}