2020-07-09 21:15:38 +02:00
|
|
|
{{ $style := "light-without-switcher" }}
|
|
|
|
{{ if and (isset site.Params "style") (ne site.Params.style "") }}
|
2024-03-13 09:14:18 +01:00
|
|
|
{{ $style = site.Params.style | lower }}
|
2020-07-09 21:15:38 +02:00
|
|
|
{{ end }}
|
|
|
|
|
2023-07-04 18:31:33 +02:00
|
|
|
{{ if not (in (slice "light-without-switcher" "dark-without-switcher") $style) }}
|
2024-03-13 09:14:18 +01:00
|
|
|
{{ if ne "auto-without-switcher" $style }}
|
|
|
|
<div class="theme-switcher">
|
|
|
|
{{ partial "font-awesome.html" (dict "iconName" "theme-light" "custom" false) }}
|
|
|
|
</div>
|
|
|
|
{{ end }}
|
|
|
|
|
|
|
|
<script>
|
|
|
|
const STORAGE_KEY = 'user-color-scheme'
|
|
|
|
const defaultTheme = {{ $style }}
|
2020-07-09 21:15:38 +02:00
|
|
|
|
2024-03-13 09:14:18 +01:00
|
|
|
let currentTheme
|
|
|
|
let switchButton
|
|
|
|
let autoDefinedScheme = window.matchMedia('(prefers-color-scheme: dark)')
|
2020-07-09 21:15:38 +02:00
|
|
|
|
2024-03-13 09:14:18 +01:00
|
|
|
const autoChangeScheme = e => {
|
|
|
|
currentTheme = e.matches ? 'dark' : 'light'
|
|
|
|
document.documentElement.setAttribute('data-theme', currentTheme)
|
|
|
|
}
|
2020-07-09 21:15:38 +02:00
|
|
|
|
2024-03-13 09:14:18 +01:00
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
|
|
switchButton = document.querySelector('.theme-switcher')
|
|
|
|
currentTheme = detectCurrentScheme()
|
|
|
|
|
|
|
|
if (currentTheme === 'auto') {
|
|
|
|
autoChangeScheme(autoDefinedScheme);
|
|
|
|
autoDefinedScheme.addListener(autoChangeScheme);
|
|
|
|
} else {
|
2024-03-06 07:22:23 +01:00
|
|
|
document.documentElement.setAttribute('data-theme', currentTheme)
|
|
|
|
}
|
2024-03-13 09:14:18 +01:00
|
|
|
|
|
|
|
if (switchButton) {
|
|
|
|
switchButton.addEventListener('click', switchTheme, false)
|
|
|
|
}
|
2022-02-18 12:40:24 +01:00
|
|
|
|
2024-03-13 09:14:18 +01:00
|
|
|
showContent()
|
|
|
|
})
|
2020-07-09 21:15:38 +02:00
|
|
|
|
2024-03-13 09:14:18 +01:00
|
|
|
function detectCurrentScheme() {
|
|
|
|
if (localStorage !== null && localStorage.getItem(STORAGE_KEY)) {
|
|
|
|
return localStorage.getItem(STORAGE_KEY)
|
2024-03-06 07:22:23 +01:00
|
|
|
}
|
2024-03-13 09:14:18 +01:00
|
|
|
if (defaultTheme) {
|
|
|
|
return defaultTheme
|
2024-03-06 07:22:23 +01:00
|
|
|
}
|
2024-03-13 09:14:18 +01:00
|
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
|
|
}
|
2022-02-18 12:40:24 +01:00
|
|
|
|
2024-03-13 09:14:18 +01:00
|
|
|
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 }}
|