Command palette

@yasinsensoy/web

Web · Components

MentionInput

Mention box: typing @ opens suggestions like people, # like tags; the selected item is added to the text highlighted.

12 exports

Live example

Preview

Code

import { MentionInput } from "@yasinsensoy/web";

<MentionInput name="comment" placeholder="Write a comment, mention someone with @"
  sources={[{ trigger: "@", label: "People", items: people },
    { trigger: "#", label: "Etiketler", items: etiketler, allowSpaces: false }]} />

Events

  1. Events appear here when you interact with the preview.

In a form and showing what was saved

The value is sent as @[Name](id).

@Ayşe Yılmaz the design is ready for #release. @Çağrı Demir the mobile side is yours.

Import

import { MentionInput, MentionView, parseMentions, serializeMentions } from "@yasinsensoy/web";

MentionInput

componentClient component

Mention box: typing @ opens suggestions like people, # like tags; the selected item is added to the text highlighted. For plain text fields like comments, chat messages and task descriptions. For rich text use RichTextEditor mentions.

  • Value: plain text; mentions are stored as @[Ayşe Yılmaz](u1) and show as @Ayşe Yılmaz in the box. On the server get the mentioned ones with parseMentions and show them with MentionView.
  • Suggestions: an array (filtered ignoring case and accents, Turkish-aware, with prefix matches first) or a function searching the server (150 ms debounce, aborts old requests). The list opens below the caret and moves above it if it doesn't fit on screen.
  • Keyboard: ↑/↓ navigate, Enter or Tab selects, Esc closes. Backspace right after a mention deletes it as a whole; typing inside turns it into plain text.
  • Chat box: with onSubmit Enter submits and Shift+Enter adds a new line; multiline={false} for a single line.
  • Form: with name the stored format is submitted; required and maxLength work. For labeled use see MentionField.
MentionInput(props: MentionInputProps): ReactNode

Examples

<MentionInput name="comment" placeholder="Write a comment, mention someone with @"
  sources={{ trigger: "@", items: people }} />

<MentionInput multiline={false} onSubmit={(value) => send(value)}
  sources={[
    { trigger: "@", label: "People", items: (q, signal) => searchPeople(q, signal) },
    { trigger: "#", label: "Channels", items: channels, allowSpaces: false },
  ]} />

MentionView

componentServer component

Shows text written with MentionInput: line breaks are kept and mentions are highlighted (optionally linked). It is a server component.

MentionView({ value, triggers, getHref, renderMention, className, mentionClassName, }: MentionViewProps): JSX.Element

Examples

<MentionView value={comment.text} getHref={(m) => m.trigger === "@" ? `/people/${m.id}` : undefined} />

Splits a stored value ("Hello @[Jane Doe](u1)") into plain text and a list of mentions. Used on the server to notify the mentioned people.

parseMentions(value: string, triggers?: readonly string[]): ParsedMentions

Examples

const { text, mentions } = parseMentions(String(data.get("comment")));
for (const m of mentions) if (m.trigger === "@") notify(m.id);

Builds the stored value from plain text and mentions (e.g. "@[Jane](u1)").

serializeMentions(text: string, mentions: readonly MentionEntity[]): string

MentionItem

interface

An item in the MentionInput suggestions.

NameTypeDescription
idstringId; stored in the value (@[Name](id)).
labelstringDisplay name (without the trigger); this is written into the box.
description?ReactNodeSmall description under the name (e.g. an email, a channel topic).
avatar?stringAvatar image URL.
icon?IconPropIcon instead of an avatar.
disabled?booleanCannot be selected.
keywords?readonly string[]Extra words that should match in search (e.g. a username).

MentionSource

interface

A trigger and its suggestions for MentionInput.

