<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Raul Rothschild on Medium]]></title>
        <description><![CDATA[Stories by Raul Rothschild on Medium]]></description>
        <link>https://medium.com/@raultonello18?source=rss-64c0be8f9ba6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Z4qGRVya-cB1c6LDGUVQjg.png</url>
            <title>Stories by Raul Rothschild on Medium</title>
            <link>https://medium.com/@raultonello18?source=rss-64c0be8f9ba6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 17:51:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raultonello18/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular Material M3 dynamic runtime colors]]></title>
            <link>https://medium.com/@raultonello18/angular-material-m3-dynamic-runtime-colors-6d6d1036d2bb?source=rss-64c0be8f9ba6------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d6d1036d2bb</guid>
            <category><![CDATA[angular-material]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[material-design]]></category>
            <dc:creator><![CDATA[Raul Rothschild]]></dc:creator>
            <pubDate>Mon, 20 May 2024 19:55:33 GMT</pubDate>
            <atom:updated>2024-05-22T00:04:10.843Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*VGRPTwvfyIzdwn_-LEhYKg.gif" /></figure><p>TL;DR — This article dives into how to implement code that overrides custom properties whenever a source color is chosen. This allows you to dynamically change the Material M3 color scheme at runtime. You can access the <a href="https://github.com/Rrothschild18/runtime-angular-material">source code</a> directly or learn more about new Angular Material M3 themes through this reading.</p><p>Everybody who has worked with Angular Material versions 14 or below has probably noticed how complicated it can be to style the components provided by the framework. There is an entire guide in the documentation for defining custom CSS rules that directly style Angular Material components. One of the major problems not explicitly mentioned in the guide, but seen over time, is the high level of _specificity_ and _selectors_ used by the framework by default. Bypassing encapsulation of deeply nested CSS classes often ends up being a mix of <strong><em>::ng-deep</em></strong> , <strong><em>!important</em></strong>, and a bit of <strong><em>lucky</em></strong> to achieve a simple modification without breaking everything.</p><p>Fortunately, accordingly to the docs this behavior has changed.</p><blockquote>[Docs] <a href="https://material.angular.io/guide/mdc-migration"><strong>Migrating to MDC-based Angular Material Components</strong></a></blockquote><blockquote>In Angular Material v15 and later, many of the components have been refactored to be based on the official <a href="https://github.com/material-components/material-components-web"><strong>Material Design Components for Web (MDC</strong></a><strong>)</strong></blockquote><p>Since I believe most of the production applications in the wild aren’t using the latest Angular versions, and considering that Material 3 theming is still experimental and supported by Angular Material 17.2 features, the benefits may go unnoticed by the community. One of the benefits, which is the focus of this article, is the ability to add runtime Angular Material theming colors.</p><h3>Creating a new angular app</h3><p>Run the command `<a href="https://material.angular.io/guide/getting-started#install-angular-material"><strong>ng n runtime-angular-material</strong></a>` and chose `<strong>scss</strong>` as your default stylesheet and no SSR.</p><pre>Which stylesheet format would you like to use? <br>❯ SCSS [ https://sass-lang.com/documentation/syntax#scss<br><br>Do you want to enable Server-Side Rendering (SSR) ?<br>❯ No</pre><p>Lets add Angular Material by running the command `<a href="https://material.angular.io/guide/getting-started#install-angular-material">ng add @angular/material</a>`</p><pre>✔ Packages successfully installed.<br><br>Choose a prebuilt theme name, or &quot;custom&quot; for a custom theme:<br>❯ Custom<br><br>Set up global Angular Material typography styles?<br>❯ Yes<br><br>Include the Angular animations module? <br>❯ Include and enable animations</pre><p>And the last and not least, through your pkg manager install <strong>Angular Experimental </strong>by running <strong>`npm i </strong><a href="https://www.npmjs.com/package/@angular/material-experimental"><strong>@angular/material-experimental</strong></a><strong>`</strong></p><h3>Creating a “custom” Angular Material M3 Theme</h3><p>As pointed at the documentation <a href="https://material.angular.io/guide/mdc-migration"><strong>Migrating to MDC-based Component</strong>s</a> guide, the solution here purposed may change in the near future as the Angular Material team evolves the framework.</p><blockquote>[Docs] <a href="https://material.angular.io/guide/material-3#what-is-material-3">What Is Material 3</a></blockquote><blockquote>As of v17.2.0, Angular Material includes experimental support for M3 styling in addition to M2. The team plans to stabilize support for M3 after a brief period in experimental in order to get feedback on the design and API.</blockquote><p>And at the end of the guide there also a FAQ.</p><blockquote>[Docs] <a href="https://material.angular.io/guide/material-3#faq">FAQ</a></blockquote><blockquote>Can I use colors other than the pre-defined Material 3 palettes?</blockquote><blockquote>- Currently, we only offer predefined palettes, but we plan to add support for using custom generated palettes as part of making the M3 APIs stable and available in <a href="http://twitter.com/angular/material">@angular/material</a>.</blockquote><p>When looking at the source code schema for `<a href="https://github.com/angular/components/blob/17.2.x/src/material-experimental/theming/_m3-palettes.scss"><strong>predefined palettes</strong></a>` you can define your own by copying them from `<strong>@forward ‘./theming/m3-palettes</strong>’` inside `<a href="https://github.com/angular/components/blob/17.2.x/src/material-experimental/_index.scss"><strong>_index.scss</strong></a>` at your `<strong>node_modules/@angular/material-experimental</strong>`.</p><p>At your <strong>styles.scss</strong> copy/paste a predefined theme and later on at this article we will generate a compatible M3 Theme using <a href="https://github.com/material-foundation/material-color-utilities?tab=readme-ov-file#material-color-utilities"><strong>@material/material-color-utilities</strong></a> an official library for color utilities from Google Material team.</p><pre>//styles.scss<br>@use &#39;sass:map&#39;;<br>@use &#39;@angular/material&#39; as mat;<br>@use &#39;@angular/material-experimental&#39; as matx;<br><br>@include mat.core();<br><br>$m3-base-config: (<br>color: (<br>// Start of copying the $cyan-palette into PRIMARY color config<br> primary: (<br>  0: #000000,<br>  10: #002020,<br>  20: #003737,<br>  25: #004343,<br>  30: #004f4f,<br>  35: #005c5c,<br>  40: #006a6a,<br>  50: #008585,<br>  60: #00a1a1,<br>  70: #00bebe,<br>  80: #00dddd,<br>  90: #00fbfb,<br>  95: #adfffe,<br>  98: #e2fffe,<br>  99: #f1fffe,<br>  100: #ffffff,<br>  secondary: ( // The pattern follows <br>   ...<br>  ),<br>  neutral: ( // by only chaniging source color <br>   ...<br>  ),<br>  neutral-variant: (// and the the tone<br>   ...<br>  ),<br>  error: ( // for each TonalPallete<br>   ...<br>  )<br>  //End of copying the $cyan-palette as config for a primary color<br>  <br>  // For our &quot;secondary&quot; color we will apply directly<br>  // an aleready built in M3 theme palette<br>  tertiary: matx.$m3-violet-palette,<br><br>),<br><br> typography: () // [Optional] Typography config for M3,<br> density: () // [Optional] Density config config for M3<br>)<br><br>//Using matx the new api for defining a M3 Theme<br>$angular-material-3-theme: matx.define-theme(<br>   map.set($m3-base-config, color, theme-type, light)<br>);<br><br>$angular-material-3-theme-dark: matx.define-theme(<br>   map.set($m3-base-config, color, theme-type, dark)<br>);<br><br>html,<br>body {<br>  height: 100%;<br>  margin: 0;<br>  // Apply the theme for all components. Matx is only for theme<br>  // @mat.core() still the API for applying the styles<br>  @include mat.all-component-themes(theme.$angular-material-3-theme);<br>}<br></pre><p>Now you might have Angular Material Components using M3 <strong>$cyan-palette</strong> and if copy and paste some components from the documentation you will see something similar to this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cP-V2gFTnOQdrfpe-sHDvA.png" /></figure><p>Is noteworthy that all buttons <strong>have the same color</strong>, even copy pasting from the button documentation section example. This is also mentioned at <a href="https://material.angular.io/guide/mdc-migration"><strong>Migrating to MDC-based Angular Material Components</strong></a> guide.</p><blockquote>[Docs] <a href="https://material.angular.io/guide/material-3#using-component-color-variants">Using component color variants</a><br> <br>A number of components have a `<strong>color</strong>` input property that allows developers to apply different color variants of the component. When using an M3 theme, this input still adds a CSS class to the component (e.g. `<strong>.mat-accent</strong>`).</blockquote><blockquote>However, there are <strong>no built-in styles targeting these classes</strong>. You can instead apply color variants by passing the `<strong>$color-variant</strong>` option to a component’s `<strong>-theme</strong>` or `<strong>-color</strong>` mixins.</blockquote><p>So, if you want to still use the old way to apply components colors you can add to your <strong>style.scss</strong> the compatibility mixin.</p><pre>// style.scss<br>html,<br>body {<br>...<br> @include matx.color-variants-back-compat($angular-material-3-theme);<br>}</pre><p>And now everything may looks familiar again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OtPNJvHArioLNztFRYmmSg.png" /></figure><h3><strong>Understanding Angular Material M3 Theme changes based on Design tokens</strong></h3><p>Starting from Angular Material v15, <a href="https://m3.material.io/foundations/design-tokens/overview">M3 design tokens</a> are now represented by <a href="https://developer.mozilla.org/pt-BR/docs/Web/CSS/--*">custom properties</a> and referenced as CSS variables instead of multiple CSS classes. By inspecting a `<strong>mat-flat-button</strong>`, we can observe this change.</p><pre>//A lot of tokens for the mdc button<br>.mat-primary.mat-mdc-button-base {<br>  --mdc-text-button-label-text-color: #005cbb;<br>  --mdc-text-button-disabled-label-text-color: rgba(26, 27, 31,    .38);<br>  --mdc-protected-button-container-color: #fdfbff;<br>  --mdc-protected-button-label-text-color: #005cbb;<br>...<br>}<br>// And some class using it<br>.mat-mdc-unelevated-button {<br>  font-family: var(--mdc-filled-button-label-text-font);<br>  font-size: var(--mdc-filled-button-label-text-size);<br>  letter-spacing: var(--mdc-filled-button-label-text-tracking);<br>  font-weight: var(--mdc-filled-button-label-text-weight);<br>  text-transform: var(--mdc-filled-button-label-text-transform);<br>  height: var(--mdc-filled-button-container-height);<br>  border-radius: var(--mdc-filled-button-container-shape);<br>  padding: 0 var(--mat-filled-button-horizontal-padding, 16px);<br>}</pre><p>If you inspect a little more, you may find the specific custom property that is defining the style you would like to change. Here is another example for `<strong>mat-flat-button</strong>`.</p><pre>// The token somewhere<br>html, body {<br>...<br>--mdc-filled-button-container-color: #007dff;<br>...<br>}<br><br>// The actual classe generated from<br>// mat.all-component-themes(theme.$angular-material-3-theme)<br>.mat-mdc-unelevated-button:not(:disabled) {<br> background-color: #007dff;<br>}</pre><p>Unfortunately, Angular Material design token names differ slightly from those of <a href="https://material-web.dev/about/intro/">MDC Web Components</a> and do not specify which tokens correspond to each CSS class or component. Furthermore, unlike the <a href="https://material-web.dev/theming/color/#tokens">Material Web Components Color Tokens</a> documentation, Angular Material does not provide a set of generic design tokens to change the entire theme at once. For changing colors dynamically at runtime, manually inspecting each token and overriding it using a CSS variable is not ideal because you don’t know the exact tone, shade, or function used to generate a specific token color.</p><p>However, after some research, I found a library called <a href="https://github.com/material-foundation/material-color-utilities">@material-color-utilities</a>, which is a recently open-sourced library from Google that came along with their latest version of Material Design (M3). This library helps generate a theme from a source color, similar to their theme builder example, which you can check out at <a href="https://material-foundation.github.io/material-theme-builder/">Material 3 Theme Builder</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7m83ZTaaWVUprYeXSHY7qw.png" /></figure><h3>Theming Angular Material 3 components with custom css properties</h3><p>In your <strong>style.scss</strong>, instead of declaring hard-coded HEX values for primary and tertiary colors directly, we will create custom properties for each tone that can be generated from <strong>material-color-utilities</strong> and referenced as variables at the M3 Theme configuration. Later on, we will override these variables at the root of the application.</p><pre>// Super fancy custom properties names<br>:root {<br>--primary-0: #000000;<br>--primary-10: #001f24;<br>--primary-20: #00363d;<br>--primary-25: #00424a;<br>--primary-30: #004f58;<br>--primary-35: #005b66;<br>--primary-40: #006874;<br>--primary-50: #068391;<br>--primary-60: #389eac;<br>--primary-70: #58b9c7;<br>--primary-80: #75d4e4;<br>--primary-90: #98f0ff;<br>--primary-95: #d0f8ff;<br>--primary-98: #edfcff;<br>--primary-99: #f6feff;<br>--primary-100: #ffffff;<br><br>--p-secondary-0: #000000;<br>--p-secondary-10: #001f24;<br>--p-secondary-20: #00363d;<br>--p-secondary-25: #00424a;<br>--p-secondary-30: #004f58;<br>--p-secondary-35: #005b66;<br>--p-secondary-40: #006874;<br>--p-secondary-50: #068391;<br>--p-secondary-60: #389eac;<br>--p-secondary-70: #58b9c7;<br>--p-secondary-80: #75d4e4;<br>--p-secondary-90: #98f0ff;<br>--p-secondary-95: #d0f8ff;<br>--p-secondary-98: #edfcff;<br>--p-secondary-99: #f6feff;<br>--p-secondary-100: #ffffff;<br><br>--p-neutral-0: #000000;<br>--p-neutral-10: #191c1c;<br>--p-neutral-20: #2e3131;<br>--p-neutral-25: #393c3c;<br>--p-neutral-30: #454748;<br>--p-neutral-35: #505353;<br>--p-neutral-40: #5c5f5f;<br>--p-neutral-50: #757778;<br>--p-neutral-60: #8f9191;<br>--p-neutral-70: #aaabac;<br>--p-neutral-80: #c5c7c7;<br>--p-neutral-90: #e1e3e3;<br>--p-neutral-95: #f0f1f1;<br>--p-neutral-98: #f9f9fa;<br>--p-neutral-99: #fbfcfc;<br>--p-neutral-100: #ffffff;<br><br>--p-neutral-variant-0: #000000;<br>--p-neutral-variant-10: #161d1e;<br>--p-neutral-variant-20: #2b3233;<br>--p-neutral-variant-25: #363d3e;<br>--p-neutral-variant-30: #414849;<br>--p-neutral-variant-35: #4d5455;<br>--p-neutral-variant-40: #586061;<br>--p-neutral-variant-50: #71787a;<br>--p-neutral-variant-60: #8b9293;<br>--p-neutral-variant-70: #a5acae;<br>--p-neutral-variant-80: #c1c8c9;<br>--p-neutral-variant-90: #dde4e5;<br>--p-neutral-variant-95: #ebf2f3;<br>--p-neutral-variant-98: #f4fbfc;<br>--p-neutral-variant-99: #f7fdff;<br>--p-neutral-variant-100: #ffffff;<br><br>--error-0: #000000;<br>--error-10: #410002;<br>--error-20: #690005;<br>--error-25: #7e0007;<br>--error-30: #93000a;<br>--error-35: #a80710;<br>--error-40: #ba1a1a;<br>--error-50: #de3730;<br>--error-60: #ff5449;<br>--error-70: #ff897d;<br>--error-80: #ffb4ab;<br>--error-90: #ffdad6;<br>--error-95: #ffedea;<br>--error-98: #fff8f7;<br>--error-99: #fffbff;<br>--error-100: #ffffff;<br><br>}<br><br>//<br>$m3-base-config: (<br>  color: (<br>  //Redefine the primary color based on your own css vars<br>    primary: (<br>      0: #000000, // <br>      10: var(--primary-10),<br>      20: var(--primary-20),<br>      25: var(--primary-25),<br>      30: var(--primary-30),<br>      35: var(--primary-35),<br>      40: var(--primary-40),<br>      50: var(--primary-50),<br>      60: var(--primary-60),<br>      70: var(--primary-70),<br>      80: var(--primary-80),<br>      90: var(--primary-90),<br>      95: var(--primary-95),<br>      98: var(--primary-98),<br>      99: var(--primary-99),<br>      100: #ffffffff,<br>    secondary: (<br>      0: #000000,<br>      10: var(--p-secondary-10),<br>      20: var(--p-secondary-20),<br>      ...<br>     ),<br>    neutral: (<br>      0: #000000,<br>      10: var(--p-neutral-10),<br>      20: var(--p-neutral-20),<br>      ...<br>      100: #ffffffff,<br>     ),<br>    neutral-variant: (<br>      0: #000000,<br>      10: var(--p-neutral-variant-10),<br>      20: var(--p-neutral-variant-20),<br>      ...<br>      100: #ffffffff,<br>     ),<br>    error: (<br>      0: #000000,<br>      10: var(--error-10),<br>      20: var(--error-20),<br>      ...<br>      100: #ffffffff,<br>     )<br>   ),<br>   // You can do the same for you &quot;secondary&quot; color<br>   // which they call tertiary<br>   tertiary: (<br>   ...<br>   )<br>)</pre><p>By doing this, Angular Material’s internal mixins will create CSS variables that point to your CSS variables, resulting in an unexpected pointer of a pointer. If everything is done correctly, you should not notice any changes in your styles, only in the generated variables following the tokens from Material 3.</p><p>If you inspect your components tokens you might see:</p><pre><br>//A lot of tokens for the mdc button pointing to your color vars<br>html, body {<br>  --mdc-text-button-label-text-color: #005cbb;<br>  --mdc-text-button-disabled-label-text-color: rgba(26, 27, 31,    .38);<br>  --mdc-filled-button-container-color: var(--primary-40);<br>  --mdc-protected-button-label-text-color: #005cbb;<br>  --mdc-fab-container-color: var(--primary-90);<br>  <br>  --mat-fab-small-foreground-color: var(--primary-10);<br>  --mat-fab-small-state-layer-color: var(--primary-10);<br>  --mat-fab-small-ripple-color: var(--primary-10);<br>  <br>  --mat-datepicker-calendar-period-button-text-color: var(--p-neutral-variant-30);<br>  --mat-datepicker-calendar-period-button-icon-color: var(--p-neutral-variant-30);<br>  --mat-datepicker-calendar-navigation-button-icon-color: var(--p-neutral-variant-30);<br>  --mat-datepicker-calendar-header-text-color:<br>...<br>}</pre><p>Some custom properties may not change because they are not correlated with the theme colors configuration. However, now, you can at least more easily check which palette/tone is being used in all Material components and combine it with M2 utility functions.</p><blockquote>[Docs] <a href="https://material.angular.io/guide/material-3">Theme your own components using a Material 3 theme</a></blockquote><blockquote>The same utility functions for reading properties of M2 themes (described in <a href="https://material.angular.io/guide/theming-your-components">our guide for theming your components</a>) can be used to read properties from M3 themes. However, the named palettes, typography levels, etc. available are different for M3 themes, in accordance with the spec.</blockquote><p>For example, if you want to set a component’s background to your secondary color, consider using the `<strong>S-90</strong>` color token. For text colors within this component background, use the `<strong>S-10</strong>` color token. Alternatively, you can automate this by using the utility functions and passing the `<strong>$role</strong>` as `<strong>secondary-container</strong>` and `<strong>on-secondary-container</strong>`, as suggested by the theme builder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rhAFKd4GOlN_jbKnO4vnqA.png" /></figure><p>Here is a code example:</p><pre>//_button-theme.scss<br>@use &#39;sass:map&#39;;<br>@use &#39;@angular/material&#39; as mat;<br>@use &#39;./theme&#39; as theme; //just a file where theme is set<br><br>@include mat.core();<br><br>.primary-button {<br>  color: mat.get-theme-color(<br>  theme.$angular-material-3-theme,<br>  inverse-on-surface // will generate a color based on css var<br>);<br>background-color: mat.get-theme-color(<br>  theme.$angular-material-3-theme,<br>  primary,<br>  70 // It will points to your custom css var token :)<br> );<br>border-radius: 10px;<br>}</pre><p>You can have a better understanding how to theme your components based at <a href="https://material-foundation.github.io/material-theme-builder/">Material 3 Theme Builder</a> where you can check which color or role to use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*DLgA82L_YlHwfau2dc9-BA.png" /></figure><h3>Applying a dynamic runtime theme</h3><p>Now it’s time to implement the code that will override the custom properties when a source color is selected. The code snippets below provide a brief idea of how to achieve the runtime goal of changing Material M3 colors. I strongly recommend looking at the <a href="https://github.com/Rrothschild18/runtime-angular-material">source code here</a>.</p><pre>// app.component.ts<br><br>export const DEFAULT_COLOR = &#39;#8714fa&#39;;<br><br>export class AppComponent {<br> color = signal(DEFAULT_COLOR);<br> <br> onColorChange(event: any) {<br>  this.color.set(event.value);<br> }<br>}</pre><p>At <strong>app.compontent.html</strong> lets add some code for a color picker input</p><pre>// app.component.html<br><br>&lt;div class=&quot;theme-controls&quot;&gt;<br> &lt;div class=&quot;color-picker-wrapper&quot;&gt;<br>  &lt;div class=&quot;color-picker-overflow&quot;&gt;<br>   &lt;input <br>    matInput<br>    id=&quot;color-input&quot;<br>    type=&quot;color&quot;<br>    [(ngModel)]=&quot;color&quot;<br>    (ngModelChange)=&quot;onColorChange($event)&quot;<br>   /&gt;<br>  &lt;/div&gt;<br> &lt;/div&gt;<br>&lt;/div&gt;</pre><p>Lets have some styles</p><pre>// app.component.scss<br><br>.theme-controls {<br>  display: flex;<br>  gap: 0.65rem;<br>  align-items: center;<br>  margin: auto;<br><br>  .color-picker-wrapper {<br>    display: flex;<br>    width: 40px;<br>    height: 40px;<br>    border-radius: 50%;<br>    position: relative;<br><br>    .color-picker-overflow {<br>      display: flex;<br>      align-items: center;<br>      justify-content: center;<br>      width: 100%;<br>      height: 100%;<br>      overflow: hidden;<br>      border-radius: 50%;<br><br>      #color-input {<br>        cursor: pointer;<br>        border: none;<br>        background: none;<br>        min-width: 150%;<br>        min-height: 150%;<br>      }<br>    }<br>  }<br>}</pre><p>Now we are going to use <a href="https://github.com/material-foundation/material-color-utilities">@material-color-utilities</a> library o generate a M3 theme from the selected color</p><pre>themeFromSelectedColor(color?: string, isDark?: boolean): void {<br> // All calculations are made using numbers<br> // we need HEX strings for use @material-utilitis-color apis<br> const theme = themeFromSourceColor(<br>  argbFromHex(this.color() ?? DEFAULT_COLOR)<br> );<br><br> // ngular material tones<br> const tones = [0, 10, 20, 25, 30, 35, 40, 50, 60, 70, 80, 90, 95, 99, 100];<br><br> // A colors Dictionary <br> const colors = Object.entries(theme.palettes).reduce(<br>  (acc: any, curr: [string, TonalPalette]) =&gt; {<br>  <br>  const hexColors = tones.map((tone) =&gt; ({ tone, hex: <br>       hexFromArgb(curr[1].tone(tone)),<br> }));<br><br> return { ...acc, [curr[0]]: hexColors }; <br>}, {});<br><br>// Then we will apply the colors to the DOM :root element<br> this.createCustomProperties(colors, &#39;p&#39;);<br>}</pre><p>Then we will create the custom properties</p><pre>createCustomProperties(<br> colorsFromPaletteConfig: colorsFromPaletteConfig,<br> paletteKey: &#39;p&#39; | &#39;t&#39;,<br>) {<br><br>let styleString = &#39;:root,:host{&#39;;<br> <br> for (const [key, palette] of Object.entries(colorsFromPaletteConfig)) {<br>palette.forEach(({ hex, tone }) =&gt; {<br><br>  if (key === &#39;primary&#39;) {<br>   styleString += `--${key}-${tone}:${hex};`;<br>  } else {<br>   styleString += `--${paletteKey}-${key}-${tone}:${hex};`;<br>  }<br> });<br>}<br><br>styleString += &#39;}&#39;;<br><br>this.applyThemeString(styleString, &#39;angular-material-theme&#39;);<br>}</pre><p>The last part we need to do is attach our custom properties to the DOM</p><pre>applyThemeString(<br>  themeString: string, <br>  ssName = &#39;angular-material-theme&#39;) <br>{<br>let sheet = (globalThis as WithStylesheet)[ssName];<br><br>if (!sheet) {<br> sheet = new CSSStyleSheet();<br> (globalThis as WithStylesheet)[ssName] = sheet;<br> this.#document.adoptedStyleSheets.push(sheet);<br>}<br> sheet.replaceSync(themeString);<br>}</pre><p>And the is the final result is the example gif from the begining.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*VGRPTwvfyIzdwn_-LEhYKg.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d6d1036d2bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>