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
The current text content of the input.
Setter for the content. Called when a mention is selected and the username is inserted.
A function that re-focuses the input. Called after a mention is selected.
The current cursor position (character index) within the content.
Whether the user has text selected. Mention detection is suppressed while a selection is active.
The character that activates mention mode. Defaults to
"@".Minimum characters after the trigger before fetching suggestions. Defaults to
3.Milliseconds to wait after the user stops typing before fetching suggestions. Defaults to
1000.Regex pattern (as a string) that the text after the trigger must match. Defaults to
"[\\w.]+".Return Values
true when the cursor is inside a valid mention trigger sequence.true while user suggestions are being fetched.The list of matching users to display as suggestions.
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.Accumulated list of confirmed mentions in the current content. Each entry has
id, foreignId, username, and type: "user".Manually adds a user to the
mentions list. Used internally by handleMentionClick.Clears the
mentions list and resets all mention state. Call this after the content is submitted.
