Tips for updating sass import files as the body class changes

Is there a way to dynamically change the SCSS file with color variables based on the changing body class?

For example, if my HTML includes:

<body class="custom"> ... </body>

I would like to load in style.scss:

@import 'custom';

And if I have:

<body class="dark-mode"> ... </body>  

I want to load in style.scss:

@import 'dark-mode';

Answer №1

It's not possible to use an @import based on a condition, but there are many alternative methods you can try out. Below is a simple framework I created in the past.

@function keyFilter($iKey, $fKey, $rKey) {
  @if ($iKey == $fKey) {
    @return $rKey;
  }
  @return $iKey;
}

@function v($key) {
  @return var(--#{$key});
}

//

$modes: (
  "&": (
    "color": #000,
  ),
  "dark": (
    "color": #fff,
  ),
);

//

@each $key, $map in $modes {
  body#{keyFilter("[#{$key}]", "[&]", null)} {
    @each $key, $value in $map {
      --#{$key}: #{$value};
    }
  }
}

To add a new mode, simply include another map within the $modes variable. You can create as many modes as needed. Remember that the "&" mode represents the default mode.

$modes: (
  "&": (
    //...
  ),
  "dark": (
    //...
  ),
  //...
);

If you want to add a new variable depending on the mode, just specify the key and value for that specific mode.

$modes: (
  "&": (
    "color": #000,
    "bgc": #fff,
    "bgc-contrast": #eee,
    //...
  ),
  "dark": (
    "color": #fff,
    "bgc": #000,
    "bgc-contrast": #424242,
    //...
  ),
);

To retrieve a variable, use the v($key) function.

body {
  color: v(color);
  background-color: v(bgc);
}

div.contrasted {
  background-color: v(bgc-contrast);
}

This will compile into:

body {
  --color: #000;
  --bgc: #fff;
  --bgc-contrast: #eee;
}

body[dark] {
  --color: #fff;
  --bgc: #000;
  --bgc-contrast: #424242;
}

body {
  color: var(--color);
  background-color: var(--bgc);
}

div.contrasted {
  background-color: var(--bgc-contrast);
}

Note: It's not necessary to define every variable for each mode. If a variable is missing for a particular mode, it won't cause an error.

For example, this is perfectly acceptable:

