DarkMode(5):深色模式不同实现方案切换

sass自定义函数转 sass预处理

在《DarkMode(2):深色模式解决方案——css颜色变量实现Dark Mode》与《DarkMode(3):深色模式解决方案——颜色反转与函数 》,如果使用

@mixin themeify {
  @each $theme-name, $theme-map in $themes {
    $theme-map: $theme-map !global;
    body[data-theme=#{$theme-name}] & {
      @content;
    }
  }
}

@function themed($key) {
  @return map-get($theme-map, $key);
}

这种方案写出来的样式代码,在改为普通模式,非常难搞。

不过推荐使用正则表达式的方式,去替换

正对第一个函数,替换的正则表达式如下:\@include themeify \{\n([\s\w\:\-\"\(\)\;\$\!]*)\}

const reg =/\@include themeify \{\n([\s\w\:\-\"\(\)\;\$\!]*)\}/
const reg = /@include themeify {\n([\s\w:\-"();$!]*)}/;

正则表达式匹配暗黑模式

替换为$1即可

第二个函数正则表达:themed\(\"([\w\-]*)\"\)\;

const reg = /themed\(\"([\w\-]*)\"\)\;/

替换为\$$1

sass变量主题输出切换为css变量主题输出

如果单纯sass变量输出两套主题,切换主题样式,需要刷新页面。如果是css变量,就无需刷新变量

目的无非就是想要输出:

:root {
  --primary-color: #{$primary-color};
}

如果之前直接是声明的,也没有啥好说的

$accent-color: #fbbc04;
:root {
  --accent-color-wrong: $accent-color;
  --accent-color-right: #{$accent-color};
}

其实还是声明一个函数,即可:

$var-element:'--';
$colors: ( 
    -black: #000,
    -blue: #088DC6
);
:root {
    @each $color in $color-variables {
     #{$var-element}#{nth($color, 1)}: #{nth($color, 2)};   
    }
}

如何在把读取 variable.scss 变量,并自动处理成css 变量文件,这个正在研究,等时间空点,再续

这个用sass或者less函数可以直接处理

如果是map 形势的赋值,直接操作

 

 

 

转载本站文章《DarkMode(5):深色模式不同实现方案切换》,
请注明出处:https://www.zhoulujun.cn/html/webfront/style/darkMode/8586.html

posted @ 2020-12-11 22:58  zhoulujun  阅读(1645)  评论(0编辑  收藏  举报