diff --git a/packages/mui-material/src/Typography/Typography.js b/packages/mui-material/src/Typography/Typography.js index 37d1d450cfe65f..fd4e59b0a6ffb9 100644 --- a/packages/mui-material/src/Typography/Typography.js +++ b/packages/mui-material/src/Typography/Typography.js @@ -1,9 +1,9 @@ import * as React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; -import { unstable_extendSxProp as extendSxProp } from '@mui/system'; +import { unstable_extendSxProp as extendSxProp, getPath } from '@mui/system'; import { unstable_composeClasses as composeClasses } from '@mui/base'; -import styled from '../styles/styled'; +import styled, { rootShouldForwardProp } from '../styles/styled'; import useThemeProps from '../styles/useThemeProps'; import capitalize from '../utils/capitalize'; import { getTypographyUtilityClass } from './typographyClasses'; @@ -28,6 +28,7 @@ const useUtilityClasses = (ownerState) => { export const TypographyRoot = styled('span', { name: 'MuiTypography', slot: 'Root', + shouldForwardProp: (prop) => rootShouldForwardProp(prop) && prop !== 'color', overridesResolver: (props, styles) => { const { ownerState } = props; @@ -42,6 +43,10 @@ export const TypographyRoot = styled('span', { }, })(({ theme, ownerState }) => ({ margin: 0, + color: + getPath(theme, `palette.${ownerState.color}.main`, false) || + getPath(theme, `palette.${ownerState.color}`, false) || + ownerState.color, ...(ownerState.variant && theme.typography[ownerState.variant]), ...(ownerState.align !== 'inherit' && { textAlign: ownerState.align, @@ -88,25 +93,27 @@ const transformDeprecatedColors = (color) => { const Typography = React.forwardRef(function Typography(inProps, ref) { const themeProps = useThemeProps({ props: inProps, name: 'MuiTypography' }); - const color = transformDeprecatedColors(themeProps.color); - const props = extendSxProp({ ...themeProps, color }); + const props = { ...extendSxProp(themeProps), color: themeProps.color }; const { align = 'inherit', className, + // eslint-disable-next-line react/prop-types + color, component, gutterBottom = false, noWrap = false, paragraph = false, variant = 'body1', variantMapping = defaultVariantMapping, + sx, ...other } = props; const ownerState = { ...props, align, - color, + color: transformDeprecatedColors(color), className, component, gutterBottom, @@ -129,6 +136,11 @@ const Typography = React.forwardRef(function Typography(inProps, ref) { ref={ref} ownerState={ownerState} className={clsx(classes.root, className)} + color={color} + sx={[ + ...(!Object.keys(colorTransformations).includes(color) ? [{ color }] : []), + ...(Array.isArray(sx) ? sx : [sx]), + ]} {...other} /> ); diff --git a/test/regressions/fixtures/TypographyColor/TypographyColor.js b/test/regressions/fixtures/TypographyColor/TypographyColor.js new file mode 100644 index 00000000000000..5b68f262f44441 --- /dev/null +++ b/test/regressions/fixtures/TypographyColor/TypographyColor.js @@ -0,0 +1,59 @@ +import * as React from 'react'; +import { createTheme, ThemeProvider } from '@mui/material/styles'; +import Typography from '@mui/material/Typography'; + +export default function TypographyColor() { + return ( +
+ ({ + ...(ownerState.color === 'secondary.main' && { + color: theme.palette.error.main, + }), + }), + }, + variants: [ + { + props: { color: 'textPrimary' }, + style: ({ theme }) => ({ + color: theme.palette.warning.main, + }), + }, + ], + }, + }, + })} + > + default + primary + error (styleOverride) + warning (variant) + success + success.light + + + #fbca04 + #ff5252 + #ff5252 + ({ color: theme.palette.secondary.main })}>secondary + theme.palette.error.main }}>error + +
+ ); +}