[TASK] add color switch option btn

This commit is contained in:
Marie Birner 2023-11-04 20:16:20 +01:00
parent b465923fa9
commit 895492946c
3 changed files with 89 additions and 6 deletions

View File

@ -8,6 +8,8 @@ export interface choiceMap {
let choiceObjects: choiceMap = {};
document.addEventListener('DOMContentLoaded', function() {
changeTheme();
initcolorTheme();
initSearch();
initSidebar();
initToggle();
@ -20,6 +22,75 @@ document.addEventListener('DOMContentLoaded', function() {
setCurrentdate(<HTMLInputElement>document.querySelector('#departure'));
});
function changeTheme() {
let toggleBtn = <HTMLElement>document.querySelector('#theme-toggle-js');
if(toggleBtn) {
toggleBtn.addEventListener('click', function() {
if(toggleBtn.dataset.theme === 'light') {
setTheme('dark', true);
} else {
setTheme('light', true);
}
});
}
}
/***
* 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: string, setLocalStorage = true) {
let toggleBtn = document.querySelector('#theme-toggle-js');
if(toggleBtn) {
if (setLocalStorage) {
localStorage.setItem('theme', theme);
}
toggleBtn.setAttribute('data-theme', theme);
if (document.documentElement.classList.contains('dark') && theme === 'light') {
document.documentElement.classList.remove('dark');
} else if(theme === 'dark'){
document.documentElement.classList.add('dark');
}
}
}
function setCurrentdate(input: HTMLInputElement) {
if(input) {
const now = new Date();

View File

@ -41,5 +41,6 @@ export default {
},
plugins: [],
important: true,
darkMode: 'class',
}

View File

@ -1,12 +1,23 @@
<footer class="bg-primary-950 dark:bg-primary-900 text-white w-full flex justify-center p-3">
<div class="max-w-screen-xl w-full flex justify-between">
<div class="max-w-screen-xl w-full flex justify-between items-center">
<div>
<span class="text-[#ff0000]">&hearts;</span>
ASKÖ Ruderverein Donau Linz
</div>
<div>
&copy;
{{ now() | date(format="%Y") }}
ASKÖ Ruderverein Donau Linz <small class="text-primary-100">&copy; {{ now() | date(format="%Y") }}</smalL>
</div>
<div>
<button id="theme-toggle-js" type="button" data-theme="light" class="btn btn-primary">
<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="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="inline dark:hidden">
<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>
</div>
</footer>