<?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>DEV Community: Parth Gupta</title>
    <description>The latest articles on DEV Community by Parth Gupta (@parthgupta118).</description>
    <link>https://dev.to/parthgupta118</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%2F1027455%2Fc8922341-d96a-4317-a23e-d365d095eab1.jpeg</url>
      <title>DEV Community: Parth Gupta</title>
      <link>https://dev.to/parthgupta118</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/parthgupta118"/>
    <language>en</language>
    <item>
      <title>Mouse Events: onMouseOut and onMouseLeave in React</title>
      <dc:creator>Parth Gupta</dc:creator>
      <pubDate>Fri, 24 Feb 2023 13:07:40 +0000</pubDate>
      <link>https://dev.to/parthgupta118/mouse-events-onmouseout-and-onmouseleave-in-react-1fpd</link>
      <guid>https://dev.to/parthgupta118/mouse-events-onmouseout-and-onmouseleave-in-react-1fpd</guid>
      <description>&lt;p&gt;JavaScript events enable us to interact with the webpage in a dynamic manner. There are wide variety of interaction events such as clicking a button, hovering over a dropdown, pressing a key on the keyboard etc. &lt;/p&gt;

&lt;p&gt;Mouse Events are the events that occur when the mouse interacts with the HTML document. Here, we will distinguish between 2 mouse events that are used when a mouse hovers over an HTML element or React component.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. onMouseOut Event:
&lt;/h3&gt;

&lt;p&gt;This event will trigger when a mouse cursor is no longer within the element or one of its children. It will also trigger when the cursor enters any of the child element, because the child element hides the visible area of the parent.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Behaviour of onMouseOut Event&lt;/b&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FElement%2Fmouseleave_event%2Fmouseout.png" class="article-body-image-wrapper"&gt;&lt;img alt="Mouse Out Flow Diagram" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FElement%2Fmouseleave_event%2Fmouseout.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;A single onMouseOut event is applied on the deepest nested &lt;code&gt;div&lt;/code&gt; in the above image. Now, when it triggers, it starts from that &lt;code&gt;div&lt;/code&gt; and start bubbling up the hierarchy until it is canceled by a handler or reaches the root.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. onMouseLeave Event:
&lt;/h3&gt;

&lt;p&gt;The onMouseLeave event is fired when the mouse cursor of a pointing device is moved out of the HTML or React element.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Behaviour of onMouseLeave Event&lt;/b&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FElement%2Fmouseleave_event%2Fmouseleave.png" class="article-body-image-wrapper"&gt;&lt;img alt="Mouse Out Flow Diagram" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FElement%2Fmouseleave_event%2Fmouseleave.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;In this image, One onMouseLeave event is applied to each of the &lt;code&gt;div&lt;/code&gt; element. Now, when the pointer moves from the text to an area outside of the most outer div represented here, four events are triggered with respect to each of the four &lt;code&gt;div&lt;/code&gt; elements in the hierarchy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difference b/w onMouseOut and OnMouseLeave
&lt;/h3&gt;

&lt;p&gt;The following example illustrates the difference between onMouseOut and onMouseLeave events. In this example video, when we move the cursor to one of the child &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; elements and after that exit from parent &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The onMouseLeave event fires only 1 time, giving &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; an orange bg-color for 1s. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The onMouseOut event fire 3 times. First, when we enter the child element because it hides the visible area of underlying &lt;code&gt;ul&lt;/code&gt; item. Second, when we exit from the child &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; element and the third time when we exit from the parent &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; element.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fgbgl6cucfqy9hcacr9qd.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fgbgl6cucfqy9hcacr9qd.gif" alt="onMouseOut and OnMouseLeave Event"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Javascript
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;br&gt;
 Javascript&lt;br&gt;
// src/App.js&lt;br&gt;
import React from "react";&lt;br&gt;
import "./styles.css";

&lt;p&gt;export default function App() {&lt;br&gt;
  const bullets = ['Item 1', 'Item 2', 'Item 3', 'Item 4'];&lt;br&gt;
  const handleMouseEvent = (e) =&amp;gt; {&lt;br&gt;
    e.persist();&lt;br&gt;
    e.target.style.backgroundColor = "orange";&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeout(() =&amp;amp;gt; {
  e.target.style.backgroundColor = "";
}, 1000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    &amp;lt;div className="App"&amp;gt;&lt;br&gt;
      &amp;lt;h3&amp;gt;onMouseOut Event:&amp;lt;/h3&amp;gt;&lt;br&gt;
      &amp;lt;ul className="menu" onMouseOut={handleMouseEvent}&amp;gt;&lt;br&gt;
        {bullets.map((item) =&amp;gt; (&lt;br&gt;
          &amp;lt;li className="menu-item"&amp;gt;{item}&amp;lt;/li&amp;gt;&lt;br&gt;
        ))}&lt;br&gt;
      &amp;lt;/ul&amp;gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;amp;lt;h3&amp;amp;gt;onMouseLeave Event:&amp;amp;lt;/h3&amp;amp;gt;
  &amp;amp;lt;ul className="menu" onMouseLeave={handleMouseEvent}&amp;amp;gt;
    {bullets.map((item) =&amp;amp;gt; (
      &amp;amp;lt;li className="menu-item"&amp;amp;gt;{item}&amp;amp;lt;/li&amp;amp;gt;
    ))}
  &amp;amp;lt;/ul&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;);&lt;br&gt;
}&lt;/p&gt;

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

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Takeaway&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;It's very easy to get confused between these two handlers when applying it on hovering a dropdown menu or any other element. I hope this article has helped you understand these handlers clearly. Additionally, when using other event handlers, we need to have a clear understanding of them before applying in our project.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy Coding!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>3 things to take care while making multi-level dropdown Menus</title>
      <dc:creator>Parth Gupta</dc:creator>
      <pubDate>Wed, 15 Feb 2023 22:23:38 +0000</pubDate>
      <link>https://dev.to/parthgupta118/3-things-to-keep-in-mind-while-making-multi-level-dropdown-menus-or-navbar-5727</link>
      <guid>https://dev.to/parthgupta118/3-things-to-keep-in-mind-while-making-multi-level-dropdown-menus-or-navbar-5727</guid>
      <description>&lt;p&gt;There are various types of menus on the web. For example, Standard Horizontal Menu, Hamburger Menu, Sticky or Fixed Menu etc. Creating inclusive experiences is a question of using the right menu pattern in the right place, with the right markup and behaviour.&lt;/p&gt;

&lt;p&gt;Here are 3 things to take care while implementing these types of menus:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Focus and hover interactions&lt;/li&gt;
&lt;li&gt;Mobile and desktop devices compatibility.&lt;/li&gt;
&lt;li&gt;ARIA semantics intended to help screen reader users.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Focus and Hover Interactions
&lt;/h2&gt;

&lt;p&gt;When it comes to implementing hover interactions with vertical Menubar and then showing up the submenus, it can cause chaos if not handled properly, so better to keep in mind the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;One must use the event-handlers &lt;code&gt;onMouseEnter&lt;/code&gt; and &lt;code&gt;onMouseLeave&lt;/code&gt; for hover interactions in 'menu-item'.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We also want our 'menu-item' to be accessible by pressing the &lt;code&gt;TAB&lt;/code&gt; key on the keyboard. To do that, we can use tab-index ARIA. We need to specify the unique &lt;code&gt;tabindex&lt;/code&gt; value to each 'menu-item'.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Don't use the &lt;code&gt;onMouseOut&lt;/code&gt; handler in place of &lt;code&gt;onMouseLeave&lt;/code&gt; handler. &lt;code&gt;onMouseOut&lt;/code&gt; handler triggers when the mouse pointer moves just out of the inner element. &lt;code&gt;onMouseLeave&lt;/code&gt; handler triggers when the mouse pointer moves out of the inner element and its descendant child element also.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;menu-bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;MENU&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;
      &lt;span class="nx"&gt;onMouseEnter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleMouseEnter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;onMouseLeave&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleMouseLeave&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;menu-item&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;tabindex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;))}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When showing the dropdown-menu on hover, make sure that when a user moves the mouse pointer from the hovered 'menu-item' element to the dropdown menu, there should be no gap between it, otherwise, our hovered element should be out of focus which invokes &lt;code&gt;onMouseLeave&lt;/code&gt; event, making the dropdown menu disappear before selecting anything.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fj29op3fvn0hyclryspkp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fj29op3fvn0hyclryspkp.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  Touch and desktop devices compatibility
&lt;/h2&gt;

&lt;p&gt;Navigation submenus or dropdown work well with a mouse and keyboard but they’re not so hot when it comes to touch. As a touchscreen user, we can't hover on it, we have to press or click on that menu-item. &lt;br&gt;
There are two possible resolutions here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can make a separate vertical Hamburger Menubar for mobile devices that opens the menu and submenus in vertical order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can make the top-level menu-item hoverable on desktop devices and clickable on mobile devices.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ARIA Semantics
&lt;/h2&gt;

&lt;p&gt;Like semantic HTML, ARIA is a W3 standard that helps make interfaces more accessible to people who use screen readers and other assistive technologies to consume content. &lt;br&gt;
So, let's look into the following steps on how we can apply the ARIA semantics in our menu-bar:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assign role='menu-bar' to 'ul' so that screen readers can identify it as a menubar.&lt;/li&gt;
&lt;li&gt;Assign role='menu-item' to each of its 'li' item also.&lt;/li&gt;
&lt;li&gt;Also give a unique tabindex value to each 'li' item to make it accessible with keyboards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it, these are the least things that we need to think while implementing the multi-level dropdown Menubar or Navbar.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy coding for you&lt;/em&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
