<?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: Nitin Jadhav</title>
    <description>The latest articles on Forem by Nitin Jadhav (@nitinja).</description>
    <link>https://forem.com/nitinja</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%2F67005%2Ffcc2166a-3a7d-4e04-bab4-fd4c236125b0.jpeg</url>
      <title>Forem: Nitin Jadhav</title>
      <link>https://forem.com/nitinja</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nitinja"/>
    <language>en</language>
    <item>
      <title>Easy theming with CSS Variables: Add dark mode to your app</title>
      <dc:creator>Nitin Jadhav</dc:creator>
      <pubDate>Wed, 27 May 2020 07:25:54 +0000</pubDate>
      <link>https://forem.com/nitinja/easy-theming-with-css-variables-add-dark-mode-to-your-app-2p8d</link>
      <guid>https://forem.com/nitinja/easy-theming-with-css-variables-add-dark-mode-to-your-app-2p8d</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--COEtEic0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nsfdlcd3y02g8o3575x2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--COEtEic0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nsfdlcd3y02g8o3575x2.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why join the dark side &lt;code&gt;(｡▽皿▽)&lt;/code&gt; ?
&lt;/h3&gt;

&lt;p&gt;Adding dark mode to websites/web-apps are trending now. Here are some of it's benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dark mode is easy on eyes for some users&lt;/li&gt;
&lt;li&gt;You can add various themes along with dark mode (for example, Sepia). This makes your app more interesting.&lt;/li&gt;
&lt;li&gt;Your app looks infinitely better with dark mode.&lt;/li&gt;
&lt;li&gt;Users may save some battery using dark mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's really easy (will take a day or so for small-sized website or app).&lt;/p&gt;

&lt;h3&gt;
  
  
  Demo
&lt;/h3&gt;

&lt;p&gt;here is demo: &lt;a href="https://codesandbox.io/s/theming-css-variables-7tsnw"&gt;Codesandbox&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  CSS Variables
&lt;/h3&gt;

&lt;p&gt;If you are living under rock, CSS variables are like JavaScript variables. We can define CSS variables at one place in stylesheet and can use those variables all over stylesheets instead of hard coded values. These are the building blocks of model css architecture.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;--myFavColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#BADA55&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;variable&lt;/span&gt; &lt;span class="err"&gt;defination&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.myDiv&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--myFavColor&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="err"&gt;//usage&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You need to have a basic html page. I will use a simple html login form and some basic styling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"myForm"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Login Form&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"field"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;User Name&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"useraname"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"field"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Password&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"rememberme"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; Remember Me &lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"button-container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Login&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"src/index.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that all the styling is done through CSS classes, &lt;em&gt;no inline styles is used&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step #1: Add &lt;code&gt;light&lt;/code&gt; CSS class with some CSS variable definitions
&lt;/h4&gt;

&lt;p&gt;Add a &lt;code&gt;light&lt;/code&gt; class. This will be a default light theme for our app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.light&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#f1faee&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--background-alt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#a8dadc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--foreground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#1d3557&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--accent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#0e8185&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can use these variables in our stylesheet, instead of hardcoded values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100vh&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--foreground&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--background&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.myForm&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--background-alt&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;24rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.myForm&lt;/span&gt; &lt;span class="nc"&gt;.field&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.myForm&lt;/span&gt; &lt;span class="nc"&gt;.field&lt;/span&gt; &lt;span class="nt"&gt;label&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.myForm&lt;/span&gt; &lt;span class="nc"&gt;.field&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--accent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.button-container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--background&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--foreground&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5rem&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Optional&lt;/strong&gt;: We may also want to add some CSS variables that are independent of any theme. Usually they are defined in &lt;code&gt;html&lt;/code&gt; tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* these variables are theme-independent */&lt;/span&gt;
&lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--purewhite&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#ffffff&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--pureblack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Step #2: Add &lt;code&gt;light&lt;/code&gt; class to your &lt;code&gt;body&lt;/code&gt; tag
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"light"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- html here -- &amp;gt;
&amp;lt;/body&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Step #3: Add &lt;code&gt;dark&lt;/code&gt; CSS class with exact variable definitions as of &lt;code&gt;light&lt;/code&gt; class, but with different colours that are suitable for dark mode
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.dark&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;--background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#1d3557&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--background-alt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#457b9d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--foreground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#f1faee&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;--accent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#84a59d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once again, we have to add all of the variables that are there is light class. &lt;/p&gt;

&lt;p&gt;Colours will usually be inverted (e.g. light text on dark background) as opposed to light class.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step #4: Add a  button to switch between themes
&lt;/h4&gt;

