Skip to content

Commit 12488cb

Browse files
committed
feat(material-experimental/mdc-typography): add a function to create
config from MDC
1 parent b8cdb83 commit 12488cb

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

src/dev-app/theme.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// have to load a single css file for Angular Material in your app.
2020
// **Be sure that you only ever include this mixin once!**
2121
@include mat-core();
22-
@include angular-material-mdc-typography();
22+
@include angular-material-mdc-typography(mat-mdc-typography-config());
2323
@include mat-popover-edit-typography(mat-typography-config());
2424

2525
// Include base styles for strong focus indicators.

src/material-experimental/mdc-helpers/_mdc-helpers.scss

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,14 @@ $mat-typography-2018-level-mappings: (
136136

137137
// Converts an MDC typography level config to an Angular Material one.
138138
@function mat-typography-config-level-from-mdc($mdc-level) {
139+
$mdc-level-config: map-get($mdc-typography-styles, $mdc-level);
140+
139141
@return mat-typography-level(
140-
map-get($mdc-typography-styles, font-size),
141-
map-get($mdc-typography-styles, line-height),
142-
map-get($mdc-typography-styles, font-weight),
143-
map-get($mdc-typography-styles, font-family),
144-
map-get($mdc-typography-styles, letter-spacing));
142+
map-get($mdc-level-config, font-size),
143+
map-get($mdc-level-config, line-height),
144+
map-get($mdc-level-config, font-weight),
145+
map-get($mdc-level-config, font-family),
146+
map-get($mdc-level-config, letter-spacing));
145147
}
146148

147149
// Configures MDC's global variables to reflect the given theme, applies the given styles,

src/material-experimental/mdc-typography/_all-typography.scss

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
1+
@import '@material/typography/variables.import';
12
@import '../mdc-theming/all-theme';
23

4+
/// Generates an Angular Material typography config based on values from the official Material
5+
/// Design spec implementation (MDC Web). All arguments are optional, but may be passed to override
6+
/// the default values. The `mat-typography-level` function can be used to generate a custom
7+
/// typography level map which can be passed to this function to override one of the default levels.
8+
///
9+
/// @param {String} $font-family The font family to use for levels where it is not explicitly
10+
/// specified.
11+
/// @param {Map} $headline-1 The font settings for the headline-1 font level.
12+
/// @param {Map} $headline-2 The font settings for the headline-2 font level.
13+
/// @param {Map} $headline-3 The font settings for the headline-3 font level.
14+
/// @param {Map} $headline-4 The font settings for the headline-4 font level.
15+
/// @param {Map} $headline-5 The font settings for the headline-5 font level.
16+
/// @param {Map} $headline-6 The font settings for the headline-6 font level.
17+
/// @param {Map} $subtitle-1 The font settings for the subtitle-1 font level.
18+
/// @param {Map} $subtitle-2 The font settings for the subtitle-2 font level.
19+
/// @param {Map} $body-1 The font settings for the body-1 font level.
20+
/// @param {Map} $body-2 The font settings for the body-2 font level.
21+
/// @param {Map} $caption The font settings for the caption font level.
22+
/// @param {Map} $button The font settings for the button font level.
23+
/// @param {Map} $overline The font settings for the overline font level.
24+
/// @return {Map} A map containing font settings for each of the levels in the Material Design spec.
25+
@function mat-mdc-typography-config(
26+
$font-family: $mdc-typography-font-family,
27+
$headline-1: mat-typography-config-level-from-mdc(headline1),
28+
$headline-2: mat-typography-config-level-from-mdc(headline2),
29+
$headline-3: mat-typography-config-level-from-mdc(headline3),
30+
$headline-4: mat-typography-config-level-from-mdc(headline4),
31+
$headline-5: mat-typography-config-level-from-mdc(headline5),
32+
$headline-6: mat-typography-config-level-from-mdc(headline6),
33+
$subtitle-1: mat-typography-config-level-from-mdc(subtitle1),
34+
$subtitle-2: mat-typography-config-level-from-mdc(subtitle2),
35+
$body-1: mat-typography-config-level-from-mdc(body1),
36+
$body-2: mat-typography-config-level-from-mdc(body2),
37+
$caption: mat-typography-config-level-from-mdc(caption),
38+
$button: mat-typography-config-level-from-mdc(button),
39+
$overline: mat-typography-config-level-from-mdc(overline),
40+
) {
41+
// Declare an initial map with all of the levels.
42+
$config: (
43+
headline-1: $headline-1,
44+
headline-2: $headline-2,
45+
headline-3: $headline-3,
46+
headline-4: $headline-4,
47+
headline-5: $headline-5,
48+
headline-6: $headline-6,
49+
subtitle-1: $subtitle-1,
50+
subtitle-2: $subtitle-2,
51+
body-1: $body-1,
52+
body-2: $body-2,
53+
caption: $caption,
54+
button: $button,
55+
overline: $overline,
56+
);
57+
58+
// Loop through the levels and set the `font-family` of the ones that don't have one to the base.
59+
// Note that Sass can't modify maps in place, which means that we need to merge and re-assign.
60+
@each $key, $level in $config {
61+
@if map-get($level, font-family) == null {
62+
$new-level: map-merge($level, (font-family: $font-family));
63+
$config: map-merge($config, ($key: $new-level));
64+
}
65+
}
66+
67+
// Add the base font family to the config.
68+
@return map-merge($config, (font-family: $font-family));
69+
}
70+
371
@mixin angular-material-mdc-typography($config-or-theme: null) {
472
$config: if(mat-private-is-theme-object($config-or-theme),
573
mat-get-typography-config($config-or-theme), $config-or-theme);

0 commit comments

Comments
 (0)