ai autocomplete.md
AI Autocomplete
A text box that suggests how your sentence ends in grey after the caret. Tab takes it all, the right arrow takes one word, and typing the same letters keeps it.
Installation
npx shadcn@latest add @kokonutui/ai-autocomplete
Props
| Prop | Type | Default | Description |
|---|---|---|---|
className? |
string |
Extra classes for the root element. |
Source
"use client";
/**
* @author: @dorianbaffier
* @description: AI Autocomplete
* @version: 1.0.0
* @date: 2026-10-16
* @license: MIT
* @website: https://kokonutui.com
* @github: https://github.com/kokonut-labs/kokonutui
*/
import {
type ChangeEvent,
type KeyboardEvent,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { cn } from "@/lib/utils";
export interface AIAutocompleteProps {
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
/**
* Returns how the text could go on, or null for no suggestion. Called once
* typing pauses, only while the caret is at the end. Demo completer when
* omitted.
*/
suggest?: (text: string) => string | null | Promise<string | null>;
/** How long typing has to pause before asking for a suggestion, in ms. */
delay?: number;
placeholder?: string;
/** Accessible name for the text box. */
label?: string;
className?: string;
}
const WORD = /^\s*\S+\s?/;
const WORDS = /\s*\S+\s*/g;
/** The next word of a suggestion, with the space around it. */
function nextWord(ghost: string) {
return ghost.match(WORD)?.[0] ?? ghost;
}
/**
* A text box that offers how your sentence could end, in grey after the
* caret. Tab takes all of it, → takes one word, Esc waves it off, and
* typing the same letters keeps it instead of asking again.
*/
export function AIAutocomplete({
value: valueProp,
defaultValue = "",
onValueChange,
suggest = demoSuggest,
delay = 400,
placeholder = "Start writing…",
label = "Message",
className,
}: AIAutocompleteProps) {
const [ownValue, setOwnValue] = useState(defaultValue);
const value = valueProp ?? ownValue;
const [ghost, setGhost] = useState("");
// ...[Code continues]...
}
// Further code and functions...
/** An email reply with an assistant finishing your sentences. */
export default function AIAutocompleteDemo({
className,
}: {
className?: string;
}) {
return (
<div
className={cn(
"w-full max-w-lg overflow-hidden rounded-2xl border border-zinc-200 bg-white dark:border-zinc-800 dark:bg-zinc-950",
className
)}
>
<div className="divide-y divide-zinc-100 border-zinc-200 border-b text-sm dark:divide-zinc-900 dark:border-zinc-800">
<p className="flex gap-3 px-4 py-2.5">
<span className="w-14 text-zinc-400 dark:text-zinc-500">To</span>
<span className="text-zinc-900 dark:text-zinc-100">Maya Chen</span>
</p>
<p className="flex gap-3 px-4 py-2.5">
<span className="w-14 text-zinc-400 dark:text-zinc-500">Subject</span>
<span className="truncate text-zinc-900 dark:text-zinc-100">
Re: Launch review
</span>
</p>
</div>
<AIAutocomplete
defaultValue={"Hi Maya,\n\nThanks for the update on the "}
label="Reply to Maya"
/>
</div>
);
}