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

[Typography] Improve inherit variant logic #38123

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/mui-material/src/Typography/Typography.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface TypographyOwnProps extends SystemProps<Theme> {
* Applies the theme typography styles.
* @default 'body1'
*/
variant?: OverridableStringUnion<Variant | 'inherit', TypographyPropsVariantOverrides>;
variant?: OverridableStringUnion<Variant, TypographyPropsVariantOverrides>;
/**
* The component maps the variant prop to a range of different HTML element types.
* For instance, subtitle1 to `<h6>`.
Expand All @@ -69,7 +69,7 @@ export interface TypographyOwnProps extends SystemProps<Theme> {
* }
*/
variantMapping?: Partial<
Record<OverridableStringUnion<Variant | 'inherit', TypographyPropsVariantOverrides>, string>
Record<OverridableStringUnion<Variant, TypographyPropsVariantOverrides>, string>
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
>;
}

Expand Down
6 changes: 5 additions & 1 deletion packages/mui-material/src/Typography/Typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export const TypographyRoot = styled('span', {
},
})(({ theme, ownerState }) => ({
margin: 0,
...(ownerState.variant && theme.typography[ownerState.variant]),
...(ownerState.variant === 'inherit' && {
// Some elements, like <button> on Chrome have default font that doesn't inherit, reset this.
font: 'inherit',
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
}),
...(ownerState.variant !== 'inherit' && theme.typography[ownerState.variant]),
...(ownerState.align !== 'inherit' && {
textAlign: ownerState.align,
}),
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-material/src/Typography/typography.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const typographyTest = () => {
<Typography component={CustomComponent} prop1="1" />
{/* @ts-expect-error */}
<Typography component={CustomComponent} prop1="1" prop2="12" />
<Typography variant="inherit" />
<Typography variantMapping={{ inherit: 'span' }} />
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
</div>
);
};
3 changes: 2 additions & 1 deletion packages/mui-material/src/styles/createTypography.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type Variant =
| 'body2'
| 'caption'
| 'button'
| 'overline';
| 'overline'
| 'inherit';
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved

export interface FontStyle
extends Required<{
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material/src/styles/createTypography.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default function createTypography(palette, typography) {
button: buildVariant(fontWeightMedium, 14, 1.75, 0.4, caseAllCaps),
caption: buildVariant(fontWeightRegular, 12, 1.66, 0.4),
overline: buildVariant(fontWeightRegular, 12, 2.66, 1, caseAllCaps),
// TODO v6: Remove handling of 'inherit' variant from the theme as it is already handled in Material UI's Typography component. Also, remember to remove the associated types.
inherit: {
fontFamily: 'inherit',
fontWeight: 'inherit',
Expand Down
7 changes: 7 additions & 0 deletions packages/mui-material/src/styles/createTypography.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ import { expectType } from '@mui/types';
...theme.typography.body1,
}));
}

// test for theme.typography.inherit
const Div = styled('div')(({ theme }) => ({
...theme.typography.inherit,
backgroundColor: theme.palette.background.paper,
padding: theme.spacing(1),
}));
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved