[TASK] add theme toggler in footer
All checks were successful
CI/CD Pipeline / deploy-main (push) Successful in 6m53s

This commit is contained in:
Marie Birner 2024-02-19 18:11:02 +01:00
parent ba4b54490a
commit f3919f5369
7 changed files with 179 additions and 39 deletions

View File

@ -221,3 +221,21 @@ ol li::before {
ol li + li { ol li + li {
border-top: var(--pico-contrast-focus) solid 1px; border-top: var(--pico-contrast-focus) solid 1px;
} }
.hidden {
display: none;
}
.inline {
display: inline;
}
:root:not([data-theme=dark]) .dark-inline,
[data-theme=light] .dark-inline{
display: inline;
}
:root:not([data-theme=dark]) .dark-hidden,
[data-theme=light] .dark-hidden {
display: none;
}

View File

@ -124,6 +124,9 @@ fn main() {
let js = fs::read_to_string("templates/app.js").unwrap(); let js = fs::read_to_string("templates/app.js").unwrap();
fs::write(&format!("output/app.js"), &js).expect("Unable to write file"); fs::write(&format!("output/app.js"), &js).expect("Unable to write file");
let js = fs::read_to_string("templates/toggle.js").unwrap();
fs::write(&format!("output/toggle.js"), &js).expect("Unable to write file");
let mut index = fs::read_to_string("templates/index.html").unwrap(); let mut index = fs::read_to_string("templates/index.html").unwrap();
index = index.replace("{{content}}", &li_of_files); index = index.replace("{{content}}", &li_of_files);
fs::write(&format!("output/index.html"), &index).expect("Unable to write file"); fs::write(&format!("output/index.html"), &index).expect("Unable to write file");

View File

@ -1,21 +1,71 @@
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
toggleSummary(); changeTheme();
initcolorTheme();
}); });
function toggleSummary() { function changeTheme() {
const openBtns = document.querySelectorAll('.open-js'); let toggleBtn = document.querySelector('#theme-toggle-js');
const closeBtns = document.querySelectorAll('.close-js');
const detailElements = document.querySelectorAll('details');
openBtns.forEach(function(openBtn) { if(toggleBtn) {
openBtn.addEventListener('click', function(){ toggleBtn.addEventListener('click', function() {
detailElements.forEach((detail) => detail.setAttribute("open", "")); if(toggleBtn.dataset.theme === 'light') {
}); setTheme('dark', true);
}); } else {
setTheme('light', true);
closeBtns.forEach(function(closeBtn) { }
closeBtn.addEventListener('click', function(){
detailElements.forEach((detail) => detail.removeAttribute("open", ""));
});
}); });
} }
}
/***
* init javascript
* 1) detect native color scheme or use set theme in local storage
* 2) detect view (desktop or responsive) if on mobile device with touch screen
* 3) set base font size to 112.5% -> 18px
*/
function initcolorTheme() {
colorThemeWatcher();
let theme = localStorage.getItem('theme');
if (theme == null || theme === 'auto') {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
setTheme('dark', false);
} else {
setTheme('light', false);
}
} else {
setTheme(theme)
}
}
/***
* Listener operating system native color configuration
*/
function colorThemeWatcher() {
try {
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
setTheme(e.matches ? 'dark' : 'light');
});
} catch {
console.warn('color theme watcher not supported');
}
}
/**
* Define color scheme, colors without losing the base font size configuration
* and add data-theme to html tag
* @param theme
*/
function setTheme(theme, setLocalStorage = true) {
let toggleBtn = document.querySelector('#theme-toggle-js');
if (setLocalStorage) {
localStorage.setItem('theme', theme);
}
if (toggleBtn) toggleBtn.setAttribute('data-theme', theme);
if (document.documentElement.dataset.theme === 'dark' && theme === 'light') {
document.documentElement.dataset.theme = 'light';
} else if(document.documentElement.dataset.theme === 'light' && theme === 'dark'){
document.documentElement.dataset.theme = 'dark';
}
}

View File

