React Mentions Input Component

4.4.10 · active · verified Sun Apr 19

React Mentions is a component library providing a flexible, accessible textarea input field with built-in mention functionality, similar to what's found on social media platforms like Twitter or Facebook. The current stable version is 4.4.10, with frequent patch releases addressing bug fixes and minor improvements, and occasional minor versions for new features or refactorings. Its core strength lies in supporting multiple, distinct mention types (e.g., users, tags) within a single input, each configurable with its own trigger character and custom rendering logic for suggestions. It differentiates itself by offering robust control over suggestion display (e.g., portal host, force suggestions above cursor) and comprehensive event callbacks, making it suitable for complex interaction patterns in production applications. It is not a full-featured text editor but focuses specifically on the mentions use case.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic React Mentions setup, allowing users to type '@' to trigger suggestions for mentioning predefined users. It includes state management for the input value, a data source function for suggestions, and a custom suggestion renderer, showcasing how to integrate the component into a React application. Basic inline styles are provided for immediate visibility.

import React, { useState, useCallback } from 'react';
import { MentionsInput, Mention } from 'react-mentions';
import './index.css'; // Assuming some basic styling for .mentions, .mention, .suggestions

const defaultStyle = {
  control: {
    backgroundColor: '#fff',
    fontSize: 14,
    fontWeight: 'normal',
  },
  '&multiLine': {
    control: {
      minHeight: 63,
    },
    highlighter: {
      padding: 9,
      border: '1px solid transparent',
    },
    input: {
      padding: 9,
      border: '1px solid silver',
    },
  },
  suggestions: {
    list: {
      backgroundColor: '#fff',
      border: '1px solid rgba(0,0,0,0.15)',
      fontSize: 14,
    },
    item: {
      padding: '5px 15px',
      borderBottom: '1px solid rgba(0,0,0,0.15)',
      '&focused': {
        backgroundColor: '#cee4e5',
      },
    },
  },
};

function MentionsEditor() {
  const [value, setValue] = useState("Hello @[John Doe](john.doe) and @[Jane Smith](jane.smith)");

  const users = [
    { id: 'john.doe', display: 'John Doe' },
    { id: 'jane.smith', display: 'Jane Smith' },
    { id: 'mike.tyson', display: 'Mike Tyson' },
    { id: 'alice.wonder', display: 'Alice Wonderland' },
  ];

  const handleChange = useCallback((event, newValue, newPlainTextValue, mentions) => {
    setValue(newValue);
    console.log('New Value:', newValue);
    console.log('Plain Text:', newPlainTextValue);
    console.log('Mentions:', mentions);
  }, []);

  const fetchUsers = useCallback((query, callback) => {
    if (!query) return callback(users);
    const filteredUsers = users.filter(user =>
      user.display.toLowerCase().includes(query.toLowerCase())
    );
    callback(filteredUsers);
  }, [users]);

  return (
    <div className="editor-container">
      <h2>React Mentions Example</h2>
      <MentionsInput
        value={value}
        onChange={handleChange}
        style={defaultStyle} // Inline styles for quick demo, recommend external CSS
        placeholder="Type @ to mention someone..."
      >
        <Mention
          trigger="@"
          data={fetchUsers}
          renderSuggestion={(suggestion, search, highlightedDisplay) => (
            <div>
              {highlightedDisplay}
            </div>
          )}
        />
      </MentionsInput>
      <div style={{ marginTop: '20px', border: '1px solid #eee', padding: '10px' }}>
        <h3>Current Raw Input Value:</h3>
        <pre>{value}</pre>
      </div>
    </div>
  );
}

export default MentionsEditor;

view raw JSON →