Mentions
Typing a prefix such as @ in multiline text opens a suggestion list. Selecting an item inserts the mention at the caret. Use it to mention users or topics in comments and messages.
Import
import { HuiMentions } from '@nscode/handy-ui'
Basic Usage
Bind the text with v-model and pass candidates through options. Type @ to open the list, use the arrow keys to navigate, Enter or Tab to select, and Esc to close. Mouse selection is also supported.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
</script>
<template>
<HuiMentions v-model="value" :options="users" placeholder="Type @ to mention a user" />
</template>
Selecting Zhang San inserts @zhangsan : prefix + the option's value + delimiter.
Custom Prefix
prefix changes the trigger prefix and defaults to @. It must follow whitespace or appear at the beginning. Type # below to mention a topic:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const topics: MentionsOption[] = [
{ value: '新功能', label: 'New features' },
{ value: '问题反馈', label: 'Issue feedback' },
{ value: '周报', label: 'Weekly report' }
]
</script>
<template>
<HuiMentions
v-model="value"
:options="topics"
prefix="#"
placeholder="Type # to mention a topic"
/>
</template>
Multiple Prefixes
Pass an array to prefix to support several triggers. The second argument of search or select identifies the active prefix. Below, @ mentions users and # mentions teams:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const prefix = ref('@')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
const teams: MentionsOption[] = [
{ value: '前端组', label: 'Frontend team' },
{ value: '测试组', label: 'Testing team' }
]
const onSearch = (_query: string, p: string) => {
prefix.value = p
}
</script>
<template>
<HuiMentions
v-model="value"
:options="prefix === '#' ? teams : users"
:prefix="['@', '#']"
placeholder="Type @ to mention a user or # to mention a team"
@search="onSearch"
/>
</template>
Filtering
Options are filtered against their label by default, without regard to case. Set filterable to false to display every supplied option, suitable for fetching results yourself in search:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const options = ref<MentionsOption[]>([])
// Simulate an API with setTimeout, searching by display name or romanized value
const allUsers: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' },
{ value: 'wangwu', label: 'Wang Wu' }
]
const searchUsers = (query: string) =>
new Promise<MentionsOption[]>((resolve) => {
setTimeout(() => {
resolve(
allUsers.filter(
(user) => user.label.includes(query) || String(user.value).includes(query)
)
)
}, 300)
})
const handleSearch = async (query: string) => {
options.value = await searchUsers(query)
}
</script>
<template>
<HuiMentions
v-model="value"
:options="options"
:filterable="false"
placeholder="Type @ followed by zhang or wang"
@search="handleSearch"
/>
</template>
The left example uses default filtering: typing @Zhang leaves only Zhang San, matching the translated display label. The right example uses :filterable="false" and shows all supplied options regardless of the query:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const other = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' },
{ value: 'wangwu', label: 'Wang Wu' }
]
</script>
<template>
<HuiSpace wrap>
<HuiMentions
v-model="value"
:options="users"
placeholder="Filtered (true)"
style="width: 240px"
/>
<HuiMentions
v-model="other"
:options="users"
:filterable="false"
placeholder="Unfiltered (false)"
style="width: 240px"
/>
</HuiSpace>
</template>
Disabled Options
An option with disabled: true is hidden from the dropdown rather than displayed as disabled. When no selectable options remain, the localized no-matching-options message is shown.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si (no longer employed)', disabled: true },
{ value: 'wangwu', label: 'Wang Wu' }
]
</script>
<template>
<HuiMentions
v-model="value"
:options="users"
placeholder="Type @; Li Si is omitted from the list"
/>
</template>
Delimiter
Selecting an option appends split, which defaults to one space. If the delimiter already follows the caret, it is not added again. The caret ends after the delimiter. Typing a delimiter also ends the active mention and closes the dropdown.
The left example uses the default space, and the right uses a comma. Select a person in each to compare the inserted text:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const comma = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
</script>
<template>
<HuiSpace wrap>
<HuiMentions
v-model="value"
:options="users"
placeholder="split defaults to a space"
style="width: 240px"
/>
<HuiMentions
v-model="comma"
:options="users"
split=","
placeholder="split is a comma"
style="width: 240px"
/>
</HuiSpace>
<p style="margin-top: 12px">
Left: {{ JSON.stringify(value) }}; right: {{ JSON.stringify(comma) }}
</p>
</template>
Rows and Automatic Height
rows sets the row count, defaulting to 1. The textarea can be resized vertically. With autosize: true, height follows the content. An object with { minRows, maxRows } limits the row range; content beyond maxRows scrolls inside the input.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const rows = ref('')
const auto = ref('')
const limited = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiMentions v-model="rows" :options="users" :rows="3" placeholder="Fixed 3 rows (rows)" />
<HuiMentions
v-model="auto"
:options="users"
autosize
placeholder="Grows with content (autosize)"
/>
<HuiMentions
v-model="limited"
:options="users"
:autosize="{ minRows: 2, maxRows: 5 }"
placeholder="2 to 5 rows (autosize: { minRows: 2, maxRows: 5 })"
/>
</HuiSpace>
</template>
Maximum Length
maxlength limits typed characters. Content inserted by selecting an option is not subject to this limit.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
</script>
<template>
<HuiMentions
v-model="value"
:options="users"
:maxlength="20"
placeholder="Enter up to 20 characters"
/>
<p style="margin-top: 12px">Entered {{ value.length }} / 20</p>
</template>
Placement
placement controls the dropdown position and defaults to bottom-start. The six values are top-start, top, top-end, bottom-start, bottom and bottom-end. It aligns with the whole input and flips when space is insufficient. Type @ into the inputs below to compare:
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption, MentionsPlacement } from '@nscode/handy-ui'
const values = reactive<Record<string, string>>({})
const tops: MentionsPlacement[] = ['top-start', 'top', 'top-end']
const bottoms: MentionsPlacement[] = ['bottom-start', 'bottom', 'bottom-end']
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace v-for="row in [tops, bottoms]" :key="row[0]" wrap>
<HuiMentions
v-for="item in row"
:key="item"
v-model="values[item]"
:options="users"
:placement="item"
:placeholder="`Type @ (${item})`"
style="width: 240px"
/>
</HuiSpace>
</HuiSpace>
</template>
Disabled and Read Only
disabled prevents editing. With readonly, text can be selected and copied but not changed, and placing the caret inside an existing mention does not open suggestions. The component also follows a disabled HuiForm.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('@zhangsan please help review this issue')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiMentions v-model="value" :options="users" disabled />
<HuiMentions v-model="value" :options="users" readonly />
</HuiSpace>
</template>
Sizes
size accepts large, default or small. Single-line heights are 40, 32 and 24px; extra rows add 24, 21 and 18px respectively. Font sizes are 16, 14 and 12px, radii 8, 6 and 4px, and horizontal padding 16, 12 and 8px. These match HuiInput, while the multiline text area matches HuiTextarea. Dropdown row heights are 40, 36 and 28px, with font sizes 16, 14 and 12px and horizontal padding 16, 12 and 8px. Without an explicit size inside HuiForm, the component follows the form item or form.
Each row compares single-line Mentions, Input and Button of the same size:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInput, HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const text = ref('')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">large</span>
<HuiMentions
v-model="value"
:options="users"
size="large"
placeholder="Type @"
style="width: 240px"
/>
<HuiInput v-model="text" size="large" placeholder="Input" style="width: 160px" />
<HuiButton size="large">Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">default</span>
<HuiMentions v-model="value" :options="users" placeholder="Type @" style="width: 240px" />
<HuiInput v-model="text" placeholder="Input" style="width: 160px" />
<HuiButton>Button</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">small</span>
<HuiMentions
v-model="value"
:options="users"
size="small"
placeholder="Type @"
style="width: 240px"
/>
<HuiInput v-model="text" size="small" placeholder="Input" style="width: 160px" />
<HuiButton size="small">Button</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Events
search fires when a prefix opens suggestions and when the query changes. select fires when an option is selected, with the prefix as its second argument. change is the native event: it fires after the textarea loses focus with changed content. focus and blur report focus entry and exit. Recent events appear below:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const value = ref('')
const logs = ref<string[]>([])
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
const log = (text: string) => {
logs.value = [text, ...logs.value].slice(0, 6)
}
const onSearch = (query: string, prefix: string) => log(`search: ${query}, prefix ${prefix}`)
const onSelect = (option: MentionsOption, prefix: string) =>
log(`select: ${option.label}, prefix ${prefix}`)
const onChange = (val: string) => log(`change:${val}`)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiMentions
v-model="value"
:options="users"
:prefix="['@', '#']"
placeholder="Type @ or #"
@search="onSearch"
@select="onSelect"
@change="onChange"
@focus="log('focus')"
@blur="log('blur')"
/>
<div class="logs">
<div v-if="logs.length === 0">No events yet</div>
<div v-for="(item, index) in logs" :key="index">{{ item }}</div>
</div>
</HuiSpace>
</template>
<style scoped>
.logs {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Calling Methods
Call focus() and blur() through a ref. textareaRef exposes the internal <textarea> element for native methods such as select() to select all text.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiMentions, HuiSpace } from '@nscode/handy-ui'
import type { MentionsOption } from '@nscode/handy-ui'
const mentionsRef = ref<InstanceType<typeof HuiMentions>>()
const value = ref('@zhangsan please take a look')
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiMentions ref="mentionsRef" v-model="value" :options="users" />
<HuiSpace wrap>
<HuiButton @click="mentionsRef?.focus()">Focus (focus)</HuiButton>
<HuiButton @click="mentionsRef?.blur()">Blur (blur)</HuiButton>
<HuiButton @click="mentionsRef?.textareaRef?.select()"
>Select text (textareaRef.select)</HuiButton
>
</HuiSpace>
</HuiSpace>
</template>
Within a Form
Inside HuiFormItem, value changes trigger change rules and blur triggers blur rules. The form item's label is associated with the textarea. Set :validate-event="false" to disable validation triggers. Focus and leave the empty comment below to see its required-field message:
<script setup lang="ts">
import { reactive, ref } from 'vue'
import {
HuiButton,
HuiForm,
HuiFormItem,
HuiMentions,
HuiMessage,
HuiSpace
} from '@nscode/handy-ui'
import type { FormRule, MentionsOption } from '@nscode/handy-ui'
const formRef = ref<InstanceType<typeof HuiForm>>()
const model = reactive({ comment: '' })
const rules: Record<string, FormRule[]> = {
comment: [
{ required: true, message: 'Enter a comment', trigger: 'blur' },
{ max: 50, message: 'Up to 50 characters', trigger: 'change' }
]
}
const users: MentionsOption[] = [
{ value: 'zhangsan', label: 'Zhang San' },
{ value: 'lisi', label: 'Li Si' }
]
const submit = async () => {
const result = await formRef.value?.validate()
if (result?.valid) HuiMessage.success('Validation passed')
}
</script>
<template>
<HuiForm ref="formRef" :model="model" :rules="rules" label-width="80px" style="max-width: 480px">
<HuiFormItem label="Comment" prop="comment">
<HuiMentions
v-model="model.comment"
:options="users"
:rows="3"
placeholder="Type @ to mention a colleague"
/>
</HuiFormItem>
<HuiFormItem>
<HuiSpace>
<HuiButton type="primary" @click="submit">Submit</HuiButton>
<HuiButton @click="formRef?.resetFields()">Reset</HuiButton>
</HuiSpace>
</HuiFormItem>
</HuiForm>
</template>
Internal Text and Controlled Updates
Omitting modelValue, or only observing updates, lets the component keep text through typing, mention insertion, and unrelated parent renders. Supplying the prop makes it controlled, including explicit undefined as empty text. Display and validation follow the actual parent value after acceptance, normalization, or rejection. Rejection and external value changes invalidate the old mention range.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMentions, HuiButton, HuiSpace } from '@nscode/handy-ui'
const redraw = ref(0)
const recent = ref('No selection')
const options = [
{ value: 'a', label: 'A' },
{ value: 'b', label: 'B' },
{ value: 'c', label: 'C (disabled)', disabled: true }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="redraw++">Parent renders: {{ redraw }}</HuiButton>
<HuiMentions :options="options" @select="recent = String($event.value)" />
<HuiMentions model-value="Fixed text" :options="options" />
<HuiMentions :model-value="undefined" :options="options" placeholder="Controlled empty text" />
<span>Recent selection: {{ recent }}</span>
</HuiSpace>
</template>
Keyboard Interaction
Up/Down cycle through the highlight and skip disabled options. Enter or Tab inserts the highlighted mention; Shift+Tab does not confirm. Home/End, left/right, and PageUp/PageDown remain text-caret keys. Composition, key code 229, Alt/Ctrl/Meta combinations, and already consumed events are ignored; committing composed text submits only once.
The highlight follows strict value identity, distinguishing numbers from strings. Reordering retains the active item; filtering, removing, or disabling it restores a nearby item without selecting by an outdated index. Closing removes the active ARIA reference. Enter remains a normal newline when all options are unavailable or none match.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string | undefined | Controlled when supplied, including explicit undefined as empty text; internally saved when omitted. |
| options | MentionsOption[] | [] | Candidates. Disabled options are hidden |
| prefix | string | string[] | '@' | Trigger prefix |
| split | string | ' ' | Delimiter appended after selection. Typing it also ends the current mention |
| filterable | boolean | true | Whether to filter options using the typed query |
| placement | 'top' | 'bottom' | 'top-start' | 'bottom-start' | 'top-end' | 'bottom-end' | 'bottom-start' | Dropdown position; flips when space is insufficient |
| placeholder | string | - | Placeholder text |
| disabled | boolean | false | Whether editing is disabled |
| readonly | boolean | false | Whether the text is read only |
| size | 'large' | 'default' | 'small' | - | Follows the enclosing form, or default outside a form |
| maxlength | number | - | Maximum typed length |
| rows | number | 1 | Row count |
| autosize | boolean | { minRows?: number; maxRows?: number } | false | Automatic height |
| validateEvent | boolean | true | Whether value changes and blur trigger form-item validation |
| popperClass | string | - | Additional class on the suggestion popup. |
Events
| Event | Callback Arguments | Description |
|---|---|---|
| update:modelValue | (value: string) | Emitted after typing or selecting an option |
| change | (value: string) | Native change event after blur with changed content |
| focus | (event: FocusEvent) | Focus enters the textarea |
| blur | (event: FocusEvent) | Focus leaves the textarea. Selecting an option does not blur it |
| select | (option: MentionsOption, prefix: string) | An option is selected |
| search | (query: string, prefix: string) | Dropdown opens or query changes. Query excludes the prefix |
Methods
| Name | Type | Description |
|---|---|---|
| focus | () => void | Focuses the textarea |
| blur | () => void | Blurs the textarea |
| textareaRef | HTMLTextAreaElement | Internal textarea element |
Types
import type {
MentionsOption,
MentionsPlacement,
MentionsSize,
MentionsExpose
} from '@nscode/handy-ui'
interface MentionsOption {
value: string | number
label: string
disabled?: boolean
}
type MentionsPlacement = 'top' | 'bottom' | 'top-start' | 'bottom-start' | 'top-end' | 'bottom-end'
type MentionsSize = 'large' | 'default' | 'small'
interface MentionsExpose {
focus: () => void
blur: () => void
readonly textareaRef: HTMLTextAreaElement | undefined
}