有没有办法通过SCSS将暗模式添加到我的应用程序中?
我的任务是在一个有角度的web应用程序上添加一个切换,允许用户从默认的灯光模式主题切换到黑暗模式主题。我找不到一种方法来成功实现这一点 当我得到任务时,styles目录中有一个有没有办法通过SCSS将暗模式添加到我的应用程序中?,css,angular,sass,Css,Angular,Sass,我的任务是在一个有角度的web应用程序上添加一个切换,允许用户从默认的灯光模式主题切换到黑暗模式主题。我找不到一种方法来成功实现这一点 当我得到任务时,styles目录中有一个\u variables.scss文件。其中包含颜色、字体、大小和间距的变量。颜色在贴图中,然后使用map-get()方法将每个阴影分配给一个变量,例如$shade-0:map-get($shades,'shade-0') 最初我认为我可以创建一个themes.scss文件,并将其与\u variables.scss一起导
\u variables.scss
文件。其中包含颜色、字体、大小和间距的变量。颜色在贴图中,然后使用map-get()
方法将每个阴影分配给一个变量,例如$shade-0:map-get($shades,'shade-0')
最初我认为我可以创建一个themes.scss
文件,并将其与\u variables.scss
一起导入。然后,该文件将链接到另外两个scss文件lightTheme.scss
和darkTheme.scss
。每个主题文件都包含一个颜色变量列表,与variables.scss
中的原始颜色变量列表相似。我可以让它为一个主题或其他工作,但我不能在主题文件之间切换
暗色
$shades: (
'shade-6': #f5f5f5,
'shade-5': #BDBDBD,
'shade-4': #9E9E9E,
'shade-3': #757575,
'shade-2': #616161,
'shade-1': #303437,
'shade-0': #404447,
);
$shade-0: map-get($shades, 'shade-0');
$shade-1: map-get($shades, 'shade-1');
$shade-2: map-get($shades, 'shade-2');
$shade-3: map-get($shades, 'shade-3');
$shade-4: map-get($shades, 'shade-4');
$shade-5: map-get($shades, 'shade-5');
$shade-6: map-get($shades, 'shade-6');
$colors: (
'forest': #239F28CC,
'aqua': #8ab4f8,
'ruby': #C93939CC,
'zing': #20CAC3CC,
'carrot': #E9853ECC,
'grape': #7542F2CC,
'midnight': #433F5CCC,
'slate': #657786CC,
);
$forest: map-get($colors, 'forest');
$aqua: map-get($colors, 'aqua');
$ruby: map-get($colors, 'ruby');
$zing: map-get($colors, 'zing');
$carrot: map-get($colors, 'carrot');
$grape: map-get($colors, 'grape');
$midnight: map-get($colors, 'midnight');
$slate: map-get($colors, 'slate');
$bg-color: map-get($shades, 'shade-1');
$border-color: map-get($shades, 'shade-2');
$border-dark-color: map-get($shades, 'shade-3');
$text-color: map-get($shades, 'shade-6');
$muted: map-get($colors, 'slate');
$subtle: map-get($shades, 'shade-4');
$colors: (
'forest': #239F28,
'aqua': #186EEF,
'ruby': #C93939,
'zing': #20CAC3,
'carrot': #E9853E,
'grape': #7542F2,
'midnight': #433F5C,
'slate': #657786,
);
$shades: (
'shade-0': #ffffff,
'shade-1': #f5f5f5,
'shade-2': #d8d8d8,
'shade-3': #bbbbbb,
'shade-4': #979797,
'shade-5': #535353,
'shade-6': #0c0c0c,
);
$shade-0: map-get($shades, 'shade-0');
$shade-1: map-get($shades, 'shade-1');
$shade-2: map-get($shades, 'shade-2');
$shade-3: map-get($shades, 'shade-3');
$shade-4: map-get($shades, 'shade-4');
$shade-5: map-get($shades, 'shade-5');
$shade-6: map-get($shades, 'shade-6');
$forest: map-get($colors, 'forest');
$aqua: map-get($colors, 'aqua');
$ruby: map-get($colors, 'ruby');
$zing: map-get($colors, 'zing');
$carrot: map-get($colors, 'carrot');
$grape: map-get($colors, 'grape');
$midnight: map-get($colors, 'midnight');
$slate: map-get($colors, 'slate');
$bg-color: map-get($shades, 'shade-1');
$border-color: map-get($shades, 'shade-2');
$border-dark-color: map-get($shades, 'shade-3');
$text-color: map-get($shades, 'shade-6');
$muted: map-get($colors, 'slate');
$subtle: map-get($shades, 'shade-4');
lightTheme.scss
$shades: (
'shade-6': #f5f5f5,
'shade-5': #BDBDBD,
'shade-4': #9E9E9E,
'shade-3': #757575,
'shade-2': #616161,
'shade-1': #303437,
'shade-0': #404447,
);
$shade-0: map-get($shades, 'shade-0');
$shade-1: map-get($shades, 'shade-1');
$shade-2: map-get($shades, 'shade-2');
$shade-3: map-get($shades, 'shade-3');
$shade-4: map-get($shades, 'shade-4');
$shade-5: map-get($shades, 'shade-5');
$shade-6: map-get($shades, 'shade-6');
$colors: (
'forest': #239F28CC,
'aqua': #8ab4f8,
'ruby': #C93939CC,
'zing': #20CAC3CC,
'carrot': #E9853ECC,
'grape': #7542F2CC,
'midnight': #433F5CCC,
'slate': #657786CC,
);
$forest: map-get($colors, 'forest');
$aqua: map-get($colors, 'aqua');
$ruby: map-get($colors, 'ruby');
$zing: map-get($colors, 'zing');
$carrot: map-get($colors, 'carrot');
$grape: map-get($colors, 'grape');
$midnight: map-get($colors, 'midnight');
$slate: map-get($colors, 'slate');
$bg-color: map-get($shades, 'shade-1');
$border-color: map-get($shades, 'shade-2');
$border-dark-color: map-get($shades, 'shade-3');
$text-color: map-get($shades, 'shade-6');
$muted: map-get($colors, 'slate');
$subtle: map-get($shades, 'shade-4');
$colors: (
'forest': #239F28,
'aqua': #186EEF,
'ruby': #C93939,
'zing': #20CAC3,
'carrot': #E9853E,
'grape': #7542F2,
'midnight': #433F5C,
'slate': #657786,
);
$shades: (
'shade-0': #ffffff,
'shade-1': #f5f5f5,
'shade-2': #d8d8d8,
'shade-3': #bbbbbb,
'shade-4': #979797,
'shade-5': #535353,
'shade-6': #0c0c0c,
);
$shade-0: map-get($shades, 'shade-0');
$shade-1: map-get($shades, 'shade-1');
$shade-2: map-get($shades, 'shade-2');
$shade-3: map-get($shades, 'shade-3');
$shade-4: map-get($shades, 'shade-4');
$shade-5: map-get($shades, 'shade-5');
$shade-6: map-get($shades, 'shade-6');
$forest: map-get($colors, 'forest');
$aqua: map-get($colors, 'aqua');
$ruby: map-get($colors, 'ruby');
$zing: map-get($colors, 'zing');
$carrot: map-get($colors, 'carrot');
$grape: map-get($colors, 'grape');
$midnight: map-get($colors, 'midnight');
$slate: map-get($colors, 'slate');
$bg-color: map-get($shades, 'shade-1');
$border-color: map-get($shades, 'shade-2');
$border-dark-color: map-get($shades, 'shade-3');
$text-color: map-get($shades, 'shade-6');
$muted: map-get($colors, 'slate');
$subtle: map-get($shades, 'shade-4');
themes.scss
@import 'global/lightTheme';
@import 'global/darkTheme';
我确实尝试将变量从scss变量更改为css变量,并将它们与var()
一起使用,但由于某些组件使用darken()
、lighte()
和mix()
而无法编译,因此我遇到了困难。有什么办法可以让它工作吗?我准备了一个
我根据应用程序容器的类定义颜色变量(.light
或.dark
)。简单地切换这些类就会改变站点的主题
请记住(全球92%)
.我在中号找到了这个,所以我想你可以看看
我们的想法是在html中查询一个body标记,然后为它设置类这个问题是一年前提出的,但对阅读本文的人仍然很有帮助,这里有一个更简单的解决方案 集锦
- Javascript仅用于切换根元素的类
- 您不必为每个元素定义单独的主题类
scss
文件中执行的所有操作是:
.content {
padding: 32px;
@include theme() {
color: theme-get('text-color');
background-color: theme-get('bg-color');
}
}
实施 您可以创建一个单独的文件,比如
themes.scss
,您可以在其中为两个主题定义属性:
$themes: (
darkTheme: (
'text-color': white,
'bg-color': #424242
),
lightTheme: (
'text-color': black,
'bg-color': #f5f5f5
)
);
使用mixin
:
@mixin theme() {
@each $theme, $map in $themes {
// $theme: darkTheme, lightTheme
// $map: ('text-color': ..., 'bg-color': ...)
// make the $map globally accessible, so that theme-get() can access it
$theme-map: $map !global;
// make a class for each theme using interpolation -> #{}
// use & for making the theme class ancestor of the class
// from which you use @include theme() {...}
.#{$theme} & {
@content; // the content inside @include theme() {...}
}
}
// no use of the variable $theme-map now
$theme-map: null !global;
}
现在,您可以使用mapget($theme-map,…)
访问主题的属性。但是我们可以通过定义一个函数来避免每次将$theme map
作为参数传递
@function theme-get($key) {
@return map-get($theme-map, $key);
}
生成的css
文件将是:
.content{
填充:32px;
}
.黑暗主题.内容{
颜色:白色;
背景色:#4242;
}
.lightTheme.内容{
颜色:黑色;
背景色:#F5;
}
例子
这是一个演示用的小提琴:
你可以导入有条件的样式,请检查这个链接:记住要考虑用户的预置,检查它。这真的很棒。Barthy,我所追求的已经足够接近了。这将解决我的大多数问题,除了我的应用程序中的某些组件,其中一种颜色变暗,例如背景色:变暗($bg color,8%)代码>。类似地,也使用了lighte()
和mix()
。我知道CSS变量和SCS不能很好地结合在一起,你有什么建议吗?是的,用与所有其他颜色相同的方式定义较暗的颜色,以后再使用它们!我将用一个例子来更新钢笔。@BenNewton你看到我在中所做的更新了吗?是的,我看到了。非常感谢你的帮助,真是太棒了!这是一个极好的解决方案!很有魅力,谢谢你!