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

Use the default theme when creating a theme with extendTheme #35236

Closed
2 tasks done
thorn0 opened this issue Nov 22, 2022 · 8 comments
Closed
2 tasks done

Use the default theme when creating a theme with extendTheme #35236

thorn0 opened this issue Nov 22, 2022 · 8 comments
Labels
customization: theme Centered around the theming features package: system Specific to @mui/system

Comments

@thorn0
Copy link

thorn0 commented Nov 22, 2022

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 馃挕

The parent theme is passed to createTheme, so that its values can be arbitrarily used in the new theme. This doesn't seem to be possible with the new extendTheme API.

Examples 馃寛

const theme = extendTheme({
  colorSchemes: {
    light: {
      palette: {
        extra: { main: '#d14836', contrastText: '#fff' },
      },
    },
    dark: {
      palette: {
        extra: defaultTheme.colorSchemes.dark.palette.primary, // how do I do this?
      },
    },
  },
});

Motivation 馃敠

No response

@thorn0 thorn0 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 22, 2022
@zannager zannager added the package: system Specific to @mui/system label Nov 23, 2022
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Nov 29, 2022

It doesn't look like this bug report has enough info for one of us to reproduce it.

Please provide a CodeSandbox (https://mui.com/r/issue-template) or a link to a repository on GitHub.

Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 29, 2022
@thorn0
Copy link
Author

thorn0 commented Nov 29, 2022

Yes, sorry, I wasn't clear. The theme prop can be a function in ThemeProvider but not in CssVarsProvider. So how do I migrate code like this to CssVarsProvider and extendTheme?

<ThemeProvider theme={outerTheme => createTheme({ ...outerTheme, typography: { fontSize: 12 } })}>
...
</ThemeProvider>

@github-actions github-actions bot removed the status: waiting for author Issue with insufficient information label Nov 29, 2022
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Nov 30, 2022

I believe you can do the following:

import { Experimental_CssVarsProvider as CssVarsProvider } from '@mui/material/styles';
import { experimental_extendTheme as extendTheme } from '@mui/material/styles';

const defaultTheme = extendTheme();

const theme = extendTheme({
  colorSchemes: {
    light: {
      palette: {
        extra: { main: '#d14836', contrastText: '#fff' },
      },
    },
    dark: {
      palette: {
        extra: defaultTheme.colorSchemes.dark.palette.primary,
      },
    },
  },
});

export default function App() {
  <CssVarsProvider theme={theme}>...</CssVarsProvider>;
}

Does this work for you?


Yes, sorry, I wasn't clear. The theme prop can be a function in ThemeProvider but not in CssVarsProvider. So how do I migrate code like this to CssVarsProvider and extendTheme?

<ThemeProvider theme={outerTheme => createTheme({ ...outerTheme, typography: { fontSize: 12 } })}>
...
</ThemeProvider>

Do you have two ThemeProvider's? One being the outer one? Like:

 <ThemeProvider theme={theme}>
    <div> // your components
     .....
      <ThemeProvider
        theme={(outerTheme) => createTheme({ ...outerTheme, typography: { fontSize: 12 } })}
      ></ThemeProvider>
    </div>
  </ThemeProvider>;

@ZeeshanTamboli ZeeshanTamboli added the status: waiting for author Issue with insufficient information label Nov 30, 2022
@thorn0
Copy link
Author

thorn0 commented Dec 2, 2022

Do you have two ThemeProvider's?

Yes, like described here in the docs: https://mui.com/material-ui/customization/theming/#nesting-the-theme

@github-actions github-actions bot removed the status: waiting for author Issue with insufficient information label Dec 2, 2022
@ZeeshanTamboli
Copy link
Member

I think @siriwatknp is working on it in #35277. You can also see the discussion here: #30485 (comment)

@ZeeshanTamboli ZeeshanTamboli added the customization: theme Centered around the theming features label Dec 3, 2022
@siriwatknp
Copy link
Member

siriwatknp commented Dec 3, 2022

@thorn0 Since you are using extendTheme, I assume that you are using CSS variables.

Referring to https://mui.com/material-ui/experimental-api/css-theme-variables/customization/#adding-new-theme-tokens, you can do this:

const theme = extendTheme({
  colorSchemes: {
    light: {
      palette: {
        extra: { main: '#d14836', contrastText: '#fff' },
      },
    },
    dark: {
      palette: {
        // when mode changes to `dark`, the value of `var(--mui-palette-primary-main)` will be `theme.colorSchemes.dark.palette.primary`.
        extra: 'var(--mui-palette-primary-main)',
      },
    },
  },
});

Note that if you have custom cssVarPrefix, you should replace mui with that value, e.g.:

const theme = extendTheme({
  cssVarPrefix: 'custom',
  colorSchemes: {
    light: {
      palette: {
        extra: { main: '#d14836', contrastText: '#fff' },
      },
    },
    dark: {
      palette: {
        // when mode changes to `dark`, the value of `var(--mui-palette-primary-main)` will be `theme.colorSchemes.dark.palette.primary`.
        extra: 'var(--custom-palette-primary-main)',
      },
    },
  },
});

@thorn0
Copy link
Author

thorn0 commented Dec 5, 2022

@siriwatknp What's the recommended way to migrate the following code?

<ThemeProvider theme={outerTheme => createTheme({ ...outerTheme, typography: { fontSize: 12 } })}>
...
</ThemeProvider>

@siriwatknp
Copy link
Member

@siriwatknp What's the recommended way to migrate the following code?

<ThemeProvider theme={outerTheme => createTheme({ ...outerTheme, typography: { fontSize: 12 } })}>
...
</ThemeProvider>

Do you mind sharing the full code via CodeSandbox? That way I can explain which part you have to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization: theme Centered around the theming features package: system Specific to @mui/system
Projects
None yet
Development

No branches or pull requests

4 participants