mirror of
https://github.com/Michatec/MiniFaceBook.git
synced 2026-03-31 23:46:30 +02:00
Files
This commit is contained in:
25
static/js/theme.js
Normal file
25
static/js/theme.js
Normal file
@@ -0,0 +1,25 @@
|
||||
function setTheme(mode, save=true) {
|
||||
document.body.classList.remove('light-mode', 'dark-mode');
|
||||
document.body.classList.add(mode + '-mode');
|
||||
document.getElementById('theme-icon').className = mode === 'dark' ? 'bi bi-moon-fill' : 'bi bi-sun-fill';
|
||||
document.getElementById('theme-label').textContent = mode === 'dark' ? 'Dark-Mode' : 'Light-Mode';
|
||||
if(save) document.cookie = "theme=" + mode + ";path=/;max-age=31536000";
|
||||
}
|
||||
function getCookie(name) {
|
||||
let v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
|
||||
return v ? v[2] : null;
|
||||
}
|
||||
|
||||
function systemPrefersDark() {
|
||||
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
let theme = getCookie('theme');
|
||||
if(!theme) theme = systemPrefersDark() ? 'dark' : 'light';
|
||||
setTheme(theme, false);
|
||||
document.getElementById('toggle-theme').onclick = function() {
|
||||
let newTheme = document.body.classList.contains('dark-mode') ? 'light' : 'dark';
|
||||
setTheme(newTheme);
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user