Skip to content

Commit

Permalink
fix(DarkToggle): 对代码进行优化
Browse files Browse the repository at this point in the history
  • Loading branch information
yizhankui committed Feb 12, 2023
1 parent 8f0f3e8 commit f5a099e
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions frontend/components/Uno/DarkToggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const color = useColorMode()
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)
Expand All @@ -19,31 +20,35 @@ if (process.client) {
// 先获取media
const media = window.matchMedia('(prefers-color-scheme:dark)')
// 判断是否为暗主题
if (media.matches) {
// 如果是暗色主题则使用localStorage储存的上次使用的主题颜色
if (media.matches)
// 如果是暗色主题则使用localStorage储存的上次使用的主题颜色
setItem()
}
onMounted(() => {
// 上面操作只会在页面加载时才会生效,因此,需要给media添加事件监听器
media.addEventListener('change', (e) => {
if (e.matches)
setDark()
else
setLight()
})
// MediaQueryList的maches属性会返回媒体查询的结果
function handleColorChange(e: MediaQueryListEvent) {
if (e.matches)
setDark()
else
setLight()
}
window.addEventListener('storage', (e) => {
function handleStorageChange(e: StorageEvent) {
if (e.key === 'nuxt-color-mode' && typeof e.newValue === 'string') {
if (e.newValue === 'light')
setLight()
else
else if (e.newValue === 'dark')
setDark()
})
}
}
onMounted(() => {
media.addEventListener('change', handleColorChange)
window.addEventListener('storage', handleStorageChange)
})
onUnmounted(() => {
media.removeEventListener('change', (e) => {}, true)
window.removeEventListener('storage', (e) => {}, true)
media.removeEventListener('change', handleColorChange)
window.removeEventListener('storage', handleStorageChange)
})
}
</script>
Expand Down

0 comments on commit f5a099e

Please sign in to comment.