<?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: Rajat Kapoor </title>
    <description>The latest articles on Forem by Rajat Kapoor  (@rajatkapoor).</description>
    <link>https://forem.com/rajatkapoor</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%2F203689%2Fa73c78b4-195b-4994-8050-24071105291d.jpg</url>
      <title>Forem: Rajat Kapoor </title>
      <link>https://forem.com/rajatkapoor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rajatkapoor"/>
    <language>en</language>
    <item>
      <title>CSS Container Queries - The future of responsive web design is almost here</title>
      <dc:creator>Rajat Kapoor </dc:creator>
      <pubDate>Tue, 25 May 2021 16:44:31 +0000</pubDate>
      <link>https://forem.com/rajatkapoor/css-container-queries-the-future-of-responsive-web-design-is-almost-here-43ab</link>
      <guid>https://forem.com/rajatkapoor/css-container-queries-the-future-of-responsive-web-design-is-almost-here-43ab</guid>
      <description>&lt;p&gt;With the huge number of smartphone users and increasing power of these devices, ensuring that your web applications run fine on all screen sizes is more important than ever. Fortunately CSS provides a couple of ways to style the same HTML in different ways on different devices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Responsive web design right now
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;You can skip to the next section if you're already familiar with &lt;code&gt;@media&lt;/code&gt; queries.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can use &lt;code&gt;@media&lt;/code&gt; queries in our CSS to conditionally apply CSS styles on the basis of the device or browser/user-agent properties. These properties could be the width of the screen, orientation of the device, whether the device is a screen reader or a printer, etc. &lt;/p&gt;

&lt;p&gt;Here is a small demo showing how media queries work.&lt;/p&gt;

&lt;p&gt;We will just add one &lt;code&gt;div&lt;/code&gt; in our html.&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;div class="box"&amp;gt;
    This is a responsive box. Resize your browser width to see the styles change
&amp;lt;div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here is our CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.box {
  background: lightblue;
  width:100%;
  height:100vh;
}

@media(max-width:800px) {
  .box {
    background: hotpink;
    color: white;
    font-weight: 700;
  }
}

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

&lt;/div&gt;



&lt;p&gt;You can check the whole demo here:&lt;br&gt;
&lt;a href="https://codepen.io/rajatkapoor/full/NWpgWor" rel="noopener noreferrer"&gt;https://codepen.io/rajatkapoor/full/NWpgWor&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you open the above link on a desktop/laptop you will see a screen with a blue background with black colored text written on it. Something like this:&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621947588544%2FQ_HP3Wciv.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621947588544%2FQ_HP3Wciv.png" alt="screely-1621947531671.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But if you open this on a smartphone, you will have a pink background instead of blue and the text would be written in white.&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621947616886%2FtdrRstGOW.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621947616886%2FtdrRstGOW.png" alt="screely-1621947570197.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Magic right?
&lt;/h4&gt;

&lt;p&gt;This is possible because of the &lt;code&gt;@media&lt;/code&gt; queries. This happens because we apply certain styles only when the width of our device screen is less than &lt;code&gt;800px&lt;/code&gt;. This is the CSS that causes the different styles on mobile and desktop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@media(max-width:800px) {
  .box {
    background: hotpink;
    color: white;
    font-weight: 700;
  }
}

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Enter &lt;code&gt;@container&lt;/code&gt; queries
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;@container queries are only supported on Chrome Canary with the &lt;code&gt;enable-container-queries&lt;/code&gt; flag enabled. You can download Chrome Canary  &lt;a href="https://www.google.com/intl/en_in/chrome/canary/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Once installed, open the URL chrome://flags/#enable-container-queries and enable container queries.&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621937509149%2F4B5WzT8fK.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621937509149%2F4B5WzT8fK.png" alt="Screenshot 2021-05-25 at 3.41.42 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is a polyfill under development that you can use to use container queries today. You can check it out &lt;a href="https://www.bram.us/2021/04/27/a-first-look-at-cqfill-a-polyfill-for-css-container-queries/" rel="noopener noreferrer"&gt;here.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;@container&lt;/code&gt; queries are one of the many features that are coming to CSS. Unlike media queries, which can only let you conditionally apply CSS styles on the basis of the browser properties(width, orientation, etc), container queries allow you to conditionally apply styles based on the properties of the container!&lt;/p&gt;

