Skip to content

Commit

Permalink
Fix dark mode switcher flash bug (#4167) (#4174)
Browse files Browse the repository at this point in the history
Ref: #4167
  • Loading branch information
NriotHrreion committed Oct 15, 2021
1 parent 245a7a9 commit f9b2cd0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions layouts/partials/html-head.hbs
Expand Up @@ -34,4 +34,6 @@
{{#each site.feeds}}
<link rel="alternate" href="/{{ ../site.locale }}/{{ link }}" title="{{ text }}" type="application/rss+xml">
{{/each}}

<script src="/static/js/themeSwitcher.js"></script>
</head>
7 changes: 0 additions & 7 deletions static/js/main.js
Expand Up @@ -48,13 +48,6 @@
;(function () {
const themeAttr = 'data-theme'
var darkThemeSwitcherElement = document.querySelector('.dark-theme-switcher')
var isInDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches

if (getTheme() == null) {
setTheme(isInDarkMode ? 'dark' : 'light')
} else {
setTheme(getTheme())
}

darkThemeSwitcherElement.addEventListener('click', function () {
var currentTheme = getTheme()
Expand Down
19 changes: 19 additions & 0 deletions static/js/themeSwitcher.js
@@ -0,0 +1,19 @@
;(function () {
const themeAttr = 'data-theme'
const isInDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches

if (getTheme() == null) {
setTheme(isInDarkMode ? 'dark' : 'light')
} else {
setTheme(getTheme())
}

function setTheme (theme) {
document.querySelector('html').setAttribute(themeAttr, theme)
window.localStorage.setItem('theme', theme)
}

function getTheme () {
return window.localStorage.getItem('theme')
}
})()

0 comments on commit f9b2cd0

Please sign in to comment.