fix: el-button组件和其他部分使用到相关变量的组件无法适配主题色变化问题
This commit is contained in:
parent
16b93757d3
commit
00cac6a831
|
@ -1,6 +1,7 @@
|
||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { store } from '../index'
|
import { store } from '../index'
|
||||||
import { setCssVar, humpToUnderline } from '@/utils'
|
import { setCssVar, humpToUnderline } from '@/utils'
|
||||||
|
import { mix } from '@/utils/color'
|
||||||
import { ElMessage, ComponentSize } from 'element-plus'
|
import { ElMessage, ComponentSize } from 'element-plus'
|
||||||
|
|
||||||
interface AppState {
|
interface AppState {
|
||||||
|
@ -239,6 +240,7 @@ export const useAppStore = defineStore('app', {
|
||||||
document.documentElement.classList.add('light')
|
document.documentElement.classList.add('light')
|
||||||
document.documentElement.classList.remove('dark')
|
document.documentElement.classList.remove('dark')
|
||||||
}
|
}
|
||||||
|
this.setPrimaryLight()
|
||||||
},
|
},
|
||||||
setCurrentSize(currentSize: ComponentSize) {
|
setCurrentSize(currentSize: ComponentSize) {
|
||||||
this.currentSize = currentSize
|
this.currentSize = currentSize
|
||||||
|
@ -253,9 +255,21 @@ export const useAppStore = defineStore('app', {
|
||||||
for (const key in this.theme) {
|
for (const key in this.theme) {
|
||||||
setCssVar(`--${humpToUnderline(key)}`, this.theme[key])
|
setCssVar(`--${humpToUnderline(key)}`, this.theme[key])
|
||||||
}
|
}
|
||||||
|
this.setPrimaryLight()
|
||||||
},
|
},
|
||||||
setFooter(footer: boolean) {
|
setFooter(footer: boolean) {
|
||||||
this.footer = footer
|
this.footer = footer
|
||||||
|
},
|
||||||
|
setPrimaryLight() {
|
||||||
|
if (this.theme.elColorPrimary) {
|
||||||
|
const elColorPrimary = this.theme.elColorPrimary
|
||||||
|
const color = this.isDark ? '#000000' : '#ffffff'
|
||||||
|
const lightList = [3, 5, 7, 8, 9]
|
||||||
|
lightList.forEach((v) => {
|
||||||
|
setCssVar(`--el-color-primary-light-${v}`, mix(color, elColorPrimary, v / 10))
|
||||||
|
})
|
||||||
|
setCssVar(`--el-color-primary-dark-2`, mix(color, elColorPrimary, 0.2))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
persist: true
|
persist: true
|
||||||
|
|
|
@ -151,3 +151,22 @@ const subtractLight = (color: string, amount: number) => {
|
||||||
const c = cc < 0 ? 0 : cc
|
const c = cc < 0 ? 0 : cc
|
||||||
return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`
|
return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mixes two colors.
|
||||||
|
*
|
||||||
|
* @param {string} color1 - The first color, should be a 6-digit hexadecimal color code starting with `#`.
|
||||||
|
* @param {string} color2 - The second color, should be a 6-digit hexadecimal color code starting with `#`.
|
||||||
|
* @param {number} [weight=0.5] - The weight of color1 in the mix, should be a number between 0 and 1, where 0 represents 100% of color2, and 1 represents 100% of color1.
|
||||||
|
* @returns {string} The mixed color, a 6-digit hexadecimal color code starting with `#`.
|
||||||
|
*/
|
||||||
|
export const mix = (color1: string, color2: string, weight: number = 0.5): string => {
|
||||||
|
let color = '#'
|
||||||
|
for (let i = 0; i <= 2; i++) {
|
||||||
|
const c1 = parseInt(color1.substring(1 + i * 2, 3 + i * 2), 16)
|
||||||
|
const c2 = parseInt(color2.substring(1 + i * 2, 3 + i * 2), 16)
|
||||||
|
const c = Math.round(c1 * weight + c2 * (1 - weight))
|
||||||
|
color += c.toString(16).padStart(2, '0')
|
||||||
|
}
|
||||||
|
return color
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue