# AI inline edit

> Document editor with in-place selection rewrite diff (Accept/Reject) and Tab ghost-text completion.

- Kind: block · Category: AI · Interactivity: script
- Install: `npx shadcn@latest add @haze/ai-inline-edit`
- File: `src/components/haze/blocks/ai-inline-edit.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/kbd, @haze/utils
- Docs page: https://hazeui.com/blocks/ai-inline-edit

## Use when
- Supporting in-place AI rewrites inside a document without opening a separate sidebar
- Showing inline ghost-text completions that users can accept with one click or Tab

## Avoid when
- You want a persistent chat/diff sidebar beside the document; use ai-side-panel

## Alternatives
- [ai-side-panel](https://hazeui.com/blocks/ai-side-panel.md)
- [rich-text-editor](https://hazeui.com/ui/rich-text-editor.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` | "haze-ai-inline-edit" | no | Unique prefix for section heading IDs. |
| title | `string` | "Release checklist" | no | Document heading. |
| class | `string` |  | no | Extra classes on the outer section. |

## Accessibility
- Inline diffs use semantic del and ins markup with clear Accept and Reject buttons.
- Ghost-text acceptance has an explicit button alongside the Tab key hint so keyboard and pointer users can trigger it reliably.
- Key Tab: Move across Edit selection, Accept, Reject and ghost-completion controls
- Key Enter / Space: Open the inline rewrite, accept/reject the diff or accept ghost text

## Agent prompts

### Install and use

Install the Haze UI block "AI inline edit" with `npx shadcn@latest add @haze/ai-inline-edit`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import AiInlineEdit from "@/components/haze/blocks/ai-inline-edit.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/blocks/ai-inline-edit.md

### Customize with real content

Adapt the Haze ai-inline-edit block for our product. Replace the demo values of these props with our real content: id, title, class. Keep the layout, the heading level rules and these accessibility details: Inline diffs use semantic del and ins markup with clear Accept and Reject buttons. Ghost-text acceptance has an explicit button alongside the Tab key hint so keyboard and pointer users can trigger it reliably. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using ai-inline-edit, check it fits: use it when supporting in-place ai rewrites inside a document without opening a separate sidebar; showing inline ghost-text completions that users can accept with one click or tab. Avoid it when you want a persistent chat/diff sidebar beside the document; use ai-side-panel. Alternatives: @haze/ai-side-panel, @haze/rich-text-editor.

## Example requests that should lead here
- Add an AI inline edit document block with selection rewrite diff and Tab ghost-text completion

## Source