&lt;p&gt;For example, using container queries you can style cards to appear differently depending on the size of the element they are placed in. And that is what we will try to build.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here's what what we will build
&lt;/h2&gt;

&lt;p&gt;We will build a card that will change styles depending on the width of its container, and not the width of the screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://container-queries.vercel.app/" rel="noopener noreferrer"&gt;Here's a demo that works only on Chrome canary.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://container-queries-polyfill.vercel.app/" rel="noopener noreferrer"&gt;Here's the same demo with the &lt;code&gt;cqfill&lt;/code&gt; polyfill.&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You can see the full code of this demo in this &lt;a href="https://github.com/rajatkapoor/container-queries" rel="noopener noreferrer"&gt;github repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's try it out
&lt;/h2&gt;

&lt;p&gt;We'll use good old plain HTML and CSS for this project. Here is our initial HTML. Notice that we have also linked to our own "styles.css file".&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;!-- index.html --&amp;gt;
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Container Queries | Rajat Kapoor&amp;lt;/title&amp;gt;
    &amp;lt;!-- our own styles --&amp;gt;
    &amp;lt;link href="./styles.css" rel="stylesheet"&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will start by creating a very simple card, with an image, title, and some text. This card will have a &lt;code&gt;card--header&lt;/code&gt; with an image and a &lt;code&gt;card--body&lt;/code&gt; with a title and other card content. The card is styled using &lt;code&gt;flex&lt;/code&gt; with &lt;code&gt;flex-direction: row&lt;/code&gt;, so the image stays on the left-hand side and the rest of the text content on the right.&lt;/p&gt;

&lt;p&gt;We will wrap this card in a &lt;code&gt;div&lt;/code&gt; with class &lt;code&gt;parent large&lt;/code&gt; and will wrap all of this in a &lt;code&gt;container&lt;/code&gt; to center everything on the page.&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;!-- index.html  --&amp;gt;
&amp;lt;!-- ... --&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
        &amp;lt;div class="parent large"&amp;gt;
            &amp;lt;div class="card "&amp;gt;
                &amp;lt;div class="card--header "&amp;gt;
                    &amp;lt;img class="card--image" src="./img.jpeg" alt="card image"&amp;gt;
                    &amp;lt;!-- we have this img.jpeg file in our folder --&amp;gt;
                &amp;lt;/div&amp;gt;
                &amp;lt;div class="card--body "&amp;gt;
                    &amp;lt;h1 class="card--title "&amp;gt;
                        Lorem ipsum dolor sit amet.
                    &amp;lt;/h1&amp;gt;
                    &amp;lt;h3 class="card--content "&amp;gt;
                        Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nihil expedita accusantium, quidem
                        ducimus doloribus architecto a est deleniti, rerum saepe hic. Repellendus itaque ab officiis
                        voluptate ea earum perferendis consequatur!
                    &amp;lt;/h3&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;

&amp;lt;/body&amp;gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* styles.css */
body {
    background-color: lightblue;
}

.container {
    display: grid;
    place-items: center;
    grid-gap: 1em;
}

.card {
    background: white;
    border-radius: 2em;
    display: flex;
    flex-direction: row;
    overflow: hidden;
    padding: 2em;
}

.card--header {
    width: 100%;
}

.card--image {
    border-radius: 1em;
    object-fit: cover;
    height: 100%;
    width: 100%;
}

.card--body {
    padding: 2em;
    flex: 3
}

.card--header {
    flex: 2
}

.large {
    width: 100%;
}

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

&lt;/div&gt;



