2021-07-18 20:35:42 +07:00
|
|
|
const userPref = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'
|
|
|
|
const currentTheme = localStorage.getItem('theme') ?? userPref
|
2022-07-02 01:03:52 +07:00
|
|
|
const syntaxTheme = document.querySelector("#theme-link");
|
|
|
|
|
|
|
|
|
|
|
|
{{ $darkSyntax := resources.Get "styles/_dark_syntax.scss" | resources.ToCSS (dict "outputStyle" "compressed") | resources.Fingerprint "md5" | resources.Minify }}
|
|
|
|
{{ $lightSyntax := resources.Get "styles/_light_syntax.scss" | resources.ToCSS (dict "outputStyle" "compressed") | resources.Fingerprint "md5" | resources.Minify }}
|
2021-07-18 20:35:42 +07:00
|
|
|
|
|
|
|
if (currentTheme) {
|
|
|
|
document.documentElement.setAttribute('saved-theme', currentTheme);
|
2022-07-04 01:42:35 +07:00
|
|
|
syntaxTheme.href = currentTheme === 'dark' ? '{{ $darkSyntax.Permalink }}' : '{{ $lightSyntax.Permalink }}';
|
2021-07-18 20:35:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
const switchTheme = (e) => {
|
|
|
|
if (e.target.checked) {
|
2022-07-02 01:03:52 +07:00
|
|
|
document.documentElement.setAttribute('saved-theme', 'dark');
|
|
|
|
localStorage.setItem('theme', 'dark');
|
|
|
|
syntaxTheme.href = '{{ $darkSyntax.Permalink }}';
|
2021-07-18 20:35:42 +07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
document.documentElement.setAttribute('saved-theme', 'light')
|
|
|
|
localStorage.setItem('theme', 'light')
|
2022-07-02 01:03:52 +07:00
|
|
|
syntaxTheme.href = '{{ $lightSyntax.Permalink }}';
|
2021-07-18 20:35:42 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-14 03:46:00 +07:00
|
|
|
window.addEventListener('DOMContentLoaded', () => {
|
|
|
|
// Darkmode toggle
|
|
|
|
const toggleSwitch = document.querySelector('#darkmode-toggle')
|
|
|
|
|
|
|
|
// listen for toggle
|
|
|
|
toggleSwitch.addEventListener('change', switchTheme, false)
|
|
|
|
|
|
|
|
if (currentTheme === 'dark') {
|
|
|
|
toggleSwitch.checked = true
|
|
|
|
}
|
|
|
|
})
|