Skip to content

Commit 4989aac

Browse files
committed
persist color-scheme pref to localStorage
1 parent 6de3350 commit 4989aac

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html lang="en" class="dark">
2+
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
55
<link rel="icon" href="/favicon.ico">

src/components/DarkModeToggle.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ let isDark = ref(html?.classList.contains('dark'))
2020
function toggleDark() {
2121
if (isDark.value) {
2222
html?.classList.remove('dark')
23-
isDark.value = false
2423
} else {
2524
html?.classList.add('dark')
26-
isDark.value = true
2725
}
26+
isDark.value = !isDark.value
27+
localStorage.setItem('color-scheme', isDark.value ? 'dark' : 'light')
2828
}
2929
</script>

src/main.ts

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import './tailwind.css'
44
import App from './demo/App.vue'
55
import VueTw from '.'
66

7+
const colorScheme = localStorage.getItem('color-scheme')
8+
if (colorScheme === 'dark') {
9+
document.querySelector('html')?.classList.add('dark')
10+
} else {
11+
document.querySelector('html')?.classList.remove('dark')
12+
}
13+
714
const router = createRouter({
815
history: createWebHistory(),
916
routes: [],

0 commit comments

Comments
 (0)