Skip to content

Commit

Permalink
Persist dark mode (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
PushpenderSaini0 authored Oct 18, 2024
1 parent 5354827 commit 792a433
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// @ts-check

import { showDebugTab } from "./utils/debug.js";
import { persistDarkMode } from "./utils/dark_mode.js";

// Show the debug tab if the debug mode is enabled.
showDebugTab();

// Persist dark mode
persistDarkMode();
73 changes: 73 additions & 0 deletions app/utils/dark_mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// @ts-check

/**
* @type {string}
*/
const DARK_MODE_KEY = "DARK_MODE_PREFERENCE";

// Wrappin in a function to keep the check box and local storage in sync
function setDarkMode() {
localStorage.setItem(DARK_MODE_KEY, "dark");
document.body.classList.add("dark");
}

function setLightMode() {
localStorage.setItem(DARK_MODE_KEY, "light");
document.body.classList.remove("dark");
}

/**
* @description
* Gets the dark mode input switch html element
* @returns {HTMLInputElement | null}
*/
function getDarkModeSwitch() {
return document.querySelector(".dark-mode-switch input");
}

/**
* @desription Check if user has dark mode preference
* We use localStorage to store user's dark mode preference
* If the localStorage item is not we will set the default to user's system preference
* @returns {boolean} - true if user has dark mode preference
*/
function checkDarkMode() {
const darkModePreference = localStorage.getItem(DARK_MODE_KEY);
if (!darkModePreference) {
// as the dark mode key is not set, we will set it to user's system preference
// return true if user's system preference is dark
return (
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
);
}
return darkModePreference === "dark";
}

/**
* @description
* Switch dark mode on or off
*/
function switchDarkMode() {
const darkModeSwitch = getDarkModeSwitch();
darkModeSwitch?.checked ? setDarkMode() : setLightMode();
}

/**
* @description
* If user has dark mode preference, set dark mode
* If user has light mode preference, set light mode
* If localStorage item is not set, set user's system preference
*/
function persistDarkMode() {
const darkModeSwitch = getDarkModeSwitch();
if (darkModeSwitch) {
darkModeSwitch.checked = checkDarkMode();
}
checkDarkMode() ? setDarkMode() : setLightMode();
}

/** event listener for dark mode switch */
getDarkModeSwitch()?.addEventListener("change", switchDarkMode);

export { checkDarkMode, persistDarkMode };
3 changes: 1 addition & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<body>
<div class="dark-mode">
<label class="dark-mode-switch">
<input type="checkbox" onclick="switchDarkMode()">
<input type="checkbox">
<span class="slider"></span>
</label>
</div>
Expand Down Expand Up @@ -203,7 +203,6 @@ <h2>Debug Informations</h2>
<h1> Move Timer: </h1>
<progress value="0" max="2.5" id="timer-bar"></progress>
</div>
<script>document.querySelector('.dark-mode-switch input').checked = false;</script>
<script src="js/utils.js"></script>
<script src="js/arcade.js"></script>
<script src="js/piece_object.js"></script>
Expand Down
10 changes: 0 additions & 10 deletions js/dark_mode.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
function switchDarkMode() {
darkMode = document.querySelector('.dark-mode-switch input').checked;
if (darkMode) {
document.body.classList.add("dark");
} else {
document.body.classList.remove("dark");
}
}


function projectMovement() {
!PH.getStatus() ? PH.enable() : PH.disable();
}

0 comments on commit 792a433

Please sign in to comment.