```astro
---
import { Check, Sparkles, Wand2, X } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Kbd from "../ui/kbd.astro";

interface Props {
  id?: string;
  title?: string;
  class?: string;
}

const {
  id = "haze-ai-inline-edit",
  title = "Release checklist",
  class: className,
} = Astro.props;
---

<section aria-labelledby={`${id}-title`} data-haze-ai-inline-edit class={cn("bg-background px-4 py-10 sm:px-6 sm:py-14", className)}>
  <div class="mx-auto max-w-3xl rounded-2xl border border-border bg-card p-6 sm:p-8">
    <div class="flex flex-wrap items-center justify-between gap-2 border-b border-border pb-4">
      <h2 id={`${id}-title`} class="text-lg/7 font-semibold tracking-tight text-foreground">{title}</h2>
      <div class="flex items-center gap-2">
        <button type="button" data-ie-open class="inline-flex h-8 cursor-pointer items-center gap-1.5 rounded-lg border border-frame bg-field px-2.5 text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
          <Wand2 class="size-3.5 text-muted-foreground" aria-hidden="true" />
          Edit selection
          <Kbd size="sm" keys={["Ctrl", "K"]} />
        </button>
      </div>
    </div>

    <div class="mt-5 space-y-4 text-sm/7 text-foreground">
      <p>
        Before cutting a tagged release, verify every registry artifact builds cleanly and passes contrast checks across all semantic surfaces.
      </p>

      <div data-ie-passage class="rounded-xl border border-frame bg-muted p-3.5">
        <p data-ie-text>
          Run the end-to-end suite locally so that you can catch any regressions before pushing the tag to main.
        </p>

        <div data-ie-diff class="mt-3 rounded-lg border border-frame bg-popover p-3 shadow-key">
          <div class="flex flex-wrap items-center justify-between gap-2">
            <span class="inline-flex items-center gap-1.5 text-xs font-medium text-foreground">
              <Sparkles class="size-3.5" aria-hidden="true" />
              Inline rewrite
            </span>
            <div class="flex items-center gap-1.5">
              <button type="button" data-ie-accept class="inline-flex h-7 cursor-pointer items-center gap-1 rounded-md bg-primary px-2.5 text-xs font-medium text-primary-foreground hover:opacity-92 focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
                <Check class="size-3.5" aria-hidden="true" />
                Accept
              </button>
              <button type="button" data-ie-reject class="inline-flex h-7 cursor-pointer items-center gap-1 rounded-md border border-frame bg-field px-2.5 text-xs font-medium text-muted-foreground hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
                <X class="size-3.5" aria-hidden="true" />
                Reject
              </button>
            </div>
          </div>
          <p class="mt-2 text-xs/6">
            <del class="rounded-sm bg-destructive/14 px-1 text-destructive">Run the end-to-end suite locally so that you can catch any regressions before pushing the tag to main.</del>
            <ins class="ml-1 rounded-sm bg-success/14 px-1 text-success no-underline">Run the E2E suite locally to catch regressions before tagging main.</ins>
          </p>
        </div>
      </div>

      <div class="rounded-xl border border-border bg-muted/40 p-3.5">
        <p class="text-sm/7 text-foreground">
          After publishing the release notes, notify the team in
          <span data-ie-ghost class="text-muted-foreground"> #releases with a link to the changelog and migration guide.</span>
          <button type="button" data-ie-tab class="ml-2 inline-flex h-6 cursor-pointer items-center gap-1 rounded-md border border-frame bg-field px-2 text-[0.6875rem] font-medium text-muted-foreground hover:border-frame-strong hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
            Accept
            <Kbd size="sm">Tab</Kbd>
          </button>
        </p>
      </div>
    </div>

    <p data-ie-status role="status" class="mt-4 min-h-4 text-xs/4 text-muted-foreground"></p>
  </div>
</section>

<script>
  function initInlineEdits() {
    document.querySelectorAll<HTMLElement>("[data-haze-ai-inline-edit]").forEach((root) => {
      if (root.hasAttribute("data-enhanced")) return;
      root.setAttribute("data-enhanced", "");
      const diff = root.querySelector<HTMLElement>("[data-ie-diff]");
      const text = root.querySelector<HTMLElement>("[data-ie-text]");
      const ghost = root.querySelector<HTMLElement>("[data-ie-ghost]");
      const tabBtn = root.querySelector<HTMLButtonElement>("[data-ie-tab]");
      const status = root.querySelector<HTMLElement>("[data-ie-status]");

      root.querySelector<HTMLButtonElement>("[data-ie-open]")?.addEventListener("click", () => {
        if (diff) diff.hidden = false;
        if (status) status.textContent = "Inline rewrite ready to review.";
      });
      root.querySelector<HTMLButtonElement>("[data-ie-accept]")?.addEventListener("click", () => {
        if (text) text.textContent = "Run the E2E suite locally to catch regressions before tagging main.";
        if (diff) diff.hidden = true;
        if (status) status.textContent = "Inline rewrite accepted.";
      });
      root.querySelector<HTMLButtonElement>("[data-ie-reject]")?.addEventListener("click", () => {
        if (diff) diff.hidden = true;
        if (status) status.textContent = "Inline rewrite dismissed.";
      });
      tabBtn?.addEventListener("click", () => {
        if (ghost) ghost.className = "text-foreground";
        tabBtn.hidden = true;
        if (status) status.textContent = "Ghost completion accepted.";
      });
    });
  }
  initInlineEdits();
  document.addEventListener("astro:page-load", initInlineEdits);
</script>
```
