Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing theme does not change background color when using CSS variables #34911

Open
2 tasks done
Nikhil1920 opened this issue Oct 27, 2022 · 5 comments
Open
2 tasks done
Labels
docs Improvements or additions to the documentation package: material-ui Specific to @mui/material support: question Community support but can be turned into an improvement

Comments

@Nikhil1920
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 馃暪

Live example links

https://codesandbox.io/s/create-react-app-with-typescript-forked-46llm7?file=/src/index.tsx

https://nikhil1920.github.io/next-js-mui-pwa-starter/

Current behavior 馃槸

Background color is not changing based on theme

Expected behavior 馃

Background color should change based on current theme

Context 馃敠

No response

Your environment 馃寧

No response

@Nikhil1920 Nikhil1920 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Oct 27, 2022
@siriwatknp
Copy link
Member

siriwatknp commented Oct 28, 2022

You should add CssBaseline to the page.

import CssBaseline from '@mui/material/CssBaseline';

<CssVarsProvider>
  <CssBaseline />
  ...
</CssVarsProvider>

I think this deserves some documentation after #34639 is implemented.

@siriwatknp siriwatknp added docs Improvements or additions to the documentation package: material-ui Specific to @mui/material support: question Community support but can be turned into an improvement and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Oct 28, 2022
@Nikhil1920
Copy link
Author

import from '@mui/material/styles' was giving me
JSX element type 'CssBaseline' does not have any construct or call signatures.ts(2604)
error but the import from "@mui/material/CssBaseline" is working as expected.

@itsajay1029
Copy link

Can you assign this issue to me ?

@siriwatknp
Copy link
Member

import from '@mui/material/styles' was giving me

`

JSX element type 'CssBaseline' does not have any construct or call signatures.ts(2604)

`

error but the import from "@mui/material/CssBaseline" is working as expected.

馃憤Yes, you are right! changed to the correct import.

@Kilvny
Copy link

Kilvny commented Jul 26, 2023

a good solution to this for anyone encountering the same problem is Not to create the theme on the fly

here's an example :

    // ! passing this as creating a mode on the fly was resulting in a default dark - light modes from MUI 
    const theme = React.useMemo(
        () =>
            createTheme({
                palette: {
                    mode,
                },
            }),
        [mode],
    );
            <ThemeProvider theme={theme}>
                    <CssBaseline />
                    ...
            </ThemeProvider>

what would you do instead:

    // ! solution is to use the createTheme method with actual themes we created in separate file
    const darkThemeChosen = React.useMemo(
        () =>
            createTheme({
                ...darkTheme
            }),
            [mode]
    )
    const lightThemeChosen = React.useMemo(
        () =>
            createTheme({
                ...lightTheme
            }),
            [mode]
    )

            <ThemeProvider theme={mode === 'dark' ? darkThemeChosen : lightThemeChosen}>
                    <CssBaseline />
                    ...
            </ThemeProvider>

I hope this would be helpful as I faced this problem and resolved it this way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation package: material-ui Specific to @mui/material support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

4 participants