[TASK] add color switch option btn
This commit is contained in:
@ -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();
|
||||
|
@ -41,5 +41,6 @@ export default {
|
||||
},
|
||||
plugins: [],
|
||||
important: true,
|
||||
darkMode: 'class',
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user