mirror of
https://github.com/MeiK2333/github-style.git
synced 2025-10-07 10:04:04 +00:00
28 lines
718 B
JavaScript
28 lines
718 B
JavaScript
function switchTheme() {
|
|
const currentStyle = currentTheme();
|
|
|
|
if (currentStyle == 'light') {
|
|
setTheme('dark');
|
|
}
|
|
else {
|
|
setTheme('light');
|
|
}
|
|
}
|
|
|
|
function setTheme(style) {
|
|
document.querySelectorAll('.isInitialToggle').forEach(elem => {
|
|
elem.classList.remove('isInitialToggle');
|
|
});
|
|
document.documentElement.setAttribute('data-color-mode', style);
|
|
localStorage.setItem('data-color-mode', style);
|
|
}
|
|
|
|
function currentTheme() {
|
|
const localStyle = localStorage.getItem('data-color-mode');
|
|
const systemStyle = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
return localStyle || systemStyle;
|
|
}
|
|
|
|
(() => {
|
|
setTheme(currentTheme());
|
|
})(); |