-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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] Add custom variants support #22006
Changes from 13 commits
9769894
75cd07b
ca559fc
0ecd5d6
add9f3d
332330c
b34d233
16e61fe
c201240
e1f156b
98c54da
a295bce
8f062ea
202b972
e05f141
ffd21aa
8b6374c
eb12d8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import clsx from 'clsx'; | ||
import { useThemeVariants } from '@material-ui/styles'; | ||
import withStyles from '../styles/withStyles'; | ||
import capitalize from '../utils/capitalize'; | ||
|
||
|
@@ -35,6 +36,8 @@ export const styles = (theme) => ({ | |
subtitle2: theme.typography.subtitle2, | ||
/* Styles applied to the root element if `variant="overline"`. */ | ||
overline: theme.typography.overline, | ||
/* Styles applied to the root element if `variant="inherit"`. */ | ||
inherit: {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency I've added this variant |
||
/* Styles applied to the root element if `align="left"`. */ | ||
alignLeft: { | ||
textAlign: 'left', | ||
|
@@ -110,6 +113,7 @@ const defaultVariantMapping = { | |
subtitle2: 'h6', | ||
body1: 'p', | ||
body2: 'p', | ||
inherit: 'p', | ||
}; | ||
|
||
const Typography = React.forwardRef(function Typography(props, ref) { | ||
|
@@ -128,6 +132,21 @@ const Typography = React.forwardRef(function Typography(props, ref) { | |
...other | ||
} = props; | ||
|
||
const themeVariantsClasses = useThemeVariants( | ||
{ | ||
...props, | ||
align, | ||
color, | ||
display, | ||
gutterBottom, | ||
noWrap, | ||
paragraph, | ||
variant, | ||
variantMapping, | ||
}, | ||
'MuiTypography', | ||
); | ||
|
||
const Component = | ||
component || | ||
(paragraph ? 'p' : variantMapping[variant] || defaultVariantMapping[variant]) || | ||
|
@@ -137,15 +156,16 @@ const Typography = React.forwardRef(function Typography(props, ref) { | |
<Component | ||
className={clsx( | ||
classes.root, | ||
classes[variant], | ||
{ | ||
[classes[variant]]: variant !== 'inherit', | ||
[classes[`color${capitalize(color)}`]]: color !== 'initial', | ||
[classes.noWrap]: noWrap, | ||
[classes.gutterBottom]: gutterBottom, | ||
[classes.paragraph]: paragraph, | ||
[classes[`align${capitalize(align)}`]]: align !== 'inherit', | ||
[classes[`display${capitalize(display)}`]]: display !== 'initial', | ||
}, | ||
themeVariantsClasses, | ||
className, | ||
)} | ||
ref={ref} | ||
|
@@ -215,43 +235,32 @@ Typography.propTypes = { | |
/** | ||
* Applies the theme typography styles. | ||
*/ | ||
variant: PropTypes.oneOf([ | ||
'body1', | ||
'body2', | ||
'button', | ||
'caption', | ||
'h1', | ||
'h2', | ||
'h3', | ||
'h4', | ||
'h5', | ||
'h6', | ||
'inherit', | ||
'overline', | ||
'subtitle1', | ||
'subtitle2', | ||
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([ | ||
PropTypes.oneOf([ | ||
'body1', | ||
'body2', | ||
'button', | ||
'caption', | ||
'h1', | ||
'h2', | ||
'h3', | ||
'h4', | ||
'h5', | ||
'h6', | ||
'inherit', | ||
'overline', | ||
'subtitle1', | ||
'subtitle2', | ||
]), | ||
PropTypes.string, | ||
]), | ||
/** | ||
* The component maps the variant prop to a range of different HTML element types. | ||
* For instance, subtitle1 to `<h6>`. | ||
* If you wish to change that mapping, you can provide your own. | ||
* Alternatively, you can use the `component` prop. | ||
*/ | ||
variantMapping: PropTypes.shape({ | ||
body1: PropTypes.string, | ||
body2: PropTypes.string, | ||
button: PropTypes.string, | ||
caption: PropTypes.string, | ||
h1: PropTypes.string, | ||
h2: PropTypes.string, | ||
h3: PropTypes.string, | ||
h4: PropTypes.string, | ||
h5: PropTypes.string, | ||
h6: PropTypes.string, | ||
overline: PropTypes.string, | ||
subtitle1: PropTypes.string, | ||
subtitle2: PropTypes.string, | ||
}), | ||
variantMapping: PropTypes /* @typescript-to-proptypes-ignore */.object, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With adding new variants new keys can be added here, so this was relaxed to |
||
}; | ||
|
||
export default withStyles(styles, { name: 'MuiTypography' })(Typography); |
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.
There's a typo but why do we want to ignore it? Seems pretty important for prototyping to me.
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.
The variants is now a union of enums + string, my understanding was that we are excluding those props.
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.
Why?
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 believe we would need to fix the Framer generation script, not sure it's worth it:
https://github.com/mui-org/material-ui/blob/41e81f229e8f878287ea082b88b5808061a189ad/framer/scripts/framerConfig.js#L289