# TextArea suggestions

> useTextSuggest - inline completions for the Inputs TextArea from any source (a local phrase list, the browser, a server or a model) as ghost text or a popover, accepted with Tab, cancelled on every keystroke, and paid for only by the fields that use them.

Canonical: https://salmonui.dev/docs/chat/text-area-suggestions

Smart text suggestions are a hook you wire to the Inputs `TextArea`: `useTextSuggest` from `@salmonui/chat/text-suggest` returns a
`ref` for the field. Give it a `suggest` function that returns the text to
insert at the caret, and the textarea shows it as ghost text the user
accepts with Tab. Nothing in it is tied to a model: the function can match a local
phrase list, call your server, or ask a model through your own endpoint.

```tsx
import { TextArea } from '@salmonui/inputs/text-field';
import { useTextSuggest } from '@salmonui/chat/text-suggest';
import '@salmonui/inputs/text-field/styles.css';
import '@salmonui/chat/text-suggest/styles.css';

function Reply() {
  const suggestion = useTextSuggest({
    suggest: async ({ textBeforeCaret }, { signal }) => {
      const response = await fetch('/api/complete', { method: 'POST', body: textBeforeCaret, signal });
      return response.ok ? response.text() : null;
    },
  });
  return <TextArea label="Reply" name="reply" ref={suggestion.ref} />;
}
```

`suggestion.ref` is a stable callback ref; it works on any native
`<textarea>` and survives re-renders of components that forward refs
through a new callback each render. When the field needs a ref of its own
too, call both from one callback (`(el) => { mine.current = el; suggestion.ref(el); }`). The options
(`suggest`, `suggestionDisplay`, `suggestDelay`, the events,
`localeText.acceptHint` and `container`) are read when events happen, so
inline functions are fine. `suggest` is the only required option.
`container` is the element that receives the ghost text overlay, the hint
and the live region (default: the textarea's parent, which the Inputs
`TextArea` already positions); set it when you wire a bare `<textarea>`
whose parent is not `position: relative`. Every option and type is in the
[API reference](https://salmonui.dev/docs/reference/chat/text-suggest).

The hook keeps no value of its own: the field stays controlled
(`value` / `onValueChange`) or uncontrolled (`defaultValue`) exactly as
without suggestions, and an accepted suggestion reaches it as ordinary
input.

## How a suggestion is requested

- `suggest(context, { signal })` receives `context.value`,
  `context.textBeforeCaret` and `context.textAfterCaret`, and resolves the
  text to insert at the caret, or `null` for nothing.
- A request starts after `suggestDelay` milliseconds without typing
  (default 300), only with a collapsed selection, outside IME composition,
  with some text before the caret, and when the caret is at a word
  boundary (the next character is not a letter or digit).
- Every keystroke aborts the request in flight through `signal` and drops
  its answer, even when your function ignores the signal; a rejection or an
  empty answer shows nothing. Pass `signal` to `fetch` so the
  network request is cancelled too.

## Display, accepting and dismissing

`suggestionDisplay` picks the look:

| Value | Look |
| --- | --- |
| `'inline'` | Ghost text after the caret, in the placeholder color, clipped at the field's edge. |
| `'popover'` | A small box below the caret line; it can be pressed to accept, which is how touch users accept. |
| `'auto'` (default) | Inline on fine pointers, a popover on coarse pointers (`@media (pointer: coarse)`). |

The `'auto'` choice is made by the stylesheet, not by script, so the server
HTML and the hydrated page are identical: the ref never runs on the server,
so `useTextSuggest` adds nothing to the server markup.

While a suggestion is visible:

- **Tab** inserts it at the caret and keeps focus in the textarea. Without a
  visible suggestion Tab moves focus as usual, so keyboard users are never
  trapped.
- **Escape** dismisses it; moving the caret, clicking, blurring or starting
  an IME composition dismisses it too.
- Typing the next characters of the suggestion keeps the rest of it.
- The textarea's `aria-describedby` gains a hint ("Tab to accept", set with
  `localeText.acceptHint`). In popover mode the suggestion is also read by
  a polite live region.

Accepted text goes through the native input path (`insertText`, with a
`setRangeText` plus `input` event fallback): **Ctrl+Z** / **Cmd+Z** undoes
it, `onValueChange` and `onChange` fire, and `FormData`, `form.reset()` and
form libraries see it exactly as typed text. A suggestion that is only
shown is never part of the value.

## Events

| Prop | When |
| --- | --- |
| `onBeforeSuggestionShow(event)` | Before a suggestion shows; `event.text` is the suggestion, `event.preventDefault()` hides it. |
| `onSuggestionShow({ text })` | After it shows. |
| `onSuggestionAccept({ text })` | After Tab or a press inserted it. |

