{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rich-text-editor",
  "title": "Rich Text Editor",
  "description": "A rich text editor with a formatting toolbar, headings, lists, quotes, inline code and links, Markdown-style shortcuts, clean pasting from Docs and Word, and a word count, with no editor library.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/rich-text-editor/rich-text-editor.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type RichTextEditorSize = \"sm\" | \"md\" | \"lg\";\nexport type RichTextTool = \"bold\" | \"italic\" | \"underline\" | \"strike\" | \"h2\" | \"h3\" | \"bulletList\" | \"orderedList\" | \"quote\" | \"code\" | \"link\" | \"undo\" | \"redo\";\n\nexport interface RichTextEditorProps {\n  /** HTML string. */\n  value?: string;\n  defaultValue?: string;\n  /** Called with the cleaned HTML and the plain text on every change. */\n  onValueChange?: (html: string, text: string) => void;\n  placeholder?: string;\n  label?: string;\n  helperText?: string;\n  /** Tools shown in the toolbar, in order. */\n  tools?: RichTextTool[];\n  /** Shows a live word and character count; with maxLength it counts down to the limit. */\n  showCount?: boolean;\n  /** Soft limit in characters: the counter turns red past it. */\n  maxLength?: number;\n  minHeight?: number;\n  maxHeight?: number;\n  size?: RichTextEditorSize;\n  theme?: \"dark\" | \"light\";\n  width?: number | string;\n  disabled?: boolean;\n  className?: string;\n}\n\nconst DEFAULT_TOOLS: RichTextTool[] = [\"bold\", \"italic\", \"underline\", \"strike\", \"h2\", \"h3\", \"bulletList\", \"orderedList\", \"quote\", \"code\", \"link\", \"undo\", \"redo\"];\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.3)\", bg: \"rgba(255,255,255,0.03)\", border: \"rgba(255,255,255,0.12)\", borderHover: \"rgba(255,255,255,0.24)\", focus: \"rgba(245,244,241,0.85)\", divider: \"rgba(255,255,255,0.08)\", hover: \"rgba(255,255,255,0.07)\", sel: \"#F5F4F1\", selText: \"#0A0A0A\", codeBg: \"rgba(255,255,255,0.08)\", menuBg: \"#0E0E0E\", shadow: \"0 18px 44px rgba(0,0,0,0.5)\", error: \"#FF7A6B\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.3)\", bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.16)\", borderHover: \"rgba(10,10,10,0.3)\", focus: \"rgba(10,10,10,0.85)\", divider: \"rgba(10,10,10,0.08)\", hover: \"rgba(10,10,10,0.05)\", sel: \"#0A0A0A\", selText: \"#FFFFFF\", codeBg: \"rgba(10,10,10,0.06)\", menuBg: \"#FFFFFF\", shadow: \"0 14px 36px rgba(0,0,0,0.14)\", error: \"#E5484D\" },\n};\n\nconst SIZES: Record<RichTextEditorSize, { font: number; radius: number; pad: number; btn: number }> = {\n  sm: { font: 13.5, radius: 11, pad: 12, btn: 28 },\n  md: { font: 15, radius: 13, pad: 14, btn: 30 },\n  lg: { font: 16.5, radius: 15, pad: 16, btn: 34 },\n};\n\n// ─── Sanitising ────────────────────────────────────────────────────────────\n// Pasted HTML (Word, Google Docs, web pages) and incoming values are reduced to this small set of tags,\n// with every attribute dropped except a safe href on links. Always sanitise again on your server.\nconst ALLOWED = new Set([\"P\", \"BR\", \"STRONG\", \"EM\", \"U\", \"S\", \"H2\", \"H3\", \"UL\", \"OL\", \"LI\", \"BLOCKQUOTE\", \"CODE\", \"A\"]);\nconst RENAME: Record<string, string> = { B: \"STRONG\", I: \"EM\", STRIKE: \"S\", DEL: \"S\", H1: \"H2\", H4: \"H3\", H5: \"H3\", H6: \"H3\", DIV: \"P\", PRE: \"P\" };\nconst DROP = new Set([\"SCRIPT\", \"STYLE\", \"IFRAME\", \"OBJECT\", \"EMBED\", \"META\", \"LINK\", \"TITLE\", \"NOSCRIPT\", \"SVG\", \"MATH\", \"FORM\", \"INPUT\", \"BUTTON\", \"TEMPLATE\"]);\n\nfunction safeHref(href: string | null) {\n  if (!href) return null;\n  const h = href.trim();\n  return /^(https?:|mailto:|\\/|#)/i.test(h) ? h : null;\n}\n\nexport function sanitizeRichText(html: string): string {\n  if (typeof window === \"undefined\") return html;\n  const doc = new DOMParser().parseFromString(`<body>${html}</body>`, \"text/html\");\n  const walk = (node: Node, into: Node) => {\n    node.childNodes.forEach((child) => {\n      if (child.nodeType === Node.TEXT_NODE) {\n        into.appendChild(document.createTextNode(child.textContent ?? \"\"));\n        return;\n      }\n      if (child.nodeType !== Node.ELEMENT_NODE) return;\n      const el = child as Element;\n      const tag = RENAME[el.tagName] ?? el.tagName;\n      if (DROP.has(el.tagName)) return;\n      // Google Docs wraps bold text in <span style=\"font-weight:700\">; keep that as <strong>.\n      const style = el.getAttribute(\"style\") ?? \"\";\n      const spanBold = el.tagName === \"SPAN\" && /font-weight:\\s*(bold|[6-9]00)/.test(style);\n      const spanItalic = el.tagName === \"SPAN\" && /font-style:\\s*italic/.test(style);\n      if (!ALLOWED.has(tag) && !spanBold && !spanItalic) {\n        walk(el, into);\n        return;\n      }\n      const out = document.createElement(spanBold ? \"strong\" : spanItalic ? \"em\" : tag.toLowerCase());\n      if (tag === \"A\") {\n        const href = safeHref(el.getAttribute(\"href\"));\n        if (!href) {\n          walk(el, into);\n          return;\n        }\n        out.setAttribute(\"href\", href);\n        out.setAttribute(\"rel\", \"noopener noreferrer nofollow\");\n        out.setAttribute(\"target\", \"_blank\");\n      }\n      walk(el, out);\n      // A paragraph can't hold lists, quotes or headings; keep its children and drop the wrapper.\n      if (out.tagName === \"P\" && out.querySelector(\":scope > ul, :scope > ol, :scope > blockquote, :scope > h2, :scope > h3, :scope > p\")) {\n        while (out.firstChild) into.appendChild(out.firstChild);\n        return;\n      }\n      into.appendChild(out);\n    });\n  };\n  const clean = document.createElement(\"div\");\n  walk(doc.body, clean);\n  return clean.innerHTML;\n}\n\n// ─── Icons ─────────────────────────────────────────────────────────────────\nfunction Icon({ name }: { name: RichTextTool }) {\n  const common = { width: 16, height: 16, viewBox: \"0 0 16 16\", fill: \"none\", stroke: \"currentColor\", strokeWidth: 1.6, strokeLinecap: \"round\" as const, strokeLinejoin: \"round\" as const, \"aria-hidden\": true };\n  switch (name) {\n    case \"bold\":\n      return <svg {...common} strokeWidth={2}><path d=\"M4.5 2.75h4.25a2.6 2.6 0 0 1 0 5.2H4.5zM4.5 7.95h5a2.65 2.65 0 0 1 0 5.3h-5z\" /></svg>;\n    case \"italic\":\n      return <svg {...common}><path d=\"M9.5 2.75h-3M9.5 13.25h-3M9 2.75 7 13.25\" /></svg>;\n    case \"underline\":\n      return <svg {...common}><path d=\"M4.5 2.5v5a3.5 3.5 0 0 0 7 0v-5M3.5 13.75h9\" /></svg>;\n    case \"strike\":\n      return <svg {...common}><path d=\"M2.75 8h10.5M10.9 4.6c-.4-1.1-1.5-1.85-2.9-1.85-1.75 0-2.9.95-2.9 2.2 0 .7.35 1.2.95 1.6M5 11.4c.4 1.1 1.55 1.85 3.05 1.85 1.8 0 2.95-.95 2.95-2.25 0-.45-.15-.85-.4-1.15\" /></svg>;\n    case \"h2\":\n      return <svg {...common}><path d=\"M2.5 3.5v9M7.5 3.5v9M2.5 8h5M10 6.4c.3-.9 1-1.4 1.9-1.4 1 0 1.75.7 1.75 1.6 0 1.8-3.65 2.6-3.65 5.9h3.75\" /></svg>;\n    case \"h3\":\n      return <svg {...common}><path d=\"M2.5 3.5v9M7.5 3.5v9M2.5 8h5M10.1 5.6c.3-.4.85-.65 1.55-.65 1 0 1.7.55 1.7 1.4s-.7 1.4-1.7 1.4c1.15 0 1.95.6 1.95 1.55 0 .95-.8 1.7-2 1.7-.7 0-1.3-.3-1.6-.8\" /></svg>;\n    case \"bulletList\":\n      return <svg {...common}><path d=\"M6.5 4h7M6.5 8h7M6.5 12h7\" /><circle cx=\"3\" cy=\"4\" r=\"0.9\" fill=\"currentColor\" stroke=\"none\" /><circle cx=\"3\" cy=\"8\" r=\"0.9\" fill=\"currentColor\" stroke=\"none\" /><circle cx=\"3\" cy=\"12\" r=\"0.9\" fill=\"currentColor\" stroke=\"none\" /></svg>;\n    case \"orderedList\":\n      return <svg {...common}><path d=\"M6.5 4h7M6.5 8h7M6.5 12h7M2.4 2.9l.9-.5v3.1M2.3 7.1c.2-.4.55-.6.95-.6.5 0 .85.3.85.75 0 .8-1.8 1.1-1.8 2.25h1.85\" strokeWidth={1.3} /></svg>;\n    case \"quote\":\n      return <svg {...common}><path d=\"M3 4.5h10M6 8h7M6 11.5h7M3 7.5v5\" /></svg>;\n    case \"code\":\n      return <svg {...common}><path d=\"M5.5 4.5 2 8l3.5 3.5M10.5 4.5 14 8l-3.5 3.5\" /></svg>;\n    case \"link\":\n      return <svg {...common}><path d=\"M6.75 9.25a2.5 2.5 0 0 0 3.55.1l2-2a2.5 2.5 0 0 0-3.55-3.55l-.7.7M9.25 6.75a2.5 2.5 0 0 0-3.55-.1l-2 2a2.5 2.5 0 0 0 3.55 3.55l.7-.7\" /></svg>;\n    case \"undo\":\n      return <svg {...common}><path d=\"M4.5 6.5h5.25a3.25 3.25 0 0 1 0 6.5H7M6.75 3.75 4 6.5l2.75 2.75\" /></svg>;\n    case \"redo\":\n      return <svg {...common}><path d=\"M11.5 6.5H6.25a3.25 3.25 0 0 0 0 6.5H9M9.25 3.75 12 6.5 9.25 9.25\" /></svg>;\n  }\n}\n\nconst LABELS: Record<RichTextTool, string> = {\n  bold: \"Bold (⌘B)\",\n  italic: \"Italic (⌘I)\",\n  underline: \"Underline (⌘U)\",\n  strike: \"Strikethrough\",\n  h2: \"Heading\",\n  h3: \"Subheading\",\n  bulletList: \"Bulleted list\",\n  orderedList: \"Numbered list\",\n  quote: \"Quote\",\n  code: \"Inline code\",\n  link: \"Link (⌘K)\",\n  undo: \"Undo (⌘Z)\",\n  redo: \"Redo (⌘⇧Z)\",\n};\n\n// Where each tool sits in the toolbar groups; a divider goes between groups.\nconst GROUP: Record<RichTextTool, number> = { bold: 0, italic: 0, underline: 0, strike: 0, h2: 1, h3: 1, bulletList: 2, orderedList: 2, quote: 2, code: 3, link: 3, undo: 4, redo: 4 };\n\ntype ActiveState = Partial<Record<RichTextTool, boolean>>;\n\nconst exec = (command: string, value?: string) => document.execCommand(command, false, value);\n\n// Inserts HTML at the caret through the Range API. execCommand(\"insertHTML\") would stamp inline\n// colours on the new nodes in Chrome, which then ignore the theme.\nfunction insertHtmlAtCaret(html: string) {\n  const sel = window.getSelection();\n  if (!sel || !sel.rangeCount) return;\n  const range = sel.getRangeAt(0);\n  range.deleteContents();\n  const fragment = range.createContextualFragment(html);\n  const last = fragment.lastChild;\n  range.insertNode(fragment);\n  if (last) {\n    const after = document.createRange();\n    after.setStartAfter(last);\n    after.collapse(true);\n    sel.removeAllRanges();\n    sel.addRange(after);\n  }\n}\n\nexport function RichTextEditor({\n  value,\n  defaultValue = \"\",\n  onValueChange,\n  placeholder = \"Write something…\",\n  label,\n  helperText,\n  tools = DEFAULT_TOOLS,\n  showCount = true,\n  maxLength,\n  minHeight = 160,\n  maxHeight = 420,\n  size = \"md\",\n  theme = \"dark\",\n  width = 560,\n  disabled = false,\n  className,\n}: RichTextEditorProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const editorRef = React.useRef<HTMLDivElement>(null);\n  const savedRange = React.useRef<Range | null>(null);\n  const [focused, setFocused] = React.useState(false);\n  const [hovered, setHovered] = React.useState(false);\n  const [active, setActive] = React.useState<ActiveState>({});\n  const [empty, setEmpty] = React.useState(!defaultValue && !value);\n  const [counts, setCounts] = React.useState({ words: 0, chars: 0 });\n  const [linkOpen, setLinkOpen] = React.useState(false);\n  const [linkUrl, setLinkUrl] = React.useState(\"\");\n\n  const readCounts = (el: HTMLElement) => {\n    const text = el.innerText.replace(/​/g, \"\").trim();\n    setCounts({ words: text ? text.split(/\\s+/).length : 0, chars: text.length });\n    setEmpty(text.length === 0 && !el.querySelector(\"li, blockquote, h2, h3\"));\n    return text;\n  };\n\n  // Load the initial (or a changed controlled) value, without fighting the caret while typing.\n  React.useEffect(() => {\n    const el = editorRef.current;\n    if (!el) return;\n    const next = sanitizeRichText(value ?? defaultValue);\n    if (el.innerHTML !== next && document.activeElement !== el) {\n      el.innerHTML = next;\n      readCounts(el);\n    }\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [value]);\n\n  const emit = () => {\n    const el = editorRef.current;\n    if (!el) return;\n    const text = readCounts(el);\n    onValueChange?.(sanitizeRichText(el.innerHTML), text);\n  };\n\n  const refreshActive = React.useCallback(() => {\n    const el = editorRef.current;\n    const sel = window.getSelection();\n    if (!el || !sel || !sel.anchorNode || !el.contains(sel.anchorNode)) return;\n    const node = sel.anchorNode.nodeType === Node.ELEMENT_NODE ? (sel.anchorNode as Element) : sel.anchorNode.parentElement;\n    const block = String(document.queryCommandValue(\"formatBlock\") || \"\").toLowerCase();\n    setActive({\n      bold: document.queryCommandState(\"bold\") && block !== \"h2\" && block !== \"h3\",\n      italic: document.queryCommandState(\"italic\"),\n      // Links are underlined by CSS, which the browser reports as underline; don't light it up there.\n      underline: document.queryCommandState(\"underline\") && !node?.closest(\"a\"),\n      strike: document.queryCommandState(\"strikeThrough\"),\n      bulletList: document.queryCommandState(\"insertUnorderedList\"),\n      orderedList: document.queryCommandState(\"insertOrderedList\"),\n      h2: block === \"h2\",\n      h3: block === \"h3\",\n      quote: block === \"blockquote\" || Boolean(node?.closest(\"blockquote\")),\n      code: Boolean(node?.closest(\"code\")),\n      link: Boolean(node?.closest(\"a\")),\n    });\n  }, []);\n\n  React.useEffect(() => {\n    document.addEventListener(\"selectionchange\", refreshActive);\n    return () => document.removeEventListener(\"selectionchange\", refreshActive);\n  }, [refreshActive]);\n\n  const saveRange = () => {\n    const sel = window.getSelection();\n    if (sel && sel.rangeCount && editorRef.current?.contains(sel.anchorNode)) savedRange.current = sel.getRangeAt(0).cloneRange();\n  };\n\n  const restoreRange = () => {\n    const sel = window.getSelection();\n    editorRef.current?.focus();\n    if (sel && savedRange.current) {\n      sel.removeAllRanges();\n      sel.addRange(savedRange.current);\n    }\n  };\n\n  const toggleBlock = (tag: \"h2\" | \"h3\" | \"blockquote\") => {\n    const current = String(document.queryCommandValue(\"formatBlock\") || \"\").toLowerCase();\n    exec(\"formatBlock\", current === tag ? \"p\" : tag);\n  };\n\n  const toggleCode = () => {\n    const sel = window.getSelection();\n    if (!sel || !sel.rangeCount) return;\n    const node = sel.anchorNode?.nodeType === Node.ELEMENT_NODE ? (sel.anchorNode as Element) : sel.anchorNode?.parentElement;\n    const code = node?.closest(\"code\");\n    if (code && editorRef.current?.contains(code)) {\n      code.replaceWith(document.createTextNode(code.textContent ?? \"\"));\n      return;\n    }\n    const text = sel.toString();\n    if (!text) return;\n    const escaped = text.replace(/&/g, \"&amp;\").replace(/</g, \"&lt;\").replace(/>/g, \"&gt;\");\n    insertHtmlAtCaret(`<code>${escaped}</code>`);\n  };\n\n  const openLink = () => {\n    saveRange();\n    const sel = window.getSelection();\n    const node = sel?.anchorNode?.nodeType === Node.ELEMENT_NODE ? (sel.anchorNode as Element) : sel?.anchorNode?.parentElement;\n    const existing = node?.closest(\"a\");\n    setLinkUrl(existing?.getAttribute(\"href\") ?? \"\");\n    setLinkOpen(true);\n  };\n\n  const applyLink = (remove = false) => {\n    restoreRange();\n    if (remove) exec(\"unlink\");\n    else {\n      const raw = linkUrl.trim();\n      const url = raw && !/^(https?:|mailto:|\\/|#)/i.test(raw) ? `https://${raw}` : raw;\n      if (url) {\n        const sel = window.getSelection();\n        if (sel && sel.isCollapsed) insertHtmlAtCaret(`<a href=\"${url.replace(/\"/g, \"&quot;\")}\">${url.replace(/</g, \"&lt;\")}</a>`);\n        else exec(\"createLink\", url);\n        editorRef.current?.querySelectorAll(\"a\").forEach((a) => {\n          a.setAttribute(\"rel\", \"noopener noreferrer nofollow\");\n          a.setAttribute(\"target\", \"_blank\");\n        });\n      }\n    }\n    setLinkOpen(false);\n    emit();\n    refreshActive();\n  };\n\n  const run = (tool: RichTextTool) => {\n    if (disabled) return;\n    if (tool === \"link\") return openLink();\n    editorRef.current?.focus();\n    switch (tool) {\n      case \"bold\": exec(\"bold\"); break;\n      case \"italic\": exec(\"italic\"); break;\n      case \"underline\": exec(\"underline\"); break;\n      case \"strike\": exec(\"strikeThrough\"); break;\n      case \"h2\": toggleBlock(\"h2\"); break;\n      case \"h3\": toggleBlock(\"h3\"); break;\n      case \"quote\": toggleBlock(\"blockquote\"); break;\n      case \"bulletList\": exec(\"insertUnorderedList\"); break;\n      case \"orderedList\": exec(\"insertOrderedList\"); break;\n      case \"code\": toggleCode(); break;\n      case \"undo\": exec(\"undo\"); break;\n      case \"redo\": exec(\"redo\"); break;\n    }\n    emit();\n    refreshActive();\n  };\n\n  // Markdown-style shortcuts at the start of a line: \"## \", \"### \", \"- \", \"* \", \"1. \", \"> \".\n  const markdownShortcut = (): boolean => {\n    const sel = window.getSelection();\n    if (!sel || !sel.isCollapsed || !sel.anchorNode || sel.anchorNode.nodeType !== Node.TEXT_NODE) return false;\n    const textNode = sel.anchorNode as Text;\n    const before = (textNode.textContent ?? \"\").slice(0, sel.anchorOffset);\n    // Only at the very start of a line: the text from the start of the block up to the caret must be the marker.\n    const block = textNode.parentElement?.closest(\"p, div, h2, h3, li, blockquote\") ?? editorRef.current;\n    if (!block) return false;\n    const prefix = document.createRange();\n    prefix.selectNodeContents(block);\n    prefix.setEnd(textNode, sel.anchorOffset);\n    if (prefix.toString() !== before) return false;\n    const map: Record<string, () => void> = {\n      \"##\": () => exec(\"formatBlock\", \"h2\"),\n      \"###\": () => exec(\"formatBlock\", \"h3\"),\n      \"-\": () => exec(\"insertUnorderedList\"),\n      \"*\": () => exec(\"insertUnorderedList\"),\n      \"1.\": () => exec(\"insertOrderedList\"),\n      \">\": () => exec(\"formatBlock\", \"blockquote\"),\n    };\n    const action = map[before];\n    if (!action) return false;\n    const range = document.createRange();\n    range.setStart(textNode, 0);\n    range.setEnd(textNode, sel.anchorOffset);\n    sel.removeAllRanges();\n    sel.addRange(range);\n    exec(\"delete\");\n    action();\n    return true;\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {\n    const mod = e.metaKey || e.ctrlKey;\n    if (mod && e.key.toLowerCase() === \"k\") {\n      e.preventDefault();\n      openLink();\n      return;\n    }\n    if (e.key === \" \" && markdownShortcut()) {\n      e.preventDefault();\n      emit();\n      refreshActive();\n    }\n  };\n\n  const onPaste = (e: React.ClipboardEvent<HTMLDivElement>) => {\n    e.preventDefault();\n    const html = e.clipboardData.getData(\"text/html\");\n    const text = e.clipboardData.getData(\"text/plain\");\n    if (html) insertHtmlAtCaret(sanitizeRichText(html));\n    else exec(\"insertText\", text);\n    emit();\n  };\n\n  const borderColor = focused ? p.focus : hovered ? p.borderHover : p.border;\n  const over = maxLength !== undefined && counts.chars > maxLength;\n  const shownTools = tools.filter((t) => t in LABELS);\n\n  return (\n    <div className={cn(\"relative inline-flex flex-col gap-2\", className)} style={{ width, maxWidth: \"100%\", fontFamily: \"Inter, sans-serif\", opacity: disabled ? 0.5 : 1 }}>\n      {label && (\n        <label id={`${uid}-label`} className=\"font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: s.font - 0.5 }} onClick={() => editorRef.current?.focus()}>\n          {label}\n        </label>\n      )}\n\n      <div\n        onMouseEnter={() => setHovered(true)}\n        onMouseLeave={() => setHovered(false)}\n        className=\"relative flex flex-col\"\n        style={{ borderRadius: s.radius, background: p.bg, border: `1px solid ${borderColor}`, boxShadow: focused ? `0 0 0 3px color-mix(in srgb, ${p.focus} 14%, transparent)` : \"0 0 0 0 transparent\", transition: \"border-color 0.18s ease, box-shadow 0.18s ease\" }}\n      >\n        <div role=\"toolbar\" aria-label=\"Formatting\" aria-controls={`${uid}-editor`} className=\"flex flex-wrap items-center\" style={{ gap: 2, padding: 6, borderBottom: `1px solid ${p.divider}` }}>\n          {shownTools.map((tool, i) => {\n            const on = Boolean(active[tool]);\n            const newGroup = i > 0 && GROUP[tool] !== GROUP[shownTools[i - 1]];\n            return (\n              <React.Fragment key={tool}>\n                {newGroup && <span aria-hidden=\"true\" style={{ width: 1, height: s.btn - 12, margin: \"0 5px\", background: p.divider }} />}\n                <button\n                  type=\"button\"\n                  title={LABELS[tool]}\n                  aria-label={LABELS[tool].replace(/ \\(.*\\)$/, \"\")}\n                  aria-pressed={tool === \"undo\" || tool === \"redo\" ? undefined : on}\n                  disabled={disabled}\n                  onMouseDown={(e) => e.preventDefault()}\n                  onClick={() => run(tool)}\n                  className=\"relative flex shrink-0 cursor-pointer items-center justify-center border-none bg-transparent outline-none focus-visible:ring-2\"\n                  style={{ width: s.btn, height: s.btn, borderRadius: 8, color: on ? p.selText : p.muted, [\"--tw-ring-color\" as string]: p.focus }}\n                  onMouseEnter={(e) => !on && (e.currentTarget.style.background = p.hover)}\n                  onMouseLeave={(e) => (e.currentTarget.style.background = \"transparent\")}\n                >\n                  {on && <motion.span className=\"absolute inset-0\" style={{ borderRadius: 8, background: p.sel }} initial={{ opacity: 0, scale: 0.8 }} animate={{ opacity: 1, scale: 1 }} transition={{ type: \"spring\", stiffness: 520, damping: 34 }} />}\n                  <span className=\"relative flex\">\n                    <Icon name={tool} />\n                  </span>\n                </button>\n              </React.Fragment>\n            );\n          })}\n        </div>\n\n        <AnimatePresence>\n          {linkOpen && (\n            <motion.div\n              className=\"absolute z-20 flex items-center\"\n              style={{ top: s.btn + 18, left: 8, right: 8, maxWidth: 420, gap: 6, padding: 6, borderRadius: 11, background: p.menuBg, border: `1px solid ${p.border}`, boxShadow: p.shadow }}\n              initial={{ opacity: 0, y: -4, scale: 0.98 }}\n              animate={{ opacity: 1, y: 0, scale: 1 }}\n              exit={{ opacity: 0, y: -4, scale: 0.98 }}\n              transition={{ duration: 0.14 }}\n            >\n              <input\n                autoFocus\n                type=\"url\"\n                aria-label=\"Link URL\"\n                value={linkUrl}\n                placeholder=\"Paste or type a link\"\n                onChange={(e) => setLinkUrl(e.target.value)}\n                onKeyDown={(e) => {\n                  if (e.key === \"Enter\") {\n                    e.preventDefault();\n                    applyLink();\n                  } else if (e.key === \"Escape\") {\n                    e.preventDefault();\n                    setLinkOpen(false);\n                    restoreRange();\n                  }\n                }}\n                className=\"min-w-0 flex-1 border-none bg-transparent px-2 outline-none placeholder:text-[color:var(--rte-placeholder)]\"\n                style={{ height: 30, color: p.text, fontSize: s.font - 1.5, [\"--rte-placeholder\" as string]: p.faint }}\n              />\n              {active.link && (\n                <button type=\"button\" onClick={() => applyLink(true)} className=\"cursor-pointer rounded-md border-none bg-transparent px-2.5 outline-none\" style={{ height: 28, color: p.muted, fontSize: s.font - 2 }}>\n                  Remove\n                </button>\n              )}\n              <button type=\"button\" onClick={() => applyLink()} className=\"cursor-pointer rounded-md border-none px-3 font-medium outline-none\" style={{ height: 28, background: p.sel, color: p.selText, fontSize: s.font - 2 }}>\n                Apply\n              </button>\n            </motion.div>\n          )}\n        </AnimatePresence>\n\n        <div className=\"relative\">\n          {empty && (\n            <div aria-hidden=\"true\" className=\"pointer-events-none absolute\" style={{ top: s.pad, left: s.pad + 2, color: p.faint, fontSize: s.font }}>\n              {placeholder}\n            </div>\n          )}\n          <div\n            ref={editorRef}\n            id={`${uid}-editor`}\n            role=\"textbox\"\n            aria-multiline=\"true\"\n            aria-labelledby={label ? `${uid}-label` : undefined}\n            aria-label={label ? undefined : \"Rich text editor\"}\n            aria-describedby={helperText ? `${uid}-help` : undefined}\n            contentEditable={!disabled}\n            suppressContentEditableWarning\n            onInput={emit}\n            onKeyDown={onKeyDown}\n            onKeyUp={refreshActive}\n            onMouseUp={refreshActive}\n            onPaste={onPaste}\n            onFocus={() => {\n              setFocused(true);\n              // New lines become <p> (not <div>) in every browser.\n              exec(\"defaultParagraphSeparator\", \"p\");\n            }}\n            onBlur={() => {\n              setFocused(false);\n              saveRange();\n            }}\n            className=\"rte-content overflow-y-auto outline-none\"\n            style={{ minHeight, maxHeight, padding: `${s.pad}px ${s.pad + 2}px`, color: p.text, fontSize: s.font, lineHeight: 1.6, [\"--rte-muted\" as string]: p.muted, [\"--rte-code\" as string]: p.codeBg, [\"--rte-border\" as string]: p.border }}\n          />\n        </div>\n\n        {showCount && (\n          <div className=\"flex items-center justify-end\" style={{ gap: 12, padding: `6px ${s.pad}px 8px`, color: over ? p.error : p.faint, fontSize: s.font - 3, fontVariantNumeric: \"tabular-nums\" }} aria-live=\"polite\">\n            <span>{counts.words} words</span>\n            <span>{maxLength !== undefined ? `${counts.chars}/${maxLength}` : `${counts.chars} characters`}</span>\n          </div>\n        )}\n      </div>\n\n      {helperText && (\n        <span id={`${uid}-help`} style={{ color: p.muted, fontSize: s.font - 2, lineHeight: 1.4 }}>\n          {helperText}\n        </span>\n      )}\n\n      <style>{`\n        .rte-content > * + * { margin-top: 0.6em; }\n        .rte-content p, .rte-content div { margin: 0; }\n        .rte-content h2 { font-size: 1.35em; font-weight: 650; letter-spacing: -0.015em; line-height: 1.3; margin: 0.9em 0 0.3em; }\n        .rte-content h3 { font-size: 1.12em; font-weight: 650; line-height: 1.35; margin: 0.8em 0 0.25em; }\n        .rte-content h2:first-child, .rte-content h3:first-child { margin-top: 0; }\n        .rte-content ul, .rte-content ol { padding-left: 1.4em; margin: 0.4em 0; }\n        .rte-content ul { list-style: disc; }\n        .rte-content ol { list-style: decimal; }\n        .rte-content li + li { margin-top: 0.2em; }\n        .rte-content blockquote { margin: 0.6em 0; padding-left: 0.9em; border-left: 2px solid var(--rte-border); color: var(--rte-muted); }\n        .rte-content code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.88em; padding: 0.12em 0.35em; border-radius: 5px; background: var(--rte-code); }\n        .rte-content a { text-decoration: underline; text-underline-offset: 3px; }\n        .rte-content strong, .rte-content b { font-weight: 650; }\n      `}</style>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}