Giter VIP home page Giter VIP logo

light-dark-mode's Introduction

Light & Dark Mode

cover

The purpose of this project is to implement dark / light theme to a template website using plain JavaScript.




Learning Outcome


  1. Using CSS variables
  2. background: rgb(255 255 255 / 50%) instead of rgba(255, 255, 255, 0.5)
  3. Smooth scroll with html { scroll-behavior: smooth };
  4. Creating a toggle switch
  5. Setting attributes on the root element (html)
  6. Using document.documentElement
  7. Saving theme in localStorage


Process


Creating the toggle switch


index.html

<div class="theme-switch-wrapper">
  <!-- Text and icon -->
  <span id="toggle-icon">
  	<span class="toggle-text">Light Mode</span>
    <i class="fas fa-sun"></i>
  </span>
  <!-- Switcher -->
  <label class="theme-switch">
  	<input type="checkbox">
    <div class="slider round"></div>
  </label>
</div>
  • The text Light Mode has to change to Dark Mode when in dark mode.
  • The styling of the switch is done via slider and round classes.

script.js

Add event listener to the toggle switch. We use the change event.

const toggleSwitch = document.querySelector('input[type="checkbox"]');

// Switch Theme Dynamically
//...


// Event Listener
toggleSwitch.addEventListener('change', switchTheme);

We create function that dynamically changes the theme

// Switch Theme Dynamically
function switchTheme(event) {
  if (event.target.checked) {
    document.documentElement.setAttribute('data-theme', 'dark');
    localStorage.setItem('theme', 'dark');
    toggleDarkLightMode(DARK_THEME);
  } else {
    document.documentElement.setAttribute('data-theme', 'light');
    localStorage.setItem('theme', 'light');
    toggleDarkLightMode(LIGHT_THEME);
  }
}

We use property target.checked to check if the switch toggle has been checked (show dark mode) or not (light mode).

  1. We set the data-theme attribute at the highest level of the html
  2. document.documentElement returns the root element -in this case <html>

We still need to change te icons, the text of the theme and the images. We create a single function toggleDarkLightMode(mode) that given a parameter, it changes the icon, text and images according to the theme choice.


I use two constants to identify the themes: DARK_THEME and LIGHT_THEME.

There is a function imageMode(mode) that's in charge of changing the source of the images in the HTML document according to the chosen mode:

function imageMode(color) {
  image1.src = `img/undraw_proud_coder_${color}.svg`;
  image2.src = `img/undraw_feeling_proud_${color}.svg`;
  image3.src = `img/undraw_conceptual_idea_${color}.svg`;
}

LocalStorage


We use local storage to store the user theme preference so that when the user refreshes the page, the previous chosen theme remains.



light-dark-mode's People

Contributors

elemarmar avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

camillegoro

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.