What steps should I take to incorporate dark mode into my current SCSS and Angular configuration?

I am currently utilizing Angular 13 and SCSS in my project. My goal is to incorporate Dark Mode for the application, here is my existing setup.

_variables.scss

$body-color: #f0f0f0;

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

Throughout the application, it is referenced like this,

.header {
  background: var(--body-color) ;
}

The Dark Mode configurations are stored in the _theme.scss file.

_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,
    ),
  )
);

@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);

  transition: all 1s ease;
}

I have been struggling to set the body-color variable globally regardless of how I attempt it. Is there a method to define it once in the .dark-mode class and have it applied universally?

Answer №1

After much research and testing, I have finally come up with my preferred method:

styles.scss

...
@import "./assets/style_varients/themes";
...

_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

@mixin color-mixin() {
  $main-theme-variable: #262626 !global;

  //=========== Main Background Color ===========//
  background-color: #141515;

  //=========== Main Text Color ===========//
  color: #D6D6D6;
}

_light-theme.scss

@mixin color-mixin() {
  $main-theme-variable: #FFF !global;

  //=========== Main Background Color ===========//
  background-color: #f0f0f0;

  //=========== Main Text Color ===========//
  color: black;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure that a new window is opened while maintaining the exact location of the parent window

Having an issue with printing a calendar. The problem arises when trying to load CSS with absolute paths like /path/to/css.css. Opening a window with: var win = open('', '_blank') win.document.write(htmlContent) will display the HTML ...

How can I incorporate calc() with fit-content or a similar function?

Is there a way to set the height of a div to "fit-content minus 15px" without using JavaScript? I attempted to use calc(), but it did not yield the desired result. My objective is to dynamically adjust the height of an element containing multiple p tags, ...

Tips on automatically changing the background image every few seconds

As a newcomer to Angular and programming in general, I am facing an issue with changing the background image of my Page using the setInterval method. The intended behavior is for it to change every second, but for some reason, it changes much faster than t ...

Increase the size of a div to equal the combined width of two divs located directly below it

I need help aligning 3 divs so that the top div stretches to match the width of the bottom 2 divs combined. Here is an image showing the expected div positioning. I attempted to use display: table-row for the divs. <div id="main_div" style="display: ta ...

javascript, issues with floating and displaying elements

I am currently working on creating a JavaScript menu that has a click function to smoothly slide up and down. Although the JavaScript code appears to be functioning correctly, it seems that there is some interference with the CSS. Despite attempting vario ...

Is it possible to trigger a reflow prior to initiating a lengthy JavaScript operation?

Ready to face the criticism, I understand that this question has been asked many times before, and I am aware that there are likely more efficient ways to achieve what I'm trying to do... In a JavaScript function, I have a process that can take up to ...

Transforming html designs into pdf files using wkhtmltopdf tool

While attempting to convert three HTML files (body template, header template, footer template) to PDF using wkhtmltopdf, I encountered an error stating "Unknown long argument --header-htmlá". This issue arose after a recent PC update, as everything was fu ...

Is it possible to turn a <span> element into a clickable hyperlink?

Can a span be turned into a clickable link? I have successfully made a span with only a background image (using the Gilder/Levin image replacement technique) into a clickable link. This seems to work well on my desktop in Chrome, Opera, and IE 11. Is thi ...

Animating a CSS shape with the .animate method

I need help creating a dynamic graphic for my school project using css, jquery, and html. I want to make a rectangle that moves across the screen, but I'm having trouble getting it to work properly. Despite trying different variations of the animate f ...

CSS fails to function properly when the structure of the DOM in the HTML file is

.empty-space{ width: 30%; float: left; } .download-information{ width: 70%; float: left; } .download-thumbnail img { height: 30px; width: 30px; float: left; } .download-profile{ float: left; } <div class="download-cont ...

Angular: efficient exchange of information among components

I have a component X that handles a WebSocket. And within component X, I also have multiple presentation components (e.g. Y). Whenever the WebSocket receives a specific message, I need to perform an action in a particular component (e.g. refresh data). To ...

Sign up for the category that failed to assign a worth

Currently, I have a service that includes a getter and setter: // Service import { Subject } from 'rxjs'; export class MyService { public currentUser: any = new Subject(); ... there is a function where I call setCurrent and assign value ...

I am facing an issue where HTML is not loading images or some parts of my CSS in PHP

I've been working on connecting my website to a MySQL database using PHP. Initially, I thought I could keep the HTML and CSS the same, but after adding everything, the images on the website stopped showing up as they did before. The fonts and other el ...

Issue with Bootstrap navigation bar not displaying on iPad devices

Currently, I am working on creating a custom bootstrap menu that displays properly on an iPad screen size. I have implemented the standard navbar code from Bootstrap with a few tweaks. While it works well on smartphones and laptops, the issue arises when ...

Whenever I press the view button, all the cards open instead of just the one I chose from the index. Can anyone tell me where I am going wrong?

I've encountered an issue with the View buttons I added to display more detailed information about the characters in the card bodies. When I click on the view button, it opens all the boxes instead of the one I selected. I've tried using a for lo ...

Styling a component in Angular 2 based on a button click from another component

I have a button in one component that triggers a function: <button class="btn-main" (click)="hideElement()"></button> In another component, which is a popup and has no parent-child connection, I have three divs like this: <div class="ele ...

How can I align text vertically inside a button using HTML and CSS to ensure it is at the top?

Looking for a solution to a design issue I'm facing. You can view the situation here: jsfiddle <button type="submit"> <div class="icon"></div> </button> button { width: 250px; height: 250px; background: orange ...

Getting the CSS for an element's "Active" state using jQuery

Is there a way to retrieve the CSS for the :active state using jQuery? I am looking to create dynamic code so that I don't need to manually adjust the jQuery every time I want to style an element. Edit To clarify, I am trying to avoid using .addClas ...

Tips for converting a date string to a date object and then back to a string in the same format

I seem to be encountering an issue with dates (shocker!), and I could really use some assistance. Allow me to outline the steps I have been taking. Side note: The "datepipe" mentioned here is actually the DatePipe library from Angular. var date = new Dat ...

Modifying values in a reactive form does not function as expected for dropdown selections

I'm currently working on a project in Angular2 and I am facing an issue with setting the (change) event in a select element inside a reactive form. Unfortunately, it does not seem to be working. What steps should I take to resolve this? Below is the ...