当前位置: 首页 > ds >正文

element-plus主题换色

提示:暂未完善全,仅仅提供思路
在这里插入图片描述

主题方式是通过切换主题的方式实现换色,例如blue、green,不推荐,仅参考逻辑
原因:
1.不好更改element-plus的默认css变量
2.对于我的项目(基于element-plus封装了公司组件库,且引入项目使用),无法将修改好的element-plus的css变量渗透到公司的组件库

推荐使用该链接方式实现换色


文章目录

  • 一、步骤

一、步骤

1.src/style/element.scss

// index.scss
@forward "element-plus/theme-chalk/src/common/var.scss" with ($colors: ("white": #ffffff,"black": #000000,"primary": ("base": #007d7b),"success": ("base": #8bc34a),"warning": ("base": #ffc107),"danger": ("base": #f56c6c),"error": ("base": #ff5722),"info": ("base": #909399))
);
// @use 'element-plus/theme-chalk/dark/css-vars.css'

2.多主题配置
src/style/theme_index.scss

// 基础颜色变量(保留原始配置)
$color-map: (primary: #409eff,success: #67c23a,warning: #e6a23c,danger: #f56c6c,info: #909399,text-primary: #303133,text-regular: #606266,text-secondary: #909399,text-placeholder: #c0c4cc,border-base: #dcdfe6,border-light: #e4e7ed,border-lighter: #ebeef5,border-extra-light: #f2f6fc,background-base: #f5f7fa
);// 基础2
$color-map2: (button-hover-bg-color: red
);// 自定义变量模板(新增)
$custom-vars-template: (chart: (color-1: red,color-2: red)
);// 主题扩展(保留原始+新增自定义)
$themes: (blue: (// 原始Element变量primary: #2e72b1,success: #2bc667,warning: #ffb800,danger: #ff5722,info: #9e9e9e,text-primary: #1f2d3d,text-regular: #324057,border-radius-base: 4px,box-shadow-light: 0 2px 12px 0 rgba(46, 114, 177, 0.1),// 新增自定义变量custom:map-merge($custom-vars-template,(special: (shadow-opacity: blue,highlight-color: #26c6da),my: (color1: #1fff)))),green: (primary: #007d7b,success: #8bc34a,warning: #ffc107,danger: #f44336,info: #607d8b,text-primary: #263238,text-regular: #455a64,text-secondary: #78909c,text-placeholder: #b0bec5,// 绿色主题特有调整border-radius-base: 6px,box-shadow-light: 0 2px 12px 0 rgba(0, 125, 123, 0.1),// 新增自定义变量custom:map-merge($custom-vars-template,(special: (highlight-color: #26c6da),my: (color1: #f1ff))))
);// 生成CSS变量(保留所有功能)
:root {@each $theme, $config in $themes {&[data-theme="#{$theme}"] {// 1. 输出原始Element变量@each $key, $value in map-merge($color-map, $config) {@if $key != "custom" {--el-color-#{$key}: #{$value};}}// 解决不是--el-color的颜色@each $key, $value in map-merge($color-map2, $config) {@if $key != "custom" {--el-#{$key}: #{$value};}}// 2. 保留衍生颜色计算// --el-color-primary-light-3: mix(#f1ff, map-get($config, primary), 30%);// --el-color-primary-light-5: mix(#f1ff, map-get($config, primary), 50%);// --el-color-primary-light-7: mix(#f1ff, map-get($config, primary), 70%);// --el-color-primary-light-9: mix(#f1ff, map-get($config, primary), 90%);// 3. 保留组件变量--el-border-radius-base: #{map-get($config, border-radius-base)};--el-box-shadow-light: #{map-get($config, box-shadow-light)};// 4. 新增自定义变量输出@each $category, $vars in map-get($config, "custom") {@each $name, $value in $vars {@if $value != null {--app-#{$category}-#{$name}: #{$value};}}}}}
}

3.构建配置
vite.config.ts 配置themes和additionalData

 { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'export default defineConfig({plugins: [vue(),Components({dts: false, // 关闭自动生成 components.d.tsdirs: ["src/components"],resolvers: [ElementPlusResolver({importStyle: "sass",themes: ['light', 'dark'] // 关键配置})]}),],css: {preprocessorOptions: {scss: {additionalData: `@use "@/style/element.scss" as *; @use "@/style/theme_index.scss" as *;`}}}
})

4.‌主样式入口文件‌
通常在src/styles/main.scss中引入主题配置:

@use "element-plus/theme-chalk/src/index.scss";
@use "./theme_index.scss" as theme;

5.‌切换控制器
/src/utils/theme.js

export function switchTheme(themeName) {// console.log('%c【' + 'themeName' + '】打印', 'color:#fff;background:#0f0', themeName)// const styleEl = document.createElement('style')// styleEl.id = 'theme-style'// styleEl.textContent = `:root { @include theme-vars(${themeName}); }`// console.log(styleEl.textContent);// const oldStyle = document.getElementById('theme-style')// oldStyle ? document.head.replaceChild(styleEl, oldStyle)//   : document.head.appendChild(styleEl)// console.log('%c【' + 'oldStyle' + '】打印', 'color:#fff;background:#0f0', oldStyle)document.documentElement.setAttribute('data-theme', themeName)
}

6.‌主题切换组件调用‌
在组件中使用切换逻辑:

 <el-button @click="toggleTheme0">{{ currentTheme === "light" ? "暗色模式" : "亮色模式" }}</el-button>import { switchTheme } from '../utils/theme'let themValue = ref('blue')
// 函数
const toggleTheme0 = () => {switchTheme(themValue.value === 'blue' ? 'green' : 'blue') // 调用主题切换方法themValue.value = themValue.value === 'blue' ? 'green' : 'blue'
}

7.main.js初始化主题

document.documentElement.setAttribute('data-theme', 'blue')
http://www.xdnf.cn/news/9613.html

相关文章:

  • React-native的新架构
  • unity一个箭矢的轨迹
  • 湖北理元理律师事务所:债务优化中的“生活锚点”设计
  • AI 让无人机跟踪更精准——从视觉感知到智能预测
  • HTML实战:响应式个人资料页面
  • 每日Prompt:心中的佛
  • 操作系统学习(一)——操作系统基础
  • 数据库管理与高可用-MySQL数据库操作
  • Prometheus学习之pushgateway和altermanager组件
  • Linux的SHELL脚本基础
  • docker-记录一次容器日志<container_id>-json.log超大问题的处理
  • opencv + jpeg_turbo(启用SIMD加速)
  • Flutter3.22适配运行鸿蒙系统问题记录
  • 算力卡上部署OCR文本识别服务与测试
  • w~视觉~合集6
  • 【组件】跳动的图标 动画
  • 实验设计与分析(第6版,Montgomery)第4章随机化区组,拉丁方, 及有关设计4.5节思考题4.1~4.4 R语言解题
  • GRIT:让AI“指着图说话“的新思路
  • get_rga_thread线程和low_camera_venc_thread线程获取低分辨率VENC码流数据
  • ORB-SLAM2学习笔记:ComputeKeyPointsOctTree分析过程记录
  • 【C语言】详解 指针
  • 使用 PySpark 从 Kafka 读取数据流并处理为表
  • 【25软考网工】第九章 网络管理(1)网络管理基础、SNMP
  • 端到端测试最佳实践:从入门到精通的完整指南
  • vue+ts+TinyEditor 是基于 Quill 2.0 开发的富文本编辑器,提供丰富的扩展功能,适用于现代 Web 开发的完整安装使用教程
  • 集成电路制造设备防震基座选型指南:为稳定护航-江苏泊苏系统集成有限公司
  • 手机如何压缩文件为 RAR 格式:详细教程与工具推荐
  • 井喷式增长下的证件缺口:特种设备人才供需矛盾如何破局?
  • 数值积分实验
  • 深入理解计算机科学中的“递归”:原理、应用与优化