<?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: Robert</title>
    <description>The latest articles on DEV Community by Robert (@gatti).</description>
    <link>https://dev.to/gatti</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%2F895241%2F1c21da51-9329-4859-9120-71d06b6a7a90.jpeg</url>
      <title>DEV Community: Robert</title>
      <link>https://dev.to/gatti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gatti"/>
    <language>en</language>
    <item>
      <title>ReactJS - Dark mode with Tailwind</title>
      <dc:creator>Robert</dc:creator>
      <pubDate>Thu, 15 Sep 2022 15:47:26 +0000</pubDate>
      <link>https://dev.to/gatti/reactjs-dark-mode-with-tailwind-296</link>
      <guid>https://dev.to/gatti/reactjs-dark-mode-with-tailwind-296</guid>
      <description>&lt;p&gt;Nowadays most of the websites/apps has built in dark mode and its becoming one of the most important implementations in websites.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But how can you implement it on our website?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;First step:&lt;/strong&gt; If you already have your create-react-app installed you now need install tailwind in your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Second step:&lt;/strong&gt; This step is more optional, but for simplicity of this tutorial I recommend installing DarkModeSwitch React library. This library already have build in component with cool animations and styling and its really easy to customize.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react-toggle-dark-mode
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.npmjs.com/package/react-toggle-dark-mode" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--r7XxGAJ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.npmjs.com/338e4905a2684ca96e08c7780fc68412.png" height="462" class="m-0" width="880"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.npmjs.com/package/react-toggle-dark-mode" rel="noopener noreferrer" class="c-link"&gt;
          react-toggle-dark-mode - npm
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Animated dark mode toggle as seen in blogs!. Latest version: 1.1.0, last published: 3 months ago. Start using react-toggle-dark-mode in your project by running `npm i react-toggle-dark-mode`. There are 2 other projects in the npm registry using react-toggle-dark-mode.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--h1do8fu7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://static.npmjs.com/b0f1a8318363185cc2ea6a40ac23eeb2.png" width="32" height="32"&gt;
        npmjs.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Now lets create useDarkMode.js hook&lt;/strong&gt;&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, useState} from "react"

