Skip to content

Commit 6baf06c

Browse files
committed
refactor(material/core): use util to build token vars
1 parent 0af3b61 commit 6baf06c

File tree

138 files changed

+889
-880
lines changed

Some content is hidden

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

138 files changed

+889
-880
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
@use 'sass:color';
2+
@use 'sass:list';
3+
@use 'sass:map';
4+
@use 'sass:meta';
5+
@use 'sass:string';
6+
@use 'sass:math';
7+
@use '../m2/palette' as m2-palette;
8+
@use '../m2/theming' as m2-theming;
9+
@use '../m2/typography' as m2-typography;
10+
@use './m3/definitions' as m3-token-definitions;
11+
12+
// Indicates whether we're building internally. Used for backwards compatibility.
13+
$private-is-internal-build: false;
14+
15+
$_placeholder-color-palette: m2-theming.define-palette(m2-palette.$red-palette);
16+
17+
// Placeholder color config that can be passed to token getter functions when generating token
18+
// slots.
19+
$placeholder-color-config: (
20+
primary: $_placeholder-color-palette,
21+
accent: $_placeholder-color-palette,
22+
warn: $_placeholder-color-palette,
23+
is-dark: false,
24+
foreground: m2-palette.$light-theme-foreground-palette,
25+
background: m2-palette.$light-theme-background-palette,
26+
);
27+
28+
$_placeholder-typography-level-config: m2-typography.typography-config-level-from-mdc(body1);
29+
30+
// Placeholder typography config that can be passed to token getter functions when generating token
31+
// slots.
32+
$placeholder-typography-config: (
33+
font-family: 'Roboto, sans-serif',
34+
headline-1: $_placeholder-typography-level-config,
35+
headline-2: $_placeholder-typography-level-config,
36+
headline-3: $_placeholder-typography-level-config,
37+
headline-4: $_placeholder-typography-level-config,
38+
headline-5: $_placeholder-typography-level-config,
39+
headline-6: $_placeholder-typography-level-config,
40+
subtitle-1: $_placeholder-typography-level-config,
41+
subtitle-2: $_placeholder-typography-level-config,
42+
body-1: $_placeholder-typography-level-config,
43+
body-2: $_placeholder-typography-level-config,
44+
caption: $_placeholder-typography-level-config,
45+
button: $_placeholder-typography-level-config,
46+
overline: $_placeholder-typography-level-config,
47+
subheading-1: $_placeholder-typography-level-config,
48+
title: $_placeholder-typography-level-config,
49+
);
50+
51+
// Placeholder density config that can be passed to token getter functions when generating token
52+
// slots.
53+
$placeholder-density-config: 0;
54+
55+
$_tokens: null;
56+
$_component-prefix: null;
57+
$_system-fallbacks: null;
58+
59+
/// Gets all the MDC token values for a specific component. This function serves as single
60+
/// point at which we directly reference a specific version of the MDC tokens.
61+
/// @param {String} $component Name of the component for which to get the tokens
62+
/// @param {Map} $systems The MDC system tokens
63+
/// @param {Boolean} $exclude-hardcoded Whether to exclude hardcoded token values
64+
/// @return {List} Map of token names to values
65+
@function get-mdc-tokens($component, $systems, $exclude-hardcoded) {
66+
$full-name: 'md-comp-' + $component + '-values';
67+
$fn: meta.get-function($name: $full-name, $module: 'm3-token-definitions');
68+
@return meta.call($fn, $systems, $exclude-hardcoded);
69+
}
70+
71+
/// Gets the MDC tokens for the given prefix, M3 token values, and supported token slots.
72+
/// @param {List} $prefix The token prefix for the given tokens.
73+
/// @param {Map|(Map, Map)} $values A map of M3 token values for the given prefix.
74+
/// This param may also be a tuple of maps, the first one representing the default M3 token values,
75+
// and the second containing overrides for different color variants.
76+
// Single map example:
77+
// (token1: green, token2: 2px)
78+
// Tuple example:
79+
// (
80+
// (token1: green, token2: 2px),
81+
// (
82+
// secondary: (token1: blue),
83+
// error: (token1: red),
84+
// )
85+
// )
86+
/// @param {Map} $slots A map of token slots, with null value indicating the token is not supported.
87+
/// @param {String|null} $variant The name of the variant the token values are for.
88+
/// @return {Map} A map of fully qualified token names to values, for only the supported tokens.
89+
@function namespace-tokens($prefix, $values, $slots, $variant: null) {
90+
$result: ();
91+
@if $variant == null and meta.type-of($values) == 'list' and list.length($values == 2) {
92+
$variants: list.nth($values, 2);
93+
$values: list.nth($values, 1);
94+
@each $variant, $overrides in $variants {
95+
$result: map.merge($result, namespace-tokens($prefix, $overrides, $slots, $variant));
96+
}
97+
}
98+
$used-token-names: map.keys(_filter-nulls(map.get($slots, $prefix)));
99+
$used-m3-tokens: _pick(_filter-nulls($values), $used-token-names);
100+
$prefix: if($variant == null, $prefix, list.append($prefix, $variant));
101+
@return map.merge($result, ($prefix: $used-m3-tokens));
102+
}
103+
104+
/// Hardcode the given value, or null if hardcoded values are excluded.
105+
@function hardcode($value, $exclude-hardcoded) {
106+
@return if($exclude-hardcoded, null, $value);
107+
}
108+
109+
/// Sets all of the standard typography tokens for the given token base name to the given typography
110+
/// level.
111+
/// @param {Map} $systems The MDC system tokens
112+
/// @param {String} $base-name The token base name to get the typography tokens for
113+
/// @param {String} $typography-level The typography level to base the token values on
114+
/// @return {Map} A map containing the typography tokens for the given base token name
115+
@function generate-typography-tokens($systems, $base-name, $typography-level) {
116+
$result: ();
117+
@each $prop in (font, line-height, size, tracking, weight) {
118+
$result: map.set($result, #{$base-name}-#{$prop},
119+
map.get($systems, md-sys-typescale, #{$typography-level}-#{$prop}));
120+
}
121+
@return $result;
122+
}
123+
124+
/// Maps the values in a map to new values using the given mapping function
125+
/// @param {Map} $map The maps whose values will be mapped to new values.
126+
/// @param {Function} $fn The value mapping function.
127+
/// @param {Map} A new map with its values updated using the mapping function.
128+
@function map-values($map, $fn) {
129+
$result: ();
130+
@each $key, $value in $map {
131+
$result: map.set($result, $key, meta.call($fn, $value));
132+
}
133+
@return $result;
134+
}
135+
136+
/// Renames the keys in a map
137+
/// @param {Map} $map The map whose keys should be renamed
138+
/// @param {Map} $rename-keys A map of original key to renamed key to apply to $map
139+
/// @return {Map} The result of applying the given key renames to the given map.
140+
@function rename-map-keys($map, $rename-keys) {
141+
$result: $map;
142+
@each $old-key-name, $new-key-name in $rename-keys {
143+
@if map.has-key($map, $old-key-name) {
144+
$result: map.set($result, $new-key-name, map.get($map, $old-key-name));
145+
}
146+
}
147+
@return $result;
148+
}
149+
150+
/// At the time of writing, some color tokens (e.g. disabled state) are defined as a solid color
151+
/// token and a separate opacity token. This function applies the opacity to the color and drops the
152+
/// opacity key from the map. Can be removed once b/213331407 is resolved.
153+
/// @param {Map} $tokens The map of tokens currently being generated
154+
/// @param {Map} $all-tokens A map of all tokens, including hardcoded values
155+
/// @param {List} $pairs Pairs of color token names and their opacities. Should be in the shape of
156+
/// `((color: 'color-key', opacity: 'opacity-key'))`.
157+
/// @return {Map} The initial tokens with the combined color values.
158+
@function combine-color-tokens($tokens, $opacity-lookup, $pairs) {
159+
$result: $tokens;
160+
161+
@each $pair in $pairs {
162+
$color-key: map.get($pair, color);
163+
$opacity-key: map.get($pair, opacity);
164+
$color: map.get($tokens, $color-key);
165+
$opacity: map.get($opacity-lookup, $opacity-key);
166+
167+
@if(meta.type-of($color) == 'color') {
168+
$result: map.remove($result, $opacity-key);
169+
$result: map.set($result, $color-key, rgba($color, $opacity));
170+
}
171+
@else if($color != null) {
172+
$result: map.remove($result, $opacity-key);
173+
$combined-color: #{color-mix(in srgb, #{$color} #{($opacity * 100) + '%'}, transparent)};
174+
$result: map.set($result, $color-key, $combined-color);
175+
}
176+
}
177+
178+
@return $result;
179+
}
180+
181+
/// Inherited function from MDC that computes which contrast tone to use on top of a color.
182+
/// This is used only in a narrow set of use cases when generating M2 button tokens to maintain
183+
/// backwards compatibility.
184+
/// @param {Color} $value Color for which we're calculating the contrast tone.
185+
/// @param {Boolean} $is-dark Whether the current theme is dark.
186+
/// @return {Map} Either `dark` or `light`.
187+
@function contrast-tone($value, $is-dark) {
188+
@if ($value == 'dark') {
189+
@return 'light';
190+
}
191+
192+
@if ($value == 'light') {
193+
@return 'dark';
194+
}
195+
196+
// Fallback if the app is using a non-color palette (e.g. CSS variable based).
197+
@if (meta.type-of($value) != 'color') {
198+
@return if($is-dark, 'light', 'dark');
199+
}
200+
201+
$minimum-contrast: 3.1;
202+
$light-contrast: _contrast($value, #fff);
203+
$dark-contrast: _contrast($value, rgba(0, 0, 0, 0.87));
204+
205+
@if ($light-contrast < $minimum-contrast) and ($dark-contrast > $light-contrast) {
206+
@return 'dark';
207+
}
208+
209+
@return 'light';
210+
}
211+
212+
@function _linear-channel-value($channel-value) {
213+
$normalized-channel-value: math.div($channel-value, 255);
214+
215+
@if ($normalized-channel-value < 0.03928) {
216+
@return math.div($normalized-channel-value, 12.92);
217+
}
218+
219+
@return math.pow(math.div($normalized-channel-value + 0.055, 1.055), 2.4);
220+
}
221+
222+
// Calculate the luminance for a color.
223+
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
224+
@function _luminance($color) {
225+
$red: _linear-channel-value(color.red($color));
226+
$green: _linear-channel-value(color.green($color));
227+
$blue: _linear-channel-value(color.blue($color));
228+
229+
@return 0.2126 * $red + 0.7152 * $green + 0.0722 * $blue;
230+
}
231+
232+
// Calculate the contrast ratio between two colors.
233+
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
234+
@function _contrast($back, $front) {
235+
$back-lum: _luminance($back) + 0.05;
236+
$fore-lum: _luminance($front) + 0.05;
237+
238+
@return math.div(math.max($back-lum, $fore-lum), math.min($back-lum, $fore-lum));
239+
}
240+
241+
/// Picks a submap containing only the given keys out the given map.
242+
/// @param {Map} $map The map to pick from.
243+
/// @param {List} $keys The map keys to pick.
244+
/// @return {Map} A submap containing only the given keys.
245+
@function _pick($map, $keys) {
246+
$result: ();
247+
@each $key in $keys {
248+
@if map.has-key($map, $key) {
249+
$result: map.set($result, $key, map.get($map, $key));
250+
}
251+
}
252+
@return $result;
253+
}
254+
255+
256+
/// Filters keys with a null value out of the map.
257+
/// @param {Map} $map The map to filter.
258+
/// @return {Map} The given map with all of the null keys filtered out.
259+
@function _filter-nulls($map) {
260+
$result: ();
261+
@each $key, $val in $map {
262+
@if $val != null {
263+
$result: map.set($result, $key, $val);
264+
}
265+
}
266+
@return $result;
267+
}

0 commit comments

Comments
 (0)