PXD
v0.2.1-dev

Mention

Mention people or entities inside rich text with @ or any other configured trigger, backed by a searchable suggestion list.

Default

The component inherits from popover and supports all the properties of popover. For details, See: Popover.

<script setup>
import { ref } from 'vue'

const value = ref('Hello <mention key="1">Alice</mention>')

const options = [
  { label: 'Alice', value: '1' },
  { label: 'Bob', value: '2' },
  { label: 'Charlie', value: '3' },
]
</script>

<template>
  <PMention v-model="value" class="max-w-md" :options="options" placeholder="Type @ to mention" search-placeholder="Search..." />
</template>

remote-filter turns the search remote: a non-empty query goes to it 200ms after the last keystroke instead of through the local filter-method, and the options it returns replace the suggestion list while the query is active.

<script setup>
import { ref } from 'vue'

const value = ref('')

const all = [
  { label: 'Alice', value: '1' },
  { label: 'Bob', value: '2' },
  { label: 'Charlie', value: '3' },
  { label: 'Diana', value: '4' },
]

async function search(query) {
  await new Promise((resolve) => setTimeout(resolve, 200))
  const needle = query.toLowerCase()
  return all.filter((item) => item.label.toLowerCase().includes(needle))
}
</script>

<template>
  <PMention
    v-model="value"
    class="max-w-md"
    :remote-filter="search"
    placeholder="Type @ then search in the popover"
    search-placeholder="Filter people"
  />
</template>

Multiple triggers

triggers lists every keyword that opens the popover. When options is a function it receives the keyword that was typed, so one editor can offer files on @ and commands on /.

<script setup>
import { ref } from 'vue'

const value = ref('')

const files = [
  { label: 'App.vue', value: 'src/App.vue' },
  { label: 'router.ts', value: 'src/router.ts' },
  { label: 'mention-html.ts', value: 'src/utils/mention-html.ts' },
]

const commands = [
  { label: 'review', value: 'review' },
  { label: 'test', value: 'test' },
]

function optionsFor(trigger) {
  return trigger === '/' ? commands : files
}
</script>

<template>
  <PMention
    v-model="value"
    class="max-w-md"
    :triggers="['@', '/']"
    :options="optionsFor"
    placeholder="Type @ for a file or / for a command"
  />
</template>

remote-filter receives the keyword as its second argument, so one search can serve every trigger.

<script setup>
import { ref } from 'vue'

const value = ref('')

const files = ['src/App.vue', 'src/router.ts', 'src/utils/mention-html.ts']
const commands = ['review', 'test', 'lint']

async function search(query, trigger) {
  await new Promise((resolve) => setTimeout(resolve, 200))
  const source = trigger === '/' ? commands : files
  const needle = query.toLowerCase()

  return source
    .filter((entry) => entry.toLowerCase().includes(needle))
    .map((entry) => ({ label: entry, value: entry }))
}
</script>

<template>
  <PMention
    v-model="value"
    class="max-w-md"
    :triggers="['@', '/']"
    :remote-filter="search"
    placeholder="Type @ for a file or / for a command"
    search-placeholder="Filter..."
  />
</template>

Submit on Enter

submit fires with the current HTML on Enter (Shift+Enter still breaks the line, IME confirmation is untouched). Set submit-on-enter to false to handle Enter yourself.

<script setup>
import { ref } from 'vue'

const value = ref('')
const sent = ref('')

const options = [
  { label: 'Alice', value: '1' },
  { label: 'Bob', value: '2' },
]

function send(html) {
  sent.value = html
  value.value = ''
}
</script>

<template>
  <PStack direction="vertical" class="max-w-md gap-2">
    <PMention
      v-model="value"
      :options="options"
      :rows="3"
      placeholder="Type @ to mention, press Enter to send"
      @submit="send"
    />
    <PText v-if="sent" class="text-sm text-foreground-secondary">Sent: {{ sent }}</PText>
  </PStack>
</template>

Mention click

<script setup>
import { ref } from 'vue'

const value = ref('Ping <mention key="1">Alice</mention>')
const last = ref('')

const options = [
  { label: 'Alice', value: '1' },
  { label: 'Bob', value: '2' },
]
</script>

<template>
  <PStack direction="vertical" class="max-w-md">
    <PMention
      v-model="value"
      :options="options"
      placeholder="Type @ to mention"
      search-placeholder="Search..."
      @mention-click="(payload) => (last = `${payload.label}#${payload.key}`)"
    />
    <PText v-if="last" class="text-sm text-foreground-secondary">Clicked: {{ last }}</PText>
  </PStack>
</template>

Props

