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
[TypeScript] Add CSS vars type augmentation for Material UI #33211
Conversation
…rial-ui/css-vars-types
There was a problem hiding this 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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
At first, I thought that I will update the docs in a separate PR but seems like it is better to do it here. |
Added. |
docs/data/material/experimental-api/css-variables/css-variables.md
Outdated
Show resolved
Hide resolved
}); | ||
|
||
const TestStyled = styled('div')(({ theme }) => ({ | ||
color: theme.vars.palette.primary.main, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good 👍
…rial-ui/css-vars-types
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.After importing, the theme types in
styled
,sx
, anduseTheme
are extended. See DemoHow it works?
The new types lives in the new
extendTheme
file which combine the existingPalette
with new tokens. The new types used inextendTheme
andCssVarsProvider
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.
This mean if developers does not import the augmentation, they can use
extendTheme
withCssVarsProvider
(the implementation would work, they can seetheme.vars
in console.log) but they won't seetheme.vars
as types.