Merge pull request #140 from Mitrichius/remove-flash-dark-theme/#139
Remove flash for dark theme on loading
This commit is contained in:
commit
ed55969059
@ -16,6 +16,23 @@
|
|||||||
{{ end -}}
|
{{ end -}}
|
||||||
|
|
||||||
{{ partial "favicons.html" . }}
|
{{ 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") }}
|
{{ partial "resource.html" (dict "context" . "type" "css" "filename" "css/main.css") }}
|
||||||
|
|
||||||
{{ if .Site.Params.copyCodeButton | default true }}
|
{{ if .Site.Params.copyCodeButton | default true }}
|
||||||
|
@ -7,67 +7,79 @@
|
|||||||
<button class="theme-switcher">
|
<button class="theme-switcher">
|
||||||
{{ i18n "darkTheme" }}
|
{{ i18n "darkTheme" }}
|
||||||
</button>
|
</button>
|
||||||
|
{{ 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.documentElement.setAttribute('data-theme', currentTheme)
|
||||||
changeButtonText()
|
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() {
|
if (switchButton) {
|
||||||
switchButton = document.querySelector('.theme-switcher')
|
|
||||||
currentTheme = detectCurrentScheme()
|
|
||||||
if (currentTheme == 'dark') {
|
|
||||||
document.documentElement.setAttribute('data-theme', 'dark')
|
|
||||||
}
|
|
||||||
if (currentTheme == 'auto') {
|
|
||||||
autoChangeScheme(autoDefinedScheme);
|
|
||||||
autoDefinedScheme.addListener(autoChangeScheme);
|
|
||||||
}
|
|
||||||
changeButtonText()
|
changeButtonText()
|
||||||
switchButton.addEventListener('click', switchTheme, false)
|
switchButton.addEventListener('click', switchTheme, false)
|
||||||
})
|
|
||||||
|
|
||||||
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'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showContent()
|
||||||
|
})
|
||||||
|
|
||||||
function changeButtonText()
|
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(switchButton)
|
||||||
|
{
|
||||||
|
if (switchButton) {
|
||||||
switchButton.textContent = currentTheme == 'dark' ? {{ i18n "lightTheme" }} : {{ i18n "darkTheme" }}
|
switchButton.textContent = currentTheme == 'dark' ? {{ i18n "lightTheme" }} : {{ i18n "darkTheme" }}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function switchTheme(e) {
|
function switchTheme(e) {
|
||||||
if (currentTheme == 'dark') {
|
if (currentTheme == 'dark') {
|
||||||
localStorage.setItem(STORAGE_KEY, 'light')
|
localStorage.setItem(STORAGE_KEY, 'light')
|
||||||
document.documentElement.setAttribute('data-theme', 'light')
|
document.documentElement.setAttribute('data-theme', 'light')
|
||||||
currentTheme = 'light'
|
currentTheme = 'light'
|
||||||
} else {
|
} else {
|
||||||
localStorage.setItem(STORAGE_KEY, 'dark')
|
localStorage.setItem(STORAGE_KEY, 'dark')
|
||||||
document.documentElement.setAttribute('data-theme', 'dark')
|
document.documentElement.setAttribute('data-theme', 'dark')
|
||||||
currentTheme = 'dark'
|
currentTheme = 'dark'
|
||||||
}
|
|
||||||
changeButtonText()
|
|
||||||
}
|
}
|
||||||
</script>
|
changeButtonText()
|
||||||
{{ end }}
|
}
|
||||||
|
|
||||||
|
function showContent() {
|
||||||
|
document.body.style.visibility = 'visible';
|
||||||
|
document.body.style.opacity = 1;
|
||||||
|
}
|
||||||
|
</script>
|
Loading…
Reference in New Issue
Block a user