{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ghost-text-demo",
  "title": "Ghost Text Demo",
  "description": "A worked example of Ghost Text: An input or textarea that shows a suggested completion right after the caret. Tab takes it, Escape puts it away, and typing the suggested letters keeps it in place.",
  "registryDependencies": [
    "https://ui.tinkererslabs.com/r/ghost-text.json"
  ],
  "files": [
    {
      "path": "components/demos/ghost-text-demo.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { GhostText } from \"@/registry/default/ghost-text/ghost-text\"\n\nconst subjects = [\n  \"Refund for the duplicate charge on your March invoice\",\n  \"Your export is ready to download\",\n  \"Resetting two-factor authentication on your account\",\n]\n\nconst sentences = [\n  \"Thanks for writing in about the duplicate charge.\",\n  \"I have refunded the second payment to the card you used.\",\n  \"It can take five to ten business days to appear on your statement.\",\n  \"Your plan and your saved projects are not affected.\",\n  \"Reply here if anything still looks wrong and I will take another look.\",\n]\n\nfunction wait(ms: number, signal: AbortSignal) {\n  return new Promise<void>((resolve, reject) => {\n    const timer = setTimeout(resolve, ms)\n    signal.addEventListener(\"abort\", () => {\n      clearTimeout(timer)\n      reject(signal.reason)\n    })\n  })\n}\n\nfunction completeFrom(options: string[], fragment: string) {\n  if (fragment.trim().length < 2) return \"\"\n\n  const lower = fragment.toLowerCase()\n  const match = options.find(\n    (option) =>\n      option.toLowerCase().startsWith(lower) && option.length > fragment.length\n  )\n\n  return match ? match.slice(fragment.length) : \"\"\n}\n\nasync function suggestSubject(text: string, signal: AbortSignal) {\n  await wait(180, signal)\n  return completeFrom(subjects, text)\n}\n\nasync function suggestReply(text: string, signal: AbortSignal) {\n  await wait(240, signal)\n\n  const fragment = text.slice(\n    Math.max(text.lastIndexOf(\". \"), text.lastIndexOf(\"\\n\")) + 1\n  )\n  const trimmed = fragment.trimStart()\n\n  // After a finished sentence, offer the next one nobody has written yet.\n  if (trimmed === \"\" && /[.!?]\\s*$/.test(text)) {\n    const next = sentences.find((sentence) => !text.includes(sentence))\n    if (!next) return \"\"\n    return text.endsWith(\" \") || text.endsWith(\"\\n\") ? next : ` ${next}`\n  }\n\n  return completeFrom(sentences, trimmed)\n}\n\nexport function GhostTextDemo() {\n  const [subject, setSubject] = React.useState(\"\")\n  const [reply, setReply] = React.useState(\"Hi Priya,\\n\\n\")\n\n  return (\n    <div className=\"bg-background border-border grid w-full max-w-[34rem] gap-4 rounded-[1.25rem] border p-5 shadow-sm\">\n      <div className=\"grid gap-1.5\">\n        <label className=\"text-sm font-medium\" htmlFor=\"ghost-text-subject\">\n          Subject\n        </label>\n        <GhostText\n          id=\"ghost-text-subject\"\n          getSuggestion={suggestSubject}\n          onValueChange={setSubject}\n          placeholder=\"Try typing Refund\"\n          value={subject}\n        />\n      </div>\n\n      <div className=\"grid gap-1.5\">\n        <label className=\"text-sm font-medium\" htmlFor=\"ghost-text-reply\">\n          Reply\n        </label>\n        <GhostText\n          id=\"ghost-text-reply\"\n          getSuggestion={suggestReply}\n          multiline\n          onValueChange={setReply}\n          rows={6}\n          value={reply}\n        />\n      </div>\n\n      <p className=\"text-muted-foreground text-xs leading-relaxed\">\n        Start with Thanks. Tab takes the whole suggestion, Ctrl or Cmd with the\n        right arrow takes one word, and Escape puts it away. Typing the\n        suggested letters keeps it in place.\n      </p>\n    </div>\n  )\n}\n",
      "type": "registry:example"
    }
  ],
  "type": "registry:example"
}