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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[system] Allow theme inheritance in createStyled #39025

Closed
2 tasks done
jansepke opened this issue Sep 17, 2023 · 2 comments
Closed
2 tasks done

[system] Allow theme inheritance in createStyled #39025

jansepke opened this issue Sep 17, 2023 · 2 comments
Assignees
Labels
enhancement This is not a bug, nor a new feature package: system Specific to @mui/system

Comments

@jansepke
Copy link

jansepke commented Sep 17, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

While working on solutions for #38994 I came up with the idea that it would be very helpful that nested createStyled() should have an option to pass the defaultTheme. What I like to accomplish is to overwrite the theme that was used in the first createStyled() by the second createStyled().

Examples 🌈

import { createTheme } from "@mui/material";
import { blue, yellow } from "@mui/material/colors";
import { createStyled } from "@mui/system";

const theme1 = createTheme({
  palette: { primary: yellow },
});
const theme2 = createTheme({
  palette: { primary: blue },
});

const styled1 = createStyled({ defaultTheme: theme1 });
const styled2 = createStyled({ defaultTheme: theme2 });

const MyButton1 = styled1("button")(({ theme }) => ({
  color: theme.palette.primary.main,
}));
const MyButton2 = styled2(MyButton1)();

export const App = () => (
  <>
    <MyButton1>Button text is yellow</MyButton1>
    <MyButton2>Button text is yellow but should be blue</MyButton2>
  </>
);

styled2(MyButton1) should pass the theme of styled2 to the callback of styled1 so it would be possible to apply a different theme to the button from the outside and without using a ThemeProvider.

Currently styled will only use the directly passed theme or the theme passed by a ThemeProvier. An alternative solution would be to allow passing the theme directly as a prop to every MUI component (that seems to be already possible but not document and not allowed in the Typescript Props interface)

Motivation 🔦

I want to create an internal component library that is wrapping and styling MUI components. I want to use a theme for that and the theme should be applied without forcing the users of my library to add a ThemeProvider to their applications.

@jansepke jansepke added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 17, 2023
@danilo-leal danilo-leal changed the title [MUI System] allow theme inheritance in createStyled [system] Allow theme inheritance in createStyled Sep 18, 2023
@danilo-leal danilo-leal added package: system Specific to @mui/system enhancement This is not a bug, nor a new feature labels Sep 18, 2023
@brijeshb42
Copy link
Contributor

Same comment as this.

@brijeshb42 brijeshb42 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 Sep 18, 2023
@jansepke
Copy link
Author

Closed in the other issue.

@github-actions github-actions bot removed the status: waiting for author Issue with insufficient information label Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is not a bug, nor a new feature package: system Specific to @mui/system
Projects
None yet
Development

No branches or pull requests

3 participants