I'm using Material-UI with React. I have the following approach:
For global styles I'm using the ThemeProvider
palette: {
type: 'dark',
primary: {
main: '#123',
},
secondary: {
main: '#456',
},
For local styles (in components) I'm using withStyles:
const styles = (theme: any) => ({ ... });
export default withStyles(styles)(UperNavigationBar);
Question 1: My basic idea is to separate colors, fonts etc. (= global styles) etc. from stuff like spacing, alignment etc. (local styles). This way I can easily switch from a light theme to a dark. What do you think about this approach?
Question 2: I have several background colors (default background color, navigation elements have other background color also grids) Then there are hover effects with different background colors etc. But theme.pallette.backgroundColor has only two properties. What's the best way to define further background colors for several components?
It uses a syntax called CSS-in-JS (or JSS) which is just a JavaScript-friendly way of writing styles that compile into actual browser-readable CSS. Though deprecated, you can still use makeStyles() in MUI 5, but we expect it (to be removed in v6, but hence my confusion between past and present tense).
It's part Material-ui's Styled-Components API. It is a reference for the element's parent's classname. The ampersand (&) can be used to refer back to the main component. Ampersands (&) get replaced by our generated, unique classname for that styled component, making it easy to have complex logic.
You can easily develop highly engaging and SEO-friendly websites with excellent performance and efficiency. By integrating Material UI into your React applications, you can elevate your web development experience to the next level and build dynamic and high-quality websites.
I hope it's still relevant.
When it comes to best practices and approaches - I tend to stick to the newest API which is useStyles. About the separation of concerns for styling- in my opinion, it might be very confusing unless it's a global setting. In that case you may want to override it in whatever way explained in the documentation.
I suggest you to first explore the default theme as it really does answer most of your concerns. When changing theme type, you can see that values in the palette will change.
You can override many properties using the theme, and you can also add custom variables. This will help you follow all the specific background colors you have. If you wish these custom colors to match the theme type - simply conditionally apply them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With