<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aly Seoudi on Medium]]></title>
        <description><![CDATA[Stories by Aly Seoudi on Medium]]></description>
        <link>https://medium.com/@aly.essam.seoudi?source=rss-d5bdf6a9e38d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3ATUeyfL_5g3jtjU8ttuYQ.jpeg</url>
            <title>Stories by Aly Seoudi on Medium</title>
            <link>https://medium.com/@aly.essam.seoudi?source=rss-d5bdf6a9e38d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 11:14:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aly.essam.seoudi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Simple React guide to implement Chat/Comment Input Component (React-Mentions + Emoji-Mart)]]></title>
            <link>https://medium.com/@aly.essam.seoudi/simple-react-guide-to-implement-chat-comment-input-component-react-mentions-emoji-mart-a48a4d8c4fad?source=rss-d5bdf6a9e38d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a48a4d8c4fad</guid>
            <category><![CDATA[comment]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[emoji]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-mentions]]></category>
            <dc:creator><![CDATA[Aly Seoudi]]></dc:creator>
            <pubDate>Sat, 11 Jul 2020 01:05:54 GMT</pubDate>
            <atom:updated>2020-07-14T23:24:30.420Z</atom:updated>
            <content:encoded><![CDATA[<h3><a href="https://medium.com/@aly.essam.seoudi/simple-react-guide-to-implement-chat-comment-input-component-react-mentions-emoji-mart-a48a4d8c4fad?source=---------2------------------">How to implement a React Chat/Comment Input Component using (React-Mentions + Emoji-Mart</a>)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q4uYAEOzZ0y8HtgHWCAPSw.png" /></figure><h3>Introduction</h3><p>In this article I will share the way to implement a React Input Component that supports:<br>i. Mentioning users or items and displaying data saved to the DB.<br>ii. Selecting and detecting Emojis.</p><p>So at the beginning I found that I have two tasks that needs to be done and it’s better to work on each task separately. In order to achieve a component like that</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*5OFhYVjy56kx70KY7CjZLA.png" /></figure><p>And then retrieve the data and display it like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*YNtACi7P1S1oGrA33F29Bg.png" /></figure><h3>1. Mentions</h3><h3>Mentioning users</h3><p>After doing my research I found that the most popular React packages used for Mentioning users are <a href="https://draftjs.org/">Draft.js</a> with it’s <a href="https://www.draft-js-plugins.com/">plugins</a> and <a href="https://github.com/signavio/react-mentions">React-Mentions</a> the reason why I chose React-Mentions is that it’s more simple when it comes to</p><ol><li>The format of the comment saved by the package.</li><li>Storing Mentions in the DB.</li><li>Displaying saved comments from the DB.</li><li>Styling mentions drop down.</li></ol><h4>The Implementation</h4><p>First step: Install the <a href="https://www.npmjs.com/package/react-mentions">React-Mentions</a> package and <a href="https://www.npmjs.com/package/substyle-jss">Substyle-jss</a> package.</p><p>Second step: Add the packages to your CommentInput Component</p><pre>import { Mention, MentionsInput } from “react-mentions”;</pre><pre>import { StylesViaJss } from “substyle-jss”;</pre><p>Third step: Add the Imported Components</p><pre>&lt;StylesViaJss&gt;<br>  &lt;MentionsInput<br>    value={comment}<br>    onChange={event =&gt; setComment(event.target.value)}<br>    className=&quot;mentions&quot;<br>    classNames={Style}<br>    placeholder=&quot;Leave a comment...&quot;<br>    allowSuggestionsAboveCursor={true}<br>  &gt;<br>    &lt;Mention data={users} className={Style.mentions__mention} /&gt;      </pre><pre>  &lt;/MentionsInput&gt;&lt;/StylesViaJss&gt;</pre><p>The above piece contains multiple variables</p><ol><li>comment: is the string that handles your text <br><br>Comment mention looks like this @[display][id] <br>so Hey Aly Seoudi =&gt; Hey [Aly Seoudi][xxx]</li></ol><pre>const [comment, setComment] = useState(“”);</pre><p>2. classNames: Are styles for the inner components of the MentionsInput</p><pre>classNames={Style}</pre><p>there is a sample for a style component provided in the codesandbox at the end</p><p>3. users: Is an array of objects that you want to mentions consist of Id and Display</p><pre>const users = [<br>{id: &quot;walter&quot;,display: &quot;Walter White&quot;},<br>{id: &quot;jesse&quot;,display: &quot;Jesse Pinkman&quot;}<br>]</pre><p>Fourth step: Add the button that sends the data to the DB</p><pre><strong><em>&lt;div<br>  className=&quot;comment-input-container px-2 row col-12 mx-auto&quot;<br>  style={comment ? { border: &quot;1px solid #71cee3&quot; } : {}}<br>&gt;</em></strong><br>  &lt;div <em>className</em>=&quot;col-11 px-0&quot;&gt;<br>    &lt;MentionsInput<br>      <em>value</em>={comment}<br>      <em>onChange</em>={handleChange}<br>      <em>className</em>=&quot;mentions&quot;<br>      <em>classNames</em>={Style}<br>      <em>placeholder</em>=&quot;Leave a comment...&quot;<br>      <em>allowSuggestionsAboveCursor</em>={true}<br>    &gt;<br>      &lt;Mention <em>data</em>={members} <em>className</em>={Style.mentions__mention} /&gt;   </pre><pre>    &lt;/MentionsInput&gt;<br>  &lt;/div&gt;<br>  &lt;div <em>className</em>=&quot;col-1&quot;&gt;<br><strong><em>    &lt;button<br>      className=&quot;send-text&quot;<br>      style={comment ? { color: &quot;#3B5E66&quot; } : {}}<br>      onClick={handleSendClick}<br>    &gt;<br>      Send<br>    &lt;/button&gt;</em></strong><br>  &lt;/div&gt;<br><strong><em>&lt;/div&gt;</em></strong></pre><pre>const handleSendClick = () =&gt; {</pre><pre>   API.Call(comment);<br>}</pre><h3>Retrieving Data</h3><p>Retrieving data here is pretty simple all what you need is a component that contains a paragraph and a method that handles the comment retrieved from the DB</p><h4>The paragraph:</h4><pre>&lt;p<br><em>className</em>=&quot;d-inline comment-paragraph-text&quot;<br><em>dangerouslySetInnerHTML</em>={{<br>__html: text.replace(/\n\r?/g, &quot;&lt;br /&gt;&quot;),}}<br>/&gt;</pre><h4>The Method:</h4><pre>const [text, setText] = useState(&quot;&quot;);</pre><pre>useEffect(() =&gt; {<br> if (comment!== &quot;&quot;) {<br> let regex = /@\[.+?\]\(.+?\)/gm;<br> let displayRegex = /@\[.+?\]/g;<br> let idRegex = /\(.+?\)/g;<br> let matches = comment.match(regex);<br> let arr = [];<br> matches &amp;&amp;<br> matches.forEach((<em>m</em>) =&gt; {<br>  let id = <em>m</em>.match(idRegex)[0].replace(&quot;(&quot;, &quot;&quot;).replace(&quot;)&quot;, &quot;&quot;);<br>  let display =    </pre><pre><em>       m</em>.match(displayRegex)[0].replace(&quot;@[&quot;,&quot;&quot;).replace(&quot;]&quot;,&quot;&quot;);<br>  arr.push({ id: id, display: display });<br> });</pre><pre>let newComment = comment.split(regex);<br> let output = &quot;&quot;;<br> for (let i = 0; i &lt; newComment.length; i++) {<br>   const c = newComment[i];<br>   if (i === newComment.length - 1) <br>     output += c;<br>   else<br>     output +=<br>       c + `&lt;a href=&quot;/People/${arr[i].id}&quot;&gt;${arr[i].display}&lt;/a&gt;`;<br> }<br> setText(output);<br>}}, [comment]);</pre><p>And this is the <a href="https://codesandbox.io/s/recursing-brahmagupta-pvtt6?file=/src/App.js">sandbox Link</a> for mentioning.</p><h3>2. Emojis</h3><h4>Implementation</h4><ol><li>Install <a href="https://www.npmjs.com/package/emoji-mart">Emoji-Mart</a></li></ol><p>2. Add the packages to your CommentInput Component</p><pre>import { NimblePicker, emojiIndex } from &quot;emoji-mart&quot;;<br>import &quot;emoji-mart/css/emoji-mart.css&quot;;<br>import data from &quot;emoji-mart/data/google.json&quot;;</pre><p>3. Add the Imported Components</p><pre>&lt;NimblePicker<br> <em>onSelect</em>={(emoji)=&gt; setComment(`${comment} ${<em>emoji</em>.native}`)}<br> <em>showSkinTones</em>={false}<br> <em>emojiTooltip</em>={false}<br> <em>showPreview</em>={false}<br> <em>sheetSize</em>={32}<br> <em>data</em>={data}<br>/&gt;</pre><p>There is a lot of options here that you can check at the <a href="https://github.com/missive/emoji-mart">Github Repo.</a></p><p>The above piece of code contains the emoji object that we select the native property from it which looks like</p><pre>{<br> id<strong>:</strong> &#39;smiley&#39;,<br> name<strong>:</strong> &#39;Smiling Face with Open Mouth&#39;,<br> colons<strong>:</strong> &#39;:smiley:&#39;,<br> text<strong>:</strong> &#39;:)&#39;,<br> emoticons<strong>:</strong> [<br>  &#39;=)&#39;,<br>  &#39;=-)&#39;<br> ],<br> skin<strong>:</strong> null,<br> native<strong>:</strong> &#39;😃&#39;<br>}</pre><p>4. Add button to toggle the visibility of the emoji picker Component by adding a ref for the span containing the emoji picker</p><pre>const [showEmojis, setShowEmojis] = useState(false);</pre><pre>{showEmojis ? (<br> &lt;span <em>className</em>={Style.emoji__picker} <strong><em>ref={emojiPicker}</em></strong>&gt;</pre><pre>&lt;NimblePicker<br>   <em>onSelect</em>={handleSelectEmoji}<br>   <em>showSkinTones</em>={false}<br>   <em>emojiTooltip</em>={false}<br>   <em>showPreview</em>={false}<br>   <em>sheetSize</em>={32}<br>   <em>data</em>={data}<br>  /&gt;<br>&lt;/span&gt;<br>) : (<br>&lt;button<br> <em>className</em>={Style.emoji__button}<br> <em>onClick</em>={()=&gt;setShowEmojis(true)}<br>&gt;<br> {String.fromCodePoint(0x1f60a)}<br>&lt;/button&gt;<br>)}</pre><p>Add event listener to handle clicks outside the Picker and close it</p><pre>useEffect(() =&gt; {<br> function handleClickOutside(<em>event</em>) {<br>  if (<br>   emojiPicker.current&amp;&amp;!emojiPicker.current.contains(<em>event</em>.target))     </pre><pre>  {<br>    setShowEmojis(false);}<br>  }<br>  document.addEventListener(&quot;mousedown&quot;, handleClickOutside);<br>  return () =&gt; {<br>   document.removeEventListener(&quot;mousedown&quot;, handleClickOutside);<br>  };<br>}, [emojiPicker]);</pre><h4>Detecting Common Emojis by text</h4><p>While handling the text change in the MentionsInput instead of just setting the comment to event.target.value I ran a regex expression to detect commonly used Emojis first</p><pre><em>onChange</em>={handleChange}</pre><pre>const handleChange = (<em>event</em>) =&gt; {<br> let text = <strong><em>colonsToUnicode</em></strong>(<em>event</em>.target.value);<br> onChange(text);<br>};</pre><pre>const colonsToUnicode = (<em>text</em>) =&gt; {<br> const colonsRegex = new RegExp(&quot;(^|\\s):([)|D|(|P|O|o])+&quot;, &quot;g&quot;);<br> let newText = <em>text</em>;<br> let match = colonsRegex.exec(<em>text</em>);<br> if(match!==null) {<br>  let colons = match[2];<br>  let offset = match.index + match[1].length;<br>  newText = <br>            newText.slice(0, offset) + <br>            <strong><em>getEmoji</em></strong>(colons) + <br>            newText.slice(offset + 2);<br> }<br> return newText;<br>};</pre><pre>const getEmoji = (<em>emoji</em>) =&gt; {<br> let emoj;<br> switch (<em>emoji</em>) {<br>  case &quot;D&quot;:<br>   emoj = emojiIndex.search(&quot;:)&quot;)[1].native;<br>   break;<br>  case &quot;)&quot;:<br>   emoj = emojiIndex.search(&quot;:)&quot;)[0].native;<br>   break;<br>  case &quot;(&quot;:<br>   emoj = emojiIndex.search(&quot;:(&quot;)[0].native;<br>   break;<br>  case &quot;P&quot;:<br>   emoj = emojiIndex.search(&quot;:P&quot;)[0].native;<br>   break;</pre><pre>  and so on...</pre><pre>  default:<br>   emoj = &quot;&quot;;<br> }<br> return emoj;<br>};</pre><p>Last Step was to add some styling that is provided in the<a href="https://codesandbox.io/s/cool-jepsen-wzimf?file=/src/App.js"> codesandbox Link</a></p><h3>Thank you</h3><p>So that’s how I implemented the Chat/Comment Input Component with React-Mentions and Emoji-Mart.</p><p>Feel free to contact me in case you have any question regarding this article or any other topic to discuss.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a48a4d8c4fad" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>