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
Setter for the content. Called when a mention is selected and the username is inserted.
Whether the user has text selected. Mention detection is suppressed while a selection is active.
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\\-\\u0080-\\uFFFF]+": ASCII letters, digits, underscores and hyphens, plus any character outside ASCII, so a name in any language matches. That covers everything a username can contain. The pattern is used without the u flag.Return Values
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. The suggestions then stay closed until the text or the cursor position changes.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.Replaces the
mentions list. Use it to put back a list you saved before resetMentions — for example when a submit fails and the text is restored.
