Overview
By default, Material UI uses Emotion to generate CSS styles. However, you can use styled-components instead by configuring the@mui/styled-engine-sc package.
Installation
First, install the required packages:Bundler Configuration
Material UI’s packages have@mui/styled-engine as a dependency. You need to configure your bundler to replace it with @mui/styled-engine-sc.
Yarn
Use package resolutions inpackage.json:
npm
Configure your bundler’s alias. For webpack:TypeScript Configuration
Updatetsconfig.json to map the styled engine:
Next.js
For Next.js projects, updatenext.config.js:
Version Compatibility
The major version of@mui/styled-engine-sc must match your styled-components version:
styled-componentsv5 →@mui/styled-engine-scv5styled-componentsv6 →@mui/styled-engine-scv6 (currently in alpha)
Usage
Once configured, Material UI components will use styled-components under the hood:StyledEngineProvider
The@mui/styled-engine-sc package provides a StyledEngineProvider component to control style injection order:
injectFirst is true, Material UI styles are injected before other styles, allowing you to easily override them.
Implementation Details
The styled-components wrapper implements the same interface as the Emotion wrapper, providing:styled()- The main styling APIGlobalStyles- Component for global styleskeyframes- Animation keyframes helpercss- CSS template literal helperThemeContext- Access to theme context
@mui/styled-engine-sc/src/index.js:
Global Styles
Use theGlobalStyles component to inject global styles: