Skip to content

Commit 7d03b20

Browse files
committed
refactor(material-experimental/theming): Add M3 density support
M3 does not yet have systemized density. For now we'll just use the same one from M2, based on the density scale of [0, -5].
1 parent 027b171 commit 7d03b20

File tree

4 files changed

+106
-12
lines changed

4 files changed

+106
-12
lines changed

src/dev-app/theme-token-api.scss

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
@use 'sass:map';
12
@use '@angular/material' as mat;
23
@use '@angular/material-experimental' as matx;
34

5+
// Add an indicator to make it clear that the app is styled with the experimental theming API.
46
dev-app {
57
&::before {
68
content: 'Using experimental theming API';
@@ -16,23 +18,47 @@ dev-app {
1618
}
1719
}
1820

19-
.demo-unicorn-dark-theme {
20-
background: black;
21-
color: white;
22-
}
23-
21+
// Emit Angular Material core styles.
2422
@include mat.core();
2523

26-
$m3-theme: matx.define-theme((
24+
// Base theme configuration for our M3 theme.
25+
$m3-base-config: (
2726
color: (
2827
primary: matx.$m3-green-palette
2928
),
3029
typography: (
3130
brand-family: 'monospace'
3231
)
33-
));
32+
);
3433

34+
// Our M3 light theme.
35+
$light-theme: matx.define-theme($m3-base-config);
36+
37+
// Our M3 dark theme.
38+
$dark-theme: matx.define-theme(map.set($m3-base-config, color, theme-type, dark));
39+
40+
// Emit default theme styles.
3541
html {
36-
@include mat.checkbox-theme($m3-theme);
37-
@include mat.card-theme($m3-theme);
42+
@include mat.checkbox-theme($light-theme);
43+
@include mat.card-theme($light-theme);
44+
}
45+
46+
// Emit dark theme styles.
47+
.demo-unicorn-dark-theme {
48+
// TODO(mmalerba): choose colors from the theming API.
49+
background: black;
50+
color: white;
51+
52+
@include mat.checkbox-color($dark-theme);
53+
@include mat.card-color($dark-theme);
54+
}
55+
56+
// Emit density styles for each scale.
57+
@each $scale in (maximum, 0, -1, -2, -3, minimum) {
58+
$scale-theme: matx.define-theme(map.set($m3-base-config, density, scale, $scale));
59+
60+
.demo-density-#{$scale} {
61+
@include mat.checkbox-density($scale-theme);
62+
@include mat.card-density($scale-theme);
63+
}
3864
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
@use 'sass:list';
2+
@use 'sass:map';
3+
@use '@angular/material' as mat;
4+
5+
/// Maps namespaced tokens to per-density-scale values.
6+
/// This is used as a temporary solution for density, since Material Design currently does not have
7+
/// systemized density.
8+
/// Format:
9+
/// (
10+
/// (mdc, comp): (
11+
/// token: (<scale 0 value>, <scale -1 value>, <scale -2 value>, ...),
12+
/// ...
13+
/// ),
14+
/// ...
15+
/// )
16+
// TODO(mmalerba): Add density tokens for remaining components.
17+
$_density-tokens: (
18+
(mdc, checkbox): (
19+
state-layer-size: (40px, 36px, 32px, 28px),
20+
),
21+
(mdc, elevated-card): (),
22+
(mdc, outlined-card): (),
23+
(mat, card): (),
24+
);
25+
26+
/// Gets the value for the given density scale from the given set of density values.
27+
/// @param {List} $density-values The list of values for each density scale.
28+
/// @param {Number} $scale The density scale to get the value for.
29+
/// @return {*} The value for the given scale.
30+
@function _get-value-for-scale($density-values, $scale){
31+
$scale: mat.private-clamp-density($scale, -1 * list.length($density-values) + 1);
32+
$index: -$scale + 1;
33+
@return list.nth($density-values, $index);
34+
}
35+
36+
/// Gets a map of all density tokens for the given scale
37+
/// @param {Number} $scale The density scale
38+
/// @return {Map} Map of all fully qualified density tokens for the given scale.
39+
@function get-tokens-for-scale($scale) {
40+
$result: ();
41+
@each $namespace, $tokens in $_density-tokens {
42+
@each $token, $density-values in $tokens {
43+
$tokens: map.set($tokens, $token, _get-value-for-scale($density-values, $scale));
44+
}
45+
$result: map.set($result, $namespace, $tokens);
46+
}
47+
@return $result;
48+
}
49+
50+
/// Checks whether the given token is systemized by Angular Material's made up density system.
51+
/// @param {List} $namespace The namespace of the token
52+
/// @param {String} $token The name of the token
53+
/// @return {Boolean} Whether the token is systemized by the density system
54+
@function is-systemized($namespace, $token) {
55+
@return map.get($_density-tokens, $namespace, $token) != null;
56+
}

src/material-experimental/theming/_m3-tokens.scss

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@use 'sass:map';
22
@use '@angular/material' as mat;
33
@use '@material/tokens/v0_161' as mdc-tokens;
4+
@use './m3-density';
45
@use './m3-palettes';
56
@use './custom-tokens';
67

@@ -166,7 +167,7 @@
166167
$token-slots: mat.m2-tokens-from-theme($fake-theme);
167168

168169
// TODO(mmalerba): Fill in remaining tokens.
169-
@return mat.private-merge-all(
170+
$result: mat.private-merge-all(
170171
// Add the system color & typography tokens (so we can give users access via an API).
171172
(
172173
(mdc, theme): map.get($systems, md-sys-color),
@@ -196,6 +197,17 @@
196197
$token-slots
197198
),
198199
);
200+
201+
// Strip out tokens that are systemized by our made up density system.
202+
@each $namespace, $tokens in $result {
203+
@each $token, $value in $tokens {
204+
@if m3-density.is-systemized($namespace, $token) {
205+
$tokens: map.remove($tokens, $token);
206+
}
207+
}
208+
$result: map.set($result, $namespace, $tokens);
209+
}
210+
@return $result;
199211
}
200212

201213
/// Generates a set of namespaced color tokens for all components.
@@ -239,8 +251,7 @@
239251
/// @param {String|Number} $scale The regular font-weight
240252
/// @return {Map} A map of namespaced density tokens
241253
@function generate-density-tokens($scale) {
242-
// TODO(mmalerba): Implement density.
243-
@return ();
254+
@return m3-density.get-tokens-for-scale($scale);
244255
}
245256

246257
/// Generates a set of namespaced tokens not related to color, typography, or density for all

src/material/_index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
define-palette, get-contrast-color-from-palette, get-color-from-palette,
44
get-color-config, get-typography-config, get-density-config,
55
$theme-ignore-duplication-warnings;
6+
@forward './core/theming/theming' as private-* show private-clamp-density;
67
@forward './core/theming/palette' show $red-palette, $pink-palette, $indigo-palette,
78
$purple-palette, $deep-purple-palette, $blue-palette, $light-blue-palette, $cyan-palette,
89
$teal-palette, $green-palette, $light-green-palette, $lime-palette, $yellow-palette,

0 commit comments

Comments
 (0)