Step-by-step guide on transitioning code to utilize @use instead of @import

Currently, I am in the process of revamping a clubs app from Angular 9 to 18, as it has not been well maintained. My goal is to preserve as much of the frontend as possible and maintain the design more or less the same.

Within the code, I have identified the themes for our app as follows:

@import '~@angular/material/theming';
@include mat-core();
$my-app-primary: mat-palette($material-primary);
$my-app-accent:  mat-palette($mat-pink, 500, 900, A100);
$my-app-warn:    mat-palette($mat-red);
$my-app-theme: mat-light-theme($my-app-primary, $my-app-accent, $my-app-warn);
@include angular-material-theme($my-app-theme);

These settings are located in the colours.scss file, referenced in the styles.scss file of our old app. I intended to keep it all as is, but with Angular 17, the use of @import is no longer supported, replacing it with @use (as mentioned in this GitHub issue: https://github.com/angular/components/issues/28204)

I attempted to modify the code myself and also sought assistance from ChatGPT, resulting in the following updated snippet:

@use '@angular/material' as mat;
    
@include mat.core();
    
$my-app-primary: mat.define-palette(mat.$indigo-palette);
$my-app-accent: mat.define-palette(mat.$pink-palette, 500, 900, 'A100');
$my-app-warn: mat.define-palette(mat.$red-palette);
    
$my-app-theme: mat.define-light-theme((
  color: (
    primary: $my-app-primary,
    accent: $my-app-accent,
    warn: $my-app-warn,
  )
));
    
@include mat.all-component-themes($my-app-theme);

However, I encountered an error message stating:

Unknown Function: 'define-palete' ...

Is there anyone who can guide me on the correct syntax to use? The tutorials from Angular still refer to the old system:

Answer №1

When working with Angular 18 and M3 theming, the define-palette() function has been replaced. Now, to create a M2 palette, you should use mat.m2-define-palette(). For custom M3 themes, you can utilize the custom theme generation schematics:

ng generate @angular/material:m3-theme

Answer №2

Your Angular 9 application has transitioned away from Material 2 and is now utilizing Material 3 as the default framework. Below is the updated syntax for Angular Material 18 integration with Material 3:

@use '@angular/material' as mat;

@include mat.core();

$my-theme: mat.define-theme((
  color: (
  theme-type: light,
  primary: mat.$violet-palette,
  )
));

html {
  @include mat.all-component-themes($my-theme);
}

Answer №3

Here's a helpful snippet for integrating Angular Material 18:

@use '@angular/material' as mat;

@include mat.core();

$my-app-primary: mat.define-palette(mat.$indigo-palette);
$my-app-accent: mat.define-palette(mat.$pink-palette, A200, A100, A400);
$my-app-warn: mat.define-palette(mat.$red-palette);

$my-app-theme: mat.define-light-theme((
  color: (
    primary: $my-app-primary,
    accent: $my-app-accent,
    warn: $my-app-warn,
  )
));

@include mat.all-component-themes($my-app-theme);

Key alterations and clarifications:

  1. We import Material styles using @use '@angular/material' as mat; and assign them the namespace mat.

  2. The palette definitions make use of mat.define-palette() and access the predefined palettes through mat.$color-palette.

  3. For the accent palette, I've followed the recommended Material Design approach for accent colors (A200, A100, A400). Feel free to customize these as needed.

  4. The theme is constructed using mat.define-light-theme() with a structured object for improved readability and future modifications.

  5. To finalize, we incorporate all component themes by calling

    mat.all-component-themes($my-app-theme)
    .

Additional things to keep in mind:

  • If you're utilizing custom colors, you may have to define custom palettes. Here’s an example:

    $custom-primary: (
      50: #e8eaf6,
      100: #c5cae9,
      // ... other shades
      500: #3f51b5,  // main shade
      // ... continue with other shades
      contrast: (
        50: rgba(black, 0.87),
        100: rgba(black, 0.87),
        // ... other contrasts
      )
    );
    $my-app-primary: mat.define-palette($custom-primary);
    

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to align both a list icon and its contents in the center?

Can someone please assist me? I am attempting to replicate a website, but I am unsure how to center align an icon list and content. Currently, it appears like this; https://i.stack.imgur.com/6FZCr.png I would like it to look like this; https://i.stack.im ...

What is the reason for the disappearance of unordered list bullets when using padding: 0?

Updating an older website that contains numerous unordered lists has presented a challenge. When the padding is set to 0, the display markers on the unordered list disappear. The root cause of this issue was traced back to the CSS setting *{padding: 0; ma ...

Stylish CSS: Jagged 3-dimensional rotated text

Have a look at this Codepen to see the issue. As the text moves into the background, more aliasing appears. A screenshot taken in Chrome on macOS (similar appearance in FF & Safari): https://i.sstatic.net/n746I.png I've experimented with different ...

Prevent redundancy by caching svg icons to minimize repeated requests

I have a collection of info cards on my page, each featuring its own unique illustration along with a set of common icons (SVG) for options such as edit, delete, and more. While the illustrations vary from card to card, the icons remain consistent across a ...

Achieving nested classes in CSS: a comprehensive guide

I am encountering an issue with my CSS while trying to create a dropdown on hover. The problem seems to be related to nested classes inside a div structure. It appears that the dropdown list is not displaying properly, leading me to believe there might be ...

Please ensure that all fields in the form are filled out before clicking the enable button

Is it possible to enable the button only when all fields are filled out in a form? It's easy if there's just one field, but I have multiple fields with a select field as well. I'm not sure how to do this. Here's the form I've creat ...

Attempting to arrange six images in a row using dividers

Having trouble aligning six images (2 per row with text in between) to the center of your webpage? Don't worry, we've got you covered! Check out the screenshot attached for reference and let us know how we can assist. Click here for a snapshot of ...

Blog entries alternating between a pair of distinct hues

I want to create a design where each post container has a different color from the one next to it. Essentially, I would like the containers to alternate between two distinct colors. The left side shows how it currently appears, while the right side depict ...

Tips for optimizing Angular source code to render HTML for better SEO performance

Our web platform utilizes Angular JS for the front-end and node js for the backend, creating dynamic pages. When inspecting the code by viewing the source, it appears like this: For our business to succeed, our website needs to be SEO-friendly in order to ...

Dynamic Navbar that Adapts to Your Scroll

I'm experiencing an issue with my navbar. I want it to stay at the top of the page when I scroll, but whenever I apply the "position: fixed;" property, the navbar disappears. Below is my HTML and CSS code: <nav> <div class="navbar"> <b ...

The FullCalendar does not appear as expected on Angular version 16

https://i.stack.imgur.com/DTAKr.pngI followed the steps to install FullCalendar in my Ionic 6 Angular 16 app as outlined on https://fullcalendar.io/docs/angular. However, nothing is showing up in the browser (chrome). The Inspector tool shows that the Ful ...

Error encountered while validating jQuery word cloud

Can anyone please assist me with validating a jQuery word cloud in HTML5? I'm encountering issues with all of the rel values showing this error message: "Bad value 1 for attribute rel on element a: Not an absolute IRI. The string 1 is not a registere ...

Is it possible to deactivate the click event on an Angular mat table row?

Within my Angular mat table, I have implemented code that expands a table row when clicked. However, I now need to prevent certain rows from being clickable based on the "element.disable" property. <ng-container matColumnDef="id"> <th mat-hea ...

A guide to optimizing material ui Datagrid performance in React using memoization

Having trouble implementing memoization on my Material UI Datagridtable in React const Mockdata = [{id: 1 , user: "john",School: "LAL" }, {id: 2 , user: "Ruby",School: "LAL" }] const column = [ { field: "u ...

What steps do I need to take to successfully integrate Firebase authentication with Angular2 FINAL?

After upgrading to Angular2 Final, I completed the migration project steps to transition everything over. Surprisingly, there were no errors during authentication; however, the issue arises post-authentication. Upon redirection back to the page, the authen ...

Creating overlapping layouts with absolute positioning in Bootstrap leads to a visually appealing

I'm currently working on a layout that involves absolute positioning elements, similar to the one shown in this example: https://i.sstatic.net/TixbZ.jpg It seems like the elements are overlapping and not clearing properly. I've been looking for ...

Angular 12 isn't showing any providers available for HttpHeaders

Encountering an error when attempting to utilize the get and post methods within a component. Trying to extend proved futile as HttpClient is marked as final. Even after trying to extend, the same error persists. ERROR Error: Uncaught (in promise): NullI ...

How to Implement Single Selection in Material UI Accordion with Multiple Select Options

As a newcomer to material-ui, I came across an example in their documentation showcasing the Accordion component. I am interested in using it but would like to restrict it to single select only. Unfortunately, the default version provided in the documentat ...

Looking for assistance with changing the class of a span when a radio button is selected

I'm facing an issue with toggling the class of a <span> when a radio button is clicked. <html> <head></head> <style type="text/css"> .eHide { display:none; } </style> <script type="text/javas ...

To resolve the issue of expired tokens, we must implement a mechanism in Angular to detect when a token has expired. When a token expiration

When utilizing an angular interceptor to include the authorization token in the header, everything functions smoothly until the token expires. Following the expiration of the token, Laravel returns a token_expired error. My goal is to detect this error wit ...