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] Support incomplete headlineMapping property #13078

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/src/pages/style/typography/DeprecatedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ function DeprecatedTypes(props) {
return (
<MuiThemeProvider theme={typographyV1Theme}>
<div className={classes.root}>
<Typography variant="display4" gutterBottom>
<Typography component="h2" variant="display4" gutterBottom>
Display 4
</Typography>
<Typography variant="display3" gutterBottom>
<Typography component="h2" variant="display3" gutterBottom>
Display 3
</Typography>
<Typography variant="display2" gutterBottom>
<Typography component="h2" variant="display2" gutterBottom>
Display 2
</Typography>
<Typography variant="display1" gutterBottom>
<Typography component="h2" variant="display1" gutterBottom>
Display 1
</Typography>
<Typography variant="headline" gutterBottom>
<Typography component="h2" variant="headline" gutterBottom>
Headline
</Typography>
<Typography variant="title" gutterBottom>
Expand Down
107 changes: 43 additions & 64 deletions docs/src/pages/style/typography/Types.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from 'react';
import PropTypes from 'prop-types';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import Typography from '@material-ui/core/Typography';
import { withStyles } from '@material-ui/core/styles';

Expand All @@ -10,75 +8,56 @@ const styles = {
width: '100%',
maxWidth: 500,
},
list: {
padding: 0,
},
listItem: {
display: 'inline-block',
margin: '5px 0',
padding: 0,
},
};

function TypographyVariant(props) {
const { children, classes, ...typographyProps } = props;
return (
<ListItem className={classes.listItem}>
<Typography {...typographyProps}>{children}</Typography>
</ListItem>
);
}

function Types(props) {
const { classes } = props;

return (
<div className={classes.root}>
<List className={classes.list}>
<TypographyVariant classes={classes} variant="h1">
H1
</TypographyVariant>
<TypographyVariant classes={classes} variant="h2">
H2
</TypographyVariant>
<TypographyVariant classes={classes} variant="h3">
H3
</TypographyVariant>
<TypographyVariant classes={classes} variant="h4">
H4
</TypographyVariant>
<TypographyVariant classes={classes} variant="h5">
H5
</TypographyVariant>
<TypographyVariant classes={classes} variant="h6">
H6
</TypographyVariant>
<TypographyVariant classes={classes} variant="subtitle1">
subtitle 1
</TypographyVariant>
<TypographyVariant classes={classes} variant="subtitle2">
subtitle 2
</TypographyVariant>
<TypographyVariant classes={classes} variant="body1">
Body 1. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos blanditiis tenetur
unde suscipit, quam beatae rerum inventore consectetur, neque doloribus, cupiditate
numquam dignissimos laborum fugiat deleniti? Eum quasi quidem quibusdam.
</TypographyVariant>
<TypographyVariant classes={classes} variant="body2">
Body 2. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos blanditiis tenetur
unde suscipit, quam beatae rerum inventore consectetur, neque doloribus, cupiditate
numquam dignissimos laborum fugiat deleniti? Eum quasi quidem quibusdam.
</TypographyVariant>
<TypographyVariant classes={classes} variant="button">
button text
</TypographyVariant>
<TypographyVariant classes={classes} variant="caption">
caption text
</TypographyVariant>
<TypographyVariant classes={classes} variant="overline">
overline text
</TypographyVariant>
</List>
<Typography component="h2" variant="h1" gutterBottom>
H1
</Typography>
<Typography variant="h2" gutterBottom>
H2
</Typography>
<Typography variant="h3" gutterBottom>
H3
</Typography>
<Typography variant="h4" gutterBottom>
H4
</Typography>
<Typography variant="h5" gutterBottom>
H5
</Typography>
<Typography variant="h6" gutterBottom>
H6
</Typography>
<Typography variant="subtitle1" gutterBottom>
subtitle 1
</Typography>
<Typography variant="subtitle2" gutterBottom>
subtitle 2
</Typography>
<Typography variant="body1" gutterBottom>
Body 1. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos blanditiis tenetur
unde suscipit, quam beatae rerum inventore consectetur, neque doloribus, cupiditate numquam
dignissimos laborum fugiat deleniti? Eum quasi quidem quibusdam.
</Typography>
<Typography variant="body2" gutterBottom>
Body 2. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos blanditiis tenetur
unde suscipit, quam beatae rerum inventore consectetur, neque doloribus, cupiditate numquam
dignissimos laborum fugiat deleniti? Eum quasi quidem quibusdam.
</Typography>
<Typography variant="button" gutterBottom>
button text
</Typography>
<Typography variant="caption" gutterBottom>
caption text
</Typography>
<Typography variant="overline" gutterBottom>
overline text
</Typography>
</div>
);
}
Expand Down
51 changes: 29 additions & 22 deletions packages/material-ui/src/Typography/Typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ function getVariant(theme, props, variant) {
return variant;
}

const defaultHeadlingMapping = {
h1: 'h1',
eps1lon marked this conversation as resolved.
Show resolved Hide resolved
h2: 'h2',
h3: 'h3',
h4: 'h4',
h5: 'h5',
h6: 'h6',
subtitle1: 'h6',
subtitle2: 'h6',
body1: 'p',
body2: 'p',
// deprecated
display4: 'h1',
display3: 'h1',
display2: 'h1',
display1: 'h1',
headline: 'h1',
title: 'h2',
subheading: 'h3',
};

function Typography(props) {
const {
align,
Expand Down Expand Up @@ -195,7 +216,10 @@ function Typography(props) {
classNameProp,
);

const Component = componentProp || (paragraph ? 'p' : headlineMapping[variantProp]) || 'span';
const Component =
componentProp ||
(paragraph ? 'p' : headlineMapping[variant] || defaultHeadlingMapping[variant]) ||
'span';

return <Component className={className} {...other} />;
}
Expand Down Expand Up @@ -242,8 +266,10 @@ Typography.propTypes = {
gutterBottom: PropTypes.bool,
/**
* We are empirically mapping the variant property to a range of different DOM element types.
* For instance, h1 to h6. If you wish to change that mapping, you can provide your own.
* For instance, title to `<h2>`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

title is deprecated. Better use a new variant.

* If you wish to change that mapping, you can provide your own.
* Alternatively, you can use the `component` property.
* The default mapping is the following:
*/
headlineMapping: PropTypes.object,
/**
Expand Down Expand Up @@ -295,26 +321,7 @@ Typography.defaultProps = {
align: 'inherit',
color: 'default',
gutterBottom: false,
headlineMapping: {
h1: 'h1',
h2: 'h2',
h3: 'h3',
h4: 'h4',
h5: 'h5',
h6: 'h6',
subtitle1: 'h6',
subtitle2: 'h6',
body1: 'p',
body2: 'p',
// deprecated
display4: 'h1',
display3: 'h1',
display2: 'h1',
display1: 'h1',
headline: 'h1',
title: 'h2',
subheading: 'h3',
},
headlineMapping: defaultHeadlingMapping,
noWrap: false,
paragraph: false,
variant: 'body1',
Expand Down
20 changes: 20 additions & 0 deletions packages/material-ui/src/Typography/Typography.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,24 @@ describe('<Typography />', () => {
});
});
});

describe('prop: headlineMapping', () => {
it('should work with a single value', () => {
const wrapper = shallow(
<Typography variant="title" headlineMapping={{ title: 'aside' }}>
Hello
</Typography>,
);
assert.strictEqual(wrapper.type(), 'aside');
});

it('should work event without the full mapping', () => {
const wrapper = shallow(
<Typography variant="title" headlineMapping={{}}>
Hello
</Typography>,
);
assert.strictEqual(wrapper.type(), 'h2');
});
});
});
2 changes: 1 addition & 1 deletion pages/api/typography.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Typography from '@material-ui/core/Typography';
| <span class="prop-name">color</span> | <span class="prop-type">enum:&nbsp;'default', 'error', 'inherit', 'primary', 'secondary', 'textPrimary', 'textSecondary'<br> | <span class="prop-default">'default'</span> | The color of the component. It supports those theme colors that make sense for this component. |
| <span class="prop-name">component</span> | <span class="prop-type">union:&nbsp;string&nbsp;&#124;<br>&nbsp;func&nbsp;&#124;<br>&nbsp;object<br> |   | The component used for the root node. Either a string to use a DOM element or a component. By default, it maps the variant to a good default headline component. |
| <span class="prop-name">gutterBottom</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If `true`, the text will have a bottom margin. |
| <span class="prop-name">headlineMapping</span> | <span class="prop-type">object | <span class="prop-default">{ h1: 'h1', h2: 'h2', h3: 'h3', h4: 'h4', h5: 'h5', h6: 'h6', subtitle1: 'h6', subtitle2: 'h6', body1: 'p', body2: 'p', // deprecated display4: 'h1', display3: 'h1', display2: 'h1', display1: 'h1', headline: 'h1', title: 'h2', subheading: 'h3',}</span> | We are empirically mapping the variant property to a range of different DOM element types. For instance, h1 to h6. If you wish to change that mapping, you can provide your own. Alternatively, you can use the `component` property. |
| <span class="prop-name">headlineMapping</span> | <span class="prop-type">object | <span class="prop-default">{ h1: 'h1', h2: 'h2', h3: 'h3', h4: 'h4', h5: 'h5', h6: 'h6', subtitle1: 'h6', subtitle2: 'h6', body1: 'p', body2: 'p', // deprecated display4: 'h1', display3: 'h1', display2: 'h1', display1: 'h1', headline: 'h1', title: 'h2', subheading: 'h3',}</span> | We are empirically mapping the variant property to a range of different DOM element types. For instance, title to `<h2>`. If you wish to change that mapping, you can provide your own. Alternatively, you can use the `component` property. The default mapping is the following: |
| <span class="prop-name">internalDeprecatedVariant</span> | <span class="prop-type">bool |   | A deprecated variant is used from an internal component. Users don't need a deprecation warning here if they switched to the v2 theme. They already get the mapping that will be applied in the next major release. |
| <span class="prop-name">noWrap</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If `true`, the text will not wrap, but instead will truncate with an ellipsis. |
| <span class="prop-name">paragraph</span> | <span class="prop-type">bool | <span class="prop-default">false</span> | If `true`, the text will have a bottom margin. |
Expand Down