steve0greatness,
created on Friday, 2 February 2024, 07:31:07 (1706859067),
received on Monday, 6 May 2024, 02:55:37 (1714964137)
Author identity: Steve0Greatness <steve0greatnessiscool@gmail.com>
fc6f5fef54d5920580ae610c2b2b7c447b5e48db
blog-posts/creating_a_theme_switch.md
@@ -1,5 +1,47 @@
--- title: Creating a simple theme switcher date: 2021 Dec 08 updated: 2024 Feb 01--- This is a simple tutorial on how to make a simple theme switcher.<h1 id="step1">Step 1: Creating the themes</h1><p>The first step is to create the themes in your stylesheet, you can have as many as you want. Just make sure to remeber all their names within your CSS.</p><h1 id="step2">Step 2: Making an array</h1><p>This is why you need to remeber all their names within the CSS. You need to add them all to an array in your JS. Below is an example of an array containing some themes.</p><div class="code">const themes = ["light", "dark", "gamer"]</div><h1 id="step3">Step 3: Switching themes</h1><p>This is the part you've been waiting for! The actual content switcher. It's surprisingly simple.</p><p>First, get the index of the current theme using <span class="code">let currentTheme = themes.indexOf(document.documentElement.className)</span>. Then, use an if statement to see if it's more than or equal to the length of the array containing your themes.</p><div class="code">if (currentTheme + 1 >= themes.length) {<div style="margin-left:1em">document.documentElement.className = themes[0]</div>} else {<div style="margin-left:1em">document.documentElement.className = themes[currentTheme + 1]</div>}</div>Now just add a listener to the button(using <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener">event listener</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick">getElement.onclick</a>, or <a href="https://www.w3schools.com/TAgs/att_onclick.asp">onclick</a>)<h1 id="finished">Final Product</h1>In the end, what you just made should look something like the iFrame below.<br /><iframe id="finalProduct" src="data:text/html;base64,PCFkb2N0eXBlIGh0bWw+PGh0bWwgY2xhc3M9ImxpZ2h0Ij48aGVhZD48c3R5bGU+aHRtbC5saWdodCB7YmFja2dyb3VuZDogI2ZmZjtjb2xvcjogIzAwMDstLWJ1dHRvbkJhY2tncm91bmQ6ICNmZWZlZmU7LS1idXR0b25Cb3JkZXI6ICNjY2M7LS1idXR0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cmlwdD48L2JvZHk+PC9odG1sPg==" style="border:none"></iframe>This is a simple tutorial on how to make a simple theme switcher. ## Step 1: Creating the themes The first step is to create the themes in your stylesheet, you can have as many as you want. Just make sure to remeber all their names within your CSS. ## Step 2: Making an array This is why you need to remeber all their names within the CSS. You need to add them all to an array in your JS. Below is an example of an array containing some themes. ```js const themes = ["light", "dark", "gamer"] ``` ## Step 3: Switching themes This is the part you've been waiting for! The actual content switcher. It's surprisingly simple. First, get the index of the current theme using ```js let currentTheme = themes.indexOf(document.documentElement.className). ``` Then, use an if statement to see if it's more than or equal to the length of the array containing your themes. ```js if (currentTheme + 1 >= themes.length) { document.documentElement.className = themes[0]; } else { document.documentElement.className = themes[currentTheme + 1] } ``` Now just add a listener to the button(using [event listener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener), [getElement.onclick](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick), or [onclick](https://www.w3schools.com/TAgs/att_onclick.asp)) ## Final Product In the end, what you just made should look something like the iFrame below. <iframe id="finalProduct" src="/blog-files/theme-change-final.html" style="border:none"></iframe> [See Code](/blog-files/theme-change-final.txt)
static/blog-files/theme-change-final.html
@@ -0,0 +1,54 @@
<!DOCTYPE html> <html data-theme="light"> <head> <style type="text/css"> html[data-theme=light] { background: #fff; color: #000; --buttonBackground: #fefefe; --buttonBorder: #ccc; --buttonColor: #001; } html[data-theme=dark] { background: #000; color: #fff; --buttonBackground: #101010; --buttonBorder: #333; --buttonColor: #fff; } html[data-theme=gamer] { background: #f00; color: #00f; --buttonBackground: #050; --buttonBorder: #0a0; --buttonColor: #0f0; } button { background: var(--buttonBackground); border: var(--buttonBorder) solid 3px; color: var(--buttonColor); } </style> </head> <body> <button id="themeSwitch">Switch Theme</button> This is epic! <script type="text/javascript"> console.log("Changing theme") const themes = ["light", "dark", "gamer"]; document.querySelector("#themeSwitch").addEventListener("click", function() { console.log("Changing theme") let curTheme = themes.indexOf(document.documentElement.dataset.theme); if (curTheme + 1 >= themes.length) { document.documentElement.dataset.theme = themes[0]; return; } console.log("asfd") document.documentElement.dataset.theme = themes[curTheme + 1]; }) </script> </body> </html>
static/blog-files/theme-change-final.txt
@@ -0,0 +1,54 @@
<!DOCTYPE html> <html data-theme="light"> <head> <style type="text/css"> html[data-theme=light] { background: #fff; color: #000; --buttonBackground: #fefefe; --buttonBorder: #ccc; --buttonColor: #001; } html[data-theme=dark] { background: #000; color: #fff; --buttonBackground: #101010; --buttonBorder: #333; --buttonColor: #fff; } html[data-theme=gamer] { background: #f00; color: #00f; --buttonBackground: #050; --buttonBorder: #0a0; --buttonColor: #0f0; } button { background: var(--buttonBackground); border: var(--buttonBorder) solid 3px; color: var(--buttonColor); } </style> </head> <body> <button id="themeSwitch">Switch Theme</button> This is epic! <script type="text/javascript"> console.log("Changing theme") const themes = ["light", "dark", "gamer"]; document.querySelector("#themeSwitch").addEventListener("click", function() { console.log("Changing theme") let curTheme = themes.indexOf(document.documentElement.dataset.theme); if (curTheme + 1 >= themes.length) { document.documentElement.dataset.theme = themes[0]; return; } console.log("asfd") document.documentElement.dataset.theme = themes[curTheme + 1]; }) </script> </body> </html>