feat: 面板主题色增加跟随浏览器 (#3107)

Refs #3075
pull/3114/head
ssongliu 2023-11-29 22:06:08 +08:00 committed by GitHub
parent fa9d855523
commit ed89f8fdd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 2 deletions

View File

@ -8,6 +8,10 @@ export const useTheme = () => {
const themeConfig = computed(() => globalStore.themeConfig);
const switchDark = () => {
if (themeConfig.value.theme === 'auto') {
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
themeConfig.value.theme = prefersDark.matches ? 'dark' : 'light';
}
const body = document.documentElement as HTMLElement;
if (themeConfig.value.theme === 'dark') body.setAttribute('class', 'dark');
else body.setAttribute('class', '');

View File

@ -1162,6 +1162,7 @@ const message = {
theme: 'Theme',
dark: 'Dark',
light: 'Light',
auto: 'Follow System',
language: 'Language',
languageHelper:
'By default, it follows the browser language. This parameter takes effect only on the current browser',

View File

@ -1106,6 +1106,7 @@ const message = {
componentSize: '',
dark: '',
light: '',
auto: '',
language: '',
languageHelper: '',
sessionTimeout: '',

View File

@ -1107,6 +1107,7 @@ const message = {
componentSize: '',
dark: '',
light: '',
auto: '',
language: '',
languageHelper: '',
sessionTimeout: '',

View File

@ -73,6 +73,15 @@ const loadDataFromDB = async () => {
switchDark();
};
const updateDarkMode = async (event: MediaQueryListEvent) => {
const res = await getSettingInfo();
if (res.data.theme !== 'auto') {
return;
}
globalStore.setThemeConfig({ ...themeConfig.value, theme: event.matches ? 'dark' : 'light' });
switchDark();
};
const loadStatus = async () => {
loading.value = globalStore.isLoading;
loadinText.value = globalStore.loadingText;
@ -101,6 +110,17 @@ onBeforeUnmount(() => {
onMounted(() => {
loadStatus();
loadDataFromDB();
const mqList = window.matchMedia('(prefers-color-scheme: dark)');
if (mqList.addEventListener) {
mqList.addEventListener('change', (e) => {
updateDarkMode(e);
});
} else if (mqList.addListener) {
mqList.addListener((e) => {
updateDarkMode(e);
});
}
});
</script>

View File

@ -29,13 +29,14 @@
<el-form-item :label="$t('setting.theme')" prop="theme">
<el-radio-group @change="onSave('Theme', form.theme)" v-model="form.theme">
<el-radio-button label="light">
<el-icon><Sunny /></el-icon>
<span>{{ $t('setting.light') }}</span>
</el-radio-button>
<el-radio-button label="dark">
<el-icon><Moon /></el-icon>
<span>{{ $t('setting.dark') }}</span>
</el-radio-button>
<el-radio-button label="auto">
<span>{{ $t('setting.auto') }}</span>
</el-radio-button>
</el-radio-group>
</el-form-item>