Iterating Over a Hash Object in Stylus - stylus

I have a made a hash object of colors in stylus - like this (these are bootstrap 4 colors):
colors: {
white: #fff,
light: #f8f9fa,
black: #000,
dark: #212529,
grey: {
base: #adb5bd,
light: #ced4da,
lighter: #dee2e6,
lightest: #e9ecef,
dark: #868e96,
darker: #495057,
darkest: #343a40,
},
blue: #007bff,
indigo: #6610f2,
purple: #6f42c1,
pink: #e83e8c,
red: #dc3545,
orange: #fd7e14,
yellow: #ffc107,
green: #28a745,
teal: #20c997,
cyan: #17a2b8
}
What I would like to do is iterate over this list and create light and dark variations -- something like this:
for color in colors
dark-{color} = darken({color}, 10%)
My end goal is to have a list of variables that I can use -- like this: dark-green, light-purple, etc.
Or, alternatively, like this: colors.dark-green, colors.light-purple, etc.
Any idea how to do this?
Thanks.

You should change your code like this,
colors: {
white: #fff,
light: #f8f9fa,
black: #000,
dark: #212529,
blue: #007bff,
indigo: #6610f2,
purple: #6f42c1,
pink: #e83e8c,
red: #dc3545,
orange: #fd7e14,
yellow: #ffc107,
green: #28a745,
teal: #20c997,
cyan: #17a2b8
}
for name, attr in colors
dark-{name} = darken({attr}, 10%)
The name is Key, and Attr is value

Related

React & MUI - How to apply specific MUI theme palette color to fill MUI icon SVG?

I'm using MUI & REACT for my web application but was wondering how could you fill a SVG with the color value defined in my MUI palette.
My palette looks like this:
const theme = createTheme(
{
palette: {
mode: "light",
primary: {
main: "rgba(33, 150, 243, 1)",
dark: "rgb(31,141,229)",
light: "rgb(35,157,253)"
},
secondary: {
main: "rgba(255, 82, 82, 1)",
dark: "rgb(232,74,74)",
light: "rgb(253,92,92)"
},
background: {
main: "hsl(0, 0%, 87%)",
default: "hsl(0,0%,95%)"
},
common: {
main: "hsl(0, 0%, 96%)",
white: "hsl(0,0%,100%)"
},
text: {
primary: "#232323",
secondary: "#444444"
}
}
},
localsMui[lang]
)
And the SVG which I try to apply the color:
<LogoutIcon fill="primary.main"/>
Does anyone have an idea?
I tried to use useTheme() but it doesn't work either.

Change Secondary button text color through Material UI Theme

I am making use of both of the primary and secondary buttons on my app.
It seems that by default the primary button's color is white, which is fine. But I need my secondary button color to also be white, which it is not.
Here is what my code currently looks like:
const theme = createMuiTheme({
palette: {
primary: {
main: '#FF0000'
},
secondary: {
main: '#00FF00'
},
// Probably unrelated? But tried anyway...
text: {
primary: '#FFFFFF',
secondary: '#FFFFFF'
}
},
// Can override primary's text color from here, but no clue how to
// do so with the secondary
MuiButton: {
text: {
color: '#FFFFFF'
}
}
});
// ...
// In my component:
// Expected both buttons to have white text color, however secondary has black text. Need to override this so that both buttons have the same text color.
function MyComponent () {
return (
<ThemeProvider theme={theme}>
<Button color='primary'>Primary</Button>
<Button color='secondary'>Secondary</Button>
</ThemeProvider>
)
}
Edit: I did notice that it seems that the the button's text color is tied to it's background color. So if I put both primary and secondary buttons the same color, both their background color will default to the same. Nevertheless, my question still stands, how can I control this? Mainly for the secondary button?
You can set a contrast text as per the below
const theme = createMuiTheme({
palette: {
primary: {
main: "#FF0000",
contrastText: "#ffffff",
},
secondary: {
main: "#00FF00",
contrastText: "#ffffff",
},
},
});

How to Customize MaterialUI Color Shades

