Skip to content

Commit

Permalink
improve DarkModeToggle behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Nov 1, 2023
1 parent e742032 commit ef36af1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/components/DarkModeToggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
<script setup lang="ts">
import { ref } from 'vue'
const html = typeof document != 'undefined' ? document.querySelector('html') : null
let isDark = ref(html?.classList.contains('dark'))
const html = typeof document != 'undefined' ? document.documentElement : null
const hasDarkClass = () => html?.classList.contains('dark') ? true : false
const isDark = ref(localStorage.getItem('color-scheme') == 'dark')
function toggleDark() {
if (isDark.value) {
if (hasDarkClass()) {
html?.classList.remove('dark')
} else {
html?.classList.add('dark')
}
isDark.value = !isDark.value
isDark.value = hasDarkClass()
localStorage.setItem('color-scheme', isDark.value ? 'dark' : 'light')
}
</script>

0 comments on commit ef36af1

Please sign in to comment.