&lt;p&gt;We have to add an action, which will replace the &lt;code&gt;light&lt;/code&gt; class of &lt;code&gt;body&lt;/code&gt; tag with &lt;code&gt;dark&lt;/code&gt; class (and vice versa).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;classList.toggle()&lt;/code&gt; method toggles a CSS class in given element's classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button id="theme-toggle"&amp;gt;Toggle Dark Theme&amp;lt;/button&amp;gt;

//javascript
document.getElementById("theme-toggle").addEventListener("click", function() {
  document.body.classList.toggle("light");
  document.body.classList.toggle("dark");
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Optional&lt;/strong&gt;: You can add more themes like Sepia and use something like dropdown as theme switcher. You may also want to save/restore theme choice to/from local storage for persistence.&lt;/p&gt;

&lt;p&gt;See the completed demo here: &lt;a href="https://codesandbox.io/s/theming-css-variables-7tsnw"&gt;Codesandbox&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;That's it!&lt;/p&gt;

&lt;p&gt;You just added a dark mode to your website/app.  &lt;/p&gt;

</description>
      <category>cssvariables</category>
      <category>darkmode</category>
      <category>theming</category>
    </item>
    <item>
      <title>HTTP/2: As web developer, What you need to change</title>
      <dc:creator>Nitin Jadhav</dc:creator>
      <pubDate>Mon, 09 Mar 2020 13:49:01 +0000</pubDate>
      <link>https://forem.com/nitinja/http-2-as-web-developer-what-you-need-to-change-3jb3</link>
      <guid>https://forem.com/nitinja/http-2-as-web-developer-what-you-need-to-change-3jb3</guid>
      <description>&lt;p&gt;As of March 2020, HTTP/2 is used by 43.8% of all the websites. This numbers will only grow. This is short summary of my recent exploration of subject.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Quick Overview of HTTP/2
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;HTTP/2 is a new Protocol that is replacing HTTP/1.1 thats currently de-facto standard of web.&lt;/li&gt;
&lt;li&gt;Main motivation behind it is Performance.&lt;/li&gt;
&lt;li&gt;Its backward compatible. Clients and browsers can use HTTP/1.1 as usual if they don't support HTTP/2.&lt;/li&gt;
&lt;li&gt;With HTTP/2, SSL is recommended for performance reasons, but not compulsory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What improvements HTTP/2 brings?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Multiplexing: 
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cGN9dXnb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vz2vrrmguawfswtqmhbl.png" alt="Alt Text"&gt;
Http/2 can fetch multiple files (usually HTML, JS CSS files etc) simultaneously from server. It uses same TCP connection for multiple files, hence multiplexing. Only one TCP connection is necessary.
In comparison, Http/1.1 allows 4-8 max connections to server (per domain) at same time, restricting number of files you can fetch. One request has to wait for previous request to finish.&lt;/li&gt;
&lt;li&gt;Uses &lt;strong&gt;Binary encoding&lt;/strong&gt; for request/response as compared to http/1.1 which uses text encoding. That means &lt;strong&gt;more accurate handling of Line endings, whitespace encoding&lt;/strong&gt; etc. Binary encoding is also efficient to parse and use.&lt;/li&gt;
&lt;li&gt;Stream Prioritization
Each request stream can be prioritised. So browser can download a critical css file on priority while JS files may load with lower priority. (I haven't seen example of this in wild, but will update this point when the more details are available).&lt;/li&gt;
&lt;li&gt;Stream dependencies: Dependencies between streams can also be specified. So we can say that one js file can be dependent on another one.&lt;/li&gt;
&lt;li&gt;HTTP header compression
Http/2 supports header compression. That means less data on wire.&lt;/li&gt;
&lt;li&gt;Server Initiated Push
Unlike Http/1.1, Http/2 server can start pushing files to client on its own even before client requests those. This reduced latency and wait for html-parsing-and-asset-request.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to prepare for HTTP/2?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stop bundling assets&lt;/strong&gt;: &lt;br&gt;
No need to bundle css/JS files since http/2 supports multiplexing. There is almost no overhead in making many web requests. It may also mean that we don't have to deploy entire bundle when only couple of files are updated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stop inlining JS/CSS/Images/SVG&lt;/strong&gt;:&lt;br&gt;
Fetch those as separate files. You might get advantage of browser caching with seperate files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use lazy loading&lt;/strong&gt; for JS/css files. This is more efficient and reduces initial load time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stop creating image Sprites&lt;/strong&gt;:&lt;br&gt;
This in now unnecessary due to multiplexing. This also means simpler workflow and build script.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stop domain sharding&lt;/strong&gt;:&lt;br&gt;
No need to spread your files across multiple domains, since we can fetch everything now from same domain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Start Using HTTPS&lt;/strong&gt;:&lt;br&gt;
Its recommended standard with HTTP/2. You get more secure website/app by default.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But As always, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make sure your audience is using latest browsers(HTTP/2 is now supported in nearly all browsers, except our usual suspect, IE &amp;lt; v10)&lt;/li&gt;
&lt;li&gt;HTTP/2 is enabled on your server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See you on HTTP2!&lt;/p&gt;

</description>
      <category>http2</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
    <item>
      <title>Improve your JavaScript with functional programming</title>
      <dc:creator>Nitin Jadhav</dc:creator>
      <pubDate>Wed, 31 Jul 2019 08:26:23 +0000</pubDate>
      <link>https://forem.com/nitinja/improve-your-javascript-with-functional-programming-3jjb</link>
      <guid>https://forem.com/nitinja/improve-your-javascript-with-functional-programming-3jjb</guid>
      <description>&lt;p&gt;If you want to write your JavaScript code in a better way, one of the most effective techniques is functional programming (FP). It's no difficult than the language itself - don't let anyone else tell you otherwise. &lt;strong&gt;This is intended for beginner programmers&lt;/strong&gt;. This series will have multiple posts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Pure functions
&lt;/h3&gt;

&lt;p&gt;A pure function is one which&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Does not access anything else other than provided arguments.&lt;/li&gt;
&lt;li&gt;Does not modify anything outside of a function. It may return the calculated value based on arguments. This means that they will always return the same output when provided with the same input.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Example of pure function
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const calculateArea = radius =&amp;gt; 3.14 * radius * radius;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Use &lt;code&gt;map()&lt;/code&gt; instead of &lt;code&gt;for/forEach&lt;/code&gt; for array conversions
&lt;/h3&gt;

&lt;p&gt;(you are probably already doing this)&lt;/p&gt;

&lt;p&gt;Process and covert an array into a new array using &lt;code&gt;for/forEach&lt;/code&gt;&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums = [1,2,3,4,5,6];
const doubles = [];
for(let i = 0; i &amp;lt; nums.length; i++){
  doubles.push(nums[i] * 2);
}
// nums.forEach(item =&amp;gt; doubles.push(item * 2)); // foreach version
console.log(doubles)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Instead, you should be using &lt;code&gt;map()&lt;/code&gt; function (which is built-in in JavaScipt)&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const nums = [1,2,3,4,5,6];
const doubles = nums.map(item =&amp;gt; item * 2);
console.log(doubles)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Why use the &lt;code&gt;map()&lt;/code&gt; instead of the above methods?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The code is almost human-readable ("map this array to that with function x"), easy to understand&lt;/li&gt;
&lt;li&gt;It's minimalistic, uses fewer keystrokes, hence fewer potential bugs&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use &lt;code&gt;reduce()&lt;/code&gt; instead of for loop for additive calculations
&lt;/h3&gt;

&lt;p&gt;If you want to calculate something which depends on every value of array, use &lt;code&gt;reduce()&lt;/code&gt; function. e.g. you need to add up every element of an array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for/forEach&lt;/code&gt; method:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3, 4];
let sum = 0;
for(let i=0; i&amp;lt; array1.length; i++){
  sum+=array1[i];
}
// array1.forEach(item =&amp;gt; sum+= item); //forEach version

// 1 + 2 + 3 + 4
console.log(sum);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3, 4];
const sum = array1.reduce((sum, current) =&amp;gt; sum+current, 0);

// 1 + 2 + 3 + 4
console.log(sum);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Why use the &lt;code&gt;reduce()&lt;/code&gt; instead of the above methods?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;less boilerplater than for loop&lt;/li&gt;
&lt;li&gt;A common construct, much cleaner to read&lt;/li&gt;
&lt;li&gt;can be chained with other array functions like map: &lt;code&gt;array1.map(i =&amp;gt; i*2).reduce((sum, current) =&amp;gt; sum+current)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  use filter() method for array filter operations:
&lt;/h3&gt;

&lt;p&gt;Filtering array with for loop for even numbers:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3, 4];