&lt;p&gt;This is how your page will look:&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621947469220%2F_qfD7xtpG.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621947469220%2F_qfD7xtpG.png" alt="screely-1621947454927.png"&gt;&lt;/a&gt;&lt;br&gt;
We will now add another &lt;code&gt;div&lt;/code&gt; with class &lt;code&gt;parent small&lt;/code&gt; and copy-paste the same card into it. We will also define styles for the &lt;code&gt;small&lt;/code&gt; class.&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;!-- index.html --&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
        &amp;lt;div class="parent large"&amp;gt;
            &amp;lt;div class="card"&amp;gt;
               &amp;lt;!-- ...card --&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="parent small"&amp;gt;
            &amp;lt;div class="card"&amp;gt;
               &amp;lt;!-- ...card --&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;

&amp;lt;/body&amp;gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// styles.css

.small {
    width: 500px;
}    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that both the cards are exactly the same and are just lying in two different &lt;code&gt;div&lt;/code&gt;s. And this is how they look:&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621947760881%2F1YEI-jGLG.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621947760881%2F1YEI-jGLG.png" alt="screely-1621947739345.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding container queries
&lt;/h2&gt;

&lt;p&gt;We want the card to have the text content below the image in case the width of the container is less than 500px.  We will also change the background of our card to hotpink and make the text white to make the changes more noticable.&lt;br&gt;
For container queries to work, we need to add CSS to the container to define the "containment context" — the container queries of the children inside this containment context react to the properties of this container. We will make the &lt;code&gt;div&lt;/code&gt;s with class &lt;code&gt;parent&lt;/code&gt; as the containment context of these cards. To do that, we will modify the CSS of the &lt;code&gt;parent&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* styles.css */

.parent {
    contain: layout inline-size style;
}

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

&lt;/div&gt;



&lt;p&gt;Now we can add container queries to our card.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* styles.css */

