Skip to content

Commit 6fbde22

Browse files
committed
fix(dark): storing a user's preference 🐛
1 parent bbc839b commit 6fbde22

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

src/App.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import { localStorage } from '@/utils/local-storage'
1111
import { useStore } from '@/stores'
1212
1313
const store = useStore()
14-
const res = localStorage.get('theme')
15-
const theme = ref<ConfigProviderTheme>(res)
14+
const theme = ref<ConfigProviderTheme>('light')
1615
const mode = computed(() => store.mode)
1716
1817
watch(mode, (val) => {
19-
if(val === 'dark') {
18+
if(val === 'dark' || localStorage.get('theme') === 'dark') {
2019
theme.value = 'dark'
2120
document.querySelector('html')
2221
.setAttribute('data-theme', 'dark')

src/stores/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineStore } from 'pinia'
33
export const useStore = defineStore({
44
id: 'index',
55
state: () => ({
6-
mode: 'light'
6+
// light || dark
7+
mode: ''
78
})
89
})

src/views/index.vue

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,18 @@ import { useStore } from '@/stores'
2525
import { localStorage } from '@/utils/local-storage'
2626
2727
const store = useStore()
28-
const res = localStorage.get('theme')
29-
const tem = res === 'dark' ? true: false
30-
const checked = ref<boolean>(tem)
28+
const themeStore = localStorage.get('theme')
29+
const checked = ref<boolean>(themeStore === 'dark' ? true: false)
3130
32-
watch(checked,() => {
33-
if(checked.value) {
31+
watch(checked,(val) => {
32+
if(val) {
3433
store.mode = 'dark'
35-
document.querySelector('html')
36-
.setAttribute('data-theme', 'dark')
37-
localStorage.set('theme', store.mode)
34+
localStorage.set('theme', 'dark')
3835
} else {
3936
store.mode = 'light'
40-
document.querySelector('html')
41-
.setAttribute('data-theme', 'light')
42-
localStorage.set('theme', store.mode)
37+
localStorage.set('theme', 'light')
4338
}
44-
}, { immediate: true })
39+
})
4540
</script>
4641

4742
<style lang="less" scoped>

0 commit comments

Comments
 (0)