|
14 | 14 |
|
15 | 15 | <br>
|
16 | 16 |
|
17 |
| -1. ... |
18 |
| -2. ... |
| 17 | +1. Using CSS variables |
| 18 | +2. `background: rgb(255 255 255 / 50%)` instead of `rgba(255, 255, 255, 0.5)` |
| 19 | +3. Smooth scroll with `html { scroll-behavior: smooth };` |
| 20 | +4. Creating a [toggle switch](https://www.w3schools.com/howto/howto_css_switch.asp) |
| 21 | +5. Setting attributes on the root element (html) |
| 22 | +6. Using `document.documentElement` |
19 | 23 |
|
20 | 24 | <br>
|
21 | 25 |
|
|
25 | 29 |
|
26 | 30 | <br>
|
27 | 31 |
|
28 |
| -### Part 1 |
| 32 | +### Creating the toggle switch |
29 | 33 |
|
30 | 34 | <br>
|
31 | 35 |
|
32 |
| -Bla bla |
| 36 | +**index.html** |
| 37 | + |
| 38 | +```html |
| 39 | +<div class="theme-switch-wrapper"> |
| 40 | + <!-- Text and icon --> |
| 41 | + <span id="toggle-icon"> |
| 42 | + <span class="toggle-text">Light Mode</span> |
| 43 | + <i class="fas fa-sun"></i> |
| 44 | + </span> |
| 45 | + <!-- Switcher --> |
| 46 | + <label class="theme-switch"> |
| 47 | + <input type="checkbox"> |
| 48 | + <div class="slider round"></div> |
| 49 | + </label> |
| 50 | +</div> |
| 51 | +``` |
| 52 | + |
| 53 | +<br> |
| 54 | + |
| 55 | +**style.css** |
| 56 | + |
| 57 | +```css |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +<br> |
| 62 | + |
| 63 | +**script.js** |
| 64 | + |
| 65 | +<br> |
| 66 | + |
| 67 | +Add event listener to the toggle switch. We use the change event. |
| 68 | + |
| 69 | +```js |
| 70 | +const toggleSwitch = document.querySelector('input[type="checkbox"]'); |
| 71 | + |
| 72 | +// Switch Theme Dynamically |
| 73 | +//... |
| 74 | + |
| 75 | + |
| 76 | +// Event Listener |
| 77 | +toggleSwitch.addEventListener('change', switchTheme); |
| 78 | +``` |
| 79 | + |
| 80 | +<br> |
| 81 | + |
| 82 | +We create function that dynamically changes the theme |
| 83 | + |
| 84 | +```js |
| 85 | +// Switch Theme Dynamically |
| 86 | +function switchTheme(event) { |
| 87 | + if (event.target.checked) { |
| 88 | + document.documentElement.setAttribute('data-theme', 'dark'); |
| 89 | + } else { |
| 90 | + document.documentElement.setAttribute('data-theme', 'light'); |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +We use property `target.checked` to check if the switch toggle has been checked (show dark mode) or not (light mode). |
| 97 | + |
| 98 | +1. We set the `data-theme` attribute at the highest level of the html |
| 99 | +2. `document.documentElement` returns the... |
33 | 100 |
|
34 | 101 | <br>
|
35 | 102 |
|
36 | 103 | ---
|
37 | 104 |
|
| 105 | +Background from [hero patterns](https://www.heropatterns.com/) |
| 106 | + |
| 107 | +Illustrations from [Undraw](https://undraw.co/illustrations) |
| 108 | + |
| 109 | +Icons from [FontAwesome](https://fontawesome.com/) |
| 110 | + |
| 111 | + |
| 112 | + |
0 commit comments