This is the code that runs https://bandolier.learnjsthehardway.com/ for you to review. It uses the https://git.learnjsthehardway.com/learn-javascript-the-hard-way/bandolier-template to create the documentation for the project.
https://bandolier.learnjsthehardway.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
716 B
21 lines
716 B
This simple little component will create a dark or light switch that changes the CSS and remembers the users setting. The technique uses this line from `static/colors.css` or `static/monochrome.css`:
|
|
|
|
```
|
|
[data-theme="dark"] {
|
|
```
|
|
|
|
This changes the CSS using the variables in that block to alter their colors to darker versions. Then in the JavaScript the theme is stored in the browser's local storage to remember it:
|
|
|
|
```
|
|
const set_theme = () => {
|
|
document.documentElement.setAttribute('data-theme', theme);
|
|
localStorage.setItem('theme', theme);
|
|
}
|
|
```
|
|
|
|
When the user comes back their theme is loaded with:
|
|
|
|
```
|
|
theme = localStorage.getItem('theme') ? localStorage.getItem('theme') : 'light';
|
|
set_theme()
|
|
```
|
|
|