2020-12-23 13:03:40 -05:00
|
|
|
{{- $defaultColorScheme := default "auto" .Site.Params.colorScheme.default -}}
|
|
|
|
{{- if not (default false .Site.Params.colorScheme.toggle) -}}
|
|
|
|
{{/* If toggle is disabled, force default scheme */}}
|
|
|
|
<script>
|
|
|
|
(function() {
|
|
|
|
const colorSchemeKey = 'StackColorScheme';
|
|
|
|
localStorage.setItem(colorSchemeKey, "{{ $defaultColorScheme }}");
|
|
|
|
})();
|
|
|
|
</script>
|
|
|
|
{{- else -}}
|
|
|
|
{{/* Otherwise set to default scheme only if no preference is set by user */}}
|
|
|
|
<script>
|
|
|
|
(function() {
|
|
|
|
const colorSchemeKey = 'StackColorScheme';
|
|
|
|
if(!localStorage.getItem(colorSchemeKey)){
|
|
|
|
localStorage.setItem(colorSchemeKey, "{{ $defaultColorScheme }}");
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
</script>
|
|
|
|
{{- end -}}
|
|
|
|
|
|
|
|
<script>
|
|
|
|
(function() {
|
|
|
|
const colorSchemeKey = 'StackColorScheme';
|
|
|
|
const colorSchemeItem = localStorage.getItem(colorSchemeKey);
|
|
|
|
const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;
|
|
|
|
|
|
|
|
if (colorSchemeItem == 'dark' || colorSchemeItem === 'auto' && supportDarkMode) {
|
|
|
|
/**
|
|
|
|
* Enable dark mode if:
|
|
|
|
* 1. If dark mode is set already (in local storage)
|
|
|
|
* 2. Auto mode & prefere color scheme is dark
|
|
|
|
*/
|
2021-06-20 09:49:35 -04:00
|
|
|
document.documentElement.dataset.scheme = 'dark';
|
2020-12-23 13:03:40 -05:00
|
|
|
} else {
|
2021-06-20 09:49:35 -04:00
|
|
|
document.documentElement.dataset.scheme = 'light';
|
2020-12-23 13:03:40 -05:00
|
|
|
}
|
|
|
|
})();
|
2021-06-20 09:49:35 -04:00
|
|
|
</script>
|