mirror of
https://github.com/Smaug123/static-site-pipeline
synced 2025-10-14 12:18:45 +00:00
Import Hugo
This commit is contained in:
51
hugo/themes/anatole/assets/js/anatole-header.js
Normal file
51
hugo/themes/anatole/assets/js/anatole-header.js
Normal file
@@ -0,0 +1,51 @@
|
||||
// initialize default value
|
||||
function getTheme() {
|
||||
return localStorage.getItem('theme') ? localStorage.getItem('theme') : null;
|
||||
}
|
||||
|
||||
function setTheme(style) {
|
||||
document.documentElement.setAttribute('data-theme', style);
|
||||
localStorage.setItem('theme', style);
|
||||
}
|
||||
|
||||
function init() {
|
||||
// initialize default value
|
||||
var theme = getTheme();
|
||||
|
||||
// check if a prefered color theme is set for users that have never been to our site
|
||||
const userPrefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
if (theme === null) {
|
||||
if (userPrefersDark) {
|
||||
setTheme('dark');
|
||||
} else if (!document.documentElement.getAttribute('data-theme')) {
|
||||
setTheme('light');
|
||||
} else {
|
||||
setTheme(document.documentElement.getAttribute('data-theme'));
|
||||
}
|
||||
} else {
|
||||
// load a stored theme
|
||||
if (theme == 'light') {
|
||||
document.documentElement.setAttribute('data-theme', 'light');
|
||||
} else {
|
||||
document.documentElement.setAttribute('data-theme', 'dark');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// switch themes
|
||||
function switchTheme(e) {
|
||||
var theme = getTheme();
|
||||
if (theme == 'light') {
|
||||
setTheme('dark');
|
||||
} else {
|
||||
setTheme('light');
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
var themeSwitcher = document.querySelector('.theme-switch');
|
||||
themeSwitcher.addEventListener('click', switchTheme, false);
|
||||
}, false);
|
||||
|
||||
init();
|
Reference in New Issue
Block a user