const evenArray1 = [];
for(let i = 0; i&amp;lt;array1.length; i++){
  if(array1[i] % 2 === 0){
    evenArray1.push(array1[i]); //copy only even elements
  }
}
console.log(evenArray1);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  User &lt;code&gt;filter()&lt;/code&gt; method instead:
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3, 4];

const evenArray1 = array1.filter(item =&amp;gt; item % 2 === 0);
console.log(evenArray1);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Use &lt;code&gt;every()&lt;/code&gt; and &lt;code&gt;some()&lt;/code&gt; insted of manual search with for loops
&lt;/h3&gt;

&lt;p&gt;Checking if all items in an array satisfy certain criteria (even)&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [2,4,8];

let isAllEven = true;
for(let i = 0; i&amp;lt;array1.length; i++){
  if(array1[i] % 2 !== 0){
    isAllEven = false;
    break;
  }
}
console.log(isAllEven);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;every()&lt;/code&gt; for the same:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [2,4,8, 3];

let isAllEven = array1.every(item =&amp;gt; item % 2 === 0)
console.log(isAllEven);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Checking if at least one item in an array satisfy certain criteria (even)&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 3];

let isAtleastOneEven = false;
for(let i = 0; i&amp;lt;array1.length; i++){
  if(array1[i] % 2 === 0){
    isAtleastOneEven = true;
    break;
  }
}
console.log(isAtleastOneEven);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;some()&lt;/code&gt; for the same:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array1 = [1, 2, 3];

