Mention

Suggest names or tags while typing @ in a text field.

Installation

Import the class, and @use the entry point in your global styles: without it the component renders unstyled.

import { Component } from '@angular/core';
import { WrMention } from 'ngwr/mention';

@Component({ imports: [WrMention] })
export class MyComponent {}

Basic

Type @ at the start of a word to open the list; arrows move, Enter or Tab inserts, Esc closes.

<textarea
  wrInput
  wrMention
  [wrMentionItems]="users"
  [(ngModel)]="text"
></textarea>

Custom trigger

triggers sets the characters that open the list, such as # for tags.

<textarea
  wrInput
  wrMention
  [wrMentionItems]="tags"
  [triggers]="['#']"
  [(ngModel)]="text"
></textarea>

Types

interface WrMentionItem {
  label: string;
  [key: string]: unknown;
}
NameDescriptionTypeDefault
WrMentionItemA mentionable entry — extend with any extra metadata.interface—
labelrequiredText inserted and shown in the panel.string—
[key: string]Anything else your app needs back on (wrMentionSelected).unknown—
WrMentionCommitThe (wrMentionSelected) payload.interface—
itemrequiredThe selected entry.WrMentionItem—
triggerrequiredThe character that opened the panel — '@', '#', whatever triggers listed.string—
queryrequiredThe text typed after the trigger, matched as a substring.string—

API

NameDescriptionTypeDefault
wrMentionItemsItems to filter against the typed query.readonly T[][]
triggersTrigger characters that open the panel.readonly string[]['@']
displayWithMaps an item to its display label.(item: T) => string`item.label`
valueWithReturns the text to insert into the textarea on commit. Receives the picked item and the trigger char.((item: T, trigger: string) => string) | null`${trigger}${displayWith(item)}`
filterWithCustom filter.((query: string, item: T) => boolean) | nullcase-insensitive `includes` over `displayWith(item)`.
maxResultsMaximum number of items shown in the panel.number8
panelClassExtra CSS classes for the suggestion panel's overlay pane. The pane is appended to the overlay container, not to this component, so nothing in the consumer's own template encloses it and no descendant rule written around the trigger can reach it. This input is the only per-instance handle on it. A space-separated string works as well as an array.WrClassInputnull
(wrMentionSelected)Emits the selected item, trigger, and query whenever the user commits.WrMentionCommit<T>—

CSS variables

Set these on the component's selector or a wrapper, not on :root: the defaults are declared on ngwr/mention's own element.

VariableDefaultDeclared on
--wr-mention-panel-bordervar(--wr-border-base).wr-mention-panel
--wr-mention-panel-empty-font-sizevar(--wr-text-sm).wr-mention-panel
--wr-mention-panel-empty-padding-xvar(--wr-space-md).wr-mention-panel
--wr-mention-panel-empty-padding-yvar(--wr-space-sm).wr-mention-panel
--wr-mention-panel-font-familyvar(--wr-font-family-base).wr-mention-panel
--wr-mention-panel-font-sizevar(--wr-text-sm).wr-mention-panel
--wr-mention-panel-list-padding-yvar(--wr-space-xs).wr-mention-panel
--wr-mention-panel-option-line-heightvar(--wr-text-sm-leading).wr-mention-panel
--wr-mention-panel-option-padding-xvar(--wr-space-md).wr-mention-panel
--wr-mention-panel-option-padding-yvar(--wr-space-sm).wr-mention-panel
--wr-mention-panel-radiusvar(--wr-border-radius-base).wr-mention-panel

Dependencies

Angular CDKoverlay, portal
Built onOverlay

See also