bug fix: DOMContentLoaded
This commit is contained in:
parent
e61809d59a
commit
4cba618ce1
@ -1,79 +1,65 @@
|
|||||||
{{ $style := "light-without-switcher" }}
|
{{ $style := "light-without-switcher" }}
|
||||||
{{ if and (isset site.Params "style") (ne site.Params.style "") }}
|
{{ if and (isset site.Params "style") (ne site.Params.style "") }}
|
||||||
{{ $style = site.Params.style | lower }}
|
{{ $style = site.Params.style | lower }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ if not (in (slice "light-without-switcher" "dark-without-switcher") $style) }}
|
{{ if not (in (slice "light-without-switcher" "dark-without-switcher") $style) }}
|
||||||
{{ if ne "auto-without-switcher" $style }}
|
{{ if ne "auto-without-switcher" $style }}
|
||||||
<div class="theme-switcher">
|
<div class="theme-switcher">
|
||||||
{{ partial "font-awesome.html" (dict "iconName" "theme-light" "custom" false) }}
|
{{ partial "font-awesome.html" (dict "iconName" "theme-light" "custom" false) }}
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const STORAGE_KEY = 'user-color-scheme'
|
const STORAGE_KEY = 'user-color-scheme'
|
||||||
const defaultTheme = {{ $style }}
|
const defaultTheme = {{ $style }}
|
||||||
|
|
||||||
let currentTheme
|
let currentTheme
|
||||||
let switchButton
|
let switchButton
|
||||||
let autoDefinedScheme = window.matchMedia('(prefers-color-scheme: dark)')
|
let autoDefinedScheme = window.matchMedia('(prefers-color-scheme: dark)')
|
||||||
|
|
||||||
const autoChangeScheme = e => {
|
const autoChangeScheme = e => {
|
||||||
currentTheme = e.matches ? 'dark' : 'light'
|
currentTheme = e.matches ? 'dark' : 'light'
|
||||||
|
document.documentElement.setAttribute('data-theme', currentTheme)
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
switchButton = document.querySelector('.theme-switcher')
|
||||||
|
currentTheme = detectCurrentScheme()
|
||||||
|
|
||||||
|
if (currentTheme === 'auto') {
|
||||||
|
autoChangeScheme(autoDefinedScheme);
|
||||||
|
autoDefinedScheme.addListener(autoChangeScheme);
|
||||||
|
} else {
|
||||||
document.documentElement.setAttribute('data-theme', currentTheme)
|
document.documentElement.setAttribute('data-theme', currentTheme)
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
if (switchButton) {
|
||||||
switchButton = document.querySelector('.theme-switcher')
|
switchButton.addEventListener('click', switchTheme, false)
|
||||||
currentTheme = detectCurrentScheme()
|
|
||||||
if (currentTheme == 'dark') {
|
|
||||||
document.documentElement.setAttribute('data-theme', 'dark')
|
|
||||||
}
|
|
||||||
if (currentTheme == 'auto') {
|
|
||||||
autoChangeScheme(autoDefinedScheme);
|
|
||||||
autoDefinedScheme.addListener(autoChangeScheme);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (switchButton) {
|
|
||||||
switchButton.addEventListener('click', switchTheme, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
showContent()
|
|
||||||
})
|
|
||||||
|
|
||||||
function detectCurrentScheme() {
|
|
||||||
if (localStorage !== null && localStorage.getItem(STORAGE_KEY)) {
|
|
||||||
return localStorage.getItem(STORAGE_KEY)
|
|
||||||
}
|
|
||||||
if (defaultTheme) {
|
|
||||||
return defaultTheme
|
|
||||||
}
|
|
||||||
if (!window.matchMedia) {
|
|
||||||
return 'light'
|
|
||||||
}
|
|
||||||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
||||||
return 'dark'
|
|
||||||
}
|
|
||||||
return 'light'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function switchTheme(e) {
|
showContent()
|
||||||
if (currentTheme == 'dark') {
|
})
|
||||||
if (localStorage !== null)
|
|
||||||
localStorage.setItem(STORAGE_KEY, 'light')
|
|
||||||
document.documentElement.setAttribute('data-theme', 'light')
|
|
||||||
currentTheme = 'light'
|
|
||||||
} else {
|
|
||||||
if (localStorage !== null)
|
|
||||||
localStorage.setItem(STORAGE_KEY, 'dark')
|
|
||||||
document.documentElement.setAttribute('data-theme', 'dark')
|
|
||||||
currentTheme = 'dark'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function showContent() {
|
function detectCurrentScheme() {
|
||||||
document.body.style.visibility = 'visible';
|
if (localStorage !== null && localStorage.getItem(STORAGE_KEY)) {
|
||||||
document.body.style.opacity = 1;
|
return localStorage.getItem(STORAGE_KEY)
|
||||||
}
|
}
|
||||||
</script>
|
if (defaultTheme) {
|
||||||
{{ end }}
|
return defaultTheme
|
||||||
|
}
|
||||||
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||||
|
}
|
||||||
|
|
||||||
|
function switchTheme(e) {
|
||||||
|
currentTheme = (currentTheme === 'dark') ? 'light' : 'dark';
|
||||||
|
if (localStorage) localStorage.setItem(STORAGE_KEY, currentTheme);
|
||||||
|
document.documentElement.setAttribute('data-theme', currentTheme);
|
||||||
|
}
|
||||||
|
|
||||||
|
function showContent() {
|
||||||
|
document.body.style.visibility = 'visible';
|
||||||
|
document.body.style.opacity = 1;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
{{ end }}
|
Loading…
Reference in New Issue
Block a user