Mention

Mention suggests users, tags or commands while a trigger such as @ or # is typed in a textarea or an input, and inserts the chosen suggestion in the text. Its value is plain text, and it works with every kind of Angular form.

import { Mention } from '@exhaustive-ui/core/mention';

Mention is bound with ngModel and its value is the plain text. Typing @ at the start of a word emits onSearch with the text typed after it as event.query; the handler answers by setting suggestions. Choosing one with the mouse, enter or tab replaces the query with the trigger, the suggestion and a space. A mention typed right before a word keeps that word, while a suggestion chosen with the caret moved into an existing mention replaces the whole mention.

triggers lists the characters that start a mention, @ only by default. event.trigger tells the search handler which one was typed, here users for @ and tags for #, and the item template receives it as trigger. A trigger only counts at the start of a word, so an email address does not open the panel.

Suggestions can be objects: optionLabel names the property that is inserted in the text. The item template renders each suggestion, with an avatar for instance, and header, footer and empty templates customize the panel. With autoResize, the textarea grows with its content.

The search handler can answer later, after a request. delay waits for a pause in typing before onSearch and minQueryLength for a few characters, and loading shows a loader until the suggestions arrive. Here a fake server answers after 600 milliseconds; the handler keeps only the latest answer.

With multiline set to false, Mention renders a single-line input instead of a textarea. The panel opens under the trigger by default; position="field" opens it under the whole field, like an AutoComplete.

Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.

Mention provides small and large sizes as alternatives to the default, like the other form fields.

When disabled is present, the field cannot be edited or focused. With readonly, the text can be selected but not changed, and no panel opens.

The invalid state is applied using the invalid property to indicate failed validation, here until someone is mentioned.

The value is the plain text, so the usual validators such as required and maxlength work as on a textarea.

Mention can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.

Mention can also be used with Angular Signal Forms. The [formField] directive binds the component to a field holding the text. The field also sets the invalid and touched states of the component, so it is styled as invalid once the field is touched or the form is submitted, like the message below.

Screen Reader

The single-line input follows the combobox pattern with a listbox popup, while the focus stays in the field: it has the combobox role, aria-autocomplete="list" and aria-expanded, and while suggestions are shown aria-controls points to the listbox and aria-activedescendant to the highlighted option, which has aria-selected along with its position in the list (aria-posinset, aria-setsize).

A textarea cannot take the combobox role in HTML, and aria-expanded is not allowed on a textbox, so the multiline field keeps its native textbox role. It has aria-autocomplete="list" and aria-haspopup="listbox", and the same aria-controls and aria-activedescendant while suggestions are shown. Screen readers that follow aria-activedescendant on a textbox, such as NVDA and JAWS with Chrome, Edge or Firefox, read the highlighted option as the arrow keys move it. The field is never announced as expanded or collapsed: the live region below is what tells that suggestions appeared. Support for aria-activedescendant on a textbox is weaker in VoiceOver, which may not read the highlighted option; the live region and the inserted text are still announced. Set multiline to false when a single line is enough.

A polite live region announces the number of suggestions, the empty message and the loading state. Its texts come from searchMessage, emptyMessage and loadingMessage, or from the searchMessage and emptySearchMessage keys of the translation configuration, and the listbox is labelled with the aria.listLabel key. Give the field a name with a label and inputId, ariaLabelledBy or ariaLabel, and describe it with ariaDescribedBy.

<label for="comment">Comment</label>
<p-mention inputId="comment" [suggestions]="suggestions" (onSearch)="search($event)" />

<span id="title-label">Task title</span>
<p-mention ariaLabelledBy="title-label" [multiline]="false" [suggestions]="suggestions" (onSearch)="search($event)" />

Keyboard Support

KeyFunction
trigger such as @Typed at the start of a word, opens the suggestions; the following characters filter them.
down arrowHighlights the next suggestion.
up arrowHighlights the previous suggestion.
page down / page upHighlights the last or the first suggestion.
enter / tabInserts the highlighted suggestion. Without one, enter inserts a new line and tab moves the focus as usual.
escapeCloses the suggestions; they stay closed until the caret leaves the mention.
spaceEnds the mention and closes the suggestions.
home / end, left / right arrowMove the caret in the text, opening the suggestions again when it enters a mention.