<?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: Moniet Sawhney</title>
    <description>The latest articles on DEV Community by Moniet Sawhney (@moniet).</description>
    <link>https://dev.to/moniet</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%2F178730%2Fd1493ad1-12ad-4d91-a638-38793d6258ea.jpg</url>
      <title>DEV Community: Moniet Sawhney</title>
      <link>https://dev.to/moniet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/moniet"/>
    <language>en</language>
    <item>
      <title>JS Clean Code — Learning on the job</title>
      <dc:creator>Moniet Sawhney</dc:creator>
      <pubDate>Sun, 11 Apr 2021 14:09:05 +0000</pubDate>
      <link>https://dev.to/moniet/js-clean-code-learning-on-the-job-4me2</link>
      <guid>https://dev.to/moniet/js-clean-code-learning-on-the-job-4me2</guid>
      <description>&lt;p&gt;In this post, I've compiled a few tidbits that have helped make my React / JavaScript projects that tiny bit more readable.&lt;/p&gt;

&lt;p&gt;Looking forward to discuss, iterate, and improve !&lt;/p&gt;

&lt;h1&gt;
  
  
  prerequisites
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;optional chaining&lt;/li&gt;
&lt;li&gt;short circuiting&lt;/li&gt;
&lt;li&gt;higher order functions&lt;/li&gt;
&lt;li&gt;es6&lt;/li&gt;
&lt;li&gt;react.js&lt;/li&gt;
&lt;li&gt;styled-components || emotion&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  short circuiting to keep things clean
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// assume 'data' here is an array of objects&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapValues&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
  &lt;span class="nx"&gt;data&lt;/span&gt;
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="c1"&gt;// --------------&lt;/span&gt;
&lt;span class="c1"&gt;// cleaning up ✨&lt;/span&gt;
&lt;span class="c1"&gt;// --------------&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapValuesCleanedUp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[]).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The advantage with this pattern is that it can avoid nesting the ternary operators which can be hard to read. &lt;/p&gt;

&lt;p&gt;If we need to pass another set of data we can do so with ease. For example : &lt;code&gt;(data || otherData || []).map(...)&lt;/code&gt;. This same change with the ternary operator would result in nesting. For example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nestedTernary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nx"&gt;data&lt;/span&gt; 
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;otherData&lt;/span&gt; 
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;otherData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(...)&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;Note :&lt;/strong&gt; Short circuiting can be a cause for some pretty counterintuitive bugs. I would recommend going through &lt;a href="https://blog.bitsrc.io/the-ultimate-guide-to-the-es2020-nullish-coalescing-operator-231d2b64dfde"&gt;this&lt;/a&gt; article to learn more about it (and a little something something about ES2020).&lt;/p&gt;

&lt;h1&gt;
  
  
  arrays to keep things DRY
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isValidZipCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;

&lt;span class="c1"&gt;// --------------&lt;/span&gt;
&lt;span class="c1"&gt;// cleaning up ✨&lt;/span&gt;
&lt;span class="c1"&gt;// --------------&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isValidZipCodeCleanedUp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this trick we don't have to repeat the &lt;code&gt;value.length&lt;/code&gt; bit, and if the requirements grow beyond just &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;10&lt;/code&gt; the array can be extended like so: &lt;code&gt;[5,10,6].includes(value.length)&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  the good ol' IIFE for consistency
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// formatting variable data&lt;/span&gt;
&lt;span class="c1"&gt;// in this ex : the 'data' object&lt;/span&gt;
&lt;span class="c1"&gt;// contains one property 'cities'&lt;/span&gt;
&lt;span class="c1"&gt;// and two properties for getting states, i.e.&lt;/span&gt;
&lt;span class="c1"&gt;// 'statesData' and 'allStates'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formatLocaleData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;statesData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;allStates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getFormattedStates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;--&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;statesData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;statesData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapOneWay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;allStates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;allStates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapAnotherWay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;cities&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- &lt;/span&gt;
        &lt;span class="na"&gt;states&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;getFormattedStates&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- &lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// --------------&lt;/span&gt;
&lt;span class="c1"&gt;// cleaning up ✨&lt;/span&gt;
&lt;span class="c1"&gt;// --------------&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formatLocaleDataCleanedUp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;statesData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;allStates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;states&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;--&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;statesData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;statesData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapOneWay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;allStates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;allStates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapAnotherWay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;})()&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;cities&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;-- &lt;/span&gt;
        &lt;span class="nx"&gt;states&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;--&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;The entire idea for me in this example was to keep the &lt;code&gt;return&lt;/code&gt; object consistent by getting rid of the &lt;code&gt;getFormattedStates()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;allStates&lt;/code&gt; and &lt;code&gt;statesData&lt;/code&gt; are mapped separately (for the same result) so that my react component can consume it.&lt;/p&gt;

