-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useTheme() hook is not returning palette colors #32806
Comments
I found what was the problem ... I had to replace: import { useTheme } from '@mui/system'; with: import { useTheme } from '@mui/material'; This was fine before version 5.7.0 ... |
Strange, it should work the same. Could you provide a CodeSandbox that I can reproduce? |
I think there is a problem with When I debug my code, Mui's
My expectation is that, it would supposed to go in this different
I have code like this, my component tried to grab the theme using
I have the following custom provider which, when dumping the incoming theme object, it has everything I defined, but not in the children.
In fact, even within my
Is this related? `npx @mui/envinfo`
|
@peterzagoranski @totszwai I think it would be better if you can share the CodeSandbox so I can reproduce and provide you with the correct solution. You can start from here https://material-ui.com/r/issue-template-latest. |
@siriwatknp I'm not sure how to reproduce a codesandbox with our specific use-case. We build a library with If I run and serve the the files within the same project of In the following screen captures below, I dumped the custom theme object that I am passing over to This is my
How I loaded it:
I mean I've created sandbox for simple project before, however I'm not sure how to create such scenario where the |
@totszwai Are you using |
@siriwatknp no, we are only using With combination of Question for you,
|
Always use them from
In general, you don't need the
Honestly, I would love to help but without any tangible code that I can reproduce, I am really lost. This kind of issue is quite hard to debug and mostly comes from the project setup. It might be the framework you are using, the webpack config, the package resolution, ...etc. So I suggest you should set up a minimal repo that is reproducible and then we will look into it. |
@totszwai I encountered the same problem as you in the |
Sadly nope. IMHO, |
In React typescript, you have to declare that the variable is a type of ThemeOptions. ` const theme:ThemeOptions = useTheme(); |
@totszwai We have the same problem. useTheme always returns the default theme and not our custom theme when used in in our main app. Like you, we have also broken our theme provider out into a package and import it into our main app. The context looks to be loading the proper custom theme in the main app, but useTheme always returns the default theme. We first noticed this because whenever we used the Box component theme (in the sx prop or on the color props), it would also always use the default theme instead of the custom theme, and it looks like that component uses useTheme under the hood. This ONLY affected the Box component theme. Other MUI components we used had no problem consuming the custom theme from the context. |
Same is happening to us. We are developing a component library for our applications and we are not able to access the theme from the components in the library. All is working fine for the components in the main application. We end exporting a ThemeProvider from the library and double wrapping it as you can see in the README instructions. This way we get all the components to work. Using only the exported ThemeProvider don't work either as in that case the main app components cannot access the theme. |
@yagopv can you elaborate on how you compile your libraries vs your applications? In our case, all of our libraries are compiled using tsc (typescript compiler) while our main apps are using CRA's default webpack/babel setup (for typescript). So the libs get transpiled by tsc as esmodules, but then imported into the main apps and probably end up transpiling again using babel setup of CRA. The result is the ThemeProvider driven MUI components (e.g. Box, useThem, etc) pick up our custom theme fine when imported into the component library lib, then exported for use in the main app. But the same MUI components don't pick up the custom theme when imported directly into the app from the mui packages. I surmise that this might have something to do with our issue (not completely sure yet). |
Having the same problem using custom themeProvider. Here my files. customTheme.tsx import { COLOR_PRIMARY, COLOR_SECONDARY } from '@/constants';
import { createTheme } from '@mui/material';
export const angelesTheme = createTheme({
palette: {
primary: {
main: COLOR_PRIMARY,
},
secondary: {
main: COLOR_SECONDARY,
},
},
}); _app.tsx import '@/styles/globals.css';
import { angelesTheme } from '@/theme/angelesTheme';
import { ThemeProvider } from '@emotion/react';
import type { AppProps } from 'next/app';
import '@fontsource/roboto/300.css';
import '@fontsource/roboto/400.css';
import '@fontsource/roboto/500.css';
import '@fontsource/roboto/700.css';
import 'animate.css';
export default function App({ Component, pageProps }: AppProps) {
return (
<ThemeProvider theme={angelesTheme}>
<Component {...pageProps} />
</ThemeProvider>
);
} component.tsx import {
...
...
useTheme,
} from '@mui/material';
...
const FinancingListItem = styled(ListItem)<{
isSelected?: boolean;
color: 'primary' | 'secondary';
}>(({ isSelected = false, color, theme: { palette } }) => {
console.log('palette', palette);
return {
border: isSelected ? `1px solid ${palette.primary.main}` : 'none',
borderRadius: '5px',
backgroundColor:
color === 'primary'
? alpha(palette.primary.main, 0.9)
: alpha(palette.secondary.main, 0.9),
color:
color === 'primary'
? palette.primary.contrastText
: palette.secondary.contrastText, // This shows me the correct color of the custom secondary color ✅
};
});
...
const Step2Financing = () => {
const theme = useTheme();
console.log(theme.palette.secondary.main); // This is showing me the deafult color palette 💀
... Thank you for your attention. |
I was getting this issue and I was able to 'resolve' it. I was getting the problem because there were 2 instances of mui (I have a mono repo and while versions only differed by a single patch 5.11.13 vs 5.11.12 if created a very hard to find bug). I still believe this is an issue with a potential 'workaround within' mui. (This creates very hard to find and fragile code as only some components started adopting the defaulttheme) The build was not transpiling the ThemeProviders in the same order so react.useContext was return null for one instance and a valid value for another instance of identical code from 2 different packages. I don't understand the code base but perhaps the @mui team can review the need for 2 ThemeProviders and useTheme packages. import ThemeContext from '@mui/private-theming/useTheme/ThemeContext'; If there is a reason for 2 implementations perhaps the default useTheme should try to resolve both provider contexts before returning the default. |
I'am struggling to fix it in my pnpm Turborepo with multiple NextJs apps with shared UI package, My monorepo structure:
I use the same MUI version(5.14.7) everywhere in this Turborepo starter |
It's still there in 5.15.x When an app uses an import from mui/material and an npm module uses import from mui/material/styles (referencing the same mui package with peerDependencies), these are two different instances. Then next.. If a same import from mui/material/styles is used, and I create a module local theme from a settings file, my local changes are ignored despite useTheme returns them. So, useTheme sees a local theme, but MUI components (charts in particular) ignore it. When I change an external, application main theme, the components in the module are fine. When I define my custom component style rules with slots with using styled() everything is fine again. Please, remove an export of these related objects/hooks from mui/material (keep them only in mui/material/styles, cause there is no reason to have these two exports, and your own components could use the different imports, and we won't get an idea which one uses what), and it would be interesting why a h.. mui charts (and not only) ignore a local theme which is visible with useTheme... |
I had a similar issue where I upgraded in a ts app, @mui/material from 5.2.4 to 5.15.14 which caused some jest tests to break. The test no longer applied the theme we defined in the test but defaulted to the default MUI theme. After reading this thread, I changed the imports from
to This fixed my issue. |
Duplicates
Latest version
Current behavior 😯
`const theme = useTheme();
console.log(theme.palette); // => {mode: 'light'}`
Expected behavior 🤔
`const theme = useTheme();
console.log(theme.palette); // => {mode: 'light', primary: { ... }, secondary: { ... }, ... }`
Steps to reproduce 🕹
No response
Context 🔦
No response
Your environment 🌎
The text was updated successfully, but these errors were encountered: