Skip to content

[Theme] Restructure component definitions#22293

Merged
mnajdova merged 22 commits intomui:nextfrom
mnajdova:feat/restructure-theme
Aug 24, 2020
Merged

[Theme] Restructure component definitions#22293
mnajdova merged 22 commits intomui:nextfrom
mnajdova:feat/restructure-theme

Conversation

@mnajdova
Copy link
Member

@mnajdova mnajdova commented Aug 20, 2020

Closes #21923

BREAKING CHANGE

createMuiTheme

  • The components' definition inside the theme were restructure under the components key, to allow people easier discoverability about the definitions regarding one component.
  1. props
import { createMuiTheme } from '@material-ui/core/styles';

const theme = createMuitheme({
-  props: {
-    MuiButton: {
-      disableRipple: true,  
-    },
-  },
+  components: {
+    MuiButton: {
+      props: {
+        disableRipple: true,
+      },
+    },
+  },
});  
  1. overrides
import { createMuiTheme } from '@material-ui/core/styles';

const theme = createMuitheme({
-  overrides: {
-    MuiButton: {
-      root: { padding: 0 },
-    },
-  },
+  components: {
+    MuiButton: {
+      overrides: {
+        root: { padding: 0 },
+      },
+    },
+  },
});  

For a smoother transition, the adaptV4Theme helper allows you to iteratively upgrade to the new theme structure. Note that it will display a deprecation warning in the console, since it will be removed at the next major release.

-import { createMuiTheme } from '@material-ui/core/styles';
+import { createMuiTheme, adaptV4Theme } from '@material-ui/core/styles';

-const theme = createMuitheme({
+const theme = createMuitheme(adaptV4Theme({  
  props: {
    MuiButton: {
      disableRipple: true,
    },
  },
  overrides: {
    MuiButton: {
      root: { padding: 0 },
    },
  },
-});  
+}));  

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Aug 20, 2020
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Aug 20, 2020
@mui-pr-bot
Copy link

mui-pr-bot commented Aug 20, 2020

@material-ui/core: parsed: +0.18% , gzip: +0.19%

Details of bundle changes

Generated by 🚫 dangerJS against d1df2a0

@mnajdova mnajdova marked this pull request as ready for review August 20, 2020 19:54
@mnajdova mnajdova changed the title [WIP] [theme] restructure component definitions [theme] Restructure component definitions Aug 20, 2020
@mnajdova mnajdova changed the title [theme] Restructure component definitions [Theme] Restructure component definitions Aug 20, 2020
@oliviertassinari
Copy link
Member

Something is off with the apply density button https://deploy-preview-22293--material-ui.netlify.app/customization/density/#main-content

Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, great effort!

Comment on lines +5 to +7
props?: ComponentsProps['MuiAlert'];
variants?: Variants['MuiAlert'];
overrides?: Overrides['MuiAlert'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder about the naming convention. It could be

Suggested change
props?: ComponentsProps['MuiAlert'];
variants?: Variants['MuiAlert'];
overrides?: Overrides['MuiAlert'];
props?: Props['MuiAlert'];
variants?: Variants['MuiAlert'];
overrides?: Overrides['MuiAlert'];

or

Suggested change
props?: ComponentsProps['MuiAlert'];
variants?: Variants['MuiAlert'];
overrides?: Overrides['MuiAlert'];
props?: ComponentsProps['MuiAlert'];
variants?: ComponentsVariants['MuiAlert'];
overrides?: ComponentsOverrides['MuiAlert'];

Maybe the latter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I was expecting relative imports, it seems strange to have these values coming from the core and not the lab.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are doing module augmentation and the variants are just inherited from the ComponentProps I think, so everything works. But I agree it looks confusing, let me see how can it be defined better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding Components prefix makes sense 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I was expecting relative imports, it seems strange to have these values coming from the core and not the lab.

@oliviertassinari the ComponentsProps, ComponentsVariants and ComponentsOverrides are all created based on the underlying mappings for the components. In the lab we are extending this underlying components mappings, so basically these typings are automatically updated, that is why we are importing from the core. Should we change how we are augmenting these typings?

// This is automatically updated
export type ComponentsOverrides = {
  [Name in keyof ComponentNameToClassKey]?: Partial<StyleRules<ComponentNameToClassKey[Name]>>;
} & {
  MuiCssBaseline?: {
    '@global'?: {
      '@font-face'?: CSSProperties['@font-face'];
    } & Record<string, CSSProperties['@font-face'] | CSSProperties>; // allow arbitrary selectors
  };
};

// We are overriding these mappings
export interface ComponentNameToClassKey {
  MuiAppBar: AppBarClassKey;
  MuiAvatar: AvatarClassKey;
  MuiBackdrop: BackdropClassKey;
  // ...
}

@mbrookes mbrookes added the internal Behind-the-scenes enhancement. Formerly called “core”. label Aug 20, 2020
@mbrookes mbrookes added this to the v5 milestone Aug 20, 2020
@mnajdova
Copy link
Member Author

Something is off with the apply density button https://deploy-preview-22293--material-ui.netlify.app/customization/density/#main-content

Good catch! Fixed

@oliviertassinari
Copy link
Member

oliviertassinari commented Aug 21, 2020

@joshwooding we might be able to add the theme.mixins.gutter with this new adaptV4Theme helper.

@oliviertassinari oliviertassinari added the breaking change Introduces changes that are not backward compatible. label Aug 21, 2020
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good from my end. I have made a few small proposals in the last commit:

  • Make adaptV4Theme deprecated so developers don't forget to remove it.
  • Group the Theme changes under the same section of the migration guide.
  • Remove mention to theme.variants in the upgrade from v4 guide so developers don't get confused if they can't find it (I'm not entirely sure that it's better)

Great work!

Co-authored-by: Matt <github@nospam.33m.co>
Copy link

@jimmyandrade jimmyandrade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Introduces changes that are not backward compatible. internal Behind-the-scenes enhancement. Formerly called “core”.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] Restructure the keys inside the theme by component name

6 participants