`onBeforeSuggestionShow` is the place for application rules: hide a
suggestion that would exceed `maxLength`, contains a blocked word, or
arrives while the user has paused suggestions. The demo below does the
first and the last on a `TextArea` that also uses `maxLength`,
`clearable`, `resize`, `readOnly` and `disabled`.

### Live demo: Vetoing suggestions on a full-featured TextArea

Live demo: https://salmonui.dev/demos/chat/text-area-suggest-filter

useTextSuggest on a TextArea with maxLength, clearable, resize, read-only and disabled states, where onBeforeSuggestionShow hides suggestions that would not fit or while suggestions are paused.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Select } from '@salmonui/dropdowns/select';
import { TextArea, type TextAreaProps } from '@salmonui/inputs/text-field';
import { useTextSuggest, type SuggestFunction } from '@salmonui/chat/text-suggest';
import { EventLog, useEventLog } from '../../components/demo-kit';

// Canned status updates: a local phrase matcher, no network and no model.
const PHRASES = ['We are investigating reports of slow page loads', 'The issue is resolved and all services are operating normally', 'Next update in 30 minutes'];

const suggestPhrase: SuggestFunction = async ({ textBeforeCaret }) => {
  const before = textBeforeCaret.toLowerCase();
  for (const phrase of PHRASES) {
    for (let k = phrase.length - 1; k > 0; k--) {
      const start = before.length - k;
      if (before.endsWith(phrase.slice(0, k).toLowerCase()) && (start === 0 || /\s/.test(before[start - 1]!))) return phrase.slice(k);
    }
  }
  return null;
};

const MAX = 120;
const RESIZE: readonly NonNullable<TextAreaProps['resize']>[] = ['vertical', 'both', 'none'];

export default function TextAreaSuggestFilterDemo() {
  const [value, setValue] = useState('');
  const [paused, setPaused] = useState(false);
  const [resize, setResize] = useState<NonNullable<TextAreaProps['resize']>>('vertical');
  const [readOnly, setReadOnly] = useState(false);
  const [disabled, setDisabled] = useState(false);
  const [log, push] = useEventLog();
  const suggestion = useTextSuggest({
    suggest: suggestPhrase,
    suggestDelay: 150,
    // Read when a suggestion is about to show: veto it while paused, or when it would not fit in maxLength.
    onBeforeSuggestionShow: (event) => {
      if (paused) {
        event.preventDefault();
        push(`onBeforeSuggestionShow: prevented "${event.text}" (paused)`);
      } else if (value.length + event.text.length > MAX) {
        event.preventDefault();
        push(`onBeforeSuggestionShow: prevented "${event.text}" (${value.length + event.text.length} > ${MAX} characters)`);
      }
    },
    onSuggestionShow: ({ text }) => push(`onSuggestionShow "${text}"`),
    onSuggestionAccept: ({ text }) => push(`onSuggestionAccept "${text}"`),
  });
  return (
    <div style={{ display: 'grid', gap: 12, maxWidth: 480 }}>
      <div style={{ display: 'flex', flexWrap: 'wrap', gap: 16, alignItems: 'center' }}>
        <Checkbox checked={paused} onCheckedChange={(checked) => setPaused(checked)}>
          Pause suggestions
        </Checkbox>
        <Checkbox checked={readOnly} onCheckedChange={(checked) => setReadOnly(checked)}>
          Read-only
        </Checkbox>
        <Checkbox checked={disabled} onCheckedChange={(checked) => setDisabled(checked)}>
          Disabled
        </Checkbox>
        <label style={{ display: 'inline-flex', gap: 8, alignItems: 'center' }}>
          Resize
          <Select aria-label="Resize" data={RESIZE} value={resize} onValueChange={(next) => next != null && setResize(next as NonNullable<TextAreaProps['resize']>)} />
        </label>
      </div>
      <TextArea
        ref={suggestion.ref}
        label="Status update"
        description={`Type We, The or Next and pause. At most ${MAX} characters.`}
        placeholder="What is happening?"
        rows={3}
        resize={resize}
        maxLength={MAX}
        clearable
        readOnly={readOnly}
        disabled={disabled}
        value={value}
        onValueChange={(next, details) => {
          setValue(next);
          if (details.reason !== 'input') push(`onValueChange (${details.reason})`);
        }}
        onFocus={() => push('focus')}
        onBlur={() => push('blur')}
      />
      <output>
        {value.length} / {MAX} characters
      </output>
      <EventLog entries={log} />
    </div>
  );
}
```

## A local phrase list

No network is needed for canned replies. This is the recipe for a fixed
phrase list:

```ts
import type { SuggestFunction } from '@salmonui/chat/text-suggest';

const PHRASES = ['Thank you for your message', 'Kind regards'];

