dark mode in laravel while using sass - javascript

I'm using sass preprocessor on my laravel project.
on all of my stylesheets, i've used css variables for all colors. all of the variables are stored in 'resource/sass/_variables.scss' and imported in the begining of all stylesheet files.
the inside of '_variables.scss' file is something like this:
$primary-background:#1b203d;
$primary-box-color:#2a2b4a;
$primary-border-color: #7277c422;
$secoundary-box-color: #393a66;
$primary-box-shadow: #1a1b2f;
......
and also in my stylesheet i've used variables like this:
#import 'variables';
#sidebar{
background-color: $secoundary-box-color;
border:1px solid $primary-border-color;
// .....
}
now i'm trying to implement dark mode switch using css variables.
I have no idea how to change css variables, consider it that all my stylesheets (with variables) will be compiled and i have no longer access to css variable on the frontend!

you can use css custom properties to assign colors
//_variables.scss
$lightTheme: #fff;
$darkTheme: #000;
//style.css
#import 'variables';
:root {
--theme: #{$lightTheme};
}
body {
background-color: var(--theme);
}
body.dark {
--theme: #{$darkTheme};
}

Related

How can I handle multiple themes in an angular component?

I'm working on an Angular 9 project where I'm creating two themes and each theme has it's own css output file.
I modified the angular.json file to handle that:
"styles": [
{
"input": "src/styles/themes/light-theme.scss",
"lazy": true,
"bundleName": "light-theme"
},
{
"input": "src/styles/themes/dark-theme.scss",
"lazy": false,
"bundleName": "dark-theme"
}
],
light-theme and dark-theme are my input files, where I'm setting variables like:
$background-color
$button-color
$text-color
etc, etc.
My problem is that I cannot use those variables from each component, because my component won't know what those variables are. I cannot import one or another theme, because I would like to use the values that I declared in the input file.
How should I handle this? Is there any way of "importing" the input file I wrote on my angular.json file?
Thanks!
If you define sass variables in your global styles, you won't be able to access them after when you dynamically change the theme. This is because the dynamically loaded theme will contain css rules only, not sass; besides at run time your components scss has also already compiled to css so there is no more notions of sass variables either way.
What you can do instead is use CSS variables, which have good browser support (apart from IE and opera mini).
So for instance, you can define these variables in your theme files
dark-theme.scss
:root{
--button-background: darkgrey;
--button-color: white;
}
light-theme.scss
:root{
--button-background: lightgrey;
--button-color: black;
}
Then in your component, use these variables
component.scss
button
{
cursor: pointer;
padding: 10px;
border: 0;
color:var(--button-color);
background-color:var(--button-background);
}
Then, when you dynamically load the light theme, it will override the existing variables. If you then dynamically remove light-theme.css, it'll go back to using your dark theme's variables.

Change styles based on dark or light mode

I want to have a dark and a light theme on my Vue app.
I can create dark.scss file and change classes styles and use !important property to override styles defined in components.
Or I can use props in my components and change className with v-if based on the theme.
e.g. set class to className__light when theme is light otherwise set to className__dark.
which one is better in all situations like performance or time needed to do it?
Well i would not do it with classes. I would create CSS variables with either SCSS or you create CSS variables in :root
If you do it with the :root method then it should look something like this:
:root {
--background: red;
}
Then you can access it in any component like this for example:
.class {
background: var(--background); // the background will appear red
}
Now you can change the background color with just 1 CSS variables.
To change the variable with Javascript you just write following:
root.style.setProperty('--background', "green");
The problem here is that it isnt supported in IE if you care about browser support.
So you should create an fallback like this:
.class {
background: red; //fallback
background: var(--background); // the background will appear red
}

Apply CSS theme based on react state

I have a REACT application (bootstrapped with create react app and react-bootstrap) for which I am trying to add option to switch to DARK theme if user enabled this in his settings. I am storing the settings on server and fetching them into properties.
I have a separate stylesheet called dark.css where all my component styles are overriden.
dark.css (example):
#root {
background-color: var(--dark);
color: var(--light)
}
.card {
background-color: var(--dark); // overriding bootstrap styles here
}
I am trying to apply it at the root of my application like this:
componentWillReceiveProps() {
if (this.props.profile && this.props.profile.theme === 'dark') {
require('./styles/dark.css');
}
}
It works great when running the application locally with yarn start. But when I actually build the app using webpack, it works really strange. Part of the new styles are applied and part on, regardless of which theme is selected. For example background is applied from the main theme and ignored in the dark theme but text color is the opposite.
Why is this happening?
It seems that the dark stylesheet is not being applied at all when building the app with webpack, although everything looks correctly when running it with yarn start.
I guess that you have a naming clashes, which overrides your css.
React supports CSS Modules alongside regular stylesheets using the [name].module.css file naming convention.
CSS Modules let you use the same CSS class name in different files without worrying about naming clashes
I solved my issues simply by importing all styled css sheets and then prefixing them like this:
.dark .card {
color: black;
}
.light .card {
color: white;
}
I assign class to the wrapper based on my props:
<div id="root" className={theme}>
// content
</div>
Works like a charm.

How do I make file with variables of all project's colors

I use React.JS to develop a web.I want to define all the colors in one class.
In fact, I am a beginner and I only use colors at CSS files. like this:
.toolbar {
background: primary1;
}
I also want to use them in other type classes that may be needed.
Thank you.
Also, sorry about my English.
in Sass, you can declare all your colors in one file:
//colors.scss
$red: #990000;
$black: #000000;
$white: #ffffff;
And then in any other scss file in your project you can import and use any of the declared colors like this:
//style.scss
#import "path/to/colors.scss";
div {
background: $red;
color: $black;
}
You probably have sass build in support inside your react project, but if not, refer to the docs: https://sass-lang.com/documentation

Css Custom Properties in local scope not working with Gatsbyjs

Using: Gatsbyjs
Problem:
Trying to declare css variables at :root (which works very well), but onces I try to override them like that:
:root { --feature-background: red; }
:root [data-theme="gradient"] {
--feature-background: blue;
}
<div data-theme="gradient">Hello</div>
I get the following warning and the I get the fallback color.
--feature-background is not getting overwritten, which is what I expect.
Custom property ignored: not scoped to the top-level :root element

Categories