Command palette

@yasinsensoy/native

React Native · Components

MentionInput

Mention input (same API as MentionInput on web): typing @ suggests people, # tags…; the selected name is highlighted in the box and stored in the value as @[Jane Doe](u1).

11 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Preview

Code

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

<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/native";

MentionInput

component

Mention input (same API as MentionInput on web): typing @ suggests people, # tags…; the selected name is highlighted in the box and stored in the value as @[Jane Doe](u1).

  • Highlight: Mentions show colored inside the text field (the same on both platforms); deleting removes the whole mention, and typing inside one turns it back into plain text.
  • Suggestions: An array is filtered ignoring Turkish letter case; with a function it searches the server (150 ms wait, old requests cancelled). The list is below the box; in a chat box use suggestionsPlacement="above".
  • Chat: With onSubmit the keyboard's send key sends.
  • Form: The stored format binds inside a Form with name.

To show a stored value use MentionView; to parse it on the server use parseMentions.

MentionInput(props: MentionInputProps): ReactElement<unknown, string | JSXElementConstructor<any>>

Examples

<MentionInput name="comment" label="Comment" placeholder="Mention someone with @"
  sources={{ trigger: "@", items: people }} />

MentionView

component

Shows text written with MentionInput: line breaks are kept, mentions are highlighted and optionally tappable.

MentionView({ value, triggers, onMentionPress, renderMention, style, mentionStyle, numberOfLines, }: MentionViewProps): JSX.Element

Examples

<MentionView value={comment.text} onMentionPress={(m) => go("Person", { id: m.id })} />

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

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.

MentionItem

interface

An item in the MentionInput suggestions (same as web).

NameTypeDescription
idstringId; stored in the value (@[Name](id)).
labelstringDisplay name (without the trigger); this is written into the box.
description?ReactNodeA small description below 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 (same as web).

NameTypeDefaultDescription
triggerstringTrigger character: "@" people, "#" tags, ":" emoji…
itemsreadonly MentionItem[] | ((query: string, signal: AbortSignal) => Promise<readonly MentionItem[]> | readonly MentionItem[])Suggestions: an array (searched in the name, description and keywords ignoring Turkish letter case) or a function called with the typed query (server-side search; old requests are cancelled via signal, with a 150 ms wait).
label?ReactNodeTitle of the list (e.g. "People").
allowSpaces?booleantrueAllow spaces in the query (e.g. "@Jane Do")?
renderItem?(item: MentionItem) => ReactNodeYour own content in place of a row in the list.

Extra info passed on a MentionInput value change.

type MentionChangeDetails = ParsedMentions

Props of MentionInput (same names as on web).

Extends: FieldProps

NameTypeDefaultDescription
ref?Ref<TextInputRef>Ref of the underlying text input.
sourcesMentionSource | readonly MentionSource[]Triggers and their suggestions: a single source or an array.
value?stringControlled value: plain text, mentions in the form @[Jane Doe](u1).
defaultValue?stringInitial value in uncontrolled use.
onValueChange?(value: string, details: MentionChangeDetails) => voidWhen the value changes, with the stored format and plain text + a list of mentions.
onMentionAdd?(item: MentionItem, trigger: string) => voidWhen a suggestion is selected.
multiline?booleantrueMultiline?
rows?number3Minimum rows when multiline.
autoResize?booleantrueGrow while typing (up to maxRows)?
maxRows?number8Maximum rows with autoResize; then it scrolls inside.
onSubmit?(value: string, details: MentionChangeDetails) => voidCalled with the keyboard's send key; when given, the key sends instead of adding a line even when multiline (like a chat box).
placeholder?stringText shown when empty.
appendSpace?booleantrueAdd a space after a selected mention?
limit?number50Maximum suggestions in the list.
suggestionsPlacement?"below" | "above""below"Position of the suggestion list. For boxes at the bottom of the screen (chat) use "above": the list stays visible while the keyboard is open.
variant?FieldVariant"outline"Field appearance.
maxLength?numberMaximum characters (visible text).
readOnly?booleanRead-only.
autoFocus?booleanFocuses when the screen opens.
aria-label?stringScreen reader name; give it when there is no visible label.
onBlur?(event: TextInputFocusEvent) => voidOn blur.
style?StyleProp<ViewStyle>Container style.

Props of MentionView.

NameTypeDescription
valuestringThe stored value ("Hello @[Jane Doe](u1)").
triggers?readonly string[]Only these triggers count as mentions. If omitted, all.
onMentionPress?(mention: MentionEntity) => voidWhen a mention is tapped (e.g. go to the profile screen). When given, the mention is read as a link. The counterpart of getHref on web.
renderMention?(mention: MentionEntity) => ReactNodeYour own content in place of a mention (must be an inline Text).
style?StyleProp<TextStyle>Text style.
mentionStyle?StyleProp<TextStyle>Style of the mentions.
numberOfLines?numberMaximum lines; the rest is cut off with ….