score:2
In my experience, using MuiThemeProvider and createMuiTheme have worked wonderfully. However, I am using Material-UI version 3.9.2.
MuiThemeProvider should wrap around your entire application. All you need to do in all of your components would be to instead of passing your styles object to with styles, pass a function that passes in the theme.
Ex:
import React from 'react';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import {NavBar, Routes} from '../'
const theme = createMuiTheme({
palette: {
primary: {
main: 'red'
},
},
/* whatever else you want to add here */
});
class App extends Component {
render() {
return (
<MuiThemeProvider theme={theme}>
<NavBar />
<Routes />
</MuiThemeProvider>
)
}
then in navbar let's say:
import React from 'react';
import { withStyles } from '@material-ui/core';
const styles = theme => ({
root: {
color: theme.palette.primary.main,,
}
})
const NavBar = ({classes}) => {
return <div className={classes.root}>navigation</div>
}
export default withStyles(styles)(NavBar);
Hope that helps, works very well for me!
score:5
For global styles you can use it like shown below. This is the best implementation that has worked for me.
const theme = createMuiTheme({
overrides: {
MuiCssBaseline: {
'@global': {
html: {
WebkitFontSmoothing: 'auto',
},
},
},
},
});
// ...
return (
<ThemeProvider theme={theme}>
<CssBaseline />
{children}
</ThemeProvider>
);
For more reference: Global CSS
score:8
In Material-UI v5, you can use GlobalStyles
to do exactly that. From what I know, GlobalStyles
is just a wrapper of emotion's Global
component. The usage is pretty straightforward:
import GlobalStyles from "@mui/material/GlobalStyles";
<GlobalStyles
styles={{
h1: { color: "red" },
h2: { color: "green" },
body: { backgroundColor: "lightpink" }
}}
/>
Note that you don't even have to put it inside ThemeProvider
, GlobalStyles
uses the defaultTheme
if not provided any:
return (
<>
<GlobalStyles
styles={(theme) => ({
h1: { color: theme.palette.primary.main },
h2: { color: "green" },
body: { backgroundColor: "lightpink" }
})}
/>
<h1>This is a h1 element</h1>
<h2>This is a h2 element</h2>
</>
);
Live Demo
score:9
Global Styles with Material UI & React
// 1. GlobalStyles.js
import { createStyles, makeStyles } from '@material-ui/core';
const useStyles = makeStyles(() =>
createStyles({
'@global': {
html: {
'-webkit-font-smoothing': 'antialiased',
'-moz-osx-font-smoothing': 'grayscale',
height: '100%',
width: '100%'
},
'*, *::before, *::after': {
boxSizing: 'inherit'
},
body: {
height: '100%',
width: '100%'
},
'#root': {
height: '100%',
width: '100%'
}
}
})
);
const GlobalStyles = () => {
useStyles();
return null;
};
export default GlobalStyles;
** Then Use it in App.js like below**
// 2. App.js
import React from 'react';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import { Router } from 'react-router-dom';
import { NavBar, Routes, GlobalStyles, Routes } from '../';
const theme = createMuiTheme({
palette: {
primary: {
main: 'blue'
}
}
});
const App = () => {
return (
<MuiThemeProvider theme={theme}>
<Router>
<NavBar />
<GlobalStyles />
<Routes />
</Router>
</MuiThemeProvider>
);
};
export default App;
This Works for me with my react project.
score:12
You can actually write global styles with material UI:
const useStyles = makeStyles((theme) => ({
'@global': {
'.MuiPickersSlideTransition-transitionContainer.MuiPickersCalendarHeader-transitionContainer': {
order: -1,
},
'.MuiTypography-root.MuiTypography-body1.MuiTypography-alignCenter': {
fontWeight: 'bold',
}
}
}));
Source: stackoverflow.com
Related Query
- Global Styles with React and MUI
- How to Create global styles with Styled components in react and next js and is it better to use combination of css files and styled component?
- How to apply global styles with CSS modules in a react app?
- Emitting and handling global events with react
- Getting keyboard navigation to work with MUI Autocomplete and SimpleBar for react
- React redux connect with with styles and withrouter
- headless WordPress with react - styles in back and front-end Gutenberg
- How to test styles and media queries rendered by a React component with Jest and/or Enzyme
- webpack minify with global variables and methods for selective files in React
- React MUI Datagrid align column data and header with type "number"
- Work with global variables in React which are supplied at runtime and therefore unknown at build time
- looking for render function will take HTML and apply styles to the string with react js
- React Hook Form with MUI Autocomplete, FreeSolo and dependent fields
- Problem with react memo in lists and thier styles
- Global popup in React with Router and Redux
- How to create an excel file in react js with custom headings and styles while doing export to excel?
- React and Tailwind multiple styles with some conditionals
- Setting a backgroundImage With React Inline Styles
- How to test a className with the Jest and React testing library
- Add Favicon with React and Webpack
- SyntaxError with Jest and React and importing CSS files
- How to import CSS modules with Typescript, React and Webpack
- How to mock React component methods with jest and enzyme
- How to set up Babel 6 stage 0 with React and Webpack
- Form validation with react and material-ui
- Formatting code with <pre> tag in React and JSX
- Multiple classNames with CSS Modules and React
- React img tag issue with url and class
- Using onBlur with JSX and React
- How do I store JWT and send them with every request using react
More Query from same tag
- How to input decimal places in Textfield from MUI in react
- Tensorflow automl model in react
- Adding OIDC to an React application with restricted routes
- axios, cant pass data from react component to PHP file
- Replace static array of objects with GraphQL queried data
- Redirecting onClick
- How to place three elements next to each other using css and centered
- Form elements do not have associated labels, but it is a MUI theme switch
- Grid falls apart after i give any margin or padding
- How to deploy separated frontend and backend?
- How to move SemanticUIReact Icon left of text element in sub-menu?
- Redux reducer initial state with Typescript
- How can I pass arguments in action different 3 level react components without redux and context API?
- React Redux app - complex init action composition executes final promise before the others are done
- React API returns empty
- React not re-rendering after a change in state, how do I setState of child components?
- React FormEvent<HTMLFormElement> form input props types
- Using '>' Inside .innerHTML Causes Premature Closing Of HTML Statement
- Typescript React: generic-based callback event handler function as prop to component
- Dynamically display newly added cards in the right Reactjs
- How to insert post method with mongoose having a reference in node.js
- Unable to persist the checkboxes
- Error with module parsing- Webpack
- How to Extract the id from a class div in react JS such that i can assign the value of div id to a variable in my code
- Testing React component with dynamic import (Enzyme/Mocha)
- React.memo isn't working - what am I missing?
- TypeError: Cannot read property 'has' of undefined
- How to retrieve the image in a form?
- What icons can be added from different versions of material-ui/icons in a react project?
- Cypress hooks in grouped describe