SmartTextarea
SmartTextarea suggests the end of the sentence being typed as ghost text, accepted with Tab and dismissed with Escape, from a role, a context and phrases you give. It works with every kind of Angular form, respects IME composition and announces its suggestions, with any model through a small adapter to your own backend.
Import #
import { SmartTextarea } from '@exhaustive-ui/core/smarttextarea';
import { provideAIAssist } from '@exhaustive-ui/core/aiassist';The adapter contract, shared with InlineAIAssist and SmartPasteButton, comes from @exhaustive-ui/core/aiassist.
Basic #
SmartTextarea is a textarea that suggests the end of the sentence being typed, shown as gray ghost text after the caret. Tab accepts it, Escape dismisses it, and typing goes on as usual: a suggestion that matches the typed letters stays. The suggestion is asked after a pause in the typing, set by delay, with the text around the caret.
The demos of this page answer offline with fakeAIAssistAdapter; type "Thank you for", "I will send" or "Let me know".
Adapter and Streaming #
SmartTextarea sends no request of its own: an adapter connects it to the model of your choice, through an endpoint of your application that holds the API key. Never put the key in the browser. The request has the completion task, the text before the caret, instructions built from role, context and phrases, and a context with them, the text after the caret and maxWords.
The answer is the words that come next, as a string, a promise or a stream of chunks, shown as they arrive. A request is cancelled with its AbortSignal as soon as the user types, moves the caret or leaves the field, so a fast and small model fits this task best. Give the adapter with adapter, or to all the components with provideAIAssist; the same endpoint serves InlineAIAssist and SmartPasteButton.
import { provideAIAssist } from '@exhaustive-ui/core/aiassist';
provideAIAssist({
complete: async function* ({ task, text, instructions, context, signal }) {
const response = await fetch('/api/ai', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ task, text, instructions, context }),
signal // aborted when the user types on
});
if (!response.ok || !response.body) {
throw new Error(`The server answered ${response.status}`);
}
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
for (let chunk = await reader.read(); !chunk.done; chunk = await reader.read()) {
yield chunk.value;
}
}
});Customization #
role tells the model who writes, context gives it the facts it may use, such as opening hours or an order number, and phrases lists sentences to suggest as they are: a phrase whose beginning matches the typed text is suggested at once, without calling the adapter. acceptKey sets the key that accepts, Tab, ArrowRight or Enter, maxWords limits the length of a suggestion, minTextLength the text to type first, and showKeyHint shows the key after the suggestion.
Forms #
Reactive Forms #
SmartTextarea can also be used with reactive forms, bound with formControlName. An accepted suggestion updates the value of the control like typed text.
Signal Forms #
With Angular Signal Forms, the [formField] directive binds the component to a field, which also sets its disabled, read-only and invalid states.
Accessibility #
Screen Reader
SmartTextarea renders a native textarea, labelled with inputId and a label, ariaLabelledBy or ariaLabel. The textarea is described by a hidden hint, "Suggestions appear as you type. Press Tab to accept one, Escape to dismiss it.", added to ariaDescribedBy. The ghost text is hidden from assistive technologies: a polite live region announces each suggestion with its key, such as "Suggestion: your quick answer. Press Tab to accept.", and "Suggestion accepted" once accepted. announce turns these announcements off.
While a suggestion is shown, the accept key accepts it instead of its usual action, so Tab moves the focus out of the field only when there is no suggestion; choose ArrowRight with acceptKey to keep Tab for the focus. No suggestion is asked during an IME composition, nor in a read-only or disabled field, and suggestions turns them off. The ghost text has a 4.5:1 contrast or more in both color schemes, and the texts are translated with labels, and the smartTextareaHint, aiSuggestion and aiSuggestionAccepted keys of the aria translation.
Keyboard Support
| Key | Function |
|---|---|
| tab | Accepts the suggestion, or moves the focus when there is none. The acceptKey property changes the key. |
| escape | Dismisses the suggestion. |
| Any other key | Types as usual; the suggestion stays while it matches the typed letters. |