NameTypeDefaultDescription
model-valuestring''Mention HTML: text + <mention key="...">label</mention>
optionsListOptions | ((trigger: string) => ListOptions | Promise<ListOptions>)() => []Options shown when the popover opens; a function receives the trigger that opened it and may return a promise, which keeps the list on its loading state
triggersstring[]() => ['@']Keywords that open the popover, in the order they are matched
size'sm' | 'md' | 'lg'-Size of the editor, falls back to the config provider size
virtualbooleanfalseEnable virtualized rendering for large option sets
filter-method(option: ListOption, query: string) => booleanfuzzy match on label and descriptionPredicate deciding which option shows while the query is being typed
remote-filter(query: string, trigger: string) => ListOptions | Promise<ListOptions>-Search through it instead of the local filter-method; trigger is the keyword that opened the popover
filter-debouncebooleantrueDebounce the remote-filter search by 200ms while typing; false searches on every keystroke
submit-on-enterbooleantrueSubmit on Enter instead of inserting a line break
rowsnumber1Visible lines; 1 keeps the size-based minimum height
auto-growbooleantrueGrow the editor with its content; false fixes the height at rows with internal scrolling
max-rowsnumber-Cap the height at this many lines and scroll internally beyond it
editor-classComponentClass-Class merged into the editable area
editor-attrsRecord<string, unknown>-Attributes bound to the editable area (data-*, aria-*, …)
before-insert(payload: MentionSelectPayload) => boolean | void-Return false to skip the default chip insertion and write the value yourself
placeholderstring''Editor placeholder
search-placeholderstring''Suggestion search input placeholder
disabledbooleanfalseMake the editor read-only and stop the suggestion popover from opening
close-on-press-escapebooleantrueClose the suggestion popover when pressing Escape

Events

NameTypeDescription
change(value: string, details: ChangeDetails) => voidEmitted when the editor HTML changes.
submit(value: string) => voidEmitted when Enter is pressed with submit-on-enter enabled.
select(payload: MentionSelectPayload) => voidEmitted when an option is picked, before the chip is inserted.
mention-click(payload: MentionClickPayload) => voidEmitted when a mention chip is clicked.
mention-remove(payload: MentionRemovePayload) => voidEmitted after Backspace or Delete removes a chip, including a selection that spans it.
update:model-value(value: string) => voidEmitted when the editor HTML changes.
interface MentionSelectPayload {
  key: string
  label: string
  trigger: string
  item: ListOptionSelected
}

interface MentionClickPayload {
  key: string
  label: string
  trigger: string
  event: MouseEvent
}

interface MentionRemovePayload {
  key: string
  label: string
  trigger: string
  /** The value before the chip was removed, ready to be written back. */
  html: string
}

Slots

NameDescription
itemCustom list item: { item, index, group, groupIndex }
groupCustom group label: { group, index }
emptyCustom empty search result content
footerBottom area of the dropdown, below the option list.

Methods

NameTypeDescription
focus() => voidFocus the editor.
blur() => voidBlur the editor.
clear() => voidEmpty the editor and emit the cleared value.
setText(text: string) => voidReplace the content with escaped plain text and park the caret at the end.
insertText(text: string) => booleanInsert escaped plain text at the caret, or at the end when the editor is not focused.
insertMention(key: string, label: string, trigger?: string) => booleanInsert a chip; replaces the active trigger span, otherwise inserts at the caret with the given trigger ('@' by default).
replaceQuery(text: string) => booleanReplace the active trigger span with escaped plain text, or delete it when empty; only works while a trigger span is alive, e.g. from a select / before-insert handler.
openSuggest(trigger?: string) => voidOpen the suggestion popover for a trigger, the first configured one by default.
closeSuggest() => voidClose the suggestion popover.

Notes

  • Type a configured trigger in the editor to open suggestions. It stays until you pick an item (so typing a literal @ remains possible if you dismiss the popover).
  • A trigger only fires at content start, after whitespace, or right after a chip — never mid-word, so @ inside an email or URL stays plain text.
  • Escape closes the popover through the search input and the overlay; a press outside closes it and leaves focus wherever the user moved it.
  • Chips carry the trigger that created them in a trigger attribute, omitted for the default @: <mention key="review" trigger="/">review</mention>.
  • Mentions are read-only chips (contenteditable="false"). Both Backspace and Delete remove a whole chip and leave the caret on the text beside it.
  • Picking a chip leaves a trailing space after it, so two chips picked back to back never run together in the value. That space belongs to the chip: the Backspace that deletes a chip takes it along.
  • Paste keeps only legal <mention key> nodes; everything else becomes plain text.
  • Undo covers only what the browser itself edits: typing and paste. Chip insertion and chip removal write the DOM directly, so Ctrl/Cmd+Z does not bring a removed chip back — mention-remove exists for the app to provide that itself.
  • pxd/utils/mention-html exports the value-format helpers: mentionHtmlToText, parseMentionHtml and escapePlainTextAsHtml for plain-text output, token parsing and safe refills.

A removed chip is reported with the value it was removed from, so the app can restore it on its own undo:

function onRemove(payload: MentionRemovePayload) {
  lastRemoved = payload.html
}

function undo() {
  value.value = lastRemoved
}

Source

Source