Skip to content

Commit 7152cf7

Browse files
committed
Typography: fix styleOverrides and variants to work
They were being overridden by the system props.
1 parent 234ed06 commit 7152cf7

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

packages/mui-material/src/Typography/Typography.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as React from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
4-
import { unstable_extendSxProp as extendSxProp } from '@mui/system';
4+
import { getPath } from '@mui/system';
55
import { unstable_composeClasses as composeClasses } from '@mui/base';
6-
import styled from '../styles/styled';
6+
import styled, { rootShouldForwardProp } from '../styles/styled';
77
import useThemeProps from '../styles/useThemeProps';
88
import capitalize from '../utils/capitalize';
99
import { getTypographyUtilityClass } from './typographyClasses';
@@ -28,6 +28,7 @@ const useUtilityClasses = (ownerState) => {
2828
export const TypographyRoot = styled('span', {
2929
name: 'MuiTypography',
3030
slot: 'Root',
31+
shouldForwardProp: (prop) => rootShouldForwardProp(prop) && prop !== 'color',
3132
overridesResolver: (props, styles) => {
3233
const { ownerState } = props;
3334

@@ -42,6 +43,7 @@ export const TypographyRoot = styled('span', {
4243
},
4344
})(({ theme, ownerState }) => ({
4445
margin: 0,
46+
color: getPath(theme, `palette.${ownerState.color}`, false) || ownerState.color,
4547
...(ownerState.variant && theme.typography[ownerState.variant]),
4648
...(ownerState.align !== 'inherit' && {
4749
textAlign: ownerState.align,
@@ -87,26 +89,27 @@ const transformDeprecatedColors = (color) => {
8789
};
8890

8991
const Typography = React.forwardRef(function Typography(inProps, ref) {
90-
const themeProps = useThemeProps({ props: inProps, name: 'MuiTypography' });
91-
const color = transformDeprecatedColors(themeProps.color);
92-
const props = extendSxProp({ ...themeProps, color });
92+
const props = useThemeProps({ props: inProps, name: 'MuiTypography' });
9393

9494
const {
9595
align = 'inherit',
9696
className,
97+
// eslint-disable-next-line react/prop-types
98+
color,
9799
component,
98100
gutterBottom = false,
99101
noWrap = false,
100102
paragraph = false,
101103
variant = 'body1',
102104
variantMapping = defaultVariantMapping,
105+
sx,
103106
...other
104107
} = props;
105108

106109
const ownerState = {
107110
...props,
108111
align,
109-
color,
112+
color: transformDeprecatedColors(color),
110113
className,
111114
component,
112115
gutterBottom,
@@ -129,6 +132,11 @@ const Typography = React.forwardRef(function Typography(inProps, ref) {
129132
ref={ref}
130133
ownerState={ownerState}
131134
className={clsx(classes.root, className)}
135+
color={color}
136+
sx={[
137+
...(!Object.keys(colorTransformations).includes(color) ? [{ color }] : []),
138+
...(Array.isArray(sx) ? sx : [sx]),
139+
]}
132140
{...other}
133141
/>
134142
);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as React from 'react';
2+
import { createTheme, ThemeProvider } from '@mui/material/styles';
3+
import Typography from '@mui/material/Typography';
4+
5+
export default function TypographyColor() {
6+
return (
7+
<div>
8+
<ThemeProvider
9+
theme={createTheme({
10+
components: {
11+
MuiTypography: {
12+
styleOverrides: {
13+
root: ({ ownerState, theme }) => ({
14+
...(ownerState.color === 'secondary.main' && {
15+
color: theme.palette.error.main,
16+
}),
17+
}),
18+
},
19+
variants: [
20+
{
21+
props: { color: 'textPrimary' },
22+
style: ({ theme }) => ({
23+
color: theme.palette.warning.main,
24+
}),
25+
},
26+
],
27+
},
28+
},
29+
})}
30+
>
31+
<Typography>default</Typography>
32+
<Typography color="primary">primary</Typography>
33+
<Typography color="secondary">error (styleOverride)</Typography>
34+
<Typography color="textPrimary">warning (variant)</Typography>
35+
</ThemeProvider>
36+
<ThemeProvider
37+
theme={createTheme({
38+
components: {
39+
MuiTypography: {
40+
styleOverrides: {
41+
root: {
42+
color: '#fbca04',
43+
},
44+
},
45+
},
46+
},
47+
})}
48+
>
49+
<Typography>#fbca04</Typography>
50+
<Typography color="#ff5252">#ff5252</Typography>
51+
<Typography sx={{ color: '#ff5252' }}>#ff5252</Typography>
52+
<Typography sx={(theme) => ({ color: theme.palette.secondary.main })}>secondary</Typography>
53+
<Typography sx={{ color: (theme) => theme.palette.error.main }}>error</Typography>
54+
</ThemeProvider>
55+
</div>
56+
);
57+
}

0 commit comments

Comments
 (0)