-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Typography.js
179 lines (171 loc) · 4.18 KB
/
Typography.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import withStyles from '../styles/withStyles';
import { capitalizeFirstLetter } from '../utils/helpers';
export const styles = theme => ({
root: {
display: 'block',
margin: 0,
},
display4: theme.typography.display4,
display3: theme.typography.display3,
display2: theme.typography.display2,
display1: theme.typography.display1,
headline: theme.typography.headline,
title: theme.typography.title,
subheading: theme.typography.subheading,
body2: theme.typography.body2,
body1: theme.typography.body1,
caption: theme.typography.caption,
button: theme.typography.button,
alignLeft: {
textAlign: 'left',
},
alignCenter: {
textAlign: 'center',
},
alignRight: {
textAlign: 'right',
},
alignJustify: {
textAlign: 'justify',
},
noWrap: {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
},
gutterBottom: {
marginBottom: '0.35em',
},
paragraph: {
marginBottom: theme.spacing.unit * 2,
},
colorInherit: {
color: 'inherit',
},
colorPrimary: {
color: theme.palette.primary[500],
},
colorSecondary: {
color: theme.palette.text.secondary,
},
colorAccent: {
color: theme.palette.secondary.A400,
},
colorError: {
color: theme.palette.error.A400,
},
});
function Typography(props) {
const {
align,
classes,
className: classNameProp,
component: componentProp,
color,
gutterBottom,
headlineMapping,
noWrap,
paragraph,
type,
...other
} = props;
const className = classNames(
classes.root,
classes[type],
{
[classes[`color${capitalizeFirstLetter(color)}`]]: color !== 'default',
[classes.noWrap]: noWrap,
[classes.gutterBottom]: gutterBottom,
[classes.paragraph]: paragraph,
[classes[`align${capitalizeFirstLetter(align)}`]]: align !== 'inherit',
},
classNameProp,
);
const Component = componentProp || (paragraph ? 'p' : headlineMapping[type]) || 'span';
return <Component className={className} {...other} />;
}
Typography.propTypes = {
/**
* Set the text-align on the component.
*/
align: PropTypes.oneOf(['inherit', 'left', 'center', 'right', 'justify']),
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* Useful to extend the style applied to components.
*/
classes: PropTypes.object.isRequired,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the component. It's using the theme palette when that makes sense.
*/
color: PropTypes.oneOf(['inherit', 'primary', 'secondary', 'accent', 'error', 'default']),
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
* By default, it maps the type to a good default headline component.
*/
component: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/**
* If `true`, the text will have a bottom margin.
*/
gutterBottom: PropTypes.bool,
/**
* We are empirically mapping the type property to a range of different DOM element type.
* For instance, h1 to h6. If you wish to change that mapping, you can provide your own.
* Alternatively, you can use the `component` property.
*/
headlineMapping: PropTypes.object,
/**
* If `true`, the text will not wrap, but instead will truncate with an ellipsis.
*/
noWrap: PropTypes.bool,
/**
* If `true`, the text will have a bottom margin.
*/
paragraph: PropTypes.bool,
/**
* Applies the theme typography styles.
*/
type: PropTypes.oneOf([
'display4',
'display3',
'display2',
'display1',
'headline',
'title',
'subheading',
'body2',
'body1',
'caption',
'button',
]),
};
Typography.defaultProps = {
align: 'inherit',
color: 'default',
gutterBottom: false,
headlineMapping: {
display4: 'h1',
display3: 'h1',
display2: 'h1',
display1: 'h1',
headline: 'h1',
title: 'h2',
subheading: 'h3',
body2: 'aside',
body1: 'p',
},
noWrap: false,
paragraph: false,
type: 'body1',
};
export default withStyles(styles, { name: 'MuiTypography' })(Typography);