let isAtleastOneEven =  array1.some(item =&amp;gt; item % 2 ===0)
console.log(isAtleastOneEven);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Use Partial functions to create new functions from existing functions using &lt;code&gt;bind()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;You can derive new functions from existing functions. E.g. you have a power function that calculates power of number. &lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const power = (p, num) =&amp;gt; num ** p;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;you can create a new function &lt;code&gt;square()&lt;/code&gt; and &lt;code&gt;cube()&lt;/code&gt; that uses existing function &lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const square = power.bind(null, 2);
const cube = power.bind(null, 3);

console.log(square(5))  // 25
console.log(cube(5))  // 125
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note on performance:&lt;/strong&gt; Note that for loops are always faster than &lt;code&gt;map()&lt;/code&gt; and other similar functions. For a small number of items, say up to a few hundred - both will have similar performance but for a large number of items, you may want to consider for loops.&lt;/p&gt;

&lt;p&gt;In the next article, we will discuss some of the advanced functional programming concepts. Thanks!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
    </item>
    <item>
      <title>[Done Quick] Easy Lazy loading with Angular 8</title>
      <dc:creator>Nitin Jadhav</dc:creator>
      <pubDate>Sun, 02 Jun 2019 10:40:44 +0000</pubDate>
      <link>https://forem.com/nitinja/done-quick-easy-lazy-loading-with-angular-8-256e</link>
      <guid>https://forem.com/nitinja/done-quick-easy-lazy-loading-with-angular-8-256e</guid>
      <description>&lt;p&gt;Here are the steps to quickly add lazy loaded modules (loads only after the user navigates to a specific feature or page):&lt;/p&gt;

&lt;p&gt;Note that your app must be arranged as feature modules instead of one big AppModule. Make sure that you have installed latest angular cli with the command &lt;code&gt;npm install -g @angular/cli&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;step 1&lt;/em&gt;: create a Demo App&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng new MyApp --routing&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;step 2&lt;/em&gt;: Add a feature module and component inside the new module which needs to be lazily loaded&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ng g module feature --routing&lt;/code&gt;&lt;br&gt;
&lt;code&gt;ng g component MyFeature --module=feature&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;step 3&lt;/em&gt;: In your &lt;code&gt;app.component.html&lt;/code&gt; add a link to the lazy module&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;a routerLink="/feature"&amp;gt;Lazy feature&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;br&gt;
note that this file also has &lt;code&gt;&amp;lt;router-outlet&amp;gt;&amp;lt;/router-outlet&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;step 4&lt;/em&gt;: Add Lazy loading to the main app router file (&lt;code&gt;app-routing.module.ts&lt;/code&gt;). Notice the import statement - this is where the magic happens!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: 'feature',
    loadChildren: () =&amp;gt;
      import('./feature/feature.module').then(module =&amp;gt; module.FeatureModule)
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;step 5&lt;/em&gt;: Finally, add lazy routing to feature module routing file (&lt;code&gt;feature-routing.module.ts&lt;/code&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MyFeatureComponent } from '../my-feature/my-feature.component';

const routes: Routes = [{
  path: '',
  component: MyFeatureComponent
}];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class FeatureRoutingModule { }

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



&lt;p&gt;That's it! Run the project with &lt;code&gt;ng serve&lt;/code&gt; and click the link in your new app. See the browser console for the lazy loaded module.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WLy4SAxa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/epjztun291kukpj20zdz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WLy4SAxa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/epjztun291kukpj20zdz.gif" alt="Devtools show lazy angular module loading"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