const suggestPhrase: SuggestFunction = async ({ textBeforeCaret }) => {
  const before = textBeforeCaret.toLowerCase();
  for (const phrase of PHRASES) {
    for (let k = phrase.length - 1; k > 0; k--) {
      const start = before.length - k;
      if (before.endsWith(phrase.slice(0, k).toLowerCase()) && (start === 0 || /\s/.test(before[start - 1]!))) return phrase.slice(k);
    }
  }
  return null;
};
```

For a model, build the prompt on your server: a role line describing the
user and any phrase list become part of that prompt. Never send an API key to the browser.

## SSR

`useTextSuggest` is SSR-safe: the hook renders nothing and its ref runs
only in the browser, so a `TextArea` wired to it renders exactly the same
server markup as one without it, and suggestions attach after hydration.
`suggest` is called only from browser events.

## Performance

The suggestion code (request timing, overlay, caret measurement) lives in
`@salmonui/chat/text-suggest` (about 2.6 KB gzip), not in the
`TextArea`: a field without `useTextSuggest` pays nothing for it, and a
field with it pays no lazy-chunk loader. The ghost text is written to an overlay
element, not rendered through React state, so a keystroke re-renders
nothing beyond the textarea's own value.

## Everything else is the TextArea

Rows, columns, resizing, `maxLength`, `placeholder`, `readOnly`,
`disabled`, the label, description, validation, adornments and the
`value` / `onValueChange` pair are the Inputs `TextArea`, documented on the
[TextField and TextArea](https://salmonui.dev/docs/inputs/text-field) page.

## Design notes

- **No role or phrase props.** A user role and a phrase
  list only shape the input of one particular AI request, and Salmon makes
  no request itself: your `suggest` function owns the source. Put the role
  line into the prompt your server builds, and a phrase list into a local
  matcher like the recipe above (no network needed).
- **Common props, methods and lifecycle.** The suggestion is a
  hook on a native `<textarea>`, not a component with its own lifecycle,
  so these follow the catalog-wide rules: persistence is your own state
  (save `value`), direction is the inherited `dir`, labels are
  `localeText`, styling and attributes are `className` and native props on
  the `TextArea`, focus is `ref.current.focus()` / `.blur()`, and setup and
  teardown are an effect.

### Live demo: TextArea suggestions from a phrase list

Live demo: https://salmonui.dev/demos/chat/text-area-suggest

A TextArea with a local phrase matcher as its suggest function - ghost text or a popover, Tab to accept, Escape to dismiss, and the show and accept events in a log.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Radio, RadioGroup } from '@salmonui/inputs/radio-group';
import { TextArea } from '@salmonui/inputs/text-field';
import { useTextSuggest, type SuggestFunction, type SuggestionDisplay } from '@salmonui/chat/text-suggest';
import { EventLog, useEventLog } from '../../components/demo-kit';

// Canned replies for a support desk: a local phrase matcher, no network and no model.
const PHRASES = ['Thank you for reaching out', 'Kind regards', 'Looking forward to hearing from you', 'Please find the invoice attached', 'Your order has shipped'];

const suggestPhrase: SuggestFunction = async ({ textBeforeCaret }) => {
  const before = textBeforeCaret.toLowerCase();
  for (const phrase of PHRASES) {
    for (let k = phrase.length - 1; k > 0; k--) {
      const start = before.length - k;
      if (before.endsWith(phrase.slice(0, k).toLowerCase()) && (start === 0 || /\s/.test(before[start - 1]!))) return phrase.slice(k);
    }
  }
  return null;
};

const DISPLAYS: readonly SuggestionDisplay[] = ['auto', 'inline', 'popover'];

export default function TextAreaSuggestDemo() {
  const [display, setDisplay] = useState<SuggestionDisplay>('auto');
  const [value, setValue] = useState('');
  const [log, push] = useEventLog();
  const suggestion = useTextSuggest({
    suggest: suggestPhrase,
    suggestionDisplay: display,
    suggestDelay: 150,
    onSuggestionShow: ({ text }) => push(`onSuggestionShow "${text}"`),
    onSuggestionAccept: ({ text }) => push(`onSuggestionAccept "${text}"`),
  });
  return (
    <div style={{ display: 'grid', gap: 12, maxWidth: 480 }}>
      <RadioGroup label="Display" name="display" orientation="horizontal" value={display} onValueChange={(value) => setDisplay(value as SuggestionDisplay)}>
        {DISPLAYS.map((d) => (
          <Radio key={d} value={d}>
            {d}
          </Radio>
        ))}
      </RadioGroup>
      <TextArea
        label="Reply"
        rows={4}
        description="Type Thank, Kind, Looking, Please or Your"
        value={value}
        onValueChange={setValue}
        ref={suggestion.ref}
      />
      <output>{value.length} characters</output>
      <EventLog entries={log} />
    </div>
  );
}
```
