Bento/assets/js/theme.js

80 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-08-14 11:49:20 -05:00
// ┌┬┐┬ ┬┌─┐┌┬┐┌─┐
// │ ├─┤├┤ │││├┤
// ┴ ┴ ┴└─┘┴ ┴└─┘
2020-09-27 19:21:23 -05:00
// Store the theme
2021-07-20 21:28:00 -05:00
let darkTheme = localStorage.getItem('darkTheme');
const themeToggle = document.querySelector('#themeButton');
const bodyBackground = document.getElementById('#body');
2020-08-29 22:47:08 -05:00
2021-01-09 23:56:22 -06:00
// Apply Dark theme
2020-08-30 01:08:03 -05:00
const enableDark = () => {
2021-07-20 21:28:00 -05:00
document.body.classList.add('darktheme');
localStorage.setItem('darkTheme', 'enabled');
themeToggle.innerHTML = `<i id="themeButton__icon" icon-name="sun"></i>`;
lucide.createIcons();
2020-08-30 01:32:40 -05:00
};
2021-01-09 23:56:22 -06:00
// Remove Dark theme
2020-08-30 01:32:40 -05:00
const disableDark = () => {
2021-07-20 21:28:00 -05:00
document.body.classList.remove('darktheme');
localStorage.setItem('darkTheme', null);
themeToggle.innerHTML = `<i id="themeButton__icon" icon-name="moon"></i>`;
lucide.createIcons();
2020-08-30 01:32:40 -05:00
};
2021-01-09 23:56:22 -06:00
//Toggle theme
2021-07-20 21:28:00 -05:00
if (darkTheme === 'enabled') {
// Temporarily disable transitions when changing theme on startup
document.body.classList.add('notransition');
2021-01-03 20:54:21 -06:00
enableDark();
document.body.offsetHeight; // Trigger reflow to flush CSS changes
document.body.classList.remove('notransition');
2020-08-30 01:32:40 -05:00
} else {
2021-01-03 20:54:21 -06:00
disableDark();
2020-08-29 22:47:08 -05:00
}
2020-08-30 01:08:03 -05:00
2022-02-03 11:04:50 -06:00
// Toggle Theme Listener
2021-07-20 21:28:00 -05:00
themeToggle.addEventListener('click', () => {
darkTheme = localStorage.getItem('darkTheme');
if (darkTheme !== 'enabled') {
2021-01-03 20:54:21 -06:00
enableDark();
} else {
disableDark();
}
2020-08-30 01:08:03 -05:00
});
2021-08-14 11:49:20 -05:00
if (CONFIG.imageBackground) {
2021-07-20 21:28:00 -05:00
document.body.classList.add('withImageBackground');
2021-08-14 11:49:20 -05:00
}
if (CONFIG.changeThemeByOS && CONFIG.autoChangeTheme) {
if (
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
enableDark();
} else {
disableDark();
}
}
2022-02-03 11:04:50 -06:00
// Theme Autochanger
if (CONFIG.changeThemeByHour && CONFIG.autoChangeTheme && !CONFIG.changeThemeByOS) {
const date = new Date();
const hours =
date.getHours() < 10
? '0' + date.getHours().toString()
: date.getHours().toString();
const minutes =
date.getMinutes() < 10
? '0' + date.getMinutes().toString()
: date.getMinutes().toString();
2022-01-22 10:16:06 -06:00
const currentTime = hours + ':' + minutes;
if (currentTime >= CONFIG.hourDarkThemeActive) {
enableDark();
} else if (currentTime >= CONFIG.hourDarkThemeInactive) {
disableDark();
}
}