@ -1,4 +1,4 @@
<html lang="de"> <html lang="de" data-theme="dark">
<head> <head>
<link rel="stylesheet" href="pico.min.css" /> <link rel="stylesheet" href="pico.min.css" />
<link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="style.css" />
@ -20,7 +20,8 @@
</ol> </ol>
</main> </main>
<footer> <footer>
<nav class="container"> <div class="container">
<nav>
<ul> <ul>
<li> <li>
<!--TODO: Impressum / Datenschutz hinzufügen wenn aktiv aria-current="page" --> <!--TODO: Impressum / Datenschutz hinzufügen wenn aktiv aria-current="page" -->
@ -31,6 +32,20 @@
</li> </li>
</ul> </ul>
</nav> </nav>
<button id="theme-toggle-js" type="button" data-theme="light" class="btn btn-primary">
<span class="inline dark-hidden">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M8 11a3 3 0 1 1 0-6 3 3 0 0 1 0 6zm0 1a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z"/>
</svg>
</span>
<span class="hidden dark-inline">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278zM4.858 1.311A7.269 7.269 0 0 0 1.025 7.71c0 4.02 3.279 7.276 7.319 7.276a7.316 7.316 0 0 0 5.205-2.162c-.337.042-.68.063-1.029.063-4.61 0-8.343-3.714-8.343-8.29 0-1.167.242-2.278.681-3.286z"/>
</svg>
</span>
</button>
</div>
</footer> </footer>
<script src="app.js"></script>
</body> </body>
</html> </html>

View File

@ -1,4 +1,4 @@
<html lang="de"> <html lang="de" data-theme="dark">
<head> <head>
<link rel="stylesheet" href="pico.min.css" /> <link rel="stylesheet" href="pico.min.css" />
<link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="style.css" />
@ -23,7 +23,8 @@
{{content}} {{content}}
</main> </main>
<footer> <footer>
<nav class="container"> <div class="container">
<nav>
<ul> <ul>
<li> <li>
<!--TODO: Impressum / Datenschutz hinzufügen wenn aktiv aria-current="page" --> <!--TODO: Impressum / Datenschutz hinzufügen wenn aktiv aria-current="page" -->
@ -34,7 +35,21 @@
</li> </li>
</ul> </ul>
</nav> </nav>
<button id="theme-toggle-js" type="button" data-theme="light" class="btn btn-primary">
<span class="inline dark-hidden">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M8 11a3 3 0 1 1 0-6 3 3 0 0 1 0 6zm0 1a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z"/>
</svg>
</span>
<span class="hidden dark-inline">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278zM4.858 1.311A7.269 7.269 0 0 0 1.025 7.71c0 4.02 3.279 7.276 7.319 7.276a7.316 7.316 0 0 0 5.205-2.162c-.337.042-.68.063-1.029.063-4.61 0-8.343-3.714-8.343-8.29 0-1.167.242-2.278.681-3.286z"/>
</svg>
</span>
</button>
</div>
</footer> </footer>
<script src="app.js"></script> <script src="app.js"></script>
<script src="toggle.js"></script>
</body> </body>
</html> </html>

View File

@ -221,3 +221,21 @@ ol li::before {
ol li + li { ol li + li {
border-top: var(--pico-contrast-focus) solid 1px; border-top: var(--pico-contrast-focus) solid 1px;
} }
.hidden {
display: none;
}
.inline {
display: inline;
}
:root:not([data-theme=dark]) .dark-inline,
[data-theme=light] .dark-inline{
display: inline;
}
:root:not([data-theme=dark]) .dark-hidden,
[data-theme=light] .dark-hidden {
display: none;
}

21
templates/toggle.js Normal file
View File

@ -0,0 +1,21 @@
document.addEventListener('DOMContentLoaded', function() {
toggleSummary();
});
function toggleSummary() {
const openBtns = document.querySelectorAll('.open-js');
const closeBtns = document.querySelectorAll('.close-js');
const detailElements = document.querySelectorAll('details');
openBtns.forEach(function(openBtn) {
openBtn.addEventListener('click', function(){
detailElements.forEach((detail) => detail.setAttribute("open", ""));
});
});
closeBtns.forEach(function(closeBtn) {
closeBtn.addEventListener('click', function(){
detailElements.forEach((detail) => detail.removeAttribute("open", ""));
});
});
}