1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-02-13 12:00:08 +08:00
1Panel/frontend/src/hooks/use-theme.ts

49 lines
1.7 KiB
Go
Raw Normal View History

2022-08-16 23:30:23 +08:00
import { computed, onBeforeMount } from 'vue';
import { getLightColor, getDarkColor } from '@/utils/theme/tool';
import { GlobalStore } from '@/store';
2023-02-14 17:14:03 +08:00
import { MsgSuccess } from '@/utils/message';
2022-08-16 23:30:23 +08:00
export const useTheme = () => {
const globalStore = GlobalStore();
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';
}
2022-08-16 23:30:23 +08:00
const body = document.documentElement as HTMLElement;
if (themeConfig.value.theme === 'dark') body.setAttribute('class', 'dark');
2022-08-16 23:30:23 +08:00
else body.setAttribute('class', '');
};
const changePrimary = (val: string) => {
if (!val) {
val = '#409EFF';
2023-02-14 17:14:03 +08:00
MsgSuccess('主题颜色已重置为 #409EFF');
2022-08-16 23:30:23 +08:00
}
globalStore.setThemeConfig({ ...themeConfig.value, primary: val });
document.documentElement.style.setProperty(
'--el-color-primary-dark-2',
`${getDarkColor(themeConfig.value.primary, 0.1)}`,
);
document.documentElement.style.setProperty('--el-color-primary', themeConfig.value.primary);
for (let i = 1; i <= 9; i++) {
document.documentElement.style.setProperty(
`--el-color-primary-light-${i}`,
`${getLightColor(themeConfig.value.primary, i / 10)}`,
);
}
};
onBeforeMount(() => {
switchDark();
changePrimary(themeConfig.value.primary);
});
return {
switchDark,
changePrimary,
};
};