优化切换主题色

This commit is contained in:
不做码农 2022-04-29 13:00:22 +08:00
parent 6ee3008470
commit 742a37aac6
3 changed files with 38 additions and 23 deletions

View File

@ -6,22 +6,24 @@
<div class="setting-drawer-block-checbox"> <div class="setting-drawer-block-checbox">
<div class="setting-drawer-block-checbox-item" @click="handleTheme('theme-dark')"> <div class="setting-drawer-block-checbox-item" @click="handleTheme('theme-dark')">
<img src="@/assets/images/dark.svg" alt="dark" /> <img src="@/assets/images/dark.svg" alt="dark" />
<div v-if="sideTheme === 'theme-dark'" class="setting-drawer-block-checbox-selectIcon" style="display: block;"> <div v-if="sideTheme === 'theme-dark'" class="setting-drawer-block-checbox-selectIcon" style="display: block">
<i aria-label="图标: check" class="anticon anticon-check"> <i aria-label="图标: check" class="anticon anticon-check">
<svg viewBox="64 64 896 896" data-icon="check" width="1em" height="1em" :fill="theme" aria-hidden="true" focusable="false" class> <svg viewBox="64 64 896 896" data-icon="check" width="1em" height="1em" :fill="theme" aria-hidden="true" focusable="false" class>
<path <path
d="M912 190h-69.9c-9.8 0-19.1 4.5-25.1 12.2L404.7 724.5 207 474a32 32 0 0 0-25.1-12.2H112c-6.7 0-10.4 7.7-6.3 12.9l273.9 347c12.8 16.2 37.4 16.2 50.3 0l488.4-618.9c4.1-5.1.4-12.8-6.3-12.8z" /> d="M912 190h-69.9c-9.8 0-19.1 4.5-25.1 12.2L404.7 724.5 207 474a32 32 0 0 0-25.1-12.2H112c-6.7 0-10.4 7.7-6.3 12.9l273.9 347c12.8 16.2 37.4 16.2 50.3 0l488.4-618.9c4.1-5.1.4-12.8-6.3-12.8z"
/>
</svg> </svg>
</i> </i>
</div> </div>
</div> </div>
<div class="setting-drawer-block-checbox-item" @click="handleTheme('theme-light')"> <div class="setting-drawer-block-checbox-item" @click="handleTheme('theme-light')">
<img src="@/assets/images/light.svg" alt="light" /> <img src="@/assets/images/light.svg" alt="light" />
<div v-if="sideTheme === 'theme-light'" class="setting-drawer-block-checbox-selectIcon" style="display: block;"> <div v-if="sideTheme === 'theme-light'" class="setting-drawer-block-checbox-selectIcon" style="display: block">
<i aria-label="图标: check" class="anticon anticon-check"> <i aria-label="图标: check" class="anticon anticon-check">
<svg viewBox="64 64 896 896" data-icon="check" width="1em" height="1em" :fill="theme" aria-hidden="true" focusable="false" class> <svg viewBox="64 64 896 896" data-icon="check" width="1em" height="1em" :fill="theme" aria-hidden="true" focusable="false" class>
<path <path
d="M912 190h-69.9c-9.8 0-19.1 4.5-25.1 12.2L404.7 724.5 207 474a32 32 0 0 0-25.1-12.2H112c-6.7 0-10.4 7.7-6.3 12.9l273.9 347c12.8 16.2 37.4 16.2 50.3 0l488.4-618.9c4.1-5.1.4-12.8-6.3-12.8z" /> d="M912 190h-69.9c-9.8 0-19.1 4.5-25.1 12.2L404.7 724.5 207 474a32 32 0 0 0-25.1-12.2H112c-6.7 0-10.4 7.7-6.3 12.9l273.9 347c12.8 16.2 37.4 16.2 50.3 0l488.4-618.9c4.1-5.1.4-12.8-6.3-12.8z"
/>
</svg> </svg>
</i> </i>
</div> </div>
@ -77,7 +79,6 @@
<el-button type="primary" plain icon="DocumentAdd" @click="saveSetting">保存配置</el-button> <el-button type="primary" plain icon="DocumentAdd" @click="saveSetting">保存配置</el-button>
<el-button plain icon="Refresh" @click="resetSetting">重置配置</el-button> <el-button plain icon="Refresh" @click="resetSetting">重置配置</el-button>
</el-drawer> </el-drawer>
</template> </template>
<script setup> <script setup>
@ -85,6 +86,7 @@ import variables from '@/assets/styles/variables.module.scss'
import originElementPlus from 'element-plus/theme-chalk/index.css' import originElementPlus from 'element-plus/theme-chalk/index.css'
import { useDynamicTitle } from '@/utils/dynamicTitle' import { useDynamicTitle } from '@/utils/dynamicTitle'
import { getLightColor } from '@/utils/index'
const { proxy } = getCurrentInstance() const { proxy } = getCurrentInstance()
const store = useStore() const store = useStore()
@ -92,16 +94,7 @@ const showSettings = ref(false)
const theme = ref(store.state.settings.theme) const theme = ref(store.state.settings.theme)
const sideTheme = ref(store.state.settings.sideTheme) const sideTheme = ref(store.state.settings.sideTheme)
const storeSettings = computed(() => store.state.settings) const storeSettings = computed(() => store.state.settings)
const predefineColors = ref([ const predefineColors = ref(['#409EFF', '#ff4500', '#ff8c00', '#ffd700', '#90ee90', '#00ced1', '#1e90ff', '#c71585'])
'#409EFF',
'#ff4500',
'#ff8c00',
'#ffd700',
'#90ee90',
'#00ced1',
'#1e90ff',
'#c71585',
])
/** 是否需要topnav */ /** 是否需要topnav */
const topNav = computed({ const topNav = computed({
@ -159,13 +152,32 @@ const dynamicTitle = computed({
useDynamicTitle() useDynamicTitle()
}, },
}) })
//
watch(
() => theme,
(val) => {
themeChange(val.value)
},
{
immediate: true,
},
)
/**
* 改变主题颜色
*/
function themeChange(val) { function themeChange(val) {
store.dispatch('settings/changeSetting', { store.dispatch('settings/changeSetting', {
key: 'theme', key: 'theme',
value: val, value: val,
}) })
theme.value = val theme.value = val
// element-plus ui
document.documentElement.style.setProperty('--el-color-primary', val)
//
for (let i = 1; i <= 9; i++) {
document.documentElement.style.setProperty(`--el-color-primary-light-${i}`, `${getLightColor(val, i / 10)}`)
}
} }
function handleTheme(val) { function handleTheme(val) {
store.dispatch('settings/changeSetting', { store.dispatch('settings/changeSetting', {
@ -202,7 +214,7 @@ defineExpose({
}) })
</script> </script>
<style lang='scss' scoped> <style lang="scss" scoped>
.setting-drawer-title { .setting-drawer-title {
margin-bottom: 12px; margin-bottom: 12px;
color: rgba(0, 0, 0, 0.85); color: rgba(0, 0, 0, 0.85);

View File

@ -10,7 +10,11 @@ export default {
/** /**
* 侧边栏主题 深色主题theme-dark浅色主题theme-light * 侧边栏主题 深色主题theme-dark浅色主题theme-light
*/ */
sideTheme: 'theme-dark', sideTheme: 'theme-dark',
/**
* 框架主题颜色值
*/
theme: '#FF8C00',
/** /**
* 是否系统布局配置 * 是否系统布局配置
*/ */

View File

@ -1,12 +1,12 @@
import defaultSettings from '@/settings' import defaultSettings from '@/settings'
import { useDynamicTitle } from '@/utils/dynamicTitle' import { useDynamicTitle } from '@/utils/dynamicTitle'
const { sideTheme, showSettings, topNav, tagsView, fixedHeader, sidebarLogo, dynamicTitle } = defaultSettings const { sideTheme, theme, showSettings, topNav, tagsView, fixedHeader, sidebarLogo, dynamicTitle } = defaultSettings
const storageSetting = JSON.parse(localStorage.getItem('layout-setting')) || '' const storageSetting = JSON.parse(localStorage.getItem('layout-setting')) || ''
const state = { const state = {
title: '', title: '',
theme: storageSetting.theme || '#409EFF', theme: storageSetting.theme || theme,
sideTheme: storageSetting.sideTheme || sideTheme, sideTheme: storageSetting.sideTheme || sideTheme,
showSettings: showSettings, showSettings: showSettings,
topNav: storageSetting.topNav === undefined ? topNav : storageSetting.topNav, topNav: storageSetting.topNav === undefined ? topNav : storageSetting.topNav,
@ -41,4 +41,3 @@ export default {
mutations, mutations,
actions actions
} }