<?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: Tathagat</title>
    <description>The latest articles on DEV Community by Tathagat (@lazylad).</description>
    <link>https://dev.to/lazylad</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%2F485973%2Fd55d95cf-6c80-4d37-8b62-dd318d705c5b.jpeg</url>
      <title>DEV Community: Tathagat</title>
      <link>https://dev.to/lazylad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lazylad"/>
    <language>en</language>
    <item>
      <title>Create Scroll sensitive navbar in ReactJs [functional component]</title>
      <dc:creator>Tathagat</dc:creator>
      <pubDate>Wed, 02 Feb 2022 22:36:54 +0000</pubDate>
      <link>https://dev.to/lazylad/create-scroll-sensitive-navbar-in-reactjs-functional-component-l3i</link>
      <guid>https://dev.to/lazylad/create-scroll-sensitive-navbar-in-reactjs-functional-component-l3i</guid>
      <description>&lt;p&gt;Hey peers,&lt;br&gt;
I was surfing the web and had seen some of the website with navbar that show and hide itself based on user's scrolling behavior. This is pretty much the standard practice for the almost all the website UX nowadays, this certainly improves user's experience. Naturally I wanted to implement this on to my React  project, But there was only one problem, I did not know how to do that, I searched on Google, YouTube, GitHub, DEV.to, stackoverflow and so on, but no success in getting straight forward answer.&lt;br&gt;
This is post is for that, it is very simple, and I will tell you how to do this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Listen for scrolling event.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.addEventListener("scroll", handleScroll, { passive: true });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create a function that handles the event, at this point our work is almost done.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleScroll = () =&amp;gt; {
      const currentScrollY = window.scrollY;
      if (scrollPosition &amp;lt; currentScrollY &amp;amp;&amp;amp; goingUp) {
        setGoingUp(false);
      }
      if (scrollPosition &amp;gt; currentScrollY &amp;amp;&amp;amp; !goingUp) {
        setGoingUp(true);
      }
      setScrollPosition(currentScrollY);
    };

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

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Just make a CSS class which hide and shows the navbar (Give some transition effect for getting cool animation).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, the wait is over and we have created a scroll-sensitive navbar in React.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;code-&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AppNavbar.js
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Link } from "react-scroll";
import { useEffect, useState } from "react";
import { Container } from "react-bootstrap";
import "./style.css";

const AppNavbar = () =&amp;gt; {
  const [scrollPosition, setScrollPosition] = useState(0);
  const [goingUp, setGoingUp] = useState(true);
  useEffect(() =&amp;gt; {
    const handleScroll = () =&amp;gt; {
      const currentScrollY = window.scrollY;
      if (scrollPosition &amp;lt; currentScrollY &amp;amp;&amp;amp; goingUp) {
        setGoingUp(false);
      }
      if (scrollPosition &amp;gt; currentScrollY &amp;amp;&amp;amp; !goingUp) {
        setGoingUp(true);
      }
      setScrollPosition(currentScrollY);
    };
    window.addEventListener("scroll", handleScroll, { passive: true });
    return () =&amp;gt; window.removeEventListener("scroll", handleScroll);
  }, [goingUp, scrollPosition]);

  return (
    &amp;lt;div className={`app-nav ${!goingUp ? `hide` : `show`}`}&amp;gt;
      &amp;lt;Container className='d-flex flex-row align-items-center'&amp;gt;
        &amp;lt;div className='logo'&amp;gt;
          &amp;lt;Link to='section-1' duration={600}&amp;gt;
            Logo
          &amp;lt;/Link&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className='ms-auto'&amp;gt;
          &amp;lt;Link to='section-2' duration={600}&amp;gt;
            Details
          &amp;lt;/Link&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;Link to='section-3' duration={600}&amp;gt;
            Register
          &amp;lt;/Link&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;Link to='section-4' duration={600}&amp;gt;
            Contact Us
          &amp;lt;/Link&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/Container&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default AppNavbar;

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

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;Please ignore react-scroll component (Link) if your requirement is not same as mine, you can replace it with tag of your choice.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;2.style.scss&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.app-nav {
  position: fixed;
  top: 0;
  right: 0;
  width: 100%;
  display: inline-flex;
  align-items: center;
  font-size: larger;
  background-color: rgba(46, 46, 46, 0);
  backdrop-filter: blur(20px);
  .logo {
    font-size: 2.5rem;
  }
  div {
    cursor: pointer;
    color: $accent-color;
    text-align: center;
    padding: 8px 16px;
  }
}

.hide {
  top: -100px;
  transition: ease 0.3s;
}
.show {
  top: 0px;
  transition: ease 0.3s;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;OUTPUT-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfbuv1f07dstip13f0bj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnfbuv1f07dstip13f0bj.gif" alt="Final output" width="1280" height="720"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🖐️🖐️ &lt;u&gt;BYE&lt;/u&gt; 🖐️🖐️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