I understand that in MaterialUI, it is possible to customize the theme to use different colors.
The colors exposed by MaterialUI, have different shades. For instance:
import purple from '#material-ui/core/colors/purple';
const theme = createMuiTheme({
palette: {
primary: {
main: purple,
},
secondary: {
main: "#22B469",
light: "rgb(78, 195, 135)",
dark: "rgb(23, 125, 73)",
contrastText: "#ffffff"
},
},
});
Looking at the console.log(theme), we see that the primary color is filled with shades, but not for the secondary color, where the color has been overridden by a hex color code.
In MaterialUI documentation, it's only mentioned to use light, main, dark, and contrastText when using a custom hex color.
Sure, I can add those shades one by one under the secondary key, but this solution doesn't look elegant.
What are the best practices when overriding a theme color with hex code and is there a way to auto-generate the shades from a custom hex color?
You can make a custom color generated here in a module and import it.
const amber = {
50: '#fff8e1',
100: '#ffecb3',
200: '#ffe082',
300: '#ffd54f',
400: '#ffca28',
500: '#ffc107',
600: '#ffb300',
700: '#ffa000',
800: '#ff8f00',
900: '#ff6f00',
A100: '#ffe57f',
A200: '#ffd740',
A400: '#ffc400',
A700: '#ffab00'
};
const _default = amber;
exports.default = _default;
import purple from '#material-ui/core/colors/purple';
import indigo from '#material-ui/core/colors/indigo';
const theme = createMuiTheme({
palette: {
primary: {
main: purple,
},
secondary: {
main: purple,
light: "rgb(78, 195, 135)",
dark: "rgb(23, 125, 73)",
contrastText: "#ffffff",
indigo: indigo //<---------- do what you want
},
},
});
then in your style definition:
{...
root: {
color: theme.palete.secondary.indigo[700],
},
...}

Why Material-UI is not overriding styles?

Im using Material-UI within my application. The typography and palletes overriding is working perfectly. But when I try to overrides the MUIButton, nothing happens. This is my Style file:
const theme = createMuiTheme(SculpStyle);
export const SculpStyle = {
typography: {
fontFamily: ["Circular", "Open Sans", "Arial", "sans-serif"].join(","),
//fontSize: 20
// htmlFontSize: 12,
},
palette: {
primary: {
// light: will be calculated from palette.primary.main,
main: "#50be9c"
// dark: will be calculated from palette.primary.main,
// contrastText: will be calculated to contrast with palette.primary.main
},
secondary: {
light: "#1e8fb2",
main: "#ee4d73",
// dark: will be calculated from palette.secondary.main,
contrastText: "#ffcc00"
},
// contrastThreshold: 3,
// tonalOffset: 0.2
overrides: {
MuiButton: {
root: {
fontWeight: "bold",
backgroundColor: "red",
margin: "10px",
"&:hover": {
backgroundColor: "green"
}
}
}
}
}
};
Im following exactly the Material-UI API, but the Button is never overrided. What Im doing wrong here?
It looks like you defined your overrides on the wrong level, in your code those are nested within palette, but according to the documentation, the overrides for a specific component should be present on the "root" level of the configuration object (See here).
const theme = createMuiTheme(SculpStyle);
export const SculpStyle = {
typography: {
fontFamily: ["Circular", "Open Sans", "Arial", "sans-serif"].join(","),
//fontSize: 20
// htmlFontSize: 12,
},
palette: {
primary: {
// light: will be calculated from palette.primary.main,
main: "#50be9c"
// dark: will be calculated from palette.primary.main,
// contrastText: will be calculated to contrast with palette.primary.main
},
secondary: {
light: "#1e8fb2",
main: "#ee4d73",
// dark: will be calculated from palette.secondary.main,
contrastText: "#ffcc00"
},
// contrastThreshold: 3,
// tonalOffset: 0.2
},
overrides: {
MuiButton: {
root: {
fontWeight: "bold",
backgroundColor: "red",
margin: "10px",
"&:hover": {
backgroundColor: "green"
}
}
}
}
};

React Material UI Mode Change Text Color Change

I have this button on my page that changes the theme on the page from dark to light.
Text changes to white and background changes to black then I click again vice-versa. I did not specifically gave a color prop or background prop to any element but everything works. The problem is with the below buttons
As you see they are red and coming from primary.light, though when the dark theme active it needs to be primary.dark but it doesn't...
const theme = createMuiTheme({
palette: {
type: mode,
primary: {
light: "#FF0000",
main: "#3f50b5",
dark: "#002884",
contrastText: "#FF0000"
},
secondary: {
light: "#ff7961",
main: "#f44336",
dark: "#ba000d",
contrastText: "#000"
}
},
typography: {
useNextVariants: true
}
});

Resources