mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
feat(projects): 集成naiveUI主题配置,将css vars添加至html
This commit is contained in:
40
src/store/modules/theme/helpers.ts
Normal file
40
src/store/modules/theme/helpers.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { colord } from 'colord';
|
||||
import { getColorPalette } from '@/utils';
|
||||
|
||||
type ColorType = 'primary' | 'info' | 'success' | 'warning' | 'error';
|
||||
|
||||
type ColorScene = '' | 'Suppl' | 'Hover' | 'Pressed' | 'Active';
|
||||
|
||||
type ColorKey = `${ColorType}Color${ColorScene}`;
|
||||
|
||||
type ThemeColor = {
|
||||
[key in ColorKey]?: string;
|
||||
};
|
||||
|
||||
interface ColorAction {
|
||||
scene: ColorScene;
|
||||
handler: (color: string) => string;
|
||||
}
|
||||
|
||||
/** 获取主题颜色的各种场景对应的颜色 */
|
||||
export function getThemeColors(colors: [ColorType, string][]) {
|
||||
const colorActions: ColorAction[] = [
|
||||
{ scene: '', handler: color => color },
|
||||
{ scene: 'Suppl', handler: color => color },
|
||||
{ scene: 'Hover', handler: color => getColorPalette(color, 5) },
|
||||
{ scene: 'Pressed', handler: color => getColorPalette(color, 7) },
|
||||
{ scene: 'Active', handler: color => colord(color).alpha(0.1).toHex() }
|
||||
];
|
||||
|
||||
const themeColor: ThemeColor = {};
|
||||
|
||||
colors.forEach(color => {
|
||||
colorActions.forEach(action => {
|
||||
const [colorType, colorValue] = color;
|
||||
const colorKey: ColorKey = `${colorType}Color${action.scene}`;
|
||||
themeColor[colorKey] = action.handler(colorValue);
|
||||
});
|
||||
});
|
||||
|
||||
return themeColor;
|
||||
}
|
@ -1,7 +1,87 @@
|
||||
import { ref, computed } from 'vue';
|
||||
import type { Ref, ComputedRef } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import { useThemeVars } from 'naive-ui';
|
||||
import type { GlobalThemeOverrides } from 'naive-ui';
|
||||
import { kebabCase } from 'lodash-es';
|
||||
import { getColorPalette } from '@/utils';
|
||||
import { getThemeColors } from './helpers';
|
||||
|
||||
// interface ThemeStore {
|
||||
// primary: string;
|
||||
// }
|
||||
interface OtherColor {
|
||||
/** 信息 */
|
||||
info: string;
|
||||
/** 成功 */
|
||||
success: string;
|
||||
/** 警告 */
|
||||
warning: string;
|
||||
/** 错误 */
|
||||
error: string;
|
||||
}
|
||||
|
||||
export const useThemeStore = defineStore('theme-store', () => {});
|
||||
interface ThemeStore {
|
||||
/** 主题颜色 */
|
||||
themeColor: Ref<string>;
|
||||
/** 其他颜色 */
|
||||
otherColor: ComputedRef<OtherColor>;
|
||||
/** naiveUI的主题配置 */
|
||||
naiveThemeOverrides: ComputedRef<GlobalThemeOverrides>;
|
||||
/** 添加css vars至html */
|
||||
addThemeCssVarsToRoot(): void;
|
||||
}
|
||||
|
||||
type ThemeVarsKeys = keyof Exclude<GlobalThemeOverrides['common'], undefined>;
|
||||
|
||||
export const useThemeStore = defineStore('theme-store', () => {
|
||||
const themeVars = useThemeVars();
|
||||
|
||||
const themeColor = ref('#1890ff');
|
||||
const otherColor = computed<OtherColor>(() => ({
|
||||
info: getColorPalette(themeColor.value, 7),
|
||||
success: '#52c41a',
|
||||
warning: '#faad14',
|
||||
error: '#f5222d'
|
||||
}));
|
||||
|
||||
const naiveThemeOverrides = computed<GlobalThemeOverrides>(() => {
|
||||
const { info, success, warning, error } = otherColor.value;
|
||||
const themeColors = getThemeColors([
|
||||
['primary', themeColor.value],
|
||||
['info', info],
|
||||
['success', success],
|
||||
['warning', warning],
|
||||
['error', error]
|
||||
]);
|
||||
|
||||
const colorLoading = themeColor.value;
|
||||
|
||||
return {
|
||||
common: {
|
||||
...themeColors
|
||||
},
|
||||
LoadingBar: {
|
||||
colorLoading
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
function addThemeCssVarsToRoot() {
|
||||
const updatedThemeVars = { ...themeVars.value };
|
||||
Object.assign(updatedThemeVars, naiveThemeOverrides.value.common);
|
||||
const keys = Object.keys(updatedThemeVars) as ThemeVarsKeys[];
|
||||
const style: string[] = [];
|
||||
keys.forEach(key => {
|
||||
style.push(`--${kebabCase(key)}: ${updatedThemeVars[key]}`);
|
||||
});
|
||||
const styleStr = style.join(';');
|
||||
document.documentElement.style.cssText += styleStr;
|
||||
}
|
||||
|
||||
const themeStore: ThemeStore = {
|
||||
themeColor,
|
||||
otherColor,
|
||||
naiveThemeOverrides,
|
||||
addThemeCssVarsToRoot
|
||||
};
|
||||
|
||||
return themeStore;
|
||||
});
|
||||
|
Reference in New Issue
Block a user