This is some code written in a course I'm busy taking on Udemy. It serves as a basic toggle switch to flip between light and dark mode. This includes the nav bar, background, images and the icon that changes from a sun to a moon.
The objective after the lesson was to clean up the code and make it DRY. I've done so to some of the code already. But there are some instances where it still repeats, for instance isDark
in my lightDarkMode
function. Is there a way to eliminate the repetitive usage of isDark
?
const toggleSwitch = document.querySelector('input[type="checkbox"]');
const nav = document.getElementById('nav');
const toggleIcon = document.getElementById('toggle-icon');
const textBox = document.getElementById('text-box');
const darkLightTheme = ['dark', 'light'];
function imageMode(color) {
image1.src = `img/undraw_proud_coder_${color}.svg`;
image2.src = `img/undraw_feeling_proud_${color}.svg`;
image3.src = `img/undraw_conceptual_idea_${color}.svg`;
}
lightDarkMode = (isDark) => {
nav.style.backgroundColor = isDark ? 'rgb(0 0 0 / 50%)' : 'rgb(255 255 255 / 50%)';
textBox.style.backgroundColor = isDark ? 'rgb(255 255 255 / 50%)' : 'rgb(0 0 0 / 50%)';
toggleIcon.children[0].textContent = isDark ? 'Dark Mode' : 'Light Mode';
isDark ? toggleIcon.children[1].classList.replace('fa-sun', 'fa-moon') : toggleIcon.children[1].classList.replace('fa-moon', 'fa-sun');
isDark ? imageMode(darkLightTheme[0]) : imageMode(darkLightTheme[1]);
}
1 Answer 1
One thing that you could update is how you are using the ternary operator
for isDark
.
Ternary operators are used for returning something based on a boolean response, so either return X if true, or Y if false.
However, every time it is used it is evaluating the statement being passed to it. In this case both times it is evaluating isDark
. For the average website, this won't affect your processing time, but it is something to think about.
Instead a classic if/else statement would allow you to run more code off a single evaluation.
You will notice this isn't as sleek looking
as ternary and is even more lines of code, but gives more capabilities when you need it.
if (isDark) {
toggleIcon.children[1].classList.replace('fa-sun', 'fa-moon')
imageMode(darkLightTheme[0])
} else {
toggleIcon.children[1].classList.replace('fa-moon', 'fa-sun')
imageMode(darkLightTheme[1])
}
-
\$\begingroup\$ Thank you @imvain2 , That makes sense. Right now my main goal was to eliminate some of the ternary operators and this achieves that. Not to mention you have given me a better understanding if ternary operators and if else statements. I really appreciate your help. \$\endgroup\$Wayne– Wayne2020年08月21日 07:58:56 +00:00Commented Aug 21, 2020 at 7:58
dark
assuming light is the default so it doesn't need a class. Then just reference.dark nav{}
and.dark #text-box{}
etc in your stylesheet. That would clean up the javascript tremendously as you would be simply toggling a class on the body and then only need to deal with the images. \$\endgroup\$