|
| 1 | +@use 'sass:list'; |
| 2 | +@use 'sass:map'; |
| 3 | +@use 'sass:meta'; |
| 4 | +@use '@angular/material' as mat; |
| 5 | + |
| 6 | +/// Whether to throw an error when a required dep is not configured. If false, the dep will be |
| 7 | +/// automatically configured instead. |
| 8 | +$_error-on-missing-dep: false; |
| 9 | + |
| 10 | +/// Applies the theme for the given component configuration. |
| 11 | +/// @param {Map} $tokens A map containing the default values to use for tokens not explicitly |
| 12 | +/// customized in the component config object. |
| 13 | +/// @param {List} $component The component config object to emit theme tokens for. |
| 14 | +/// @output CSS variables representing the theme tokens for this component. |
| 15 | +@mixin _apply-theme($tokens, $component) { |
| 16 | + $id: map.get($component, id); |
| 17 | + $tokens: map.deep-merge($tokens, map.get($component, customizations)); |
| 18 | + |
| 19 | + // NOTE: for now we use a hardcoded if-chain, but in the future when first-class mixins are |
| 20 | + // supported, the configuration data will contain a reference to its own theme mixin. |
| 21 | + @if $id == 'mat.card' { |
| 22 | + @include mat.private-apply-card-theme-from-tokens($tokens); |
| 23 | + } |
| 24 | + @else if $id == 'mat.checkbox' { |
| 25 | + @include mat.private-apply-checkbox-theme-from-tokens($tokens); |
| 26 | + } |
| 27 | + @else { |
| 28 | + @error 'Unrecognized component theme: #{id}'; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Gets the transitive closure of the given list of component configuration dependencies. |
| 33 | +/// @param {List} $components The list of component config objects to get the transitive deps for. |
| 34 | +/// @param {Map} $configured [()] A map of already configured component IDs. Used for recursion, |
| 35 | +/// should not be passed when calling. |
| 36 | +/// @return {List} The transitive closure of configs for the given $components. |
| 37 | +// TODO(mmalerba): Currently we use the deps to determine if additional tokens, other than the |
| 38 | +// explicitly requested ones need to be emitted, but the deps do not affect the ordering in which |
| 39 | +// the various configs are processed. Before moving out of experimental we should think more about |
| 40 | +// the ordering behavior we want. For the most part the order shouldn't matter, unless we have 2 |
| 41 | +// configs trying to set the same token. |
| 42 | +@function _get-transitive-deps($components, $configured: ()) { |
| 43 | + // Mark the given components as configured. |
| 44 | + @each $component in $components { |
| 45 | + $configured: map.set($configured, map.get($component, id), true); |
| 46 | + } |
| 47 | + $new-deps: (); |
| 48 | + |
| 49 | + // Check each of the given components for new deps. |
| 50 | + @each $component in $components { |
| 51 | + // Note: Deps are specified as getter functions that return a config object rather than a direct |
| 52 | + // config object. This allows us to only call the getter if the dep has not yet been configured. |
| 53 | + // This can be useful if we have 2 components that want to require each other to be configured. |
| 54 | + // Example: form-field and input. If we used direct config objects in this case, it would cause |
| 55 | + // infinite co-recursion. |
| 56 | + @each $dep-getter in mat.private-coerce-to-list(map.get($component, deps)) { |
| 57 | + $dep: meta.call($dep-getter); |
| 58 | + $dep-id: map.get($dep, id); |
| 59 | + @if not (map.has-key($configured, $dep-id)) { |
| 60 | + @if $_error-on-missing-dep { |
| 61 | + @error 'Missing theme: `#{map.get($component, id)}` depends on `#{$dep-id}`.' + |
| 62 | + ' Please configure the theme for `#{$dep-id}` in your call to `mat.theme`'; |
| 63 | + } |
| 64 | + @else { |
| 65 | + $configured: map.set($configured, $dep-id, true); |
| 66 | + $new-deps: list.append($new-deps, $dep); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Append on the new deps to this list of component configurations and return. |
| 73 | + @if list.length($new-deps) > 0 { |
| 74 | + $components: list.join($components, _get-transitive-deps($new-deps, $configured)); |
| 75 | + } |
| 76 | + @return $components; |
| 77 | +} |
| 78 | + |
| 79 | +/// Apply the themes for the given component configs with the given ste of fallback token values. |
| 80 | +/// @param {Map} $tokens A map of fallback values to use for tokens that are not explicitly |
| 81 | +/// customized by one of the component configs. |
| 82 | +/// @param {List} $components The list of component configurations to emit tokens for. |
| 83 | +/// @output CSS variables representing the theme tokens for the given component configs. |
| 84 | +@mixin _theme($tokens, $components) { |
| 85 | + // Call the theme mixin for each configured component. |
| 86 | + @each $component in $components { |
| 87 | + @include _apply-theme($tokens, $component); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// Takes the full list of tokens and a list of components to configure, and outputs all theme |
| 92 | +/// tokens for the configured components. |
| 93 | +/// @param {Map} $tokens A map of all tokens for the current design system. |
| 94 | +/// @param {List} $components The list of component configurations to emit tokens for. |
| 95 | +/// @output CSS variables representing the theme tokens for the given component configs. |
| 96 | +// TODO(mmalerba): Consider an alternate API where `$tokens` is not a separate argument, |
| 97 | +// but one of the configs in the `$components` list |
| 98 | +@mixin theme($tokens, $components) { |
| 99 | + @include _theme($tokens, _get-transitive-deps(mat.private-coerce-to-list($components))); |
| 100 | +} |
| 101 | + |
| 102 | +/// Takes a list of components to configure, and outputs only the theme tokens that are explicitly |
| 103 | +/// customized by the configurations. |
| 104 | +/// @param {List} $components The list of component configurations to emit tokens for. |
| 105 | +/// @output CSS variables representing the theme tokens for the given component configs. |
| 106 | +// TODO(mmalerba): What should we call this? |
| 107 | +// - update-theme |
| 108 | +// - adjust-theme |
| 109 | +// - edit-theme |
| 110 | +// - override-theme |
| 111 | +// - retheme |
| 112 | +@mixin retheme($components) { |
| 113 | + @include _theme((), $components); |
| 114 | +} |
| 115 | + |
| 116 | +/// Configure the mat-card's theme. |
| 117 | +/// @param {Map} $customizations [()] A map of custom token values to use when theming mat-card. |
| 118 | +@function card($customizations: ()) { |
| 119 | + @return ( |
| 120 | + id: 'mat.card', |
| 121 | + customizations: $customizations, |
| 122 | + deps: (), |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +/// Configure the mat-checkbox's theme. |
| 127 | +/// @param {Map} $customizations [()] A map of custom token values to use when theming mat-checkbox. |
| 128 | +@function checkbox($customizations: ()) { |
| 129 | + @return ( |
| 130 | + id: 'mat.checkbox', |
| 131 | + customizations: $customizations, |
| 132 | + deps: (), |
| 133 | + ); |
| 134 | +} |
0 commit comments