[英]How do I implement dark mode using my existing setup of SCSS and Angular?
I am using Angular 13 and SCSS.我正在使用 Angular 13 和 SCSS。 I want to implement Dark Mode for my application.
我想为我的应用程序实现深色模式。 This is my current setup.
这是我目前的设置。
_variables.scss _变量.scss
$body-color: #f0f0f0;
:root {
--body-color: #{$body-color};
}
And everywhere in the application it is called like this,在应用程序中到处都是这样调用的,
.header {
background: var(--body-color) ;
}
Dark Mode Settings are in the _theme.scss file.深色模式设置位于 _theme.scss 文件中。
_theme.scss _theme.scss
@use '@angular/material' as mat;
@use 'variables' as v;
@import "~@angular/material/theming";
@include mat.core();
$angular-primary: mat.define-palette(mat.$teal-palette, 500, 100, 900);
$angular-accent: mat.define-palette(mat.$pink-palette, A200, A100, A400);
$angular-warn: mat.define-palette(mat.$red-palette);
$angular-default-theme: mat.define-light-theme(
(
color: (
primary: $angular-primary,
accent: $angular-accent,
warn: $angular-warn,
),
)
);
@include mat.all-component-themes($angular-default-theme);
$angular-dark-theme: mat.define-dark-theme(
(
color: (
primary: $angular-primary,
accent: $angular-accent,
warn: $angular-warn,
),
)
);
// $body-color: var(--body-color);
@mixin theme-check($dark-checker:null) {
@if $dark-checker == true {
--body-color: #2c2c2c;
}
@else {
--body-color: #f0f0f0;
}
}
$body-color: null;
.dark-mode {
@include theme-check(true);
$body-color: #2c2c2c;
@include mat.all-component-colors($angular-dark-theme);
// background-color: #2c2c2c !important;
// color: #fafafa !important;
transition: all 1s ease;
}
Whichever way I try I can't seem to set the body-color
variable globally.无论我尝试哪种方式,我似乎都无法全局设置
body-color
变量。 Is there a way I can set it in the .dark-mode
class once and it will be applied globally?有没有一种方法可以在
.dark-mode
class 中设置一次并将其应用于全局?
Finally figured it out and this is the method I am using,终于弄明白了,这就是我正在使用的方法,
styles.scss styles.scss
...
@import "./assets/style_varients/themes";
...
_themes.scss _themes.scss
@use './dark-theme' as dark;
@use './light-theme' as light;
@mixin theme($theme) {
@if $theme == 'dark-mode' {
@include dark.color-mixin();
}
@else {
@include light.color-mixin();
}
}
.dark-mode {
@include dark.color-mixin();
transition: all 0.4s ease;
}
.light-mode {
@include light.color-mixin();
transition: all 0.4s ease;
}
:root {
.dark-mode {
--main-theme-variable: #{dark.$main-theme-variable};
}
.light-mode {
--main-theme-variable: #{light.$main-theme-variable};
}
}
_dark-theme.scss _dark-theme.scss
@mixin color-mixin() {
$main-theme-variable: #262626 !global;
//=========== Main Background Color ===========//
background-color: #141515;
//=========== Main Text Color ===========//
color: #D6D6D6;
}
_light-theme.scss _light-theme.scss
@mixin color-mixin() {
$main-theme-variable: #FFF !global;
//=========== Main Background Color ===========//
background-color: #f0f0f0;
//=========== Main Text Color ===========//
color: black;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.