<?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: Elaleh Aslani</title>
    <description>The latest articles on DEV Community by Elaleh Aslani (@elahehaslani).</description>
    <link>https://dev.to/elahehaslani</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%2F479030%2Ff7ef86e2-24ee-4e43-a0bc-010a8eaf9516.jpeg</url>
      <title>DEV Community: Elaleh Aslani</title>
      <link>https://dev.to/elahehaslani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elahehaslani"/>
    <language>en</language>
    <item>
      <title>How can we create the sticky sidebar for variable content in react?</title>
      <dc:creator>Elaleh Aslani</dc:creator>
      <pubDate>Thu, 22 Oct 2020 17:12:56 +0000</pubDate>
      <link>https://dev.to/elahehaslani/how-we-can-create-the-sticky-sidebar-for-variable-content-in-react-c59</link>
      <guid>https://dev.to/elahehaslani/how-we-can-create-the-sticky-sidebar-for-variable-content-in-react-c59</guid>
      <description>&lt;p&gt;We have a sidebar with variable content which must be sticky in its position, for example at the top and on the right of the main page. But when content changes with the user's selection, the sidebar should scroll with the content.&lt;br&gt;
Oh! I forget to tell you we are using REACT, and we want to manage this situation with the react library.&lt;br&gt;
Now we use react-stickynode from &lt;a href="https://github.com/yahoo/react-stickynode"&gt;https://github.com/yahoo/react-stickynode&lt;/a&gt;.&lt;br&gt;
This library helps us to create a sticky sidebar for our project.&lt;br&gt;
Let's go! The first step we need to install this library is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-stickynode
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second step is adding the type of react-stickynode in our package.json file with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save @types/react-stickynode
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can see the following dependency in the package.json file  “dependencies” part.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;“@types/react-stickynode”: “3.0.0”,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations! Now we can use the stickynode features.&lt;br&gt;
Suppose we have two components in the main section; the one for the sidebar and the other for the main content. There are two cases for the sidebar:&lt;/p&gt;

&lt;p&gt;1- The sidebar height is the same or less than the main content. In this case, the sidebar must stick at the top of the page and it mustn't scroll, i.e. whether the main page can be scrolled or not, it has to be fixed. look at the picture below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7AnJApjK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v3dci00odqed2f4dsfhb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7AnJApjK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v3dci00odqed2f4dsfhb.jpg" alt="Sidebar-01"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2-If the sidebar height is taller than the main content, i.e. it is taller than the viewport, and both the main content is being scrolled, and the sidebar is fixed at the top of the page, we can’t see the end of the sidebar. Oh, this is the bug! Just like the picture below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BnHVY7Qw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/znz3k7c8lh16gtdqy563.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BnHVY7Qw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/znz3k7c8lh16gtdqy563.jpg" alt="sidebar-02"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, what’s the solution?&lt;br&gt;
As you know, perhaps we have several ways to solve this problem. But we want to use stickynode for this case.&lt;br&gt;
After installing the react-stickynode and type of stickynode, import Sticky from ‘react-stickynode’ to the top of the sidebar component. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Sticky from 'react-stickynode';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sidebar’s component name can be an optional name like: Sidebar.&lt;br&gt;
Now we insert this component in the default component from package ‘react-stickynode’, named &lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Sticky&amp;gt;
    &amp;lt;Sidebar/&amp;gt;
&amp;lt;/Sticky&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case number one, when the user is scrolling the page down, react-stickynode will stick the sidebar to the top of the viewport. And in case number two, when the user is scrolling the page down react-stickynode will scroll along with the page until its bottom reaches the bottom of the viewport.&lt;br&gt;
So, in the end:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1YpbloXf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f4segrnzls72talxk8w4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1YpbloXf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f4segrnzls72talxk8w4.jpg" alt="sidebar-03"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The react-stickynode library provided some props for using them in the project. For example:&lt;/p&gt;

&lt;p&gt;enabled&lt;br&gt;
top&lt;br&gt;
bottomBoundary&lt;br&gt;
innerZ&lt;br&gt;
enableTransforms&lt;br&gt;
activeClass&lt;br&gt;
innerClass&lt;br&gt;
className&lt;br&gt;
releasedClass&lt;br&gt;
onStateChange&lt;br&gt;
shouldFreeze&lt;/p&gt;

&lt;p&gt;You can see these props and their documents with summary in this link :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/yahoo/react-stickynode"&gt;https://github.com/yahoo/react-stickynode&lt;/a&gt; &lt;/p&gt;

</description>
      <category>stickynode</category>
      <category>react</category>
      <category>sticky</category>
      <category>sidebar</category>
    </item>
  </channel>
</rss>
