Mention input

A text field that suggests people or things after @ (or any trigger you choose), writes the chosen one into the text, and posts their ids beside it.

Mention input

Needs Alpine and the package's script. See getting started. Without them it is an ordinary textarea.

Use it

<x-ds::mention-input
    name="body"
    label="Comment"
    :items="$members->map(fn ($m) => ['id' => $m->id, 'label' => $m->name, 'meta' => $m->team, 'src' => $m->avatar_url])"
/>

{{-- People after @, issues after #, written as tokens the server can parse --}}
<x-ds::mention-input
    name="body"
    label="Update"
    :triggers="['@' => $people, '#' => $issues]"
    insert="@[label](id)"
    :atomic="true"
/>

{{-- One line --}}
<x-ds::mention-input name="title" label="Title" :items="$people" :multiline="false" />

What it posts

FieldExample
bodyThanks @Maya Patel, can you look at #Fix login redirect?
body_mentions[]7, 101: the id of each row chosen, once each

A mention whose text is later edited ("@Maya Patxel") stops posting its id, and Undo brings it back. With insert="@[label](https://github.com/shipbytes/design-system/blob/master/docs/components/id)" the text holds @[Maya Patel](https://github.com/shipbytes/design-system/blob/master/docs/components/7), which you can parse on the server instead: preg_match_all('/[@#]\[([^\]]+)\]\((\d+)\)/', $body, $m). wire:model and x-model bind the text.

Props

PropTypeDefaultWhat it does
namestring—The field the text posts under.
labelstring—The field's label.
valuestring—The text. Mentions already in it post their ids.
itemsarray[]The rows: id, label, and optionally meta (a second line to match too), src or initials (an avatar). Arrays or objects.
triggersstring | array"@"The character that opens the list, or a map of character ⇒ rows: ['@' => $people, '#' => $issues].
insertstring"@label"What a row writes. @ is the trigger, label and id the row's: "@[label](https://github.com/shipbytes/design-system/blob/master/docs/components/id)" writes @[Maya Patel](https://github.com/shipbytes/design-system/blob/master/docs/components/7). A space follows.
minCharsint0Characters to type after the trigger before the list opens.
maxint8Rows shown at once.
multilinebooltruefalse for a one-line text field.
rowsint3The textarea's height.
atomicboolfalseBackspace just after a mention deletes all of it.
loadingboolfalseThe list says "Loading…" while you fetch.
mentionsNamestring{name}_mentions[]Where the ids post.
placeholderstring—
listLabelstring"Suggestions"The list's name for screen readers.
emptyTextstring"No matches"
loadingTextstring"Loading…"
helpstring—Guidance under the field.
errorstring—Replaces help, marks the field invalid.
disabledboolfalse

Anything else (id, required, maxlength) goes on the field; class on the root.

What it does

  • Typing @ at the start of a word opens the list under it; the letters after it filter the list ("ma" finds Maya Patel and Omar Malik, the first first). [email protected] does not open it.
  • Up and Down pick a row, Enter or Tab writes it in, Escape closes the list (and only the list, inside a modal). A click works too.
  • It closes by itself when you type a space and nothing matches, move the caret away, or leave the field.
  • Moving back into a word you already typed does not reopen it.

Long lists

Don't send every user. Listen for search, which fires 200 ms after typing stops with { trigger, query }, and render the rows that match:

<x-ds::mention-input
    name="body"
    label="Comment"
    :items="$matches"
    :loading="$searching"
    wire:model="body"
    x-on:search="$wire.findPeople($event.detail.query)"
/>

Accessibility

  • With the script the field is a combobox: a screen reader says the list is open, and reads each row as the arrows reach it, while focus stays in the text. That is role="combobox" on a textarea: WAI-ARIA 1.2 allows it (the combobox is the input that controls the list), ARIA in HTML's table of allowed roles does not, so an HTML validator flags it. Kept on purpose; the reasons are in the spec. Without the script the field is a plain, valid textarea.
  • "No matches" and "Loading…" are announced.
  • The list sits under the @ you typed, wherever it is in the text.

Don't

  • Don't trust the ids. Check on the server that each is someone the author may mention.
  • Don't expect highlighted names in the field. It is plain text; style the mentions when you display the saved text.
  • Don't use it for picking one person. That is a combobox.

More in specs/mention-input.md.


From the package repo: edit this page on GitHub.

Was this page helpful?