NameTypeDefaultDescription
triggerstringTrigger character: "@" people, "#" channels/tags, ":" emoji…
itemsreadonly MentionItem[] | ((query: string, signal: AbortSignal) => Promise<readonly MentionItem[]> | readonly MentionItem[])Suggestions: an array (searched case-insensitively, Turkish-aware, in the name, description and keywords) or a function called with the typed query (server search; old requests are aborted via signal, with a 150 ms debounce).
label?ReactNodeTitle of the list (e.g. "People").
allowSpaces?booleantrueAllow spaces in the search (e.g. "@Ayşe Yıl")?
renderItem?(item: MentionItem) => ReactNodeYour own content instead of the row in the list.

Extra info that comes with a MentionInput value change.

NameTypeDescription
textstringPlain text shown in the box ("Hello @Ayşe").
mentionsMentionEntity[]Mentions in the text.

Classes that can be passed to the inner parts of MentionInput.

NameTypeDescription
input?stringText area.
list?stringSuggestion list.
mention?stringMention highlight in the text.

Props of the MentionInput component.

NameTypeDefaultDescription
sourcesMentionSource | readonly MentionSource[]Triggers and their suggestions. For a single source { trigger: "@", items }, for several an array (e.g. @ people, # tags).
value?stringValue when controlled: plain text, with mentions in the form @[Ayşe Yılmaz](u1). On the server parse it with parseMentions.
defaultValue?stringInitial value when uncontrolled.
onValueChange?(value: string, details: MentionChangeDetails) => voidCalled with the stored format and the plain text + mention list when the value changes.
onMentionAdd?(item: MentionItem, trigger: string) => voidWhen a suggestion is selected.
multiline?booleantrueMulti-line? If false, a single line (Enter doesn't add a line; onSubmit is called).
rows?number3Minimum number of lines when multi-line.
autoResize?booleantrueGrow as you type (limit it with a max-h-* class)?
onSubmit?(value: string, details: MentionChangeDetails) => voidCalled when Enter is pressed (while the suggestion list is closed); if given, a new line is added with Shift+Enter in multi-line mode (like a chat box).
placeholder?stringText shown when empty.
appendSpace?booleantrueAdd a space after a selected mention?
limit?number50Maximum number of suggestions shown in the list.
variant?FieldVariant | CustomVariant<"field">Appearance. See FieldVariant.
effect?EffectPropEffect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). If omitted, UIProvider appearance.defaults.field.effect. See BuiltinEffect.
name?stringForm field name; the value in the stored format is submitted under this name.
form?stringId of the form the field belongs to.
required?booleanCannot be submitted empty.
maxLength?numberMaximum characters (of the visible text).
disabled?booleanDisabled.
readOnly?booleanRead-only.
id?stringId of the text area.
autoFocus?booleanFocused when the page opens.
tooltip?TooltipPropHint shown when hovering the box. See TooltipProp.
className?stringClasses added to the container.
classNames?MentionInputClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name.
aria-labelledby?stringId of the element naming the field.
aria-describedby?stringId of the element describing the field.
aria-invalid?boolean | "true" | "false"Show as invalid.

Props of the MentionView component.

NameTypeDescription
valuestringThe stored value ("Hello @[Ayşe Yılmaz](u1)").
triggers?readonly string[]Only these triggers count as mentions. If omitted, all.
getHref?(mention: MentionEntity) => string | undefinedLink of the mention (e.g. a profile page). If given, the mention is rendered as an <a>.
renderMention?(mention: MentionEntity) => ReactNodeYour own content instead of the mention (e.g. a person card with HoverCard).
className?stringClasses added to the container.
mentionClassName?stringClasses added to the mentions.

MentionEntity

interface

A mention in the text. Stored in the value as @[Jane Doe](u1); shown in the box as @Jane Doe.

NameTypeDescription
triggerstringTrigger character ("@", "#"…).
idstringId (e.g. user id).
labelstringDisplay name (without the trigger).
startnumberStart in the displayed text (including the trigger).
endnumberEnd in the displayed text (exclusive).

Result of parseMentions.

NameTypeDescription
textstringPlain text shown in the box ("Hello @Jane Doe").
mentionsMentionEntity[]Mentions in the text, in order.