From c7ba27acc50b06f0f7c2cced9b609a15cc9552d9 Mon Sep 17 00:00:00 2001 From: adam <56338480+adastx@users.noreply.github.com> Date: Sun, 28 Aug 2022 10:16:39 +0200 Subject: Added option to toggle dark mode --- src/scripts/darktheme.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/scripts/darktheme.js (limited to 'src/scripts') diff --git a/src/scripts/darktheme.js b/src/scripts/darktheme.js new file mode 100644 index 0000000..a06aca2 --- /dev/null +++ b/src/scripts/darktheme.js @@ -0,0 +1,27 @@ +// Get the theme toggle input +const currentTheme = localStorage.getItem("theme"); // If the current local storage item can be found + +if (window.matchMedia + && window.matchMedia("(prefers-color-scheme: dark)").matches + && !localStorage.getItem("theme")) { + localStorage.setItem("theme", "dark"); +} + +// Function that will switch the theme based on the if the theme toggle is checked or not +function switchTheme() { + if (document.body.classList.contains('dark-mode')) { + document.body.classList.remove("dark-mode"); + localStorage.setItem("theme", "light"); + } else { + document.body.classList.add("dark-mode"); + localStorage.setItem("theme", "dark"); + } +} +// Get the current theme from local storage +if (currentTheme === "dark") { + document.body.classList.add("dark-mode"); + localStorage.setItem("theme", "dark"); +} else { + document.body.classList.remove("dark-mode"); + localStorage.setItem("theme", "light"); +} -- cgit v1.2.3-70-g09d2