forked from ishareme/vue3-front-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(project): add theme feature dark light system model
- Loading branch information
Showing
16 changed files
with
129 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export default { | ||
categorys: (state) => state.category.categorys, | ||
theme: (state) => state.theme.themeType, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { THEME_LIGHT } from '@/constants'; | ||
export default { | ||
namespaced: true, | ||
state: () => ({ | ||
themeType: THEME_LIGHT, | ||
}), | ||
mutations: { | ||
changeThemeType(state, newTheme) { | ||
state.themeType = newTheme; | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { watch } from 'vue'; | ||
import store from '../store'; | ||
import { THEME_LIGHT, THEME_DARK, THEME_SYSTEM } from '@/constants'; | ||
|
||
// 监听主题变更 | ||
let matchMedia; | ||
const watchSystemThemeChange = () => { | ||
// 仅需初始化一次即可 | ||
if (matchMedia) return; | ||
matchMedia = window.matchMedia('(prefers-color-scheme: dark)'); | ||
// 监听主题变更 | ||
matchMedia.onchange = () => { | ||
changeTheme(THEME_SYSTEM); | ||
}; | ||
}; | ||
|
||
const changeTheme = (theme) => { | ||
let themeClassName = ''; | ||
switch (theme) { | ||
case THEME_LIGHT: | ||
themeClassName = 'light'; | ||
break; | ||
case THEME_DARK: | ||
themeClassName = 'dark'; | ||
break; | ||
case THEME_SYSTEM: | ||
watchSystemThemeChange(); | ||
themeClassName = matchMedia.matches ? 'dark' : 'light'; | ||
break; | ||
} | ||
const html = document.querySelector('html'); | ||
html.classList = themeClassName; | ||
}; | ||
|
||
// 初始化主题 | ||
export default () => { | ||
watch( | ||
() => store.getters.theme, | ||
(val) => { | ||
changeTheme(val); | ||
}, | ||
{ | ||
// 初始执行一次 | ||
immediate: true, | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters