Closed myamadriaga closed 1 month ago
We needed the website to start on light mode, so I worked with Trevor in order to revise the code, this is what he changed it to:
get isThemeInput() {
const localstorage = window.localStorage
const lightmodekey = "islightMode"
const div = document.createElement("div");
div.innerHTML = "Theme Change:"; // label
GameEnv.isLightMode = localstorage.getItem(lightmodekey).toLowerCase() === "true"
const islightMode = document.createElement("input"); // get user defined lightmode boolean
islightMode.type = "checkbox";
if (GameEnv.isLightMode) {
enableLightMode();
islightMode.checked = true;
} else {
enableDarkMode();
islightMode.checked = false;
}
islightMode.addEventListener('change', () => {
if (islightMode.checked) {
enableLightMode();
GameEnv.isLightMode = true;
localstorage.setItem(lightmodekey, GameEnv.isLightMode)
} else {
enableDarkMode();
GameEnv.isLightMode = false;
localstorage.setItem(lightmodekey, GameEnv.isLightMode)
}
console.log(GameEnv.isLightMode)
});
// Append elements to the DOM or wherever appropriate
div.appendChild(islightMode);
return div
// Append div to your settings container
// For example:
// document.getElementById('settingsContainer').appendChild(div);
}
within this code, we saved it to local storage, and changed the code from a string to boolean so that it actually inputs into local storage. We defined localstorage, and lightmodekey so that it can be stored as true or false.
There isn't a direct order you need to go in for this, although I started with finding the way I wanted to create the button and the keywords I wanted to use. I started with trying to create the theme switch as one function, but that didn't work overall.
The method I used had two different sass files, connecting to two different JS files which the function controlled in the SettingsControl file. I created a dark-mode.scss, and a light-mode.scss file.
This is the code used for the dark-mode.scss, as a reference:
for the light mode, all that needs to be done is switch the hex code properties to the opposite colors. There is an invert filter that also exists, but I'm not sure if that will work as well--I'm listing what ended up working for me.
I stored the two files in custom-styles.scss, preferably in order of how I want the files to switch.
Now that the sass is created, and it is checked that they both work, we can create 2 functions that remove and add the light and dark mode. (Again, this is not the easiest way, but the function does work.) lightMode.js:
darkMode.js:
those files will be used with the function that we created in SettingsControl.js. Import the two files into SettingsControl.js so the code knows what files you're referring to when you use it within the code with an if, else statement.
I'm not directly sure if this event-listener affects the function, but I replicated one of the previous parts of the code to create this function. It seems to just be storing it in Local Storage, so I did not mess with that part.
the code is all in a certain order, so I made sure to insert the code into the correct parts of the file.
The next part of the code, is the function and the code that will create the checkbox. Here, you can name the checkbox with inner html, then connect the event-listener function to change the theme.
I placed this code after the "isInverted" input. now, towards the end of the document we need to duplicate some of the code to construct the HTML input, along with event update:
This code helped me construct everything, although I ran into frequent problems, so I'll list my solutions below:
The game/website uses multiple sass files. If you don't indicate the two different classes for light and dark mode it will not be used on the website. in PlatformerStyles.scss, some of the parts of code will use keywords that were previously contained in the main dark-mode.scss file. Change that keyword to a similar color, or the color that works best for that element.
b/c of this issue, I thought that I would have to wrap the entire sass file/s in main, but I just needed to create separate classes for each of them.
make sure that the sass does not also overwrite the sass within the game. I added this code to both sass files to exclude the objects within the game:
Problems I currently have:
This code structure is pretty unique, so the process for a light/dark-mode in other websites may be similar, but I don't believe it is as complicated as it was here. I think I learned a lot more about sass code and problems with things I'm trying to do, so I'm glad I was able to learn a lot more. When I would research to see how I can create the function, I think that this code structure was so specific, that not many websites really taught me how to specifically do it how I had to do it inside of this code.
I actually had to reset the code the first time I finalized the theme switch, because with commit issues and stuff breaking it was too risky to commit my changes in. I think its more polished after starting over clean.