|
1 | 1 | @use 'sass:map';
|
| 2 | +@use 'sass:math'; |
| 3 | +@use 'sass:meta'; |
2 | 4 | @use '@material/typography' as mdc-typography;
|
3 | 5 | @use './typography';
|
4 | 6 | @use '../../autocomplete/autocomplete-theme';
|
|
45 | 47 | // (where define-typography-config is defined) because putting it there would cause a circular
|
46 | 48 | // dependency with mdc-helpers. We should refactor so these can live in the same place.
|
47 | 49 |
|
| 50 | +// Converts a map containing rem values to a map containing px values. |
| 51 | +@function _rem-to-px($x, $px-per-rem: 16px) { |
| 52 | + @if meta.type-of($x) == 'map' { |
| 53 | + @each $key, $val in $x { |
| 54 | + $x: map.merge($x, ($key: _rem-to-px($val))); |
| 55 | + } |
| 56 | + @return $x; |
| 57 | + } |
| 58 | + @if meta.type-of($x) == 'number' and math.unit($x) == 'rem' { |
| 59 | + @return ($x / 1rem) * $px-per-rem; |
| 60 | + } |
| 61 | + @else { |
| 62 | + @return $x; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// Generates an Angular Material typography config based on values from the official Material |
| 67 | +/// Design spec implementation (MDC Web). All arguments are optional, but may be passed to override |
| 68 | +/// the default values. The `mat-typography-level` function can be used to generate a custom |
| 69 | +/// typography level map which can be passed to this function to override one of the default levels. |
| 70 | +/// All default typography sizing generated by this function is in `rem` units. |
| 71 | +/// |
| 72 | +/// @param {String} $font-family The font family to use for levels where it is not explicitly |
| 73 | +/// specified. |
| 74 | +/// @param {Map} $headline-1 The font settings for the headline-1 font level. |
| 75 | +/// @param {Map} $headline-2 The font settings for the headline-2 font level. |
| 76 | +/// @param {Map} $headline-3 The font settings for the headline-3 font level. |
| 77 | +/// @param {Map} $headline-4 The font settings for the headline-4 font level. |
| 78 | +/// @param {Map} $headline-5 The font settings for the headline-5 font level. |
| 79 | +/// @param {Map} $headline-6 The font settings for the headline-6 font level. |
| 80 | +/// @param {Map} $subtitle-1 The font settings for the subtitle-1 font level. |
| 81 | +/// @param {Map} $subtitle-2 The font settings for the subtitle-2 font level. |
| 82 | +/// @param {Map} $body-1 The font settings for the body-1 font level. |
| 83 | +/// @param {Map} $body-2 The font settings for the body-2 font level. |
| 84 | +/// @param {Map} $caption The font settings for the caption font level. |
| 85 | +/// @param {Map} $button The font settings for the button font level. |
| 86 | +/// @param {Map} $overline The font settings for the overline font level. |
| 87 | +/// @return {Map} A map containing font settings for each of the levels in the Material Design spec. |
| 88 | +@function define-rem-typography-config( |
| 89 | + // TODO(mmalerba): rename this function to define-typography-config, |
| 90 | + // and create a predefined px based config for people that need it. |
| 91 | + $font-family: mdc-typography.$font-family, |
| 92 | + $headline-1: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(headline1)), |
| 93 | + $headline-2: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(headline2)), |
| 94 | + $headline-3: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(headline3)), |
| 95 | + $headline-4: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(headline4)), |
| 96 | + $headline-5: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(headline5)), |
| 97 | + $headline-6: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(headline6)), |
| 98 | + $subtitle-1: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(subtitle1)), |
| 99 | + $subtitle-2: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(subtitle2)), |
| 100 | + $body-1: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(body1)), |
| 101 | + $body-2: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(body2)), |
| 102 | + $caption: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(caption)), |
| 103 | + $button: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(button)), |
| 104 | + $overline: _rem-to-px(mdc-helpers.typography-config-level-from-mdc(overline)), |
| 105 | +) { |
| 106 | + // Declare an initial map with all of the levels. |
| 107 | + $config: ( |
| 108 | + headline-1: $headline-1, |
| 109 | + headline-2: $headline-2, |
| 110 | + headline-3: $headline-3, |
| 111 | + headline-4: $headline-4, |
| 112 | + headline-5: $headline-5, |
| 113 | + headline-6: $headline-6, |
| 114 | + subtitle-1: $subtitle-1, |
| 115 | + subtitle-2: $subtitle-2, |
| 116 | + body-1: $body-1, |
| 117 | + body-2: $body-2, |
| 118 | + caption: $caption, |
| 119 | + button: $button, |
| 120 | + overline: $overline, |
| 121 | + ); |
| 122 | + |
| 123 | + // Loop through the levels and set the `font-family` of the ones that don't have one to the base. |
| 124 | + // Note that Sass can't modify maps in place, which means that we need to merge and re-assign. |
| 125 | + @each $key, $level in $config { |
| 126 | + @if map.get($level, font-family) == null { |
| 127 | + $new-level: map.merge($level, (font-family: $font-family)); |
| 128 | + $config: map.merge($config, ($key: $new-level)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Add the base font family to the config. |
| 133 | + @return map.merge($config, (font-family: $font-family)); |
| 134 | +} |
| 135 | + |
48 | 136 | /// Generates an Angular Material typography config based on values from the official Material
|
49 | 137 | /// Design spec implementation (MDC Web). All arguments are optional, but may be passed to override
|
50 | 138 | /// the default values. The `mat-typography-level` function can be used to generate a custom
|
51 | 139 | /// typography level map which can be passed to this function to override one of the default levels.
|
| 140 | +/// All default typography sizing generated by this function is in `px` units. |
52 | 141 | ///
|
53 | 142 | /// @param {String} $font-family The font family to use for levels where it is not explicitly
|
54 | 143 | /// specified.
|
|
66 | 155 | /// @param {Map} $button The font settings for the button font level.
|
67 | 156 | /// @param {Map} $overline The font settings for the overline font level.
|
68 | 157 | /// @return {Map} A map containing font settings for each of the levels in the Material Design spec.
|
69 |
| -@function define-mdc-typography-config( |
| 158 | +@function define-typography-config( |
70 | 159 | // TODO(mmalerba): rename this function to define-typography-config,
|
71 | 160 | // and create a predefined px based config for people that need it.
|
72 | 161 | $font-family: mdc-typography.$font-family,
|
|
139 | 228 |
|
140 | 229 | // If no actual color configuration has been specified, create a default one.
|
141 | 230 | @if not $config {
|
142 |
| - $config: typography.define-typography-config(); |
| 231 | + $config: define-typography-config(); |
143 | 232 | }
|
144 | 233 |
|
145 | 234 | // TODO: COMP-309: Do not use individual mixins. Instead, use the all-theme mixin and only
|
|
0 commit comments