Skip to content

Commit 7de316f

Browse files
crisbetokara
authored andcommitted
feat: add initial sass typography API (#4162)
1 parent 7f0f473 commit 7de316f

File tree

3 files changed

+122
-9
lines changed

3 files changed

+122
-9
lines changed

src/lib/core/style/_variables.scss

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
@import '../typography/typography';
2-
3-
41
// Typography
5-
$mat-body-font-size-base: rem(1.4) !default;
2+
$mat-body-font-size-base: 14px !default;
63
$mat-font-family: Roboto, 'Helvetica Neue', sans-serif !default;
74

85
// Media queries
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Utility for fetching a nested value from a typography config.
2+
@function _mat-get-type-value($config, $level, $name) {
3+
@return map-get(map-get($config, $level), $name);
4+
}
5+
6+
// Gets the font size for a level inside a typography config.
7+
@function mat-font-size($config, $level) {
8+
@return _mat-get-type-value($config, $level, font-size);
9+
}
10+
11+
// Gets the line height for a level inside a typography config.
12+
@function mat-line-height($config, $level) {
13+
@return _mat-get-type-value($config, $level, line-height);
14+
}
15+
16+
// Gets the font weight for a level inside a typography config.
17+
@function mat-font-weight($config, $level) {
18+
@return _mat-get-type-value($config, $level, font-weight);
19+
}
20+
21+
// Gets the font-family from a typography config and removes the quotes around it.
22+
@function mat-font-family($config) {
23+
@return unquote(map-get($config, font-family));
24+
}
25+
26+
// Converts a typography level into CSS styles.
27+
@mixin mat-typography-level-to-styles($config, $level) {
28+
$font-size: mat-font-size($config, $level);
29+
$font-weight: mat-font-weight($config, $level);
30+
$line-height: mat-line-height($config, $level);
31+
$font-family: mat-font-family($config);
32+
33+
// Use the shorthand `font` to represent a typography level, because it's the shortest. Notes that
34+
// we need to use interpolation for `font-size/line-height` in order to prevent SASS from dividing
35+
// the two values.
36+
font: $font-weight #{$font-size}/#{$line-height} $font-family;
37+
}
Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,85 @@
1-
// Implement the rem unit with SCSS so we don't have to actually set a font-size on
2-
// the user's body element.
3-
@function rem($multiplier) {
4-
$font-size: 10px;
5-
@return $multiplier * $font-size;
1+
@import 'typography-utils';
2+
3+
// Represents a typography level from the Material design spec.
4+
@function mat-typography-level($font-size, $line-height: $font-size, $font-weight: 400) {
5+
@return (
6+
font-size: $font-size,
7+
line-height: $line-height,
8+
font-weight: $font-weight
9+
);
10+
}
11+
12+
// Represents a collection of typography levels.
13+
// Defaults come from https://material.io/guidelines/style/typography.html
14+
@function mat-typography-config(
15+
$font-family: 'Roboto, sans-serif',
16+
$display-4: mat-typography-level(112px, 112px, 300),
17+
$display-3: mat-typography-level(56px, 56px, 400),
18+
$display-2: mat-typography-level(45px, 48px, 400),
19+
$display-1: mat-typography-level(34px, 40px, 400),
20+
$headline: mat-typography-level(24px, 32px, 400),
21+
$title: mat-typography-level(20px, 20px, 500),
22+
$subheading: mat-typography-level(16px, 28px, 400),
23+
$body-2: mat-typography-level(14px, 24px, 500),
24+
$body-1: mat-typography-level(14px, 20px, 400),
25+
$caption: mat-typography-level(12px, 12px, 400),
26+
$button: mat-typography-level(14px, 14px, 500)
27+
) {
28+
@return (
29+
font-family: $font-family,
30+
display-4: $display-4,
31+
display-3: $display-3,
32+
display-2: $display-2,
33+
display-1: $display-1,
34+
headline: $headline,
35+
title: $title,
36+
subheading: $subheading,
37+
body-2: $body-2,
38+
body-1: $body-1,
39+
caption: $caption,
40+
button: $button
41+
);
42+
}
43+
44+
// Adds the base typography styles, based on a config.
45+
@mixin mat-typography($config: mat-typography-config(), $selector: '.mat-typography') {
46+
.mat-h0, .mat-hero-header {
47+
@include mat-typography-level-to-styles($config, display-4);
48+
}
49+
50+
.mat-h1, #{$selector} h1 {
51+
@include mat-typography-level-to-styles($config, display-3);
52+
}
53+
54+
.mat-h2, #{$selector} h2 {
55+
@include mat-typography-level-to-styles($config, display-2);
56+
}
57+
58+
.mat-h3, #{$selector} h3 {
59+
@include mat-typography-level-to-styles($config, display-1);
60+
}
61+
62+
.mat-h4, #{$selector} h4 {
63+
@include mat-typography-level-to-styles($config, headline);
64+
}
65+
66+
.mat-h5, #{$selector} h5 {
67+
@include mat-typography-level-to-styles($config, title);
68+
}
69+
70+
.mat-h6, #{$selector} h6 {
71+
@include mat-typography-level-to-styles($config, subheading);
72+
}
73+
74+
.mat-body-strong {
75+
@include mat-typography-level-to-styles($config, body-2);
76+
}
77+
78+
.mat-body, #{$selector} {
79+
@include mat-typography-level-to-styles($config, body-1);
80+
}
81+
82+
.mat-small {
83+
@include mat-typography-level-to-styles($config, caption);
84+
}
685
}

0 commit comments

Comments
 (0)