Skip to content

Commit a46b0a1

Browse files
authored
feat(material/core): add mixins for Material Design typography (#32959)
1 parent 1210f51 commit a46b0a1

File tree

5 files changed

+102
-30
lines changed

5 files changed

+102
-30
lines changed

src/material/_index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@forward './core/theming/theming' as private-* show private-clamp-density;
1818
@forward './core/typography/typography' show typography-hierarchy;
1919
@forward './core/typography/typography-utils' show font-shorthand;
20+
@forward './core/theming/typography' as typography-*;
2021
@forward 'core/tokens/system' show system-level-colors,
2122
system-level-typography, system-level-elevation, system-level-shape,
2223
system-level-motion, system-level-state, theme, theme-overrides, m2-theme;

src/material/core/theming/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ sass_library(
5959
],
6060
)
6161

62+
sass_library(
63+
name = "typography",
64+
srcs = ["_typography.scss"],
65+
)
66+
6267
sass_library(
6368
name = "_inspection",
6469
srcs = ["_inspection.scss"],
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Mixins that apply typography styles including font and letter-spacing for each of
2+
// Material Design's typography levels.
3+
// See https://m3.material.io/styles/typography/applying-type for guidance.
4+
5+
@mixin body-small {
6+
font: var(--mat-sys-body-small);
7+
letter-spacing: var(--mat-sys-body-small-tracking);
8+
}
9+
10+
@mixin body-medium {
11+
font: var(--mat-sys-body-medium);
12+
letter-spacing: var(--mat-sys-body-medium-tracking);
13+
}
14+
15+
@mixin body-large {
16+
font: var(--mat-sys-body-large);
17+
letter-spacing: var(--mat-sys-body-large-tracking);
18+
}
19+
20+
@mixin display-small {
21+
font: var(--mat-sys-display-small);
22+
letter-spacing: var(--mat-sys-display-small-tracking);
23+
}
24+
25+
@mixin display-medium {
26+
font: var(--mat-sys-display-medium);
27+
letter-spacing: var(--mat-sys-display-medium-tracking);
28+
}
29+
30+
@mixin display-large {
31+
font: var(--mat-sys-display-large);
32+
letter-spacing: var(--mat-sys-display-large-tracking);
33+
}
34+
35+
@mixin headline-small {
36+
font: var(--mat-sys-headline-small);
37+
letter-spacing: var(--mat-sys-headline-small-tracking);
38+
}
39+
40+
@mixin headline-medium {
41+
font: var(--mat-sys-headline-medium);
42+
letter-spacing: var(--mat-sys-headline-medium-tracking);
43+
}
44+
45+
@mixin headline-large {
46+
font: var(--mat-sys-headline-large);
47+
letter-spacing: var(--mat-sys-headline-large-tracking);
48+
}
49+
50+
@mixin label-small {
51+
font: var(--mat-sys-label-small);
52+
letter-spacing: var(--mat-sys-label-small-tracking);
53+
}
54+
55+
@mixin label-medium {
56+
font: var(--mat-sys-label-medium);
57+
letter-spacing: var(--mat-sys-label-medium-tracking);
58+
}
59+
60+
@mixin label-large {
61+
font: var(--mat-sys-label-large);
62+
letter-spacing: var(--mat-sys-label-large-tracking);
63+
}
64+
65+
@mixin title-small {
66+
font: var(--mat-sys-title-small);
67+
letter-spacing: var(--mat-sys-title-small-tracking);
68+
}
69+
70+
@mixin title-medium {
71+
font: var(--mat-sys-title-medium);
72+
letter-spacing: var(--mat-sys-title-medium-tracking);
73+
}
74+
75+
@mixin title-large {
76+
font: var(--mat-sys-title-large);
77+
letter-spacing: var(--mat-sys-title-large-tracking);
78+
}

src/material/core/tokens/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,5 @@ sass_library(
9090
sass_library(
9191
name = "classes",
9292
srcs = ["_classes.scss"],
93+
deps = ["//src/material/core/theming:typography"],
9394
)

src/material/core/tokens/_classes.scss

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@use '../theming/typography';
2+
13
// Utility classes that can be used to style elements with the most commonly used system tokens in
24
// Material Design. Includes color, typography, elevation, state, and shape.
35
@mixin system-classes() {
@@ -195,107 +197,92 @@
195197
// Sets the font to the body small typeface. Use for small body text, such as captions. In Angular
196198
// Material, this is used for the subscript text in a form field and the text in a paginator.
197199
.mat-font-body-sm {
198-
font: var(--mat-sys-body-small);
199-
letter-spacing: var(--mat-sys-body-small-tracking);
200+
@include typography.body-small();
200201
}
201202

202203
// Sets the font to the body medium typeface. Use for medium body text, this is the default
203204
// body font. In Angular Material, this is used for the text in a table row and the supporting
204205
// text in a dialog.
205206
.mat-font-body-md {
206-
font: var(--mat-sys-body-medium);
207-
letter-spacing: var(--mat-sys-body-medium-tracking);
207+
@include typography.body-medium();
208208
}
209209

210210
// Sets the font to the body large typeface. Use for large body text, such as an introductory
211211
// paragraph. In Angular Material, this is used for the text in a list item and the text in a
212212
// select trigger.
213213
.mat-font-body-lg {
214-
font: var(--mat-sys-body-large);
215-
letter-spacing: var(--mat-sys-body-large-tracking);
214+
@include typography.body-large();
216215
}
217216

218217
// Sets the font to the display small typeface. Use for small display text, such as a date.
219218
.mat-font-display-sm {
220-
font: var(--mat-sys-display-small);
221-
letter-spacing: var(--mat-sys-display-small-tracking);
219+
@include typography.display-small();
222220
}
223221

224222
// Sets the font to the display medium typeface. Use for medium display text, such as a hero
225223
// title.
226224
.mat-font-display-md {
227-
font: var(--mat-sys-display-medium);
228-
letter-spacing: var(--mat-sys-display-medium-tracking);
225+
@include typography.display-medium();
229226
}
230227

231228
// Sets the font to the display large typeface. Use for large display text, such as a hero title.
232229
.mat-font-display-lg {
233-
font: var(--mat-sys-display-large);
234-
letter-spacing: var(--mat-sys-display-large-tracking);
230+
@include typography.display-large();
235231
}
236232

237233
// Sets the font to the headline small typeface. Use for small headlines, such as a page title. In
238234
// Angular Material, this is used for the headline in a dialog.
239235
.mat-font-headline-sm {
240-
font: var(--mat-sys-headline-small);
241-
letter-spacing: var(--mat-sys-headline-small-tracking);
236+
@include typography.headline-small();
242237
}
243238

244239
// Sets the font to the headline medium typeface. Use for medium headlines, such as a section
245240
// title.
246241
.mat-font-headline-md {
247-
font: var(--mat-sys-headline-medium);
248-
letter-spacing: var(--mat-sys-headline-medium-tracking);
242+
@include typography.headline-medium();
249243
}
250244

251245
// Sets the font to the headline large typeface. Use for large headlines, such as a page title on
252246
// a large screen.
253247
.mat-font-headline-lg {
254-
font: var(--mat-sys-headline-large);
255-
letter-spacing: var(--mat-sys-headline-large-tracking);
248+
@include typography.headline-large();
256249
}
257250

258251
// Sets the font to the label small typeface. Use for small labels, such as text in a badge.
259252
.mat-font-label-sm {
260-
font: var(--mat-sys-label-small);
261-
letter-spacing: var(--mat-sys-label-small-tracking);
253+
@include typography.label-small();
262254
}
263255

264256
// Sets the font to the label medium typeface. Use for medium labels. In Angular Material, this
265257
// is used for the slider label.
266258
.mat-font-label-md {
267-
font: var(--mat-sys-label-medium);
268-
letter-spacing: var(--mat-sys-label-medium-tracking);
259+
@include typography.label-medium();
269260
}
270261

271262
// Sets the font to the label large typeface. Use for large labels. In Angular Material, this is
272263
// used for buttons, chips, and menu labels.
273264
.mat-font-label-lg {
274-
font: var(--mat-sys-label-large);
275-
letter-spacing: var(--mat-sys-label-large-tracking);
265+
@include typography.label-large();
276266
}
277267

278268
// Sets the font to the title small typeface. Use for small titles, such as a card title. In
279269
// Angular Material, this is used for the header of a table and the label of an option group.
280270
.mat-font-title-sm {
281-
font: var(--mat-sys-title-small);
282-
letter-spacing: var(--mat-sys-title-small-tracking);
271+
@include typography.title-small();
283272
}
284273

285274
// Sets the font to the title medium typeface. Use for medium titles, such as a dialog title
286275
// or the primary text in a list item. In Angular Material, this is used for the subtitle
287276
// of a card and the header of an expansion panel.
288277
.mat-font-title-md {
289-
font: var(--mat-sys-title-medium);
290-
letter-spacing: var(--mat-sys-title-medium-tracking);
278+
@include typography.title-medium();
291279
}
292280

293281
// Sets the font to the title large typeface. Use for large titles, such as a page title on a
294282
// small screen. In Angular Material, this is used for the title of a card and the title of a
295283
// toolbar.
296284
.mat-font-title-lg {
297-
font: var(--mat-sys-title-large);
298-
letter-spacing: var(--mat-sys-title-large-tracking);
285+
@include typography.title-large();
299286
}
300287

301288

0 commit comments

Comments
 (0)