当前位置:网站首页>Dynamic adjustment of web theme (2) Extraction
Dynamic adjustment of web theme (2) Extraction
2022-08-03 06:11:00 【ice breaker】

动态调整web主题(2) Extraction
自从写完 动态调整web系统主题? 看这一篇就够了! 这篇文章之后,I've always wanted to distill the ideas in this article,发布成一个 npm 包.
Just had time to write this week tailwind-css-variables-theme-generator, This package is a summary of the previous ideas,There are more detailed documentation.
Some improvements to previous ideas
原先在 之前那篇文章 里提到过 scss 与 js 通信的方法,主要依靠 webpack 的 :export 关键字,thereby exporting one js 对象.
There is now a more efficient solution.
我们知道 sass 是支持 CustomFunction 的, Then we can declare one js Function to extract what needs to be exposed sass 变量.
For example, we can write such a method:
// typescript
const exposeAarry: OrderedMap<SassString, SassColor>[] = []
'expose($map)': (args: Value[]) => {
const value = args[0].assertMap('map')
const map = value.contents as OrderedMap<SassString, SassColor>
exposeAarry.push(map)
return value
}
这样我们在 scss 里就可以这样写:
@use './constants.scss' as C;
$other-vars: (
// same name : override ↑ constants map
--color-canvas-default-transparent: rgba(32, 54, 85, 0),
--color-marketing-icon-primary: #053c74,
--color-custom-text-color: #546821
);
// 使用 expose 导出 SassMap
$expose: expose(C.$root-vars);
$expose1: expose($other-vars);
从而将 SassMap 转化为 Js Map 进行处理.
这比使用 Webpack The export is obviously more free,And a high degree of customization.
tailwind-css-variables-theme-generator 是什么?
This is a file generator,它以一个 scss 文件作为入口,Export the association exposed scss 变量,and use these variables,Generate configuration files through template rendering.
The configuration file is divided into 3 类:
tailwindcss v3扩展文件extendColors.jssass-loader附加文件variables.scss- Optional tool file
root.scss | export.scss | util.scss
where these files are used,在此不再叙述,Github文档上都有.
Example of the theme color switching of the preset scheme:
:root {
@each $var, $color in C.$root-vars {
#{$var}: Util.getRgbString($color);
}
}
[data-color-mode='light'] {
@each $var, $color in Light.$light-vars {
#{$var}: Util.getRgbString($color);
}
}
[data-color-mode='dark'] {
@each $var, $color in Dark.$dark-vars {
#{$var}: Util.getRgbString($color);
}
}
接下来,只要让 document.documentElement 的 data-color-mode 这个 Attribute It can be two-way binding with a value in the code,非常简单.(vue 可使用 vue-meta Bind directly to a value)
The backend controls dynamic switching
CSSStyleDeclaration.setProperty(property: string, value: string, priority?: string): void
可参考ThemeVariablesManager, It is a little bit of packaging.
Demo
Live Demo 右上角切换 Light / Dark 模式
边栏推荐
- 6. What is the difference between Vector, ArrayList and LinkedList?(design, performance, safety)
- MySql的Sql语句的练习(试试你能写出来几道呢)
- ZEMAX | 探索 OpticStudio中的序列模式
- 【第二周】卷积神经网络
- php连接数据库脚本
- opencv目标检测
- Delightful Nuxt3 Tutorial (2): Build a Blog Quickly and Easily
- 进程间通信IPC - 信号量
- 001_旭日X3派初探:开箱测试
- 设备树解析源码分析<devicetree>-1.基础结构
猜你喜欢

JS--正则表达式

ZEMAX | How to rotate any element around any point in space

KASLR-内核地址空间布局随机化

设备树解析源码分析<devicetree>-1.基础结构

关于芯片你了解吗?

ZEMAX | 如何使用ZOS-API创建自定义操作数

增强光学系统设计 | Zemax 全新 22.2 版本产品现已发布!

A.1#【内存管理】——1.1.3 page: struct page

自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det

全球一流医疗技术公司如何最大程度提高设计工作效率 | SOLIDWORKS 产品探索
随机推荐
page fault-页异常流程
【第三周】ResNet+ResNeXt
对象の使用
A.1#【内存管理】——1.1.2 zone: struct zone
八、抽象类的接口的区别
观看华为AI技术领域课程--深度学习前三章总结
自监督论文阅读笔记 S3Net:Self-supervised Self-ensembling Network for Semi-supervised RGB-D Salient Object Det
【七夕特效】 -- 满屏爱心
window下VS2022封装静态库以及调用静态库
MATLAB自带的dwt2和wavedec2函数实现基于小波变换的自适应阈值图像边缘检测
opencv目标检测
【第二周】卷积神经网络
稳压二极管的工作原理及稳压二极管使用电路图
三、final、finally、 finalize有什么不同?
block底层探索
What is parametric design, let's understand it through practical operation?| SOLIDWORKS How-To Videos
MMU 介绍-[TBL/page table work]
电子元器件的分类有哪些?
关于梯度下降法的一些优化方法
神经网络基础