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

[TypeScript] Add CSS vars type augmentation for Material UI #33211

Merged
merged 22 commits into from Jun 23, 2022

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented Jun 19, 2022

Note: documentation will be done separately.

Usage

Theme CSS variable is a new feature for Material UI. Apart from using the new provider (CssVarsProvider), developers have to import the type augmentation to extend the theme types to contain extra tokens and utils.

// this approach is similar to theme augmentation in the lab
import type {} from '@mui/material/themeCssVarsAugmentation';

After importing, the theme types in styled, sx, and useTheme are extended. See Demo

This works with the project that has custom module augmentation as well.

How it works?

The new types lives in the new extendTheme file which combine the existing Palette with new tokens. The new types used in extendTheme and CssVarsProvider has no impact to the existing theme.

To include new tokens to the existing theme, we export module augmentation file that internally augment the theme with new types.

image

This mean if developers does not import the augmentation, they can use extendTheme with CssVarsProvider (the implementation would work, they can see theme.vars in console.log) but they won't see theme.vars as types.


@siriwatknp siriwatknp added typescript package: material-ui Specific to @mui/material labels Jun 19, 2022
@mui-bot
Copy link

mui-bot commented Jun 19, 2022

Details of bundle changes

Generated by 🚫 dangerJS against 66258e4

@siriwatknp siriwatknp marked this pull request as ready for review June 20, 2022 07:46
Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

We need tests for the module augmentation. In addition to this, would be great to add some guide on how this should be used. We could have one initial setup, just import the module augmentation, and one more advanced, for example how can I add palette tokens for other components.

});

const TestStyled = styled('div')(({ theme }) => ({
color: theme.vars.palette.primary.main,
Copy link
Member

Choose a reason for hiding this comment

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

I am a bit confused, how those the types work here?

Copy link
Member Author

Choose a reason for hiding this comment

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

It tests that you can access theme.vars in styled. It would throw an error if it does not work. I will add a comment on this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

Choose a reason for hiding this comment

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

I am a bit confused. In order for this to work developers need to import

import type {} from '@mui/material/themeCssVarsAugmentation';

How does it work here without importing it. This was my initial question.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it. The module augmentation is a part of the repo so that's why it is included without the import. I can add it to make it explicit.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, let's do it :)

@siriwatknp
Copy link
Member Author

In addition to this, would be great to add some guide on how this should be used. We could have one initial setup, just import the module augmentation, and one more advanced, for example how can I add palette tokens for other components.

At first, I thought that I will update the docs in a separate PR but seems like it is better to do it here.

@siriwatknp
Copy link
Member Author

We need tests for the module augmentation

Added.

});

const TestStyled = styled('div')(({ theme }) => ({
color: theme.vars.palette.primary.main,
Copy link
Member

Choose a reason for hiding this comment

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

I am a bit confused. In order for this to work developers need to import

import type {} from '@mui/material/themeCssVarsAugmentation';

How does it work here without importing it. This was my initial question.

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jun 22, 2022
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jun 22, 2022
@siriwatknp siriwatknp merged commit 9cfcd8c into mui:master Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: material-ui Specific to @mui/material typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants