add color theme with dark and light mode!

This commit is contained in:
padreug 2025-02-02 18:44:05 +01:00
parent 08b505c145
commit 23081d8685
5 changed files with 184 additions and 127 deletions

View file

@ -0,0 +1,38 @@
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Moon, Sun } from 'lucide-vue-next'
import { Button } from '@/components/ui/button'
const isDark = ref(false)
const toggleTheme = () => {
isDark.value = !isDark.value
updateTheme()
}
const updateTheme = () => {
if (isDark.value) {
document.documentElement.classList.add('dark')
localStorage.setItem('theme', 'dark')
} else {
document.documentElement.classList.remove('dark')
localStorage.setItem('theme', 'light')
}
}
onMounted(() => {
const savedTheme = localStorage.getItem('theme')
isDark.value =
savedTheme === 'dark' ||
(!savedTheme && window.matchMedia('(prefers-color-scheme: dark)').matches)
updateTheme()
})
</script>
<template>
<Button variant="ghost" size="icon" @click="toggleTheme">
<Sun v-if="isDark" class="h-5 w-5" />
<Moon v-else class="h-5 w-5" />
<span class="sr-only">Toggle theme</span>
</Button>
</template>