Skip to content

Commit

Permalink
fix(DarkToggle): 解决了两个页面主题不同步的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yizhankui committed Feb 11, 2023
1 parent 34c6afc commit 8abc41f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions frontend/components/Uno/DarkToggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ function toggleDark() {
color.preference = color.value === 'dark' ? 'light' : 'dark'
}
if (process.client) {
const item = localStorage.getItem('nuxt-color-mode') || 'dark'
localStorage.setItem('nuxt-color-mode', item)
const setDark = () => {
color.preference = color.value = 'dark'
}
const setLight = () => {
color.preference = color.value = 'light'
}
const setItem = () => {
color.preference = color.value = item
}
// 先获取media
const media = window.matchMedia('(prefers-color-scheme:dark)')
// 判断是否为暗主题
if (media.matches) {
// 匹配到暗主题
setDark()
}
else {
// 没有匹配到暗主题
setLight()
// 如果是暗色主题则使用localStorage储存的上次使用的主题颜色
setItem()
}
// 上面操作只会在页面加载时才会生效,因此,需要给media添加事件监听器
media.addEventListener('change', (e) => {
if (e.matches)
Expand Down

0 comments on commit 8abc41f

Please sign in to comment.