Skip to content

Commit

Permalink
add function to toggle dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
HectorVilas committed Jan 14, 2023
1 parent 9acc6e7 commit 88a729c
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/modules/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export const ui = (() => {
//create example projects if there's no localStorage
todoHandler.loadSample();

//set dark mode if left active
loadDarkMode();

const menuBtn = document.createElement("div");
menuBtn.id = "menu-button";
menuBtn.addEventListener("click", toggleMenu);
Expand Down Expand Up @@ -92,6 +95,8 @@ export const ui = (() => {
const darkModeCheckbox = document.createElement("input");
darkModeCheckbox.type = "checkbox";
darkModeCheckbox.id = "dark-mode";
darkModeCheckbox.checked = todoHandler.isOnDarkMode();
darkModeCheckbox.addEventListener("change", toggleDarkMode);
const darkModeLabel = document.createElement("label");
darkModeLabel.innerText = "Dark mode";
darkModeLabel.htmlFor = darkModeCheckbox.id;
Expand Down Expand Up @@ -917,5 +922,20 @@ export const ui = (() => {
percentageDiv.style.opacity = thisProject.checks.length === 0 ? "0%" : "100%";
}

function loadDarkMode(){
if(todoHandler.isOnDarkMode()) {
const root = document.querySelector(":root");
root.classList.add("dark");
};
}

function toggleDarkMode(){
const root = document.querySelector(":root");

this.checked ? root.classList.add("dark")
: root.classList.remove("dark");
todoHandler.darkMode(this.checked);
}

return { placeCards, loadMenu };
})();

0 comments on commit 88a729c

Please sign in to comment.