Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Mitrichius/hugo-theme-anubis.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Kolosov <mitrichius@gmail.com>2022-02-18 14:40:24 +0300
committerDmitry Kolosov <mitrichius@gmail.com>2022-02-18 14:40:24 +0300
commit179923826fcf3af16a24e9d72e53e777cfbd8220 (patch)
treefb270b60efe1337aecedfc9f1ae5f43e7e749d07
parent5c94f0297f168387a5f89638137fa68b9776a2aa (diff)
Remove flash for dark theme on loading #139remove-flash-dark-theme/#139
-rw-r--r--layouts/partials/head.html17
-rw-r--r--layouts/partials/theme-switcher.html110
2 files changed, 78 insertions, 49 deletions
diff --git a/layouts/partials/head.html b/layouts/partials/head.html
index e62029c..4d5ff18 100644
--- a/layouts/partials/head.html
+++ b/layouts/partials/head.html
@@ -16,6 +16,23 @@
{{ end -}}
{{ partial "favicons.html" . }}
+
+<style>
+ body {
+ visibility: hidden;
+ opacity: 0;
+ }
+</style>
+
+<noscript>
+ <style>
+ body {
+ visibility: visible;
+ opacity: 1;
+ }
+ </style>
+</noscript>
+
{{ partial "resource.html" (dict "context" . "type" "css" "filename" "css/main.css") }}
{{ if .Site.Params.copyCodeButton | default true }}
diff --git a/layouts/partials/theme-switcher.html b/layouts/partials/theme-switcher.html
index 6ee5cef..f3eff3c 100644
--- a/layouts/partials/theme-switcher.html
+++ b/layouts/partials/theme-switcher.html
@@ -7,67 +7,79 @@
<button class="theme-switcher">
{{ i18n "darkTheme" }}
</button>
+{{ end }}
- <script>
- const STORAGE_KEY = 'user-color-scheme'
- const defaultTheme = {{ $style }}
+<script>
+const STORAGE_KEY = 'user-color-scheme'
+const defaultTheme = {{ $style }}
- let currentTheme
- let switchButton
- let autoDefinedScheme = window.matchMedia('(prefers-color-scheme: dark)')
+let currentTheme
+let switchButton
+let autoDefinedScheme = window.matchMedia('(prefers-color-scheme: dark)')
- const autoChangeScheme = e => {
- currentTheme = e.matches ? 'dark' : 'light'
- document.documentElement.setAttribute('data-theme', currentTheme)
- changeButtonText()
+const autoChangeScheme = e => {
+ currentTheme = e.matches ? 'dark' : 'light'
+ document.documentElement.setAttribute('data-theme', currentTheme)
+ changeButtonText()
+}
+
+document.addEventListener('DOMContentLoaded', function() {
+ switchButton = document.querySelector('.theme-switcher')
+ currentTheme = detectCurrentScheme()
+ if (currentTheme == 'dark') {
+ document.documentElement.setAttribute('data-theme', 'dark')
+ }
+ if (currentTheme == 'auto') {
+ autoChangeScheme(autoDefinedScheme);
+ autoDefinedScheme.addListener(autoChangeScheme);
}
- document.addEventListener('DOMContentLoaded', function() {
- switchButton = document.querySelector('.theme-switcher')
- currentTheme = detectCurrentScheme()
- if (currentTheme == 'dark') {
- document.documentElement.setAttribute('data-theme', 'dark')
- }
- if (currentTheme == 'auto') {
- autoChangeScheme(autoDefinedScheme);
- autoDefinedScheme.addListener(autoChangeScheme);
- }
+ if (switchButton) {
changeButtonText()
switchButton.addEventListener('click', switchTheme, false)
- })
+ }
+
+ showContent()
+})
- function detectCurrentScheme() {
- if (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'
- }
+function detectCurrentScheme() {
+ if (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 changeButtonText()
- {
+function changeButtonText(switchButton)
+{
+ if (switchButton) {
switchButton.textContent = currentTheme == 'dark' ? {{ i18n "lightTheme" }} : {{ i18n "darkTheme" }}
}
+}
- function switchTheme(e) {
- if (currentTheme == 'dark') {
- localStorage.setItem(STORAGE_KEY, 'light')
- document.documentElement.setAttribute('data-theme', 'light')
- currentTheme = 'light'
- } else {
- localStorage.setItem(STORAGE_KEY, 'dark')
- document.documentElement.setAttribute('data-theme', 'dark')
- currentTheme = 'dark'
- }
- changeButtonText()
+function switchTheme(e) {
+ if (currentTheme == 'dark') {
+ localStorage.setItem(STORAGE_KEY, 'light')
+ document.documentElement.setAttribute('data-theme', 'light')
+ currentTheme = 'light'
+ } else {
+ localStorage.setItem(STORAGE_KEY, 'dark')
+ document.documentElement.setAttribute('data-theme', 'dark')
+ currentTheme = 'dark'
}
- </script>
-{{ end }} \ No newline at end of file
+ changeButtonText()
+}
+
+function showContent() {
+ document.body.style.visibility = 'visible';
+ document.body.style.opacity = 1;
+}
+</script> \ No newline at end of file