|
| 1 | +@use 'sass:list'; |
| 2 | +@use 'sass:map'; |
| 3 | +@use 'sass:meta'; |
| 4 | +@use 'sass:string'; |
| 5 | +@use '@angular/material' as mat; |
| 6 | + |
| 7 | +// Whether to throw an error when a required dep is not configured. If false, the dep will be |
| 8 | +// automatically configured instead. |
| 9 | +$_error-on-missing-dep: false; |
| 10 | + |
| 11 | +// Applies the theme for the given component configuration. |
| 12 | +@mixin _apply-theme($tokens, $component) { |
| 13 | + $id: map.get($component, id); |
| 14 | + $tokens: map.deep-merge($tokens, map.get($component, customizations)); |
| 15 | + |
| 16 | + // NOTE: for now we use a hardcoded if-chain, but in the future when first-class mixins are |
| 17 | + // supported, the configuration data will contain a reference to its own theme mixin. |
| 18 | + @if $id == 'mat.card' { |
| 19 | + @include mat.private-apply-card-theme-from-tokens($tokens); |
| 20 | + } @else if $id == 'mat.checkbox' { |
| 21 | + @include mat.private-apply-checkbox-theme-from-tokens($tokens); |
| 22 | + } @else { |
| 23 | + @error 'Unrecognized component theme: #{id}'; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Gets the transitive dependency configurations for the given list of component configurations. |
| 28 | +@function _get-transitive-deps($components, $configured: ()) { |
| 29 | + // Mark the given components as configured. |
| 30 | + @each $component in $components { |
| 31 | + $configured: map.set($configured, map.get($component, id), true); |
| 32 | + } |
| 33 | + $new-deps: (); |
| 34 | + |
| 35 | + // Check each of the given components for new deps. |
| 36 | + @each $component in $components { |
| 37 | + @each $dep-getter in mat.private-normalize-args-list(map.get($component, deps)) { |
| 38 | + $dep: meta.call($dep-getter); |
| 39 | + $dep-id: map.get($dep, id); |
| 40 | + @if not (map.has-key($configured, $dep-id)) { |
| 41 | + @if $_error-on-missing-dep { |
| 42 | + @error 'Missing theme: `#{map.get($component, id)}` depends on `#{$dep-id}`.' + |
| 43 | + ' Please configure the theme for `#{$dep-id}` in your call to `mat.theme`'; |
| 44 | + } @else { |
| 45 | + $configured: map.set($configured, $dep-id, true); |
| 46 | + $new-deps: list.append($new-deps, $dep); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Append on the new deps to this list of component configurations and return. |
| 53 | + @if list.length($new-deps) > 0 { |
| 54 | + $components: list.join($components, _get-transitive-deps($new-deps, $configured)); |
| 55 | + } |
| 56 | + @return $components; |
| 57 | +} |
| 58 | + |
| 59 | +@mixin _theme($tokens, $components) { |
| 60 | + // Call the theme mixin for each configured component. |
| 61 | + @at-root #{& or body} { |
| 62 | + @each $component in $components { |
| 63 | + @include _apply-theme($tokens, $component); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Takes the full list of tokens and a list of components to configure, and outputs all theme |
| 69 | +// tokens for the configured components. |
| 70 | +@mixin theme($tokens: mat.m2-tokens-from-theme(), $components) { |
| 71 | + @include _theme($tokens, _get-transitive-deps(mat.private-normalize-args-list($components))); |
| 72 | +} |
| 73 | + |
| 74 | +// TODO(mmalerba): What should we call this? |
| 75 | +// - update-theme |
| 76 | +// - adjust-theme |
| 77 | +// - edit-theme |
| 78 | +// - override-theme |
| 79 | +// - retheme |
| 80 | +// Takes a list of components to configure, and outputs only the theme tokens that are explicitly |
| 81 | +// customized by the configurations. |
| 82 | +@mixin update-theme($components) { |
| 83 | + @include _theme((), $components); |
| 84 | +} |
| 85 | + |
| 86 | +// Configure the mat-card's theme. |
| 87 | +@function card($customizations: ()) { |
| 88 | + @return ( |
| 89 | + id: 'mat.card', |
| 90 | + customizations: $customizations, |
| 91 | + deps: (), |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +// Configure the mat-checkbox's theme. |
| 96 | +@function checkbox($customizations: ()) { |
| 97 | + @return ( |
| 98 | + id: 'mat.checkbox', |
| 99 | + customizations: $customizations, |
| 100 | + deps: (), |
| 101 | + ); |
| 102 | +} |
0 commit comments