# @salmonui/dropdowns/mention API reference

> 19 exported symbols of @salmonui/dropdowns/mention: 1 component, 11 interfaces, 6 types and 1 constant.

Canonical: https://salmonui.dev/docs/reference/dropdowns/mention

```ts
import { … } from '@salmonui/dropdowns/mention';
```

## Components

### Mention

Kind: component

Page: https://salmonui.dev/docs/reference/dropdowns/mention/Mention

```ts
function Mention<T = MentionItem>(props: MentionProps<T>): ReactNode;
```

Props: [`MentionProps`](https://salmonui.dev/docs/reference/dropdowns/mention/MentionProps) (its members are the props)

## Interfaces

### MentionBeforeInsertEvent

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionBeforeInsertEvent

```ts
interface MentionBeforeInsertEvent<T = MentionItem> extends MentionInsertEvent<T> {
    preventDefault(): void;
    readonly defaultPrevented: boolean;
}
```

Members (6):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `preventDefault` | `preventDefault(): void;` |  | Cancels the insertion; the list stays open. |
| `defaultPrevented` | `readonly defaultPrevented: boolean;` |  |  |
| `item` | `item: T;` | inherited |  |
| `text` | `text: string;` | inherited | The text that replaces the trigger and the search text. |
| `trigger` | `trigger: string;` | inherited | The trigger character that started this mention. |
| `source` | `source: MentionEventSource;` | inherited |  |

### MentionEditorInsertion

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionEditorInsertion

```ts
interface MentionEditorInsertion {
    readonly start: number;
    readonly end: number;
    readonly text: string;
    readonly suffix: string;
    readonly trigger: string;
    readonly value: string | number;
    readonly label: string;
    readonly item: unknown;
}
```

What `Mention` asks an editor target to insert.

Members (8):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `start` | `readonly start: number;` |  | Offset of the trigger in the text block (the text `getTextBeforeCaret()` returns). |
| `end` | `readonly end: number;` |  | Offset of the caret: `[start, end)` is the trigger and the search text to replace. |
| `text` | `readonly text: string;` |  | The text the textarea path would insert: trigger (with `keepChar`), the insert text, then `suffix`. |
| `suffix` | `readonly suffix: string;` |  | The trigger suffix included at the end of `text` (`''` by default). |
| `trigger` | `readonly trigger: string;` |  |  |
| `value` | `readonly value: string \| number;` |  |  |
| `label` | `readonly label: string;` |  |  |
| `item` | `readonly item: unknown;` |  |  |

### MentionEditorTarget

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionEditorTarget

```ts
interface MentionEditorTarget {
    readonly element: HTMLElement;
    getTextBeforeCaret(): string | null;
    getCaretRect(offset: number): {
        readonly left: number;
        readonly top: number;
        readonly bottom: number;
    } | null;
    insertMention(insertion: MentionEditorInsertion): void;
    subscribe(listener: () => void): () => void;
}
```

A rich text surface `Mention` attaches to instead of a `<textarea>` or
`<input>`: a plain `contenteditable` element through
`createContentEditableMentionTarget`, or the `@salmonui/rich-text` engine.
Offsets count the characters of the current text block, an inline atom
(an inserted mention) as one U+FFFC character.

Members (5):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `element` | `readonly element: HTMLElement;` |  | The editable element (the host gives it `role="textbox"`). `Mention` adds the combobox ARIA attributes to it while the list is open, listens for `keydown` in the capture phase (keys it handles while open call `preventDefault()` and `stopImmediatePropagation()`), for composition and for `focusout`. |
| `getTextBeforeCaret` | `getTextBeforeCaret(): string \| null;` |  | Text of the current text block from its start to the caret; `null` when the selection is not collapsed or not editable. |
| `getCaretRect` | `getCaretRect(offset: number): {     readonly left: number;     readonly top: number;     readonly bottom: number; } \| null;` |  | Viewport rectangle of the character at `offset` of that text, `null` when unknown. |
| `insertMention` | `insertMention(insertion: MentionEditorInsertion): void;` |  | Replaces `[start, end)` of the text block with the mention (and its suffix) and puts the caret after it. |
| `subscribe` | `subscribe(listener: () => void): () => void;` |  | Calls `listener` after the editor applied a text or selection change; returns the unsubscribe function. |

### MentionInsertEvent

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionInsertEvent

```ts
interface MentionInsertEvent<T = MentionItem> {
    readonly item: T;
    readonly text: string;
    readonly trigger: string;
    readonly source: MentionEventSource;
}
```

Members (4):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `item` | `readonly item: T;` |  |  |
| `text` | `readonly text: string;` |  | The text that replaces the trigger and the search text. |
| `trigger` | `readonly trigger: string;` |  | The trigger character that started this mention. |
| `source` | `readonly source: MentionEventSource;` |  |  |

### MentionItem

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionItem

```ts
interface MentionItem {
    readonly value: string | number;
    readonly label?: string;
    readonly disabled?: boolean;
}
```

Default item shape: `{ value, label, disabled }`; a string or number is its own value and label.

Members (3):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `value` | `readonly value: string \| number;` |  |  |
| `label` | `readonly label?: string;` | optional |  |
| `disabled` | `readonly disabled?: boolean;` | optional |  |

### MentionLocaleText

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionLocaleText

```ts
interface MentionLocaleText {
    readonly listLabel: string;
    readonly noResults: string;
    readonly loading: string;
    readonly loadError: string;
}
```

Members (4):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `listLabel` | `readonly listLabel: string;` |  | Accessible name of the suggestion list. |
| `noResults` | `readonly noResults: string;` |  | Shown when nothing matches. |
| `loading` | `readonly loading: string;` |  | Shown while a remote read is pending. |
| `loadError` | `readonly loadError: string;` |  | Shown when a remote read fails. |

### MentionOpenChangeEvent

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionOpenChangeEvent

```ts
interface MentionOpenChangeEvent {
    readonly source: MentionEventSource;
    readonly reason: MentionOpenChangeReason;
}
```

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `source` | `readonly source: MentionEventSource;` |  |  |
| `reason` | `readonly reason: MentionOpenChangeReason;` |  |  |

### MentionProps

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionProps

```ts
interface MentionProps<T = MentionItem> {
    readonly children?: ReactNode;
    readonly target?: RefObject<HTMLTextAreaElement | HTMLInputElement | null>;
    readonly editor?: MentionEditorTarget | null;
    readonly data?: MentionData<T>;
    readonly triggers?: readonly MentionTrigger<T>[];
    readonly requireLeadingSpace?: boolean;
    readonly multiWord?: boolean;
    readonly minSearchLength?: number;
    readonly maxResults?: number;
    readonly searchOperator?: MentionSearchOperator<T>;
    readonly matchCase?: boolean;
    readonly ignoreAccents?: boolean;
    readonly searchFields?: readonly string[];
    readonly debounce?: number;
    readonly highlightMatches?: boolean;
    readonly getItemValue?: (item: T) => string | number;
    readonly getItemLabel?: (item: T) => string;
    readonly isItemDisabled?: (item: T) => boolean;
    readonly getInsertText?: (item: T, trigger: MentionTrigger<T>) => string;
    readonly onBeforeInsert?: (event: MentionBeforeInsertEvent<T>) => void;
    readonly onInsert?: (event: MentionInsertEvent<T>) => void;
    readonly onOpenChange?: (open: boolean, event: MentionOpenChangeEvent) => void;
    readonly onSearchChange?: (event: MentionSearchChangeEvent) => void;
    readonly onLoadError?: (error: unknown) => void;
    readonly renderItem?: (context: MentionRenderItemContext<T>) => ReactNode;
    readonly renderEmpty?: () => ReactNode;
    readonly renderLoading?: () => ReactNode;
    readonly renderError?: (context: {
        readonly error: unknown;
    }) => ReactNode;
    readonly localeText?: Partial<MentionLocaleText>;
    readonly container?: Element | null;
    readonly className?: string;
    readonly style?: CSSProperties;
    readonly classNames?: Partial<Record<MentionPart, string>>;
    readonly styles?: Partial<Record<MentionPart, CSSProperties>>;
    readonly ref?: Ref<HTMLDivElement>;
}
```

Members (35):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `children` | `readonly children?: ReactNode;` | optional | One `<textarea>` or `<input>` (possibly nested in other markup). Omit it and pass `target` instead. |
| `target` | `readonly target?: RefObject<HTMLTextAreaElement \| HTMLInputElement \| null>;` | optional | The field to attach to when it is not a child. |
| `editor` | `readonly editor?: MentionEditorTarget \| null;` | optional | A `contenteditable` surface to attach to instead of a field: the result of `createContentEditableMentionTarget(element)` or the target a rich text editor provides. Keep it stable across renders; it wins over `target` and children fields. |
| `data` | `readonly data?: MentionData<T>;` | optional | Suggestions for triggers without their own `data`. |
| `triggers` | `readonly triggers?: readonly MentionTrigger<T>[];` | optional | Trigger characters, each with optional own data (default `[{ char: '@' }]`). |
| `requireLeadingSpace` | `readonly requireLeadingSpace?: boolean;` | optional | A trigger only counts at the start or after whitespace (default `true`). |
| `multiWord` | `readonly multiWord?: boolean;` | optional | The search text may contain spaces. |
| `minSearchLength` | `readonly minSearchLength?: number;` | optional | Characters typed after the trigger before the list opens. |
| `maxResults` | `readonly maxResults?: number;` | optional | Suggestions shown at most. |
| `searchOperator` | `readonly searchOperator?: MentionSearchOperator<T>;` | optional | Default `'contains'`. |
| `matchCase` | `readonly matchCase?: boolean;` | optional |  |
| `ignoreAccents` | `readonly ignoreAccents?: boolean;` | optional |  |
| `searchFields` | `readonly searchFields?: readonly string[];` | optional | Fields a remote source searches (default `['label']`). |
| `debounce` | `readonly debounce?: number;` | optional | Delay in ms before a remote read (default 300); local data filters on every keystroke. |
| `highlightMatches` | `readonly highlightMatches?: boolean;` | optional | Mark the matched text in each suggestion (`<mark>`). |
| `getItemValue` | `readonly getItemValue?: (item: T) => string \| number;` | optional |  |
| `getItemLabel` | `readonly getItemLabel?: (item: T) => string;` | optional |  |
| `isItemDisabled` | `readonly isItemDisabled?: (item: T) => boolean;` | optional |  |
| `getInsertText` | `readonly getInsertText?: (item: T, trigger: MentionTrigger<T>) => string;` | optional | Text inserted for an item (default its label). |
| `onBeforeInsert` | `readonly onBeforeInsert?: (event: MentionBeforeInsertEvent<T>) => void;` | optional |  |
| `onInsert` | `readonly onInsert?: (event: MentionInsertEvent<T>) => void;` | optional |  |
| `onOpenChange` | `readonly onOpenChange?: (open: boolean, event: MentionOpenChangeEvent) => void;` | optional |  |
| `onSearchChange` | `readonly onSearchChange?: (event: MentionSearchChangeEvent) => void;` | optional |  |
| `onLoadError` | `readonly onLoadError?: (error: unknown) => void;` | optional |  |
| `renderItem` | `readonly renderItem?: (context: MentionRenderItemContext<T>) => ReactNode;` | optional |  |
| `renderEmpty` | `readonly renderEmpty?: () => ReactNode;` | optional |  |
| `renderLoading` | `readonly renderLoading?: () => ReactNode;` | optional |  |
| `renderError` | `readonly renderError?: (context: {     readonly error: unknown; }) => ReactNode;` | optional |  |
| `localeText` | `readonly localeText?: Partial<MentionLocaleText>;` | optional |  |
| `container` | `readonly container?: Element \| null;` | optional | Portal container of the popup (default the field's closest theme scope, else `document.body`). |
| `className` | `readonly className?: string;` | optional |  |
| `style` | `readonly style?: CSSProperties;` | optional |  |
| `classNames` | `readonly classNames?: Partial<Record<MentionPart, string>>;` | optional |  |
| `styles` | `readonly styles?: Partial<Record<MentionPart, CSSProperties>>;` | optional |  |
| `ref` | `readonly ref?: Ref<HTMLDivElement>;` | optional | The root `<div>` that wraps `children`. |

### MentionRenderItemContext

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionRenderItemContext

```ts
interface MentionRenderItemContext<T = MentionItem> {
    readonly item: T;
    readonly label: string;
    readonly active: boolean;
    readonly disabled: boolean;
    readonly matches: readonly [
        number,
        number
    ] | null;
    readonly trigger: string;
}
```

Members (6):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `item` | `readonly item: T;` |  |  |
| `label` | `readonly label: string;` |  |  |
| `active` | `readonly active: boolean;` |  |  |
| `disabled` | `readonly disabled: boolean;` |  |  |
| `matches` | `readonly matches: readonly [     number,     number ] \| null;` |  | Matched range of `label` when `highlightMatches` is on, else null. |
| `trigger` | `readonly trigger: string;` |  |  |

### MentionSearchChangeEvent

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionSearchChangeEvent

```ts
interface MentionSearchChangeEvent {
    readonly text: string;
    readonly trigger: string;
}
```

Members (2):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `text` | `readonly text: string;` |  |  |
| `trigger` | `readonly trigger: string;` |  |  |

### MentionTrigger

Kind: interface

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionTrigger

```ts
interface MentionTrigger<T = MentionItem> {
    readonly char: string;
    readonly data?: MentionData<T>;
    readonly keepChar?: boolean;
    readonly suffix?: string;
}
```

Members (4):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `char` | `readonly char: string;` |  | The trigger character (or string), for example `'@'` or `'#'`. |
| `data` | `readonly data?: MentionData<T>;` | optional | Suggestions for this trigger; defaults to the `Mention` `data`. |
| `keepChar` | `readonly keepChar?: boolean;` | optional | Keep the trigger character in the inserted text. |
| `suffix` | `readonly suffix?: string;` | optional | Text appended after the insertion, such as `' '` or `'\n'`. |

## Types

### MentionData

Kind: type

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionData

```ts
type MentionData<T> = readonly T[] | MentionDataSource<T>;
```

Local items or a remote source.

### MentionDataSource

Kind: type

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionDataSource

```ts
type MentionDataSource<T> = Pick<DataSource<T>, 'read'>;
```

Remote suggestions: any `@salmonui/data` source (or another object with the
structural `read()` of the Data Grid data source contract). The search
text is sent as `query.search`, the result window as `range`.

Members (1):

| Name | Signature | Flags | Description |
| --- | --- | --- | --- |
| `read` | `read: (request: DataSourceRequest) => DataSourceResult<T> \| Promise<DataSourceResult<T>>;` | inherited |  |

### MentionEventSource

Kind: type

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionEventSource

```ts
type MentionEventSource = 'pointer' | 'keyboard' | 'touch' | 'api';
```

What the change and insert callbacks report as the cause.

### MentionOpenChangeReason

Kind: type

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionOpenChangeReason

```ts
type MentionOpenChangeReason = 'trigger' | 'insert' | 'escape' | 'outside' | 'blur' | 'caret' | 'tab';
```

### MentionPart

Kind: type

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionPart

```ts
type MentionPart = 'root' | 'popup' | 'list' | 'option';
```

### MentionSearchOperator

Kind: type

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MentionSearchOperator

```ts
type MentionSearchOperator<T> = SearchOperator<T>;
```

`'startsWith' | 'contains' | 'endsWith'` or a predicate; Mention defaults to `'contains'`.

## Constants

### MENTION_DEFAULT_LOCALE_TEXT

Kind: const

Page: https://salmonui.dev/docs/reference/dropdowns/mention/MENTION_DEFAULT_LOCALE_TEXT

```ts
const MENTION_DEFAULT_LOCALE_TEXT: MentionLocaleText;
```