export default function useDarkMode() {

    const [isDarkMode, setDarkMode] = useState(localStorage.theme)
    const colorTheme = isDarkMode === "dark" ? "light" : "dark"

    useEffect(()=&amp;gt; {
        document.documentElement.classList.remove(colorTheme)
        document.documentElement.classList.add(isDarkMode)

        localStorage.setItem("theme", isDarkMode)
    }, [isDarkMode, colorTheme])

    return [colorTheme, setDarkMode]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;So what exactly does it do?&lt;/strong&gt;&lt;br&gt;
Basically, first state will have value that is inside our localstorage, so everytime we leave and comeback to page, we will have our prefered theme on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;colorTheme&lt;/strong&gt; will contain value upon state of our isDarkMode state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useEffect()&lt;/strong&gt; hook allow us to run the code inside only when [colorTheme or setDarkMode change].&lt;/p&gt;

&lt;p&gt;Now &lt;strong&gt;inside our useEffect&lt;/strong&gt; hook we define color theme of our website. First line will remove class named upon our colorTheme state, if you remember this state will always contain opposite value of current theme (dark = light). So if we switch to dark theme, light theme will get removed and next line will add class from isDarkMode state, which is our "true" theme.&lt;/p&gt;

&lt;p&gt;Then we just set our &lt;strong&gt;localstorage&lt;/strong&gt; value.&lt;/p&gt;

&lt;p&gt;At last we &lt;strong&gt;return [colorTheme, setDarkMode]&lt;/strong&gt; so we can use the hook in different files. &lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Now lets make component DarkModeToggle.jsx&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This component will contain our darkmode toggle.&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 { DarkModeSwitch } from "react-toggle-dark-mode";
import useDarkMode from '../hooks/useDarkMode';

function DarkModeToggle() {

    const [colorTheme, setTheme] = useDarkMode();
    const [isDarkMode, setDarkMode] = useState( colorTheme === "light" ? true : false);

    const toggleDarkMode = (checked) =&amp;gt; {
        setTheme(colorTheme)
        setDarkMode(checked);
    };


  return (
    &amp;lt;div&amp;gt;
        &amp;lt;DarkModeSwitch
        onChange={toggleDarkMode} 
        checked={isDarkMode}
        size={25}
        /&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

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

&lt;/div&gt;



&lt;p&gt;We need to &lt;strong&gt;initialize colorTheme&lt;/strong&gt; state that is inside our useDarkMode() hook, then state that will switch our theme.&lt;/p&gt;

&lt;p&gt;Next &lt;strong&gt;create function&lt;/strong&gt; that will receive variable checked value from our  component (&lt;a href="https://www.npmjs.com/package/react-toggle-dark-mode"&gt;https://www.npmjs.com/package/react-toggle-dark-mode&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Now lets put our function inside  as &lt;strong&gt;onChange value&lt;/strong&gt;. &lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Now how we can implement Tailwind into this?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Its simple! All you need is to set up class name as for example &lt;strong&gt;"dark:bg-gray-900"&lt;/strong&gt;. Everytime you put &lt;strong&gt;"dark:&lt;/strong&gt;" before class, this class will "activate" only if theme is set to dark.&lt;/p&gt;

&lt;p&gt;Read more about it in Tailwind documentation: &lt;a href="https://tailwindcss.com/docs/dark-mode"&gt;https://tailwindcss.com/docs/dark-mode&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Crypto App built with ReactJS, Tailwind and Firebase</title>
      <dc:creator>Robert</dc:creator>
      <pubDate>Thu, 15 Sep 2022 14:39:38 +0000</pubDate>
      <link>https://dev.to/gatti/crypto-app-built-with-reactjs-tailwind-and-firebase-491j</link>
      <guid>https://dev.to/gatti/crypto-app-built-with-reactjs-tailwind-and-firebase-491j</guid>
      <description>&lt;p&gt;So today I finished my solo project &lt;strong&gt;Crypto app&lt;/strong&gt;! &lt;/p&gt;

&lt;p&gt;Here is &lt;strong&gt;live website&lt;/strong&gt; deployed on firebase: &lt;a href="https://coinbase-52f8f.web.app"&gt;https://coinbase-52f8f.web.app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Github:&lt;/strong&gt; &lt;a href="https://github.com/robo311/crypto-app"&gt;https://github.com/robo311/crypto-app&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;What functionality does it offer?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functional Register/Login page using Firebase database.&lt;/li&gt;
&lt;li&gt;Website shows most popular crypto currencies and trending currencies via Coingecko API.&lt;/li&gt;
&lt;li&gt;As mentioned above website using Coingecko API.&lt;/li&gt;
&lt;li&gt;User can select certain coins and add them to their watchlist.&lt;/li&gt;
&lt;li&gt;Each coin has designated page with informations/prices/changes.&lt;/li&gt;
&lt;li&gt;Dark Mode / Light Mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Technologies used&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ReactJS&lt;/li&gt;
&lt;li&gt;Tailwind&lt;/li&gt;
&lt;li&gt;Firebase/Firestore&lt;/li&gt;
&lt;li&gt;Axios (for API)&lt;/li&gt;
&lt;li&gt;HTML&lt;/li&gt;
&lt;li&gt;CSS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Author&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/robo311"&gt;https://github.com/robo311&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Discord: &lt;a class="mentioned-user" href="https://dev.to/gatti"&gt;@gatti&lt;/a&gt;#7613&lt;/li&gt;
&lt;li&gt;Dev.to: &lt;div class="ltag__user ltag__user__id__895241"&gt;
    &lt;a href="/gatti" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v3D-_49h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--1s0dqVmL--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/895241/1c21da51-9329-4859-9120-71d06b6a7a90.jpeg" alt="gatti image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/gatti"&gt;Robert&lt;/a&gt;Follow
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/gatti"&gt;Hi, I'm Robert or Gatti and I'm a starting front-end developer.&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Screenshot of website&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MIOJwILm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j983mtnes72fv3otwnts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MIOJwILm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j983mtnes72fv3otwnts.png" alt="Image description" width="880" height="1432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional info&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Website is fully responsive (using Tailwind).&lt;/li&gt;
&lt;li&gt;Mobile website using hamburger menu. (Fully working)&lt;/li&gt;
&lt;/ul&gt;

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