-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(projects): 集成naiveUI主题配置,将css vars添加至html
- Loading branch information
1 parent
0d2a562
commit 2c19684
Showing
12 changed files
with
297 additions
and
88 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 +1,19 @@ | ||
<template> | ||
<router-view /> | ||
<n-config-provider :theme-overrides="theme.naiveThemeOverrides"> | ||
<router-view /> | ||
</n-config-provider> | ||
</template> | ||
|
||
<script setup lang="ts"></script> | ||
<script setup lang="ts"> | ||
import { onMounted } from 'vue'; | ||
import { NConfigProvider } from 'naive-ui'; | ||
import { useThemeStore } from '@/store'; | ||
const theme = useThemeStore(); | ||
const { addThemeCssVarsToRoot } = useThemeStore(); | ||
onMounted(() => { | ||
addThemeCssVarsToRoot(); | ||
}); | ||
</script> | ||
<style scoped></style> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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; | ||
} |
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,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; | ||
}); |
Oops, something went wrong.