简体   繁体   English

如何在 react-js 中共享从 js object 到 sass 变量的值?

[英]How to share value from js object to sass variable in react-js?

I'm trying to change my application theme colors dynamically.我正在尝试动态更改我的应用程序主题 colors。 I want to get object from the server that has the values of the colors by JavaScript, then pass these values to sass variables.我想通过 JavaScript 从具有 colors 值的服务器获取 object,然后将这些值传递给 sass 变量。

I found many articles about this but nothing worked for me below some of them:我发现了很多关于此的文章,但其中一些对我没有任何帮助:

https://itnext.io/sharing-variables-between-js-and-sass-using-webpack-sass-loader-713f51fa7fa0 https://itnext.io/sharing-variables-between-js-and-sass-using-webpack-sass-loader-713f51fa7fa0

https://github.com/pmowrer/node-sass-json-importer#node-sass-json-importer https://github.com/pmowrer/node-sass-json-importer#node-sass-json-importer

Is there a way to import variables from javascript to sass or vice versa? 有没有办法将变量从 javascript 导入到 sass,反之亦然?

https://frontend-cookbook.ack.ee/pages/implementation/SharingVariables.html https://frontend-cookbook.ack.ee/pages/implementation/SharingVariables.html

I suggest you to use inline styling for that theme color我建议您为该主题颜色使用内联样式

<SomeComponent className={this.state.themeColor === 'black'? 'classDarkMode' : 'classNormalMode'} />

You might also use lib like styled components that allow you to pass props directly into styles你也可以使用类似 lib样式的组件,允许你将 props 直接传递给样式

Using javascript you can not change the value of scss files because scss is compiled into css in the server and then sent to the client where the javascript is executed.使用 javascript 您不能更改 scss 文件的值,因为 scss 在服务器中被编译为 css,然后发送到执行 javascript 的客户端。

However you can achieve that via css variables ( more here ).但是,您可以通过 css 变量来实现( 更多在这里)。 You could use css variables with a default value and then change the value of the variable via JS.您可以使用具有默认值的 css 变量,然后通过 JS 更改变量的值。

Another easy way is injecting color variables into your head: :)另一种简单的方法是将颜色变量注入您的大脑::)

Example:例子:

Create utils/addStyle.js and add the addStyle function in it like this:创建utils/addStyle.js并在其中添加addStyle function,如下所示:

function addStyle(styleString) {
  const style = document.createElement("style");
  style.textContent = styleString;
  document.head.append(style);
}

export default addStyle;

... and then in your config/colors.js : ...然后在您的config/colors.js

import addStyle from "../utils/AddStyle";

const colors = {
  primary: "#0084E7",
  primaryLighter: "#2897ec",
  primaryLightest: "#4db2ff",
  primaryWhitish: "#bbe0fd",
  primaryDarker: "#0076d1",
  primaryDarkest: "#016abb",
  primaryText: "#fff",
  secondary: "#FFE801",
  secondaryLightest: "#fffbd7",
  secondaryText: "#000",
};

addStyle(`
  :root {
    --color-primary: ${colors.primary};
    --color-primaryLighter: ${colors.primaryLighter};
    --color-primaryLightest: ${colors.primaryLightest};
    --color-primaryWhitish: ${colors.primaryWhitish};
    --color-primaryDarker: ${colors.primaryDarker};
    --color-primaryDarkest: ${colors.primaryDarkest};
    --color-secondary: ${colors.secondary};
    --color-secondaryLightest: ${colors.secondaryLightest};
  }
`);

export default colors;

Now you can use these colors both in css/scss and in js files.现在您可以在 css/scss 和 js 文件中使用这些 colors。 Eg,.例如,。 to share colors between MUI and sass files, So in MUI: you use these colors by importing the exported colors object:在 MUI 和 sass 文件之间共享 colors,所以在 MUI 中:您通过导入导出的 colors 对象来使用这些 colors:

import colors from "../config/Colors";
// in your theme:
  palette: {
    primary: {
      main: colors.primary,
      lighter: colors.primaryLighter,
//...

and in your scss files like this:在你的 scss 文件中是这样的:

.classname {
  background-color: var(--color-secondaryLightest)
}

Credit: This answer was inspired by this article by Santiago Vilar .学分:这个答案的灵感来自Santiago Vilar的这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM