fix: theme toggle error

This commit is contained in:
radishzzz 2025-01-23 12:46:00 +00:00
parent 60f42bd52a
commit 2f52b38100
2 changed files with 13 additions and 16 deletions

View file

@ -95,10 +95,19 @@ const { cdn, commentURL = '', imageHostURL = '', customGoogleAnalyticsURL = '',
<!-- Theme Toggle -->
<script is:inline define:vars={{ defaultMode: themeConfig.color.mode }}>
const systemTheme = matchMedia('(prefers-color-scheme: dark)')
const theme = localStorage.getItem('theme')
?? (systemTheme.matches !== null ? (systemTheme.matches ? 'dark' : 'light') : defaultMode)
function getTheme() {
const storedTheme = localStorage.getItem('theme')
if (storedTheme)
return storedTheme
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches
return systemTheme ? 'dark' : defaultMode
}
function updateTheme() {
const theme = localStorage.getItem('theme') || getTheme()
document.documentElement.classList.toggle('dark', theme === 'dark')
}
updateTheme()
document.addEventListener('astro:after-swap', updateTheme)
</script>
<!-- Google Analytics -->