$modes: (
  "&": (
    //...
  ),
  "dark": (
    "color": #fff,
    "bgc": #000,
    "bgc-contrast": #424242,
    //...
  );

//...

body {
  color: v(color);
  background-color: v(bgc);
}

div.contrasted {
  background-color: v(bgc-contrast);
}

...will work without any issues.

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

Creating a responsive web design with a user-friendly CSS grid layout

Currently, I am delving into the realm of CSS grid to better comprehend its functionality. In my layout design, there are two menus (colored in red) positioned on either side of a central main content box (colored in blue). As the screen size reduces and ...

Combining various functions into a single button

I am currently working on creating a full-screen menu that functions like a modal. Everything seems to be working fine, except for the fadeOut animation. Can someone please help me understand what is causing issues with my scripts/codes? I want the content ...

Tips for deciding on the appropriate CSS for an Angular 6 navbar component

I am currently working on an angular 6 application where users are assigned different roles that require distinct styling. Role 1 uses Stylesheet 1, while Role 2 uses Stylesheet 2. The Navbar component is a crucial part of the overall layout structure of ...

Adjusting the size of images in real time

Currently, I am in the process of creating a Fluid grid style website and I am looking to decrease the size of all images by 75% when the screen is below 1280px, 50% at 800px, and so forth. Is there a way to achieve this using the IMG tag? My attempt so ...

Tips for preventing a React component from scrolling beyond the top of the page

I am looking to achieve a specific behavior with one of my react components when a user scrolls down a page. I want the component to reach the top of the page and stay there without moving any further up. Here is an Imgur link to the 'intranet' ...

ReactJS is having trouble with inline animation styles

After successfully implementing a timer using CSS with animation, everything was working as expected. However, I encountered an issue when trying to convert it into JSX inline styles in React. The animation stopped working as smoothly as before due to the ...

Problem with input field borders in Firefox when displayed within table cells

Demo When clicking on any cell in the table within the JSFiddle using Firefox, you may notice that the bottom and right borders are hidden. Is there a clever solution to overcome this issue? I have experimented with a few approaches but none of them work ...

Android Font Icon Spacing Issue with IconFont (Fontastic)

After using Fontastic.me to create an iconfont, I encountered an issue specifically with the native browser of Android 4.2.2 and 4.3 (such as modern Samsung tablets). In these browsers, the characters in the entire font appear to have no width. This proble ...

NX monorepo: project utilizes the source files of the library rather than using the files from the distribution folder

Here is the issue I'm facing: I am using NX for a monorepo. Within this setup, there is an application built in AngularJS (using webpack) which utilizes a library that generates web components (built with React). import { test } from "@lib/someli ...

What steps should I take to create a stylish HTML profile with well-organized CSS?

Looking for a way to display photos, biographies, personal details, and more on a webpage? We've got you covered! You can customize the layout using semantic HTML and CSS styles. Build your design online at http://jsfiddle.net/ and thank us later! &l ...

Tips for optimizing Slider Code to showcase an expanded selection of slides

I am currently troubleshooting a slider code on my ASP.NET website. After examining the HTML, JS, and CSS from the page theme, I noticed that only two slides are being displayed. However, when attempting to add a new slider HTML and adjusting the CSS for t ...

Show button once modal has been closed

I have a 'start' button that triggers a modal window with an embedded video. Once the user closes the modal, I want to show a 'redeem' button after a delay of 6 seconds. HTML: <a id="video-start" onclick="openVideoModal()"><i ...

Executing cssnano Using the Command Prompt

Although I'm not an expert in node JS, I have come across cssnano as a JavaScript tool for minifying CSS, which apparently does a more sophisticated job compared to the outdated YUI compressor. My only issue is that I am struggling to understand how t ...

Place a pair of divs in the center next to one another

What is the best way to align two divs in the center beside each other with some padding in between that actually works? I attempted using display: inline, but it didn't have the desired effect. .my-container { position: rela ...

Customizing ExtJS Themes for Ext.panel.Panel

In my current project using ExtJS 4.1.1a, I am working on creating a new theme for a "tabbedPane" and a normal Panel with an "Accordion" layout. However, I am facing difficulty in changing the color of the headers for both elements. I wish to customize the ...

Struggling to find a solution for directing to the featured homes page without the content overlapping with my navbar and search component. Any assistance would be greatly

Looking for assistance with routing to the featured homes page without the content overlapping my navbar and search component. I simply want it to direct to a new URL without importing the components unless specifically needed. Check out this link I suspe ...

SVG is not extending to the entire viewport in mobile view

I need help with adjusting wave SVG's on my website to fill the entire viewport or screen width. Does anyone have any suggestions? To create the waves, I used a Wave SVG generator and placed them within a div element. <nav> <div> //align ...

Can I substitute custom tags for the traditional <p> tag?

My HTML with CSS has an issue where a custom HTML tag is not displaying the text while my CSS picks up another tag. Interestingly, replacing <title>Layout Controls</title> with <p>Layout Controls</p> resolves the problem and shows t ...

Display the entire HTML webpage along with the embedded PDF file within an iframe

I have been tasked with embedding a relatively small PDF file within an HTML page and printing the entire page, including the PDF file inside an iframe. Below is the structure of my HTML page: https://i.stack.imgur.com/1kJZn.png Here is the code I am usin ...

Enhancing Your Website with Animation Delay using Animate.css and ViewportChecker

I am attempting to apply a delay to various elements with animated classes on a specific webpage using animate.css version 3.5.1 by Daniel Eden and jquery-viewport-checker version 1.8.7 by Dirk Groenen. My initial approach was to utilize the setTimeout fu ...