Skip to content

Commit

Permalink
Update Theme to be parallel with Language
Browse files Browse the repository at this point in the history
  • Loading branch information
stanieldev committed Nov 21, 2024
1 parent 21dd9ca commit bad1a3a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 77 deletions.
6 changes: 3 additions & 3 deletions .templates/header/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
<meta name="twitter:description" content="Stanley's Website">
<meta name="twitter:image" content="header.ico">

<!-- Page Style(s) -->
<!-- Header Style -->
<link rel="stylesheet" href="/.templates/header/header.css">

<!-- Page Script(s) -->
<!-- Header Script(s) -->
<script src="/.resources/scripts/fetching.js"></script>
<script defer src="/.templates/header/header.js"></script>
<script defer src="/.templates/header/scripts/switchTheme.js"></script>
Expand Down Expand Up @@ -74,7 +74,7 @@
<a class="sdev-header__lang-link" id="sdev-header__lang-FR" onclick="sdev_header__switchLanguage('FR')">FR</a>
</div>
<div id="sdev-header__theme" class="sdev-header__padded-row-list">
<img class="sdev-header__theme-link" id="sdev-header__theme-icon" src="/.templates/header/images/sun.png" alt="Toggle Darkmode" onclick="switchTheme()">
<img class="sdev-header__theme-link" id="sdev-header__theme-icon" src="/.templates/header/images/sun.png" alt="Toggle Darkmode" onclick="sdev_header__toggleTheme()">
</div>
</div>

Expand Down
136 changes: 62 additions & 74 deletions .templates/header/scripts/switchTheme.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,74 @@
// Constants
DEFAULT_THEME = "dark";
_THEME_TOGGLE_HTML = document.getElementById("sdev-header__theme-icon");
_PAGE_BODY_HTML = document.getElementsByTagName("body")[0];
loadThemeStartup();
// Public Functions
function sdev_header__startTheme() {

// Page Startup
function loadThemeStartup() {
const theme = fetchLocalStorage("theme", DEFAULT_THEME);
if (theme === "dark") { loadDarkMode(); }
else { loadLightMode(); }
}
// Fetch the stored theme
const DEFAULT_THEME = "dark";
const themeID = fetchLocalStorage("theme", DEFAULT_THEME);

// Switch Theme
function switchTheme() {
// Load the content for the theme
_sdev_header__loadThemeContent(themeID)
}
function sdev_header__switchTheme(themeID) {

// Fetch the theme from the local storage
const current_theme = fetchLocalStorage("theme");
// Fetch the current theme
const currentThemeID = fetchLocalStorage("theme");

// Change the theme
if (current_theme === "dark") {
localStorage.setItem("theme", "light"); // Swap the theme in the local storage
loadLightMode(); // Load the light mode
_PAGE_BODY_HTML.style.transition = "var(--theme-transition, 0.25s)";
}
else if (current_theme === "light") {
localStorage.setItem("theme", "dark"); // Swap the theme in the local storage
loadDarkMode(); // Load the dark mode
_PAGE_BODY_HTML.style.transition = "var(--theme-transition, 0.25s)";
}
else {
console.error("Invalid Theme: " + current_theme);
}
// Load the content for the theme
_sdev_header__loadThemeContent(themeID);
}

// Load Themes
function loadDarkMode() {
console.debug("Selected Theme: Dark");
_THEME_TOGGLE_HTML.src = "/.templates/header/images/moon.png";
_THEME_TOGGLE_HTML.style.filter = "invert(100%)";
delLightMode();
setDarkMode();
}
function loadLightMode() {
console.debug("Selected Theme: Light");
_THEME_TOGGLE_HTML.src = "/.templates/header/images/sun.png";
_THEME_TOGGLE_HTML.style.filter = "invert(0%)";
delDarkMode();
setLightMode();
}
// Private Functions
function _sdev_header__loadThemeContent(themeID) {

// Load the content for the theme
_sdev_header__themeLinkReplace(themeID);

// Set / Remove Themes
function setDarkMode() {
const head = document.head;
const link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = "/.resources/styles/theme_dark.css";
head.appendChild(link);
// Print the selected theme
_sdev_header__themeDebugPrint(themeID);
}
function delDarkMode() {
const head = document.head;
const links = head.getElementsByTagName("link");
for (let i = 0; i < links.length; i++) {
if (links[i].href.includes("theme_dark.css")) {
head.removeChild(links[i]);
}
}
function _sdev_header__themeLinkReplace(themeID) {
const linkHTML = document.getElementById("sdev-header__page-theme")
linkHTML.href = "/.resources/styles/theme_" + themeID + ".css";
}
function setLightMode() {
const head = document.head;
const link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = "/.resources/styles/theme_light.css";
head.appendChild(link);
function _sdev_header__themeDebugPrint(themeID) {
const themeIDtoContent = {
"dark": "Dark",
"light": "Light",
};
console.debug("[switchTheme.js] Selected Theme: " + themeIDtoContent[themeID]);
}
function delLightMode() {
const head = document.head;
const links = head.getElementsByTagName("link");
for (let i = 0; i < links.length; i++) {
if (links[i].href.includes("theme_light.css")) {
head.removeChild(links[i]);
}

// Startup
sdev_header__startTheme();



// Dark-Light Integration (REMOVE LATER)
function sdev_header__toggleTheme() {
const currentThemeID = fetchLocalStorage("theme");
const newThemeID = currentThemeID === "dark" ? "light" : "dark";
sdev_header__switchTheme(newThemeID);
localStorage.setItem("theme", newThemeID);

const THEME_TOGGLE_HTML = document.getElementById("sdev-header__theme-icon");
const PAGE_BODY_HTML = document.getElementsByTagName("body")[0];
if (newThemeID === "dark") {
THEME_TOGGLE_HTML.src = "/.templates/header/images/moon.png";
THEME_TOGGLE_HTML.style.filter = "invert(100%)";
PAGE_BODY_HTML.style.transition = "var(--theme-transition, 0.25s)";
} else if (newThemeID === "light") {
THEME_TOGGLE_HTML.src = "/.templates/header/images/sun.png";
THEME_TOGGLE_HTML.style.filter = "invert(0%)";
PAGE_BODY_HTML.style.transition = "var(--theme-transition, 0.25s)";
}
}

const THEME_TOGGLE_HTML = document.getElementById("sdev-header__theme-icon");
const themeID = fetchLocalStorage("theme");
if (themeID === "dark") {
THEME_TOGGLE_HTML.src = "/.templates/header/images/moon.png";
THEME_TOGGLE_HTML.style.filter = "invert(100%)";
} else if (themeID === "light") {
THEME_TOGGLE_HTML.src = "/.templates/header/images/sun.png";
THEME_TOGGLE_HTML.style.filter = "invert(0%)";
}
1 change: 1 addition & 0 deletions home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</script>

<!-- Page Style(s) -->
<link rel="stylesheet" href="/.resources/styles/theme_dark.css" id="sdev-header__page-theme">
<link rel="stylesheet" href="/.resources/styles/default.css">
<link rel="stylesheet" href="/home/home.css">

Expand Down

0 comments on commit bad1a3a

Please sign in to comment.