Overview
useUserMentions manages the full @mention flow inside a text input. It monitors the cursor position and content, detects when the user is typing an @mention, debounces a search call, and returns suggestions to display. When the user selects a suggestion, it inserts the username into the content and tracks the mention.
This hook is intended to be used alongside a controlled text input where you manage the content and cursor position.
Usage Example
Props
string
required
The current text content of the input.
(value: string) => void
required
Setter for the content. Called when a mention is selected and the username is inserted.
() => void
required
A function that re-focuses the input. Called after a mention is selected.
number
required
The current cursor position (character index) within the content.
boolean
required
Whether the user has text selected. Mention detection is suppressed while a selection is active.
string
The character that activates mention mode. Defaults to
"@".number
Minimum characters after the trigger before fetching suggestions. Defaults to
3.number
Milliseconds to wait after the user stops typing before fetching suggestions. Defaults to
1000.string
Regex pattern (as a string) that the text after the trigger must match. Defaults to
"[\\w.]+".Return Values
boolean
true when the cursor is inside a valid mention trigger sequence.boolean
true while user suggestions are being fetched.User[]
The list of matching users to display as suggestions.
(user: User) => void
Call this when the user selects a suggestion. Replaces the trigger text in
content with the user’s username, adds the user to mentions, and closes the suggestion list.Mention[]
Accumulated list of confirmed mentions in the current content. Each entry has
id, foreignId, username, and type: "user".(user: User) => void
Manually adds a user to the
mentions list. Used internally by handleMentionClick.() => void
Clears the
mentions list and resets all mention state. Call this after the content is submitted.
