Skip to content

Commit 8e45545

Browse files
committed
refactor(material/core): split custom tokens into separate files
Separates out the custom M3 tokens into different files so they're easier to manage. (cherry picked from commit 5e98b0d)
1 parent 0ccc528 commit 8e45545

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1924
-1624
lines changed

src/material/core/tokens/_custom-tokens.scss

Lines changed: 14 additions & 1327 deletions
Large diffs are not rendered by default.

src/material/core/tokens/_m3-tokens.scss

Lines changed: 30 additions & 297 deletions
Large diffs are not rendered by default.

src/material/core/tokens/_token-utils.scss

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@use 'sass:list';
22
@use 'sass:map';
3+
@use 'sass:meta';
34
@use '@material/elevation/elevation-theme' as mdc-elevation-theme;
45
@use '@material/theme/custom-properties' as mdc-custom-properties;
56
@use '@material/theme/theme' as mdc-theme;
@@ -234,6 +235,59 @@ $_component-prefix: null;
234235
}
235236
}
236237

238+
/// Gets the MDC tokens for the given prefix, M3 token values, and supported token slots.
239+
/// @param {List} $prefix The token prefix for the given tokens.
240+
/// @param {Map|(Map, Map)} $values A map of M3 token values for the given prefix.
241+
/// This param may also be a tuple of maps, the first one representing the default M3 token values,
242+
// and the second containing overrides for different color variants.
243+
// Single map example:
244+
// (token1: green, token2: 2px)
245+
// Tuple example:
246+
// (
247+
// (token1: green, token2: 2px),
248+
// (
249+
// secondary: (token1: blue),
250+
// error: (token1: red),
251+
// )
252+
// )
253+
/// @param {Map} $slots A map of token slots, with null value indicating the token is not supported.
254+
/// @param {String|null} $variant The name of the variant the token values are for.
255+
/// @return {Map} A map of fully qualified token names to values, for only the supported tokens.
256+
@function namespace-tokens($prefix, $values, $slots, $variant: null) {
257+
$result: ();
258+
@if $variant == null and meta.type-of($values) == 'list' and list.length($values == 2) {
259+
$variants: list.nth($values, 2);
260+
$values: list.nth($values, 1);
261+
@each $variant, $overrides in $variants {
262+
$result: map.merge($result, namespace-tokens($prefix, $overrides, $slots, $variant));
263+
}
264+
}
265+
$used-token-names: map.keys(_filter-nulls(map.get($slots, $prefix)));
266+
$used-m3-tokens: _pick(_filter-nulls($values), $used-token-names);
267+
$prefix: if($variant == null, $prefix, list.append($prefix, $variant));
268+
@return map.merge($result, ($prefix: $used-m3-tokens));
269+
}
270+
271+
/// Hardcode the given value, or null if hardcoded values are excluded.
272+
@function hardcode($value, $exclude-hardcoded) {
273+
@return if($exclude-hardcoded, null, $value);
274+
}
275+
276+
/// Sets all of the standard typography tokens for the given token base name to the given typography
277+
/// level.
278+
/// @param {Map} $systems The MDC system tokens
279+
/// @param {String} $base-name The token base name to get the typography tokens for
280+
/// @param {String} $typography-level The typography level to base the token values on
281+
/// @return {Map} A map containing the typography tokens for the given base token name
282+
@function generate-typography-tokens($systems, $base-name, $typography-level) {
283+
$result: ();
284+
@each $prop in (font, line-height, size, tracking, weight) {
285+
$result: map.set($result, #{$base-name}-#{$prop},
286+
map.get($systems, md-sys-typescale, #{$typography-level}-#{$prop}));
287+
}
288+
@return $result;
289+
}
290+
237291
/// Verifies that the token overrides exist and are used in one of the given token maps.
238292
@mixin _validate-token-overrides($overrides: (), $token-maps) {
239293
$valid-token-names: ();
@@ -255,3 +309,31 @@ $_component-prefix: null;
255309
}
256310
}
257311
}
312+
313+
/// Picks a submap containing only the given keys out the given map.
314+
/// @param {Map} $map The map to pick from.
315+
/// @param {List} $keys The map keys to pick.
316+
/// @return {Map} A submap containing only the given keys.
317+
@function _pick($map, $keys) {
318+
$result: ();
319+
@each $key in $keys {
320+
@if map.has-key($map, $key) {
321+
$result: map.set($result, $key, map.get($map, $key));
322+
}
323+
}
324+
@return $result;
325+
}
326+
327+
328+
/// Filters keys with a null value out of the map.
329+
/// @param {Map} $map The map to filter.
330+
/// @return {Map} The given map with all of the null keys filtered out.
331+
@function _filter-nulls($map) {
332+
$result: ();
333+
@each $key, $val in $map {
334+
@if $val != null {
335+
$result: map.set($result, $key, $val);
336+
}
337+
}
338+
@return $result;
339+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
@use 'sass:meta';
2+
@use 'sass:map';
3+
@use './mat/app' as tokens-mat-app;
4+
@use './mat/autocomplete' as tokens-mat-autocomplete;
5+
@use './mat/badge' as tokens-mat-badge;
6+
@use './mat/text-button' as tokens-mat-text-button;
7+
@use './mat/protected-button' as tokens-mat-protected-button;
8+
@use './mat/filled-button' as tokens-mat-filled-button;
9+
@use './mat/outlined-button' as tokens-mat-outlined-button;
10+
@use './mat/dialog' as tokens-mat-dialog;
11+
@use './mat/bottom-sheet' as tokens-mat-bottom-sheet;
12+
@use './mat/card' as tokens-mat-card;
13+
@use './mat/chip' as tokens-mat-chip;
14+
@use './mat/datepicker' as tokens-mat-datepicker;
15+
@use './mat/divider' as tokens-mat-divider;
16+
@use './mat/expansion' as tokens-mat-expansion;
17+
@use './mat/fab' as tokens-mat-fab;
18+
@use './mat/fab-small' as tokens-mat-fab-small;
19+
@use './mat/form-field' as tokens-mat-form-field;
20+
@use './mat/grid-list' as tokens-mat-grid-list;
21+
@use './mat/icon' as tokens-mat-icon;
22+
@use './mat/icon-button' as tokens-mat-icon-button;
23+
@use './mat/list' as tokens-mat-list;
24+
@use './mat/menu' as tokens-mat-menu;
25+
@use './mat/option' as tokens-mat-option;
26+
@use './mat/optgroup' as tokens-mat-optgroup;
27+
@use './mat/paginator' as tokens-mat-paginator;
28+
@use './mat/checkbox' as tokens-mat-checkbox;
29+
@use './mat/full-pseudo-checkbox' as tokens-mat-full-pseudo-checkbox;
30+
@use './mat/minimal-pseudo-checkbox' as tokens-mat-minimal-pseudo-checkbox;
31+
@use './mat/radio' as tokens-mat-radio;
32+
@use './mat/ripple' as tokens-mat-ripple;
33+
@use './mat/select' as tokens-mat-select;
34+
@use './mat/sidenav' as tokens-mat-sidenav;
35+
@use './mat/slider' as tokens-mat-slider;
36+
@use './mat/switch' as tokens-mat-switch;
37+
@use './mat/snack-bar' as tokens-mat-snack-bar;
38+
@use './mat/sort' as tokens-mat-sort;
39+
@use './mat/standard-button-toggle' as tokens-mat-button-toggle;
40+
@use './mat/stepper' as tokens-mat-stepper;
41+
@use './mat/tab-header' as tokens-mat-tab-header;
42+
@use './mat/table' as tokens-mat-table;
43+
@use './mat/toolbar' as tokens-mat-toolbar;
44+
@use './mat/tree' as tokens-mat-tree;
45+
// @use './mdc/checkbox' as tokens-mdc-checkbox;
46+
// @use './mdc/text-button' as tokens-mdc-text-button;
47+
// @use './mdc/protected-button' as tokens-mdc-protected-button;
48+
// @use './mdc/filled-button' as tokens-mdc-filled-button;
49+
// @use './mdc/outlined-button' as tokens-mdc-outlined-button;
50+
// @use './mdc/chip' as tokens-mdc-chip;
51+
// @use './mdc/circular-progress' as tokens-mdc-circular-progress;
52+
// @use './mdc/dialog' as tokens-mdc-dialog;
53+
// @use './mdc/elevated-card' as tokens-mdc-elevated-card;
54+
// @use './mdc/extended-fab' as tokens-mdc-extended-fab;
55+
// @use './mdc/fab' as tokens-mdc-fab;
56+
// @use './mdc/fab-small' as tokens-mdc-fab-small;
57+
// @use './mdc/form-field' as tokens-mdc-form-field;
58+
// @use './mdc/filled-text-field' as tokens-mdc-filled-text-field;
59+
// @use './mdc/icon-button' as tokens-mdc-icon-button;
60+
// @use './mdc/linear-progress' as tokens-mdc-linear-progress;
61+
// @use './mdc/list' as tokens-mdc-list;
62+
// @use './mdc/outlined-card' as tokens-mdc-outlined-card;
63+
// @use './mdc/outlined-text-field' as tokens-mdc-outlined-text-field;
64+
// @use './mdc/plain-tooltip' as tokens-mdc-plain-tooltip;
65+
// @use './mdc/radio' as tokens-mdc-radio;
66+
// @use './mdc/slider' as tokens-mdc-slider;
67+
// @use './mdc/snack-bar' as tokens-mdc-snack-bar;
68+
// @use './mdc/switch' as tokens-mdc-switch;
69+
// @use './mdc/tab' as tokens-mdc-tab;
70+
// @use './mdc/tab-indicator' as tokens-mdc-tab-indicator;
71+
72+
$_module-names: (
73+
tokens-mat-app,
74+
tokens-mat-autocomplete,
75+
tokens-mat-badge,
76+
tokens-mat-bottom-sheet,
77+
tokens-mat-button-toggle,
78+
tokens-mat-card,
79+
tokens-mat-chip,
80+
tokens-mat-datepicker,
81+
tokens-mat-dialog,
82+
tokens-mat-divider,
83+
tokens-mat-expansion,
84+
tokens-mat-fab,
85+
tokens-mat-fab-small,
86+
tokens-mat-filled-button,
87+
tokens-mat-form-field,
88+
tokens-mat-grid-list,
89+
tokens-mat-icon-button,
90+
tokens-mat-icon,
91+
tokens-mat-menu,
92+
tokens-mat-optgroup,
93+
tokens-mat-option,
94+
tokens-mat-outlined-button,
95+
tokens-mat-paginator,
96+
tokens-mat-checkbox,
97+
tokens-mat-full-pseudo-checkbox,
98+
tokens-mat-list,
99+
tokens-mat-minimal-pseudo-checkbox,
100+
tokens-mat-protected-button,
101+
tokens-mat-radio,
102+
tokens-mat-ripple,
103+
tokens-mat-select,
104+
tokens-mat-sidenav,
105+
tokens-mat-slider,
106+
tokens-mat-switch,
107+
tokens-mat-snack-bar,
108+
tokens-mat-sort,
109+
tokens-mat-stepper,
110+
tokens-mat-tab-header,
111+
tokens-mat-table,
112+
tokens-mat-text-button,
113+
tokens-mat-toolbar,
114+
tokens-mat-tree,
115+
);
116+
117+
/// Gets the full set of M3 tokens for the given theme object.
118+
/// @param {Map} $systems The MDC system tokens
119+
/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values
120+
/// @param {Map} $token-slots Possible token slots
121+
/// @return {Map} Full set of M3 tokens
122+
@function get-m3-tokens($systems, $exclude-hardcoded, $token-slots) {
123+
$tokens: ();
124+
125+
@each $module-name in $_module-names {
126+
$fn: meta.get-function($name: 'get-tokens', $module: $module-name);
127+
$tokens: map.merge($tokens, meta.call($fn, $systems, $exclude-hardcoded, $token-slots));
128+
}
129+
130+
@return $tokens;
131+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@use 'sass:map';
2+
@use '../../token-utils';
3+
4+
// The prefix used to generate the fully qualified name for tokens in this file.
5+
$prefix: (mat, app);
6+
7+
/// Generates custom tokens for the app.
8+
/// @param {Map} $systems The MDC system tokens
9+
/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values
10+
/// @param {Map} $token-slots Possible token slots
11+
/// @return {Map} A set of custom tokens for the app
12+
@function get-tokens($systems, $exclude-hardcoded, $token-slots) {
13+
$tokens: (
14+
background-color: map.get($systems, md-sys-color, background),
15+
text-color: map.get($systems, md-sys-color, on-background),
16+
);
17+
18+
@return token-utils.namespace-tokens($prefix, $tokens, $token-slots);
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@use 'sass:map';
2+
@use '@material/elevation/elevation-theme' as mdc-elevation;
3+
@use '../../token-utils';
4+
5+
// The prefix used to generate the fully qualified name for tokens in this file.
6+
$prefix: (mat, autocomplete);
7+
8+
/// Generates custom tokens for the mat-autocomplete.
9+
/// @param {Map} $systems The MDC system tokens
10+
/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values
11+
/// @param {Map} $token-slots Possible token slots
12+
/// @return {Map} A set of custom tokens for the mat-autocomplete
13+
@function get-tokens($systems, $exclude-hardcoded, $token-slots) {
14+
$tokens: (
15+
background-color: map.get($systems, md-sys-color, surface-container),
16+
container-shape: map.get($systems, md-sys-shape, corner-extra-small),
17+
container-elevation-shadow:
18+
token-utils.hardcode(mdc-elevation.elevation-box-shadow(2), $exclude-hardcoded),
19+
);
20+
21+
@return token-utils.namespace-tokens($prefix, $tokens, $token-slots);
22+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
@use 'sass:map';
2+
@use '../../../style/sass-utils';
3+
@use '../../token-utils';
4+
5+
// The prefix used to generate the fully qualified name for tokens in this file.
6+
$prefix: (mat, badge);
7+
8+
/// Generates custom tokens for the mat-badge.
9+
/// @param {Map} $systems The MDC system tokens
10+
/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values
11+
/// @param {Map} $token-slots Possible token slots
12+
/// @return {Map} A set of custom tokens for the mat-badge
13+
@function get-tokens($systems, $exclude-hardcoded, $token-slots) {
14+
$tokens: ((
15+
background-color: map.get($systems, md-sys-color, error),
16+
text-color: map.get($systems, md-sys-color, on-error),
17+
disabled-state-background-color: sass-utils.safe-color-change(
18+
map.get($systems, md-sys-color, error),
19+
$alpha: 0.38,
20+
),
21+
disabled-state-text-color: map.get($systems, md-sys-color, on-error),
22+
text-font: map.get($systems, md-sys-typescale, label-small-font),
23+
text-size: map.get($systems, md-sys-typescale, label-small-size),
24+
text-weight: map.get($systems, md-sys-typescale, label-small-weight),
25+
small-size-text-size: token-utils.hardcode(0, $exclude-hardcoded),
26+
container-shape: map.get($systems, md-sys-shape, corner-full),
27+
container-size: token-utils.hardcode(16px, $exclude-hardcoded),
28+
legacy-container-size: token-utils.hardcode(unset, $exclude-hardcoded),
29+
legacy-small-size-container-size: token-utils.hardcode(unset, $exclude-hardcoded),
30+
small-size-container-size: token-utils.hardcode(6px, $exclude-hardcoded),
31+
container-padding: token-utils.hardcode(0 4px, $exclude-hardcoded),
32+
small-size-container-padding: token-utils.hardcode(0, $exclude-hardcoded),
33+
container-offset: token-utils.hardcode(-12px 0, $exclude-hardcoded),
34+
small-size-container-offset: token-utils.hardcode(-6px 0, $exclude-hardcoded),
35+
container-overlap-offset: token-utils.hardcode(-12px, $exclude-hardcoded),
36+
small-size-container-overlap-offset: token-utils.hardcode(-6px, $exclude-hardcoded),
37+
38+
// This size isn't in the M3 spec so we emit the same values as for `medium`.
39+
large-size-container-size: token-utils.hardcode(16px, $exclude-hardcoded),
40+
large-size-container-offset: token-utils.hardcode(-12px 0, $exclude-hardcoded),
41+
large-size-container-overlap-offset: token-utils.hardcode(-12px, $exclude-hardcoded),
42+
large-size-text-size: map.get($systems, md-sys-typescale, label-small-size),
43+
large-size-container-padding: token-utils.hardcode(0 4px, $exclude-hardcoded),
44+
legacy-large-size-container-size: token-utils.hardcode(unset, $exclude-hardcoded),
45+
), (
46+
primary: (
47+
background-color: map.get($systems, md-sys-color, primary),
48+
text-color: map.get($systems, md-sys-color, on-primary),
49+
disabled-state-background-color: sass-utils.safe-color-change(
50+
map.get($systems, md-sys-color, primary),
51+
$alpha: 0.38,
52+
),
53+
disabled-state-text-color: map.get($systems, md-sys-color, on-primary),
54+
),
55+
secondary: (
56+
background-color: map.get($systems, md-sys-color, secondary),
57+
text-color: map.get($systems, md-sys-color, on-secondary),
58+
disabled-state-background-color: sass-utils.safe-color-change(
59+
map.get($systems, md-sys-color, secondary),
60+
$alpha: 0.38,
61+
),
62+
disabled-state-text-color: map.get($systems, md-sys-color, on-secondary),
63+
),
64+
tertiary: (
65+
background-color: map.get($systems, md-sys-color, tertiary),
66+
text-color: map.get($systems, md-sys-color, on-tertiary),
67+
disabled-state-background-color: sass-utils.safe-color-change(
68+
map.get($systems, md-sys-color, tertiary),
69+
$alpha: 0.38,
70+
),
71+
disabled-state-text-color: map.get($systems, md-sys-color, on-tertiary),
72+
),
73+
error: () // Default, no overrides needed
74+
));
75+
76+
@return token-utils.namespace-tokens($prefix, $tokens, $token-slots);
77+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@use 'sass:map';
2+
@use '../../../style/sass-utils';
3+
@use '../../token-utils';
4+
5+
// The prefix used to generate the fully qualified name for tokens in this file.
6+
$prefix: (mat, bottom-sheet);
7+
8+
/// Generates custom tokens for the mat-bottom-sheet.
9+
/// @param {Map} $systems The MDC system tokens
10+
/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values
11+
/// @param {Map} $token-slots Possible token slots
12+
/// @return {Map} A set of custom tokens for the mat-bottom-sheet
13+
@function get-tokens($systems, $exclude-hardcoded, $token-slots) {
14+
$tokens: sass-utils.merge-all(
15+
token-utils.generate-typography-tokens($systems, container-text, body-large),
16+
(
17+
container-shape: token-utils.hardcode(28px, $exclude-hardcoded),
18+
container-text-color: map.get($systems, md-sys-color, on-surface),
19+
container-background-color: map.get($systems, md-sys-color, surface-container-low),
20+
),
21+
);
22+
23+
@return token-utils.namespace-tokens($prefix, $tokens, $token-slots);
24+
}

0 commit comments

Comments
 (0)