@container(max-width: 500px) {
    .card {
        flex-direction: column;
        background-color: pink;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thats it! Now if you go back to your browser, you will see that the card in the &lt;code&gt;div&lt;/code&gt; with the class &lt;code&gt;small&lt;/code&gt; has the new styles applied. &lt;/p&gt;

&lt;h3&gt;
  
  
  Yay 🎉
&lt;/h3&gt;

&lt;p&gt;We were able to control the styling on the basis of the container size and not just the device properties (screen size). That is the power of container queries.&lt;/p&gt;

&lt;h1&gt;
  
  
  Bonus — Let's make the &lt;code&gt;parent&lt;/code&gt; resizable
&lt;/h1&gt;

&lt;p&gt;Do you know you can quickly make anything resizable by just adding CSS. We'll do it to our &lt;code&gt;parent&lt;/code&gt;class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* styles.css */

.parent {
    contain: layout inline-size style;
    resize: horizontal;
    overflow: auto;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now both the &lt;code&gt;parent&lt;/code&gt; &lt;code&gt;div&lt;/code&gt;s can be resized horizontally to see our container queries working in action.&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621950303218%2FRIJaqv3ZG-.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1621950303218%2FRIJaqv3ZG-.gif" alt="CPT2105251904-1675x864-min.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;With &lt;code&gt;container&lt;/code&gt; queries, we will be able to approach responsive designs in a much smarter way. That said, &lt;code&gt;container&lt;/code&gt; queries are in early stages of development. It is not recommended to use these in production right now. In fact, CSSWG changed the syntax between the time I started to write this article to the time I finished writing it 😅.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/therajatkapoor" rel="noopener noreferrer"&gt;Follow me on twitter&lt;/a&gt; to get more web development related content.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Special thanks to &lt;a href="https://twitter.com/shadeed9" rel="noopener noreferrer"&gt;Ahmad Shadeed&lt;/a&gt; for helping me with debugging why my &lt;code&gt;container&lt;/code&gt; queries were not working in Chrome Canary. You can also read more about container queries in &lt;a href="https://ishadeed.com/article/say-hello-to-css-container-queries/" rel="noopener noreferrer"&gt;this article&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>html</category>
    </item>
    <item>
      <title>Create your dev portfolio - Part 1: First things first</title>
      <dc:creator>Rajat Kapoor </dc:creator>
      <pubDate>Mon, 17 May 2021 15:13:19 +0000</pubDate>
      <link>https://forem.com/rajatkapoor/create-your-dev-portfolio-part-1-first-things-first-55bp</link>
      <guid>https://forem.com/rajatkapoor/create-your-dev-portfolio-part-1-first-things-first-55bp</guid>
      <description>&lt;p&gt;"You can't build a great building on a weak foundation." — Gordon B. Hinckley&lt;/p&gt;

&lt;p&gt;In this post we will set up the foundations for our portfolio:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a Next JS app&lt;/li&gt;
&lt;li&gt;Set up Chakra UI with a theme&lt;/li&gt;
&lt;li&gt;Set up automatic deploys on Vercel using Github&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let's get started.&lt;/p&gt;

&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;I have been developing web applications since my college days and been coding professionally for over 6 years. I have owned the domain  &lt;a href="//rajatkapoor.me"&gt;https://rajatkapoor.me&lt;/a&gt; for so long but have never hosted anything on it. Now is the time.&lt;/p&gt;

&lt;p&gt;In this series of posts, I will create a decent-looking developer portfolio for myself using  &lt;a href="https://nextjs.org/" rel="noopener noreferrer"&gt;NextJs&lt;/a&gt;  and  &lt;a href="https://chakra-ui.com/" rel="noopener noreferrer"&gt;Chakra UI&lt;/a&gt;. I will then host it up on  &lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt; and point my domain (&lt;a href="https://rajatkapoor.me" rel="noopener noreferrer"&gt;https://rajatkapoor.me&lt;/a&gt;) to it.&lt;/p&gt;

&lt;p&gt;You can also follow along and create a developer portfolio of your own. You can follow on my progress &lt;a href="https://rajatkapoor.vercel.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;  and check the  &lt;a href="https://github.com/rajatkapoor/next.rajatkapoor.me" rel="noopener noreferrer"&gt;github repository here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Disclaimer:
&lt;/h3&gt;

&lt;p&gt;I am horrible at design, so I will be looking at design resources and other dev portfolios to get inspiration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a next JS App
&lt;/h2&gt;

&lt;p&gt;We'll start with creating a new Next js app and run it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app portfolio // "portfolio" is the name of the app, you could call it anything you like

cd portfolio

// to run the app
yarn dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see an output like this on your screen&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ready - started server on 0.0.0.0:3000, url: http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to the URL that's shown in your terminal and you will be able to see your app in action.&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620909634991%2Fmq57sEAL4.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620909634991%2Fmq57sEAL4.png" alt="Screenshot 2021-05-13 at 6.10.27 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup Chakra-UI
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://chakra-ui.com/" rel="noopener noreferrer"&gt;Chakra UI&lt;/a&gt;  is a react component library with a great set of components and a prop based model of styling them. All components in Chakra UI are accessible and can be configured with a very well defined theming system. With Chakra UI, you can quickly build accessible React apps. To install it in your app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// make sure you're inside the portfolio folder

yarn add @chakra-ui/react @emotion/react@^11 @emotion/styled@^11 framer-motion@^4

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

&lt;/div&gt;



&lt;p&gt;Chakra UI apps have to be wrapped in a &lt;code&gt;&amp;lt;ChakraProvider&amp;gt;&lt;/code&gt; for them to function correctly. We will wrap our react app's root component inside it. NextJS expects this root component to be default exported from a special file - &lt;code&gt;pages/_app.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pages/_app.js

import { ChakraProvider } from "@chakra-ui/react"

function MyApp({ Component, pageProps }) {
  return (
    &amp;lt;ChakraProvider&amp;gt;
      &amp;lt;Component {...pageProps} /&amp;gt;
    &amp;lt;/ChakraProvider&amp;gt;
  )
}
export default MyApp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding a theme
&lt;/h3&gt;

&lt;p&gt;Chakra UI has a robust theme system, that allows you to re-use styles and add styling rules in a single place. We will neither add any relevant theme-related changes, nor utilize the full power of this theme. But we will configure it and keep it ready for use when the time comes.&lt;/p&gt;

&lt;p&gt;For this, create a &lt;code&gt;theme.js&lt;/code&gt; file at the root directory of your app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ./theme.js

import { extendTheme } from "@chakra-ui/react";

const colors = {
  brand: {
    900: "#1a365d",
    800: "#153e75",
    700: "#2a69ac",
  },
};

const theme = extendTheme({ colors });

export default theme;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then pass this &lt;code&gt;theme&lt;/code&gt; to the &lt;code&gt;&amp;lt;ChakraProvider&amp;gt;&lt;/code&gt; in &lt;code&gt;pages/_app.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pages/_app.js

import { ChakraProvider } from "@chakra-ui/react";

import theme from "../theme"; // &amp;lt;- import here

function MyApp({ Component, pageProps }) {
  return (
    &amp;lt;ChakraProvider theme={theme}&amp;gt; 
      &amp;lt;Component {...pageProps} /&amp;gt;
    &amp;lt;/ChakraProvider&amp;gt;
  );
}

export default MyApp;

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

&lt;/div&gt;



&lt;p&gt;Now that we are all set up, let's update our 'pages/index.js' file to use some of the components from Chakra UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ./pages/index.js

import Head from "next/head";
import Image from "next/image";
import { Box } from "@chakra-ui/react";

export default function Home() {
  return (
    &amp;lt;Box w={"100%"}&amp;gt;
      &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;Rajat Kapoor - Full stack developer&amp;lt;/title&amp;gt;
        &amp;lt;meta
          name="description"
          content="Rajat Kapoor is a full stack developer from India"
        /&amp;gt;
        &amp;lt;link rel="icon" href="/favicon.ico" /&amp;gt;
      &amp;lt;/Head&amp;gt;
      &amp;lt;Box&amp;gt;hi&amp;lt;/Box&amp;gt;
    &amp;lt;/Box&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see a small but rewarding message on the top left 😎&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620911644615%2Fi4nuHRRji.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620911644615%2Fi4nuHRRji.png" alt="Screenshot 2021-05-13 at 6.43.57 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hosting on vercel
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;This section assumes you know basics of git and have set up this repository on Github or a similar platform. If that is not the case, please look out for already existing resources from which you can learn those things. If you're still not able to set it up, drop a message in the comments and I will be happy to help you out.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt; is a web hosting platform that allows you to host your NextJS (and many more types of apps) for free. It is made by the same people who made NextJS and provides a simple but powerful developer experience, especially for NextJS apps. Now let's get this hosted on Vercel, so that we can share the progress of our portfolio with everyone and get early feedback.&lt;/p&gt;

&lt;p&gt;Head on to &lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;https://vercel.com&lt;/a&gt;  and create an account. You could also use your social account to sign up.&lt;/p&gt;

&lt;p&gt;You will land on a page that will allow you to import a git repository. Connect your Github (or any other git provider's) account, select the repository where you have pushed the code for this project, and click on "Import".&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620914322868%2FQNVeWr95j.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620914322868%2FQNVeWr95j.png" alt="Screenshot 2021-05-13 at 6.58.14 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choose to use your personal account when prompted. You will land on the page where you could choose a name for your project and change other settings. All the settings should have been auto-configured correctly and you would not need to change anything. Just click on "Deploy" and let the magic happen.&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620912603720%2FNn-2hIVll.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620912603720%2FNn-2hIVll.png" alt="Screenshot 2021-05-13 at 6.59.40 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The deployment will start and you'll be greeted with a success message as soon as it completes.&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620913091448%2FJqlvzeKfj.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620913091448%2FJqlvzeKfj.png" alt="Screenshot 2021-05-13 at 7.07.22 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the "Visit" button to view the deployed website. For every commit that you push to your repo, Vercel will automatically deploy the latest code on this URL. Vercel will also maintain a live, deployed copy of each of your commits for you to look back (or if you wish to roll back to a previous version). Check out the "Deployments" tab on your project on Vercel dashboard to see deployments corresponding to all your commits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion — of the beginning
&lt;/h2&gt;

&lt;p&gt;That must feel like an achievement. Tap your shoulder, clap for yourself. You've done a lot.&lt;/p&gt;

&lt;p&gt;In the next post, we'll actually start building the portfolio - by adding a navbar, a main hero section and highlight some of our work. Stay tuned for more.&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>portfolio</category>
    </item>
    <item>
      <title>4 ways to perfectly center content using CSS
</title>
      <dc:creator>Rajat Kapoor </dc:creator>
      <pubDate>Fri, 14 May 2021 09:48:58 +0000</pubDate>
      <link>https://forem.com/rajatkapoor/4-ways-to-perfectly-center-content-using-css-5hfi</link>
      <guid>https://forem.com/rajatkapoor/4-ways-to-perfectly-center-content-using-css-5hfi</guid>
      <description>&lt;p&gt;Centering content in CSS is something that beginners struggle a lot with. CSS provides a lot of options that allow you to center content inside a container. We'll try some of the most commonly used ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Centering text
&lt;/h2&gt;

&lt;p&gt;Let's start by learning how to center text in a container&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;!--  our html has a container and text content inside --&amp;gt;
&amp;lt;div class="container center"&amp;gt;
  this text should be centered
&amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;We can center the text content using &lt;code&gt;text-align: center&lt;/code&gt;. Here's is how our CSS looks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* Just to see how big the container is*/
.container {
  background: black;
  font-size: 2rem;
  color: turquoise;
  height: 400px;
}

.center {
  text-align: center;  /* horizontally center aligns the text */

  vertical-align: middle;  /* aligns the text in the middle of the `line-height` of the text*/
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See it in action:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/rajatkapoor/embed/VwpvGMZ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;If you want to place the text at the center of the screen, wrap it in a &lt;code&gt;div&lt;/code&gt; and follow any of the techniques below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Centering a div inside another
&lt;/h2&gt;

&lt;p&gt;There are multiple ways to do it. Check out all of these and let me know your favorite one in the comments. For the next few examples, we'll use this common HTML and CSS and highlight only the CSS changes needed to center the contents&lt;/p&gt;

&lt;h4&gt;
  
  
  HTML:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!--  our html has a container which contains a div we wish to center--&amp;gt;
&amp;lt;div class="container center"&amp;gt;
  &amp;lt;div class="inner"&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;#### CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html,
body {
  height: 100%;
}
.container {
  background: black;
  width: 100%;
  height: 100%;
  font-size: 2rem;
}

.inner {
  background: turquoise;
  width: 50px;
  height: 50px;
  border-radius: 50%;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using &lt;code&gt;margin&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;We can horizontally center the inner div using margin&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.inner{
  margin: auto;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See it in action here&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/rajatkapoor/embed/yLMYQYN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;flex&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;To center this using flex, we need the following CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.center{
  display: flex;
  align-items: center;
  justify-content: center;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See it in action here&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/rajatkapoor/embed/qBrOJQR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;grid&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.center{
  display: grid;
  place-items: center;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See it in action here&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/rajatkapoor/embed/RwpWqwy?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is not all of it. There are many other ways to center content. Post your favorite way of centering content in the comments.&lt;/p&gt;

</description>
      <category>css</category>
      <category>cssgrid</category>
      <category>flexbox</category>
      <category>html</category>
    </item>
    <item>
      <title>Writing my first custom react hook - useOutsideClick</title>
      <dc:creator>Rajat Kapoor </dc:creator>
      <pubDate>Wed, 12 May 2021 16:53:52 +0000</pubDate>
      <link>https://forem.com/rajatkapoor/writing-my-first-custom-react-hook-useoutsideclick-46gg</link>
      <guid>https://forem.com/rajatkapoor/writing-my-first-custom-react-hook-useoutsideclick-46gg</guid>
      <description>&lt;p&gt;When react hooks were launched, they completely changed the react ecosystem. I have been using react hooks for quite some time now and I am a big fan. But like a lot of other developers, I have never written a custom react hook. This is mainly because firstly, all the functionality I need is available in a third-party hooks library, and secondly, procrastination.&lt;/p&gt;

&lt;p&gt;I am a firm believer in learning by doing. So I am going to create a very simple hook - &lt;strong&gt;useOutsideClick&lt;/strong&gt;. This hook will help us to trigger a function when a user clicks outside a component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where can we use this?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Close expanded states of a component when a user clicks outside&lt;/li&gt;
&lt;li&gt;Close modals when users click outside the modal&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;and many more&lt;/p&gt;

&lt;h2&gt;
  
  
  How will we create this?
&lt;/h2&gt;

&lt;p&gt;This may not be the best way, but I have been using a very simple approach in my older class-based components. I will just try to replicate that with a custom hook. Here's what we will do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We will add an &lt;code&gt;onClickListener&lt;/code&gt; to the &lt;code&gt;document&lt;/code&gt; when the component mounts&lt;/li&gt;
&lt;li&gt;In this click listener, we will trigger the &lt;code&gt;outsideClickHandler&lt;/code&gt; when the target of the click lies outside the desired component&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Let's get started
&lt;/h1&gt;

&lt;p&gt;You can find the final code of this tutorial in this  &lt;a href="https://github.com/rajatkapoor/useOutsideClick" rel="noopener noreferrer"&gt;github repository&lt;/a&gt; and a &lt;a href="https://use-outside-click.vercel.app/" rel="noopener noreferrer"&gt;live working demo here&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Let's create a react app and run it using the following commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app useOutsideClick
npm install # to install all dependencies
npm run start # to run the app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll first create the outside click functionality in a simple functional component and then try to extract it into  a custom hook&lt;/p&gt;

&lt;p&gt;Let's edit &lt;code&gt;src/App.js&lt;/code&gt; to look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./styles.css";

export default function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;div className="main"&amp;gt;Click me&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and update the styles in &lt;code&gt;./styles.css&lt;/code&gt;  to make things slightly less ugly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;html, body, #root {
  display: grid;
  place-items: center;
  height: 100%;
  width: 100%;
}

.main {
  background: lightskyblue;
  font-size: 2rem;
  width: 20vh;
  height: 10vh;
  display: grid;
  place-items: center;
  border-radius: 40px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you check the browser, you'll see something like this&lt;/p&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620575478835%2FbCHwkGbJd.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620575478835%2FbCHwkGbJd.png" alt="frame_safari_dark.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding outside click functionality
&lt;/h2&gt;

&lt;p&gt;We'll now try to detect when the user has clicked outside the div that says "click me" using the  &lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;useEffect&lt;/a&gt;  and  &lt;a href="https://reactjs.org/docs/hooks-reference.html#useref" rel="noopener noreferrer"&gt;useRef&lt;/a&gt; hooks.&lt;/p&gt;

&lt;p&gt;We will start by creating a new &lt;code&gt;ref&lt;/code&gt; for the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; outside which we want to detect clicks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mainRef = useRef();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and pass it as the &lt;code&gt;ref&lt;/code&gt; prop to the div&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;div className="main" ref={mainRef}&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our click handler, we will check whether the &lt;code&gt;event.target&lt;/code&gt; lies inside the target element. We can do that using the  &lt;a href="https://htmldom.dev/check-if-an-element-is-a-descendant-of-another/" rel="noopener noreferrer"&gt;&lt;code&gt;contains&lt;/code&gt; function&lt;/a&gt;. For now, we will just log if the click is outside the element&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const onOutsideClick = (e) =&amp;gt; {
    const inMain = mainRef.current.contains(e.target);
    const isOutside = !inMain;
    if (isOutside) {
      # call the outside click handler here
      console.log("Clicked ouside");
    }
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to listen to clicks on the whole document as soon as the component mounts or whenever the ref changes. We will do that using the &lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;useEffect&lt;/a&gt; hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    document.addEventListener("click", onOutsideClick);
    // cleaning up the event listener when the component unmounts
    return () =&amp;gt; {
      document.removeEventListener("click", onOutsideClick);
    };
  }, [mainRef]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;src/App.js&lt;/code&gt; will now be like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useRef } from "react";
import "./styles.css";

export default function App() {
  const mainRef = useRef();
  const onOutsideClick = (e) =&amp;gt; {
    const inMain = mainRef.current.contains(e.target);
    const isOutside = !inMain;
    if (isOutside) {
      console.log("Clicked ouside");
    }
  };
  useEffect(() =&amp;gt; {
    document.addEventListener("click", onOutsideClick);
    return () =&amp;gt; {
      console.log("cleanup");
      document.removeEventListener("click", onOutsideClick);
    };
  }, [mainRef]);
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;div className="main" ref={mainRef}&amp;gt;
        Click me
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. We now just need to extract this functionality in a custom hook.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a custom hook
&lt;/h2&gt;

&lt;p&gt;Create a new file called &lt;code&gt;useOutsideClick.js&lt;/code&gt;. We will now copy over the code from our &lt;code&gt;src/App.js&lt;/code&gt; file to &lt;code&gt;src/useOutsideClick.js&lt;/code&gt; and update it to accept the &lt;code&gt;componentRef&lt;/code&gt; and the &lt;code&gt;outsideClickHandler&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# src/useOutsideClick.js

import { useEffect } from "react";

export const useOutsideClick = (componentRef, outsideClickHandler) =&amp;gt; {
  const onOutsideClick = (e) =&amp;gt; {
    // updated this to use the passed componentRef
    if (!componentRef.current) {
      return;
    }
    const inMain = componentRef.current.contains(e.target);
    const isOutside = !inMain;
    if (isOutside) {
      outsideClickHandler();
    }
  };
  useEffect(() =&amp;gt; {
    document.addEventListener("click", onOutsideClick);
    return () =&amp;gt; {
      console.log("cleanup");
      document.removeEventListener("click", onOutsideClick);
    };
  }, [componentRef]);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will now use this inside our app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#src/App.js

import { useEffect, useRef } from "react";
import "./styles.css";
import { useOutsideClick } from "./useOutsideClick";

export default function App() {
  const mainRef = useRef();
  useOutsideClick(mainRef, () =&amp;gt; console.log("Clicked outside"));
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;div className="main" ref={mainRef}&amp;gt;
        Click me
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And things work perfectly 🎉&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;We will now update our app to showcase one of the use cases. When the user clicks the blue &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, we will show more content below it. We will hide this content when the user clicks anywhere outside this button on the screen. We maintain this state in the state variable &lt;code&gt;expanded&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#src/App.js

import { useEffect, useRef, useState } from "react";
import "./styles.css";
import { useOutsideClick } from "./useOutsideClick";

export default function App() {
  const mainRef = useRef();
  // initially not expanded
  const [expanded, setExpanded] = useState(false);

  // set `expanded` to `false` when clicked outside the &amp;lt;div&amp;gt;
  useOutsideClick(mainRef, () =&amp;gt; setExpanded(false));
  return (
    &amp;lt;div className="App"&amp;gt;
      // set `expanded` to `true` when this &amp;lt;div&amp;gt; is clicked
      &amp;lt;div className="main" ref={mainRef} onClick={() =&amp;gt; setExpanded(true)}&amp;gt;
        Click me
      &amp;lt;/div&amp;gt;
      // show more details only when `expanded` is `true`
      {expanded &amp;amp;&amp;amp; &amp;lt;div className="more"&amp;gt;Lorem ipsum dolor sit amet&amp;lt;/div&amp;gt;}
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* src/styles.css */

/* add this */
.more {
  text-align: center;
  font-size: 1.2rem;
  background: lightskyblue;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how things look now&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620580483969%2F4JsOXZgYS.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1620580483969%2F4JsOXZgYS.gif" alt="CPT2105092244-1680x939.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Hooray! We have written our first custom hook. You could also check out one of the widely used custom hook libraries( &lt;a href="https://streamich.github.io/react-use/" rel="noopener noreferrer"&gt;react-use&lt;/a&gt;  or  &lt;a href="https://react-hooks.org/" rel="noopener noreferrer"&gt;rooks&lt;/a&gt; ) and try to recreate one of the hooks for practice&lt;/p&gt;

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