For knowing which colors to use so I can simulate the real WhatsApp, I use a tool called
CSS Pepper, you just click on DOM elements and it shows all its CSS properties. In fact, that very tool helped me realize that I wasn't using a font for the message divs, so I added Segoe UI to the font-family property, and now it almost looks identical.
Little bit of trouble
I'm having some problems, the toggle("lightmode") function works apart from the function that changes the color of the buttons in CSS; and the background doesn't change colors automatically, but only in the developer tools when the variable color is changed. Also, the background color variable only get recognized when declared in the :root selector, hence the background doesn't changing colors automatically.
I have really been working really hard to get to this point, I had to change a lot of things for reasons I didn't even expect, for example, the colors in JavaScript don't work with HEX, but with RGB. And also I got the Light Mode button to work partially, although with some bugs.
It's really late and I think I did some advancements, we'll call it a day.
Tomorrow, I'll have to:
- Rework the function that changes the color of the buttons
- Figure out why some variables don't change automatically in CSS
Good night!
The next day
Good afternoon!
I was taking a look at the background color design, and found that it was not exactly identical to the original WhatsApp Web one, so I made some small adjustments, like changing the background color a little bit, and setting the opacity to 0.4 instead of 0.06; and using just one image for the background pattern.
To know which exact opacity to use, I used chrome developer tools to search for the opacity property in the original WhatsApp Web:
And yes, as you can see, another monitor would really come in handy when working with these things
Now, time to rework some color-changing functions!
First, I had to delete the old way of changing colors, which was this:
1
2 | button1.style.backgroundColor(color1);
button2.style.backgroundColor(color2);
|
Now, I defined the following CSS variables and classes:
1
2
3
4
5
6
7 | .selectedButton {
--colorback: var(--selected-button-background);
}
.unSelectedButton {
--colorback: var(--unselected-button-background);
}
|
The --selected-button-background variable is at the same time declared within the .dark-mode and .light-mode selector values.
Then, inside the button selectors, I use the --colorback variable:
1 | background-color: var(--colorback);
|
(That for each user button).
Now, the JavaScript code that adds and removes classes looks like this:
1
2
3
4 | select.classList.add("selectedButton");
select.classList.remove("unSelectedButton");
unselect.classList.add("unSelectedButton");
unselect.classList.remove("SelectedButton");
|
I was surprised to see that this worked right away. Also, now I don't have to mess around with RGB color codes.
Now, I will try to solve the background not changing color problem.
Turns out, that it was because the background, is part of the body property in html, so changing the class of the container to dark mode didn't affect the body itself, to fix this, I can also alter the body of the document, like this:
1
2
3
4 | container.classList.toggle("lightmode");
container.classList.toggle("darkmode");
document.body.classList.toggle("lightmode");
document.body.classList.toggle("darkmode");
|
Now it works as expected.
A little mention on the things that I didn't explain
There were a lot of challenges and problem while working around this function that I didn't explain. But I will mention them a little bit on the following list:
- I tried to optimize some stuff that didn't end up working in the function that creates the message divs.
- I had to add back another two arguments to the onclick="botonejecutar()" function because there was a bug that involved the buttons' colors getting confused.
- There were some tests to see if optimizing the code even more was a possibility, but most of the time there was not a viable option to do so.
- The way the color variables are declared are not good practice.
- This post and this code journey was not made in two days, but a little bit more.
So yes, there were a lot of work that I didn't register here, but I always try to keep a good balance between coding and blogging, so I don't make these posts too shallow, or lose flow state.
Now, the Light and Dark Mode button works completely well, thanks for reading!
Comments
Post a Comment