-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
_typography.scss
272 lines (241 loc) · 10.6 KB
/
_typography.scss
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
@use 'sass:map';
@use 'typography-utils';
@use '../theming/theming';
/// Defines a typography level from the Material Design spec.
/// @param {String} $font-size The font-size for this level.
/// @param {String | Number} $line-height The line-height for this level.
/// @param {String | Number} $font-weight The font-weight for this level.
/// @param {String} $font-family The font-family for this level.
/// @param {String} $letter-spacing The letter-spacing for this level.
/// @returns {Map} A map representing the definition of this typpographic level.
@function define-typography-level(
$font-size,
$line-height: $font-size,
$font-weight: 400,
$font-family: null,
$letter-spacing: normal) {
@return (
font-size: $font-size,
line-height: $line-height,
font-weight: $font-weight,
font-family: $font-family,
letter-spacing: $letter-spacing
);
}
/// Defines a collection of typography levels to configure typography for an application.
/// Any level not specified defaults to the values defined in the Material Design specification:
/// https://material.io/guidelines/style/typography.html.
///
/// Note that the Material Design specification does not describe explicit letter-spacing values.
/// The values here come from reverse engineering the Material Design examples.
/// @param {String} $font-family Default font-family for levels that don't specify font-family.
/// @param {Map} $display-4 Configuration for the "display-4" typographic level.
/// @param {Map} $display-3 Configuration for the "display-3" typographic level.
/// @param {Map} $display-2 Configuration for the "display-2" typographic level.
/// @param {Map} $display-1 Configuration for the "display-1" typographic level.
/// @param {Map} $headline Configuration for the "headline" typographic level.
/// @param {Map} $title Configuration for the "title" typographic level.
/// @param {Map} $subheading-2 Configuration for the "subheading-2" typographic level.
/// @param {Map} $subheading-1 Configuration for the "subheading-1" typographic level.
/// @param {Map} $body-2 Configuration for the "body-2" typographic level.
/// @param {Map} $body-1 Configuration for the "body-1" typographic level.
/// @param {Map} $caption Configuration for the "caption" typographic level.
/// @param {Map} $button Configuration for the "button" typographic level.
/// @param {Map} $input Configuration for the "input" typographic level.
/// @returns {Map} A typography config for the application.
@function define-typography-config(
$font-family: 'Roboto, "Helvetica Neue", sans-serif',
$display-4: define-typography-level(112px, 112px, 300, $letter-spacing: -0.05em),
$display-3: define-typography-level(56px, 56px, 400, $letter-spacing: -0.02em),
$display-2: define-typography-level(45px, 48px, 400, $letter-spacing: -0.005em),
$display-1: define-typography-level(34px, 40px, 400),
$headline: define-typography-level(24px, 32px, 400),
$title: define-typography-level(20px, 32px, 500),
$subheading-2: define-typography-level(16px, 28px, 400),
$subheading-1: define-typography-level(15px, 24px, 400),
$body-2: define-typography-level(14px, 24px, 500),
$body-1: define-typography-level(14px, 20px, 400),
$caption: define-typography-level(12px, 20px, 400),
$button: define-typography-level(14px, 14px, 500),
// Line-height must be unit-less fraction of the font-size.
$input: define-typography-level(inherit, 1.125, 400)
) {
// Declare an initial map with all of the levels.
$config: (
display-4: $display-4,
display-3: $display-3,
display-2: $display-2,
display-1: $display-1,
headline: $headline,
title: $title,
subheading-2: $subheading-2,
subheading-1: $subheading-1,
body-2: $body-2,
body-1: $body-1,
caption: $caption,
button: $button,
input: $input,
);
// Loop through the levels and set the `font-family` of the ones that don't have one to the base.
// Note that Sass can't modify maps in place, which means that we need to merge and re-assign.
@each $key, $level in $config {
@if map.get($level, font-family) == null {
$new-level: map.merge($level, (font-family: $font-family));
$config: map.merge($config, ($key: $new-level));
}
}
// Add the base font family to the config.
@return map.merge($config, (font-family: $font-family));
}
// Whether a config is for the Material Design 2018 typography system.
@function private-typography-is-2018-config($config) {
@return map.get($config, headline-1) != null;
}
// Whether a config is for the Material Design 2014 typography system.
@function private-typography-is-2014-config($config) {
@return map.get($config, headline) != null;
}
// Given a config for either the 2014 or 2018 Material Design typography system,
// produces a normalized typography config for the 2014 Material Design typography system.
// 2014 - https://material.io/archive/guidelines/style/typography.html#typography-styles
// 2018 - https://material.io/design/typography/the-type-system.html#type-scale
//
// Components using this function should be migrated to normalize to the 2018 style config instead.
// New components should not use this function.
@function private-typography-to-2014-config($config) {
@if $config == null {
@return null;
}
@if not private-typography-is-2014-config($config) {
$args: (
display-4: map.get($config, headline-1),
display-3: map.get($config, headline-2),
display-2: map.get($config, headline-3),
display-1: map.get($config, headline-4),
headline: map.get($config, headline-5),
title: map.get($config, headline-6),
subheading-2: map.get($config, subtitle-1),
subheading-1: map.get($config, subtitle-2),
body-2: map.get($config, body-1),
body-1: map.get($config, body-2),
button: map.get($config, button),
caption: map.get($config, caption),
);
$non-null-args: ();
@each $key, $value in $args {
@if $value != null {
$non-null-args: map.merge($non-null-args, ($key: $value));
}
}
@return define-typography-config($non-null-args...);
}
@return $config;
}
// Given a config for either the 2014 or 2018 Material Design typography system,
// produces a normalized typography config for the 2018 Material Design typography system.
// 2014 - https://material.io/archive/guidelines/style/typography.html#typography-styles
// 2018 - https://material.io/design/typography/the-type-system.html#type-scale
@function private-typography-to-2018-config($config) {
@if $config == null {
@return null;
}
@if not private-typography-is-2018-config($config) {
@return (
headline-1: map.get($config, display-4),
headline-2: map.get($config, display-3),
headline-3: map.get($config, display-2),
headline-4: map.get($config, display-1),
headline-5: map.get($config, headline),
headline-6: map.get($config, title),
subtitle-1: map.get($config, subheading-2),
// These mappings are odd, but body-2 in the 2014 system actually looks closer to subtitle-2
// in the 2018 system, and subeading-1 in the 2014 system looks more like body-1 in the 2018
// system.
subtitle-2: map.get($config, body-2),
body-1: map.get($config, subheading-1),
body-2: map.get($config, body-1),
button: map.get($config, button),
caption: map.get($config, caption),
overline: if(map.get($config, overline), map.get($config, overline),
define-typography-level(12px, 32px, 500)
)
);
}
@return $config;
}
// stylelint-disable material/theme-mixin-api
/// Emits baseline typographic styles based on a given config.
/// @param {Map} $config-or-theme A typography config for an entire theme.
/// @param {String} $selector Ancestor selector under which native elements, such as h1, will
/// be styled.
@mixin typography-hierarchy($config-or-theme, $selector: '.mat-typography') {
$config: private-typography-to-2014-config(theming.get-typography-config($config-or-theme));
.mat-h1, .mat-headline, #{$selector} h1 {
@include typography-utils.typography-level($config, headline);
margin: 0 0 16px;
}
.mat-h2, .mat-title, #{$selector} h2 {
@include typography-utils.typography-level($config, title);
margin: 0 0 16px;
}
.mat-h3, .mat-subheading-2, #{$selector} h3 {
@include typography-utils.typography-level($config, subheading-2);
margin: 0 0 16px;
}
.mat-h4, .mat-subheading-1, #{$selector} h4 {
@include typography-utils.typography-level($config, subheading-1);
margin: 0 0 16px;
}
// Note: the spec doesn't have anything that would correspond to h5 and h6, but we add these for
// consistency. The font sizes come from the Chrome user agent styles which have h5 at 0.83em
// and h6 at 0.67em.
.mat-h5, #{$selector} h5 {
@include typography-utils.font-shorthand(
// calc is used here to support css variables
calc(#{typography-utils.font-size($config, body-1)} * 0.83),
typography-utils.font-weight($config, body-1),
typography-utils.line-height($config, body-1),
typography-utils.font-family($config, body-1)
);
margin: 0 0 12px;
}
.mat-h6, #{$selector} h6 {
@include typography-utils.font-shorthand(
// calc is used here to support css variables
calc(#{typography-utils.font-size($config, body-1)} * 0.67),
typography-utils.font-weight($config, body-1),
typography-utils.line-height($config, body-1),
typography-utils.font-family($config, body-1)
);
margin: 0 0 12px;
}
.mat-body-strong, .mat-body-2 {
@include typography-utils.typography-level($config, body-2);
}
.mat-body, .mat-body-1, #{$selector} {
@include typography-utils.typography-level($config, body-1);
p {
margin: 0 0 12px;
}
}
.mat-small, .mat-caption {
@include typography-utils.typography-level($config, caption);
}
.mat-display-4, #{$selector} .mat-display-4 {
@include typography-utils.typography-level($config, display-4);
margin: 0 0 56px;
}
.mat-display-3, #{$selector} .mat-display-3 {
@include typography-utils.typography-level($config, display-3);
margin: 0 0 64px;
}
.mat-display-2, #{$selector} .mat-display-2 {
@include typography-utils.typography-level($config, display-2);
margin: 0 0 64px;
}
.mat-display-1, #{$selector} .mat-display-1 {
@include typography-utils.typography-level($config, display-1);
margin: 0 0 64px;
}
}
// stylelint-enable material/theme-mixin-api