suyuan/admin/src/utils/theme.ts

75 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-11-23 14:33:56 +08:00
import colors from 'css-color-function'
const lightConfig = {
'dark-2': 'shade(20%)',
'light-3': 'tint(30%)',
'light-5': 'tint(50%)',
'light-7': 'tint(70%)',
'light-8': 'tint(80%)',
'light-9': 'tint(90%)'
}
const darkConfig = {
'light-3': 'shade(20%)',
'light-5': 'shade(30%)',
'light-7': 'shade(50%)',
'light-8': 'shade(60%)',
'light-9': 'shade(70%)',
'dark-2': 'tint(20%)'
}
const themeId = 'theme-vars'
/**
* @author Jason
* @description elementui主题的行为变量
* primarysuccesswarningdangererrorinfo
*/
export const generateVars = (color: string, type = 'primary', isDark = false) => {
const colos = {
[`--el-color-${type}`]: color
}
const config: Record<string, string> = isDark ? darkConfig : lightConfig
for (const key in config) {
colos[`--el-color-${type}-${key}`] = `color(${color} ${config[key]})`
}
return colos
}
/**
* @author Jason
* @description css变量
* @param key css变量key --color-primary
* @param value css变量值 #f40
* @param dom dom元素
*/
export const setCssVar = (key: string, value: string, dom = document.documentElement) => {
dom.style.setProperty(key, value)
}
/**
* @author Jason
* @description
*/
export const setTheme = (options: Record<string, string>, isDark = false) => {
const varsMap: Record<string, string> = Object.keys(options).reduce((prev, key) => {
return Object.assign(prev, generateVars(options[key], key, isDark))
}, {})
let theme = Object.keys(varsMap).reduce((prev, key) => {
const color = colors.convert(varsMap[key])
return `${prev}${key}:${color};`
}, '')
theme = `:root{${theme}}`
let style = document.getElementById(themeId)
if (style) {
style.innerHTML = theme
return
}
style = document.createElement('style')
style.setAttribute('type', 'text/css')
style.setAttribute('id', themeId)
style.innerHTML = theme
document.head.append(style)
}