Another easy way is injecting color variables into your head! :)
Example:
Create utils/addStyle.js
and add the addStyle
function in it like this:
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
:
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! E.g., to share colors between MUI and sass files.
So in MUI, you use these colors by importing the exported colors object:
import colors from "../config/Colors";
// in your theme:
palette: {
primary: {
main: colors.primary,
lighter: colors.primaryLighter,
//...
and in your scss files like this:
.classname {
background-color: var(--color-secondaryLightest)
}
Credit: This answer was inspired by this article by Santiago Vilar.