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

[docs] Update code snippet in docs for custom color palette #32946

Conversation

ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli commented May 30, 2022

Fixes #32926

contrastText is undefined when a new custom color is added in theme without a contrastText property. Contrast text color gets calculated for default palette primary, secondary etc. properties but not for custom color keys.

The component crashes if the contrast text is used within alpha due to this particular line where it accesses on undefined.

So we calculate the contrast text color with getContrastText before passing it to alpha when a custom color is provided in theme.palette.

Before: https://codesandbox.io/s/chippropscoloroverrides-failing-example-forked-m4nvsl?file=/src/App.tsx
Now: https://codesandbox.io/s/chippropscoloroverrides-failing-example-forked-1jlyfl?file=/src/App.tsx

Edit: The docs are updated instead.


Docs preview: https://deploy-preview-32946--material-ui.netlify.app/material-ui/customization/palette/#providing-the-colors-directly

@ZeeshanTamboli ZeeshanTamboli added component: chip This is the name of the generic UI component, not the React module! bug 🐛 Something doesn't work labels May 30, 2022
@mui-bot
Copy link

mui-bot commented May 30, 2022

No bundle size changes

Generated by 🚫 dangerJS against 15ee409

@ZeeshanTamboli ZeeshanTamboli changed the title [Chip] Do not crash when a new color is added in theme and used [Chip] Fix component crash when a new color is added in theme and used May 30, 2022
@ZeeshanTamboli ZeeshanTamboli marked this pull request as ready for review May 30, 2022 07:09
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jun 17, 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 20, 2022
@ZeeshanTamboli ZeeshanTamboli changed the title [Chip] Fix component crash when a new color is added in theme and used [Chip] Update code snippet in docs for custom color palette Jun 22, 2022
@ZeeshanTamboli ZeeshanTamboli changed the title [Chip] Update code snippet in docs for custom color palette [docs] Update code snippet in docs for custom color palette Jun 22, 2022
@ZeeshanTamboli ZeeshanTamboli added the docs Improvements or additions to the documentation label Jun 22, 2022
Copy link
Member

@siriwatknp siriwatknp 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 to me.

@ZeeshanTamboli ZeeshanTamboli merged commit b90770d into mui:master Jun 23, 2022
@ZeeshanTamboli ZeeshanTamboli deleted the issue/32926-chip-crash-with-custom-color branch June 23, 2022 07:13
@yueveron
Copy link

yueveron commented Jun 16, 2023

Hi, I use jest reac test chip-component, it also show error "cannot read properties of undefined reading 'contrasttext'".
How can I fixed it in jest

@soothran

This comment was marked as off-topic.

@mnajdova
Copy link
Member

Please open a separate issue with a reproduction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: chip This is the name of the generic UI component, not the React module! docs Improvements or additions to the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Chip] Component crashes when trying to use a custom color from the theme
7 participants