mirror of https://github.com/lxndrblz/anatole.git

Alexander Bilz
07.07.2021 2003b9b2c26edad359bc5c4bd0d6457ec426b5b6
Auto Darkmode and Animations

Closes #173
2 files modified
39 ■■■■ changed files
assets/css/style.css 3 ●●●●● patch | view | raw | blame | history
assets/js/anatole-theme-switcher.js 36 ●●●● patch | view | raw | blame | history
assets/css/style.css
@@ -16,6 +16,8 @@
    --blockquote-border-color: #dfe2e5;
    --thumbnail-height: 15em;
    scroll-padding-top: 100px;
    --duration: 0.5s;
    --timing: ease;
}
html[data-theme='dark'] {
@@ -46,6 +48,7 @@
    width: 100%;
    margin: 0 auto 30px auto;
    background-color: var(--bg-color);
    transition: color var(--duration) var(--timing), background-color var(--duration) var(--timing);
}
p {
assets/js/anatole-theme-switcher.js
@@ -8,26 +8,51 @@
    localStorage.setItem('theme', style);
}
// Check if a theme was set manually
function getMode()  {
    return localStorage.getItem('mode') ? localStorage.getItem('mode') : null;
}
function setMode(mode)  {
    localStorage.setItem('mode', mode);
}
function init() {
    // initialize default value
    const theme = getTheme();
    const mode = getMode();
    // check if a preferred color theme is set for users that have never been to our site
    const userPrefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
    const userPrefersLight = window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches;
    if (theme === null) {
        if (userPrefersDark) {
            setTheme('dark');
            setMode('auto');
        } else if (userPrefersLight){
            setTheme('light');
            setMode('auto');
        } 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');
        if (mode === 'auto') {
            if (userPrefersDark) {
                document.documentElement.setAttribute('data-theme', 'dark');
            } else if (userPrefersLight){
                document.documentElement.setAttribute('data-theme', 'light');
            }
        }
        else {
            // load a user set theme theme
            if (theme === 'light') {
                document.documentElement.setAttribute('data-theme', 'light');
            } else {
                document.documentElement.setAttribute('data-theme', 'dark');
            }
        }
    }
}
@@ -41,6 +66,7 @@
    } else {
        setTheme('light');
    }
    setMode('user');
}
document.addEventListener('DOMContentLoaded', function () {