Skip to content

Commit

Permalink
feat(DarkToggle): 页面主题跟随系统
Browse files Browse the repository at this point in the history
  • Loading branch information
yizhankui committed Feb 8, 2023
1 parent 5d42996 commit 732c22e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions frontend/components/Uno/DarkToggle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ const color = useColorMode()
function toggleDark() {
color.preference = color.value === 'dark' ? 'light' : 'dark'
}
if (process.client) {
const setDark = () => {
color.preference = color.value = 'dark'
}
const setLight = () => {
color.preference = color.value = 'light'
}
// 先获取media
const media = window.matchMedia('(prefers-color-scheme:dark)')
// 判断是否为暗主题
if (media.matches) {
// 匹配到暗主题
setDark()
}
else {
// 没有匹配到暗主题
setLight()
}
// 上面操作只会在页面加载时才会生效,因此,需要给media添加事件监听器
media.addEventListener('change', (e) => {
if (e.matches)
setDark()
else
setLight()
})
}
</script>

<template>
Expand Down

0 comments on commit 732c22e

Please sign in to comment.