<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Raj Ghodekar</title>
    <description>The latest articles on Forem by Raj Ghodekar (@ruturajgh).</description>
    <link>https://forem.com/ruturajgh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3200808%2F06e0f1bc-58e8-4d13-9e95-1f74c3d059c4.png</url>
      <title>Forem: Raj Ghodekar</title>
      <link>https://forem.com/ruturajgh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ruturajgh"/>
    <language>en</language>
    <item>
      <title>Inside a emoji-picker-react: State, Events, and UI Patterns</title>
      <dc:creator>Raj Ghodekar</dc:creator>
      <pubDate>Sat, 24 May 2025 09:59:56 +0000</pubDate>
      <link>https://forem.com/ruturajgh/inside-a-emoji-picker-react-state-events-and-ui-patterns-4a13</link>
      <guid>https://forem.com/ruturajgh/inside-a-emoji-picker-react-state-events-and-ui-patterns-4a13</guid>
      <description>&lt;p&gt;lets understand what's going on under the hood of emoji-picker-react by &lt;a class="mentioned-user" href="https://dev.to/ealush"&gt;@ealush&lt;/a&gt; a meta software developer. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhzi9hmgv29bgvyse9e7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhzi9hmgv29bgvyse9e7.png" alt="Image description" width="746" height="934"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;link to &lt;a href="https://github.com/ealush/emoji-picker-react" rel="noopener noreferrer"&gt;emoji-picker-react&lt;/a&gt;&lt;br&gt;
here's his &lt;a href="https://ealush.com" rel="noopener noreferrer"&gt;website&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;(this is not how to use emoji-picker-react but what going on inside)&lt;/p&gt;

&lt;p&gt;now straight to the repository, the library makes use of custom hooks and React.context very extensively and a BIG emphasis on that, its like one contextProvider after another and then another and then another, it keeps on going.&lt;/p&gt;

&lt;p&gt;The App structure (not the folder structure) &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8u3bpz8hm9qmyoop6qnz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8u3bpz8hm9qmyoop6qnz.png" alt="Image description" width="800" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It lifts all the necessary state, ref, and config inside contexts, and wraps the whole ui content, which makes it easier to access anything, at any leaf component.&lt;/p&gt;
&lt;h3&gt;
  
  
  This is known as the [Provider pattern]
&lt;/h3&gt;

&lt;p&gt;(&lt;a href="https://www.patterns.dev/vanilla/provider-pattern/" rel="noopener noreferrer"&gt;https://www.patterns.dev/vanilla/provider-pattern/&lt;/a&gt;), which prevents passing props down multiple layers, and use using them wherever required. &lt;/p&gt;

&lt;p&gt;The Component accepts props with callbacks such as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onEmojiClick: (e)=&amp;gt;{},
onReactionClick:(e)=&amp;gt;{},
onSkinToneChange: (e)=&amp;gt;{},
previewConfig: {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;these are for the user to pass callback handler functions to use the output and react to callbacks and also configs for the picker.&lt;/p&gt;

&lt;h2&gt;
  
  
  An intuitive way of open context api endpoints
&lt;/h2&gt;

&lt;p&gt;, ( it's simple really, but for me, I haven't thought about passing them in this way) &lt;br&gt;
Usually we create a Context, get all the values, build the state and create a simple :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const useMyContext = ()=&amp;gt; useContext(context)&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The repository takes a different turn to exposing apis,&lt;/p&gt;

&lt;p&gt;for Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//usually we de-structure state right off the bat, but here its 
//just an object that is passed to the provider and then used when
//accessed. (global states everywhere which only means, one end 
//of your component can interact with another end without any 
//problems)

  const activeCategoryState = useState&amp;lt;ActiveCategoryState&amp;gt;(null);
  const emojisThatFailedToLoadState = useState&amp;lt;Set&amp;lt;string&amp;gt;&amp;gt;(new Set());
  const emojiVariationPickerState = useState&amp;lt;DataEmoji | null&amp;gt;(null);

  &amp;lt;PickerContext.Provider
      value={{
        activeCategoryState, 
        emojiVariationPickerState,
        emojisThatFailedToLoadState,
        ...
      }}
    &amp;gt;
      {children}
    &amp;lt;/PickerContext.Provider&amp;gt;

//and then create a separate useContextHook for each, ( This is 
//more expressive, modular, and easier to follow — especially 
//when you're working with multiple context values -chatgpt. will
//update this once I get to the root why it's this way)

export function useEmojisThatFailedToLoadState() {
  const { emojisThatFailedToLoadState } = React.useContext(PickerContext);
  return emojisThatFailedToLoadState;
}

export function useIsPastInitialLoad(): boolean {
  const { isPastInitialLoad } = React.useContext(PickerContext);
  return isPastInitialLoad;
}

export function useEmojiVariationPickerState() {
  const { emojiVariationPickerState } = React.useContext(PickerContext);
  return emojiVariationPickerState;
}

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3muyjwtc9do0yyhpzo1.png)


//these are then later used to setVal or use val where  //required 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Events
&lt;/h2&gt;

&lt;p&gt;Events are absolutely critical to any UI component, &lt;br&gt;
whether its keyboard events, auto closing modals or focusing inputs, better interactivity is what make a good user experience&lt;/p&gt;

&lt;p&gt;Emoji picker stitches event listeners with custom hooks and the context state used from the context apis to the component as the component loads the eventListeners are active.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp04yzbc26y79h922qfh1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp04yzbc26y79h922qfh1.png" alt="Image description" width="800" height="372"&gt;&lt;/a&gt;&lt;br&gt;
(not the most beautiful diagrammer am I) &lt;/p&gt;

&lt;p&gt;For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function useOnFocus() {
  const BodyRef = useBodyRef();
  const emojiStyle = useEmojiStyleConfig();
  const getEmojiUrl = useGetEmojiUrlConfig();

  useEffect(() =&amp;gt; {
    if (emojiStyle === EmojiStyle.NATIVE) {
      return;
    }

    const bodyRef = BodyRef.current;
    bodyRef?.addEventListener('focusin', onFocus);

    return () =&amp;gt; {
      bodyRef?.removeEventListener('focusin', onFocus);
    };

    function onFocus(event: FocusEvent) {
    //do stuff
    }
  }, [BodyRef, emojiStyle, getEmojiUrl]);
}

function PickerRootElement({ children }: RootProps) {
  const [reactionsMode] = useReactionsModeState();
  const theme = useThemeConfig();
  ...
  useKeyboardNavigation();
  useOnFocus();

  return &amp;lt;div&amp;gt;...&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Emoji-picker-react by &lt;a class="mentioned-user" href="https://dev.to/ealush"&gt;@ealush&lt;/a&gt; is a great example of implementing contextApi in a way how it's meant to be used, &lt;br&gt;
Applying separation of concerns and preventing hectic prop passing everywhere, and using react properly. &lt;/p&gt;

&lt;p&gt;This has been a great learning, also in the future I will be doing more under the hood implementations of react libraries, as I get good at it ofcourse. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>opensource</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