&lt;p&gt;After cleaning it up, the IIFE maps data and assigns it to the &lt;code&gt;states&lt;/code&gt; variable in one go.&lt;/p&gt;

&lt;h1&gt;
  
  
  destructuring props for easier fallbacks
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;summary&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// --------------&lt;/span&gt;
&lt;span class="c1"&gt;// cleaning up ✨&lt;/span&gt;
&lt;span class="c1"&gt;// --------------&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComponentCleanedUp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;summary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;summary&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;I was often using the short circuiting pattern like :  &lt;code&gt;{data.header || "fallback"}&lt;/code&gt; in my react components.&lt;/p&gt;

&lt;p&gt;However, if you need to access numerous properties it really helps to have all the fallback values in one place rather than multiple &lt;code&gt;this || that&lt;/code&gt; conditions in different places. &lt;/p&gt;

&lt;h1&gt;
  
  
  better theming in styled-components
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;themeGet&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@styled-system/theme-get&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;styled-components&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Card&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
    ...

    border-width: &lt;/span&gt;&lt;span class="p"&gt;${({&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
        &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;borderWidths&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;small&lt;/span&gt; 
            &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;borderWidths&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;small&lt;/span&gt; 
            &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;

    ...
`&lt;/span&gt;

&lt;span class="c1"&gt;// --------------&lt;/span&gt;
&lt;span class="c1"&gt;// cleaning up ✨&lt;/span&gt;
&lt;span class="c1"&gt;// --------------&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CardCleanedUp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
    ...

    border-width: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;themeGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;borderWidths.small&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;

    ...
`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the &lt;code&gt;themeGet&lt;/code&gt; utility makes things super concise. It was developed as part of the &lt;code&gt;styled-system&lt;/code&gt; library, but it can be installed and used individually.&lt;/p&gt;

&lt;p&gt;It's super simple to use. The &lt;code&gt;themeGet&lt;/code&gt; function's first argument is the property you want to access and the second arguement is the fallaback .&lt;/p&gt;

&lt;p&gt;This is by no means only limited to &lt;code&gt;styled-components&lt;/code&gt; you can also use it with other CSS-in-JS solutions like &lt;code&gt;emotion&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  sane styled components with the CSS prop
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DivRelative&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
    position: relative;
`&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DivRelative&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        "..."
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;DivRelative&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// -------------&lt;/span&gt;
&lt;span class="c1"&gt;// cleaning up ✨&lt;/span&gt;
&lt;span class="c1"&gt;// -------------&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComponentCleanedUp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; 
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"position: relative;"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
     "..."
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On large projects, I found that I often had to create styled components with super random names that had only one line of css (like &lt;code&gt;&amp;lt;DivRelative /&amp;gt;&lt;/code&gt;) above. &lt;/p&gt;

&lt;p&gt;I'd waste precious time thinking of a component name that made an inkling of sense.&lt;/p&gt;

&lt;p&gt;The css prop was the definitive answer to this problem !&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note :&lt;/strong&gt; To use the css prop you need include the &lt;a href="https://styled-components.com/docs/tooling#babel-plugin"&gt;babel-styled-components&lt;/a&gt; plugin in your babel configuration.&lt;/p&gt;

&lt;h1&gt;
  
  
  conclusion
&lt;/h1&gt;

&lt;p&gt;Above all else, I think one of the most important things is to keep things as consistent as possible. It's easy to predict how things work when there is consistency (especially in monolithic setups).  &lt;/p&gt;

&lt;p&gt;When adopting new patterns it's imperative that everyone on your team is onboard with the idea and that it actually contributes improved readability. That goes for adopting the above snippets too.&lt;/p&gt;

&lt;p&gt;I've often found myself reaching for patterns because it made me feel like a boss but didn't really contribute anything substantial to readability. I once named a component &lt;code&gt;&amp;lt;LobotomizedOwl /&amp;gt;&lt;/code&gt; for example 🤦‍♂️. But I did change it to better name like &lt;code&gt;&amp;lt;MarginProvider /&amp;gt;&lt;/code&gt;. Learn from my mistakes and don't do this !&lt;/p&gt;

&lt;p&gt;Which brings me to another topic, naming things.&lt;/p&gt;

&lt;p&gt;Naming things is also important in projects. It can cause a lot of confusion when things are not named properly.&lt;/p&gt;

&lt;p&gt;For example, it's okay to have longer variable / function names if it clarifies the context for future devs. In this light, something like : &lt;code&gt;getHumanReadableDatesForSelectElement()&lt;/code&gt; is totally fine. Maybe in this hypothetical project there's 10 different requirements for dates. In this case descriptive variable names could reduce the WTFs per minute.&lt;/p&gt;

&lt;p&gt;And of course, it's worth mentioning that comments can and should be used to clarify things. Sometimes code is forced to be complex and in those cases comments can help everyone. Especially junior devs. &lt;/p&gt;

&lt;p&gt;Here's a &lt;a href="https://www.youtube.com/watch?v=yhF7OmuIILc"&gt;great talk&lt;/a&gt; by Sara Drasner on 'The Art of Commenting'.&lt;/p&gt;

&lt;p&gt;As much as I'm about 'aesthetically pleasing' code, which longer variable names might not be. I'm also inspired by a common saying in the design community :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;"Form follows function"&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




</description>
    </item>
    <item>
      <title>Kurzgesagt Earth - A CSS Recreation</title>
      <dc:creator>Moniet Sawhney</dc:creator>
      <pubDate>Wed, 12 Jun 2019 13:40:02 +0000</pubDate>
      <link>https://dev.to/moniet/kurzgesagt-earth-a-css-recreation-23b3</link>
      <guid>https://dev.to/moniet/kurzgesagt-earth-a-css-recreation-23b3</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FYk7yUOA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/c9lnwdby3z7660hc4njw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FYk7yUOA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/c9lnwdby3z7660hc4njw.png" alt="kurgezagt earth illustration"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The idea behind this project was to recreate an illustration found in YouTube channel Kurzgesagt's videos (as seen in the image above); limiting my self to using HTML and CSS.&lt;/p&gt;

&lt;p&gt;Through creating this illustration, my aim was to learn and utilize various techniques of layouts and responsive design. In addition, I wanted to practice writing maintainable sass code using the BEM methodology. &lt;/p&gt;

&lt;p&gt;Lastly, by documenting the process of creating the illustration I hope to share my knowledge on layouts and expose a few quirks of CSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Steps
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Using the drop tool
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j6hZi22y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0muk5kfcdsldlo12toxr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j6hZi22y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0muk5kfcdsldlo12toxr.png" alt="mozilla_drop_tool"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I used the &lt;strong&gt;&lt;em&gt;colour picker tool&lt;/em&gt;&lt;/strong&gt; in Firefox to grab the colour palette from Kurzgesagt's video. In Firefox, the colour tool automatically copies the HEX value to the clipboard. In this manner, I was able to create a palette with variable in SCSS like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Palette
$earth-blue: #208bd2;
$trees: #45cb69;
$earth-outline-blue: #0a1436;
$earth-aura: #10329b;
$clouds: #ffffff;
$bg: #03081b; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the BEM methodology, I find the it is important to have descriptive names not only for html tags, but also for variables, mixins, and extends. &lt;/p&gt;

&lt;p&gt;In this manner, it will be easier to recall the code by memory which ultimately allows for quicker changes. &lt;/p&gt;

&lt;h2&gt;
  
  
  Initial Structure
&lt;/h2&gt;

&lt;p&gt;When starting, I find it important to define the structure starting with the 'blocks' or the main chuncks of html that define the blueprint for the layout. Looking at the illustration, I defined a base structure based on the following blocks: earth, clouds, trees, and earth's aura.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// base structure
&amp;lt;div class="wrapper"&amp;gt;

    &amp;lt;div class="earth"&amp;gt;
      &amp;lt;div class="earth_shadow_container"&amp;gt;
        &amp;lt;div class="earth_shadow"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;

      &amp;lt;div class="clouds"&amp;gt;
        &amp;lt;div class="lines_1"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="lines_2"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;

      &amp;lt;div class="trees"&amp;gt;
        &amp;lt;div class="line_1"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class="line_2"&amp;gt;&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;div class="earth_aura"&amp;gt;
    &amp;lt;/div&amp;gt;

    &amp;lt;div class="bg"&amp;gt;
    &amp;lt;/div&amp;gt;

  &amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Above, I use descriptive names for blocks. This is in accordance with BEM's guide for naming tags which states that you should describe the block's purpose, and not its state. &lt;/p&gt;

&lt;p&gt;Although it may be slightly abstracted for an illustration; I strongly feel it is still good practice to pay attention to small things like naming conventions, as they make the code a lot more readable. &lt;/p&gt;

&lt;p&gt;Moreover, the wrapper - which will contain all the elements - has the following CSS code to render the art in full-screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.wrapper {
  display: block;
  position: relative;
  margin: 0 auto;
  width: 100vw;
  height: 100vh;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apart from making it full-screen, the above code will allow for elements within to be easily centered relative to the users 'viewport', which is their device's screen. This is accomplished by using the vw and vh relative units in CSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making Earth
&lt;/h2&gt;

&lt;p&gt;Next, I began putting together the 'base' for the entire illustration which was the earth; its background, border, width, and height.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.earth {
  position: relative;
  width: 30vw;
  height: 30vw;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: $earth-blue;
  border: solid 1vw $earth-outline-blue;
  border-radius: 50%;
  z-index: 0;
  overflow: hidden;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, again, I use the vw unit to size the earth, this is because it will re-size based on the viewport's dimensions, so anyone can see it on their device.&lt;/p&gt;

&lt;p&gt;Most notably, the following code centers the earth, right in the middle of the screen (both vertically and horizontally):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: $earth-blue;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To keep my code &lt;em&gt;DRY&lt;/em&gt;, I took the above snippet and created an instance of &lt;a href="http://www.sass-lang.com/guide"&gt;@extends&lt;/a&gt; that would allow me to re-use it in other places. &lt;/p&gt;

&lt;p&gt;The extend snippet looks 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;%centerElement {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have an earth that looks like this: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O7g9iL3R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/eb69v0vhfy2ctj8aqpva.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O7g9iL3R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/eb69v0vhfy2ctj8aqpva.png" alt="earth illustration initial"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Aura
&lt;/h2&gt;

&lt;p&gt;Next, I focused on creating the &lt;em&gt;earth aura&lt;/em&gt;. My idea behind this was that I could create the basic background with the 'earth-aura' block, and build the other three aura rings with the :before, and :after pseudo-elements. &lt;/p&gt;

&lt;p&gt;One important takeaway from the project was how useful the &lt;code&gt;z-index&lt;/code&gt; attribute is. The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/z-index"&gt;z-index&lt;/a&gt; attribute will allow you to defined the 'layers' of the elements. In a nutshell (pun intended), allowing you to decide which element is at the top, bottom, and the middle. &lt;/p&gt;

&lt;p&gt;Completing the aura, the earth now looks like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sMubKV09--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/htvjgzk4yinimvem70ix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sMubKV09--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/htvjgzk4yinimvem70ix.png" alt="init_earth_aura"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Dark Side of the Earth
&lt;/h2&gt;

&lt;p&gt;This was a tricky bit of code to write, and there are several ways to do it, such as the clip-path. However, I stuck with using a simple div and changing it's overflow to none. The code can be a bit confusing, but it's definitely a useful technique. &lt;/p&gt;
&lt;h3&gt;
  
  
  The CSS for the earth shadow:
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.earth_shadow_container {
  position: absolute;
  left: 50%;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.earth_shadow {
  position: absolute;
  left: -50%;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.2);
  border-radius: 50%;
  z-index: 1000;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Notice the z-index is large, as it will allow the clouds and trees to be under it. &lt;/p&gt;

&lt;p&gt;It looks like this: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zAXDlLwF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/v1pp5mlgexrztbboyc6u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zAXDlLwF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/v1pp5mlgexrztbboyc6u.png" alt="initial earth with shadow"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Trees
&lt;/h2&gt;

&lt;p&gt;Similar to the earth aura, I used a combination of elements, their pseudo elements, and box-shadow, to create trees. &lt;/p&gt;

&lt;p&gt;First, I centered the 'trees' container within the earth block. Following this I coded the trees like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.line_1 {
    position: absolute;
    top: 75%;
    left: 20%;
    width: 12vw;
    height: 4vw;
    background: $trees;
    border-radius: 50px;

    &amp;amp;:after {
      content: '';
      position: absolute;
      display: block;
      top: -150%;
      left: 70%;
      width: 8vw;
      height: 3vw;
      background: $trees;
      border-radius: 50px;
    }

    &amp;amp;:before {
      content: '';
      position: absolute;
      display: block;
      top: -200%;
      left: -50%;
      width: 5vw;
      height: 2vw;
      background: $trees;
      border-radius: 50px;
    }

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

&lt;/div&gt;



&lt;p&gt;The same process was done for the clouds. Lastly, I touched up the illustration with CSS animations, and added a descriptive header. &lt;/p&gt;

&lt;h2&gt;
  
  
  The result:
&lt;/h2&gt;

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

</description>
      <category>animation</category>
      <category>css</category>
      <category>javascript</category>
      <category>sass</category>
    </item>
  </channel>
</rss>
