I have a custom MUI 5 component library NPM package that exports some basic components built with MUI 5, exports theme file and wraps the theme provider into reusable component like so:
import { ThemeProvider } from 'styled-components';
import { StyledEngineProvider } from '@mui/material/styles';
import { theme as defaultTheme } from './theme';
function CustomThemeProvider(props: {
theme: any;
children: JSX.Element[];
}): JSX.Element {
const { theme = defaultTheme, children } = props;
return (
<StyledEngineProvider injectFirst>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
</StyledEngineProvider>
);
}
export default CustomThemeProvider;
However when I try using this custom theme provider and the components in the project that uses this shared library, theme is not applied at all. This approach did work with MUI 4, but there was no Emotion layer at that point and setup was much simpler.
I have added all of these in peer dependencies for the shared component package to keep the references intact.
"peerDependencies": {
"@emotion/react": "^11.7.1",
"@emotion/styled": "^11.6.0",
"@mui/icons-material": "^5.2.4",
"@mui/lab": "^5.0.0-alpha.60",
"@mui/styles": "^5.2.3",
"@mui/material": "^5.2.4",
"@mui/styled-engine-sc": "^5.1.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"styled-components": "^5.3.3"
},
But it does not make any difference.
Any ideas how to make it work, or any other approach that allows for a shared MUI 5 component library / theme to work, are welcome!