<?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: Nikhil Singh</title>
    <description>The latest articles on DEV Community by Nikhil Singh (@thenickrj).</description>
    <link>https://dev.to/thenickrj</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%2F714112%2Fbf6011a0-6394-4954-9181-ec6e3f385ec3.jpeg</url>
      <title>DEV Community: Nikhil Singh</title>
      <link>https://dev.to/thenickrj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thenickrj"/>
    <language>en</language>
    <item>
      <title>Dark Mode: Conditional Styling using styled-components</title>
      <dc:creator>Nikhil Singh</dc:creator>
      <pubDate>Mon, 27 Sep 2021 10:29:54 +0000</pubDate>
      <link>https://dev.to/thenickrj/dark-mode-conditional-styling-using-styled-components-4dm3</link>
      <guid>https://dev.to/thenickrj/dark-mode-conditional-styling-using-styled-components-4dm3</guid>
      <description>&lt;p&gt;The styled-components allows you to write actual CSS code to style your components. It also removes the mapping between components and styles – using components as a low-level styling construct could not be easier. Here the official docs of &lt;a href="https://styled-components.com/docs" rel="noopener noreferrer"&gt;styled-components&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting Started
&lt;/h1&gt;

&lt;p&gt;For the prerequisite part, you need to have &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Node&lt;/a&gt; already installed on your machine.&lt;/p&gt;

&lt;p&gt;Next let's create a react app and install styled-components too and then start the application.&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 react-app
cd react-app
npm install styled-components
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Let's Code
&lt;/h1&gt;

&lt;p&gt;We need to import styled from "styled-components", and we need to create a Container component that will render an div tag. The Container component will act as a wrapper component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-components";

const Container = styled.div`
//CSS to be added here
`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To add the button to switch between the dark mode we will using a toggle button created using a check box.&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;Container&amp;gt;
      &amp;lt;div className="contain"&amp;gt;
        &amp;lt;h5&amp;gt;Dark Mode&amp;lt;/h5&amp;gt;
        &amp;lt;label class="switch"&amp;gt;
        &amp;lt;input type="checkbox" onChange={(e) =&amp;gt; setDark(!dark)} /&amp;gt;
        &amp;lt;span class="slider round"&amp;gt;&amp;lt;/span&amp;gt;
        &amp;lt;/label&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/Container&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CSS for the toggle button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.contain {
    margin: auto;
    position: relative;
    top: 40%;
  }

  h5 {
    margin: 0;
  }
  .switch input {
    opacity: 0;
    width: 0;
    height: 0;
  }

  .slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: black;
    -webkit-transition: 0.4s;
    transition: 0.4s;
  }

  .slider:before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    left: 2px;
    bottom: 1px;
    background-color: white;
    -webkit-transition: 0.4s;
    transition: 0.4s;
  }

  input:checked + .slider {
    background-color: white;
  }

  input:focus + .slider {
    box-shadow: 0 0 1px white;
  }

  input:checked + .slider:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(12px);
    background-color: black;
  }

  .slider.round {
    border-radius: 34px;
  }

  .slider.round:before {
    border-radius: 50%;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this is how the toggle button would look like,&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8zxnxxl7z2j2ywe15e42.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8zxnxxl7z2j2ywe15e42.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's add the functionality, we will be using useState Hook,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [dark, setDark] = useState(false);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default the toggle should be off and when toggled the dark value should be true, so let's add onChange property on the input tag,&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;input type="checkbox" onChange={(e) =&amp;gt; setDark(!dark)} /&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Next we need to pass the dark value to the Container component so that we can change the background color accordingly&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;Container dark={dark}&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After passing dark value we can change the background conditionally&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Container = styled.div`
  background: ${({ dark }) =&amp;gt; (dark ? "black" : "white")}; //Conditional styling the background color
  height: 100vh;
  min-height: fit-content;
  .switch {
    position: relative;
    display: inline-block;
    width: 32px;
    height: 16px;
    border-radius: 50%;
    border: 3px solid black;
  }

  h5 {
    margin: 0;
    color: ${({ dark }) =&amp;gt; (!dark ? "black" : "white")}; // To change the color of the text opposite to the background color
  }

//Remaining CSS remains the same
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The entire code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import styled from "styled-components";

const Container = styled.div`
  background: ${({ dark }) =&amp;gt; (dark ? "black" : "white")};
  height: 100vh;
  min-height: fit-content;
  .switch {
    position: relative;
    display: inline-block;
    width: 32px;
    height: 16px;
    border-radius: 50%;
    border: 3px solid black;
  }

  .contain {
    margin: auto;
    position: relative;
    top: 40%;
  }

  h5 {
    margin: 0;
    color: ${({ dark }) =&amp;gt; (!dark ? "black" : "white")};
  }
  .switch input {
    opacity: 0;
    width: 0;
    height: 0;
  }

  .slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: black;
    -webkit-transition: 0.4s;
    transition: 0.4s;
  }

  .slider:before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    left: 2px;
    bottom: 1px;
    background-color: white;
    -webkit-transition: 0.4s;
    transition: 0.4s;
  }

  input:checked + .slider {
    background-color: white;
  }

  input:focus + .slider {
    box-shadow: 0 0 1px white;
  }

  input:checked + .slider:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(12px);
    background-color: black;
  }

  .slider.round {
    border-radius: 34px;
  }

  .slider.round:before {
    border-radius: 50%;
  }
`;

function App() {
  const [dark, setDark] = useState(false);
  return (
    &amp;lt;Container dark={dark}&amp;gt;
      &amp;lt;div className="contain"&amp;gt;
        &amp;lt;h5&amp;gt;Dark Mode&amp;lt;/h5&amp;gt;
        &amp;lt;label class="switch"&amp;gt;
          &amp;lt;input type="checkbox" onChange={(e) =&amp;gt; setDark(!dark)} /&amp;gt;
          &amp;lt;span class="slider round"&amp;gt;&amp;lt;/span&amp;gt;
        &amp;lt;/label&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/Container&amp;gt;
  );
}

export default App;

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

&lt;/div&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fndkanzamdcc0630npu8u.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fndkanzamdcc0630npu8u.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is my first blog on Dev.to.&lt;br&gt;
Hope it was helpful and easy to implement.&lt;br&gt;
Will be looking forward for feedback on this, also check out my Github &lt;a href="https://github.com/thenickrj" rel="noopener noreferrer"&gt;thenickrj&lt;/a&gt;&lt;/p&gt;

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