Customizing Angular Material just got easier in v18!

Zoaib Khan
7 May 202414:11

Summary

TLDRThe script discusses the upcoming Angular Material 3 and its integration with Angular version 18, which simplifies component customization using CSS variables. It showcases a custom button styler and explains how to change properties like roundness, height, text size, and colors at a granular level without affecting other components. The video also covers the limitations of previous versions and the benefits of the new MDC-based components, including the use of design tokens as CSS variables for consistent styling across projects.

Takeaways

  • 😀 Angular Material 3 in version 18 makes customizing components easier with the use of CSS variables.
  • 🛠️ A custom button Styler is showcased to demonstrate the ability to adjust properties like roundness, height, text size, and colors at a granular level.
  • 🔄 Changes made to CSS variables are scoped and do not affect other components unless intentionally scoped globally.
  • 📚 Theming APIs in Angular Material previously offered limited customization options, mainly color, typography, and density.
  • 🔧 Prior to Material 3, customization often involved deep-diving into CSS internals and manually overriding styles, which could break with updates.
  • 🌐 CSS variables in Material 3 are tied to Material Design tokens, providing a consistent set of properties for design and engineering teams.
  • 🔑 Each Material Design token is implemented as a CSS variable, allowing for easy identification and customization of component properties.
  • 📝 Customizing a component involves setting the value of the specific design token, which can be scoped to a global or component level.
  • 🎨 The video demonstrates creating a custom button Styler with sliders to adjust CSS variables in real-time for properties like border radius, height, and font size.
  • 📈 The introduction of MDC-based components in Angular Material 15 led to a breaking change in CSS selectors, requiring developers to update their code and styles.
  • 🔍 The video suggests a need for a comprehensive list of design tokens for easier customization without relying on style inspectors.

Q & A

  • What is the main topic of the video script?

    -The main topic of the video script is the customization of Angular Material 3 components using CSS variables in the upcoming Angular version 18.

  • What is a custom button Styler in the context of the video?

    -A custom button Styler is a tool created to showcase or test the customization of Angular Material 3 components, allowing users to adjust properties like roundness, height, text size, and colors using CSS variables.

  • How does the new Angular version 18 make customization of components easier?

    -Angular version 18 makes customization easier by utilizing CSS variables and design tokens, which can be scoped to specific components or levels, allowing granular control over component appearance.

  • What were the limitations of customizing Angular Material components in version 11?

    -In version 11, theming APIs provided limited customization options, mainly color, typography, and density, without specific settings to change properties like border radius.

  • What is the significance of design tokens in the context of Angular Material components?

    -Design tokens are material design properties with consistent names and values that can be referred to by all teams, implemented as CSS variables in Angular Material components, allowing for consistent and granular customization.

  • How can CSS variables help in maintaining consistency across different teams in a project?

    -CSS variables, as implemented from design tokens, provide a common set of style properties that can be used by engineering and design teams, ensuring consistency in the appearance and behavior of the application.

  • What is the problem with directly modifying the internal CSS of Angular Material components?

    -Directly modifying internal CSS can lead to maintenance issues, as changes in the component internals may require updates to the custom CSS, potentially breaking existing styles.

  • What is the MDC-based components refactor in Angular Material 15?

    -The MDC-based components refactor in Angular Material 15 is an internal change that introduced Material Design Components, which altered the CSS selectors and required developers to update their code to maintain their customizations.

  • How can the new customization approach in Angular Material 3 help with client-specific requirements?

    -The new approach allows developers to create custom classes or directives with scoped CSS variables, making it easier to accommodate client-specific requirements and project needs without global style impacts.

  • What will the video demonstrate in a future episode regarding custom theming?

    -The video will demonstrate how to implement a simple custom theme using CSS variables in a real project, showing an alternative to the Angular Material theming API for a simpler customization process.

  • What is the potential issue with relying on style inspectors to find design tokens for customization?

    -Relying on style inspectors can be inefficient and may lead to inconsistencies, as developers would prefer a comprehensive list of design tokens for each component to streamline the customization process.

Outlines

00:00

🛠️ Customizing Angular Material with CSS Variables

The script introduces the ease of customizing Angular Material 3 components with the upcoming Angular version 18. The presenter demonstrates a custom button styler, showcasing how to adjust various properties like roundness, height, text size, and colors using CSS variables. This feature allows for granular customization at different levels, from global to component-specific, without affecting other components. The presenter also discusses the limitations of previous versions, where customization required deep CSS manipulation, which was prone to breaking with internal changes in component structure.

05:01

🔄 Transitioning from Prem MDC to MDC-based Components

This paragraph delves into the challenges faced by developers when customizing Angular Material components in version 11 and the transition to MDC-based components in version 15. The presenter explains how theming APIs were insufficient for detailed customization, leading to workarounds like using different components or deep CSS manipulation. The latter approach, however, resulted in extensive code changes when Angular Material underwent internal refactoring. The paragraph highlights the introduction of CSS variables in MDC-based components, which represent Material Design tokens and offer a more stable and customizable approach to theming.

10:01

🎨 Implementing Design Tokens for Component Customization

The final paragraph focuses on the practical implementation of design tokens as CSS variables in Angular Material 3. The presenter explains how to identify and modify these variables to customize component properties, such as border radius, height, and text size. A demonstration of creating a custom button styler is provided, illustrating how to bind CSS variables to component properties using Angular's host binding. The benefits of this approach include scoped customization and the assurance of stable design token names, with the Angular Material team committing to provide migrations for any future changes. The presenter also hints at future content on using custom theming with CSS variables, simplifying the process beyond the theming API.

Mindmap

Keywords

💡Angular Material

Angular Material is a UI component library for Angular applications, providing a wide range of pre-built components that follow Material Design guidelines. In the video, the speaker discusses the evolution of Angular Material and its impact on customizing UI components, particularly with the release of Angular version 18.

💡Customization

Customization refers to the process of adapting or modifying a product or component to meet specific needs or preferences. The video focuses on how Angular Material 3 and the upcoming version 18 have made it easier for developers to customize the appearance and behavior of Material components using CSS variables and design tokens.

💡CSS Variables

CSS Variables, also known as custom properties, are a feature in CSS that allows for the creation of variables to store values that can be reused throughout the stylesheet. In the context of the video, CSS variables are used to customize the properties of Angular Material components, such as border radius, height, and text size.

💡Design Tokens

Design tokens are a set of design-agnostic, system-wide values that can be used to ensure consistency in design and development. In the video, the speaker explains how Angular Material 3 implements design tokens as CSS variables, enabling developers to easily customize component styles across an application.

💡Theming APIs

Theming APIs in Angular Material are used to apply a consistent theme to an application, including primary, accent, and warn colors, typography, and density. The video script mentions that while Theming APIs are powerful, they were previously limited in terms of the customization options they provided for individual component properties.

💡MDC-based Components

MDC stands for Material Design Components, which is a project by Google that implements Material Design in various programming languages, including Angular. The video discusses the transition to MDC-based components in Angular Material 15, which changed the internal structure of components and required developers to update their custom styles.

💡Component Scoping

Component scoping refers to the practice of limiting the scope of certain styles or behaviors to specific components within an application. The video demonstrates how changes made to CSS variables in Angular Material can be scoped to individual components, preventing style changes from affecting other components.

💡Material Design Tokens

Material Design Tokens are the specific values used in Material Design to maintain consistency in visual properties like color, typography, and shape. In the video, the speaker explains that these tokens are implemented as CSS variables in Angular Material 3, allowing for a more granular level of customization.

💡Host Binding

Host binding in Angular is a feature that allows for binding values directly to the host element of a component. In the video, the speaker uses host binding to associate the values of CSS variables with the properties of the Material components, demonstrating how to apply custom styles at the component level.

💡Custom Button Styler

The Custom Button Styler mentioned in the video is a tool or feature created by the speaker to showcase and test the customization capabilities of Angular Material components. It allows for adjusting properties like roundness, height, text size, and colors using sliders, with the changes being applied in real-time to the components.

💡Granular Customization

Granular customization refers to the ability to make very specific and detailed adjustments to a component or system. The video emphasizes the new level of granular customization available in Angular Material 3 and version 18, where developers can change nearly every property of a component using design tokens and CSS variables.

Highlights

Customizing Angular Material 3 components has become easier with the upcoming release of Angular version 18.

A custom button Styler showcases the ability to create and test custom styles for Angular Material components.

Four identical components with sliders demonstrate the customization of roundness, height, text size, and colors using CSS variables.

Changes made to components can be scoped to specific levels, avoiding unwanted propagation of styles.

Customization at a granular level allows for client-specific or project-specific requirements to be met.

Theming APIs in Angular Material previously offered limited customization options for component appearance.

Using different components or deep CSS inspection were common workarounds for customization before Angular Material 3.

Internal refactoring in Angular Material 15 with MDC-based components broke existing CSS overrides, requiring developers to update their code.

CSS variables in Angular Material 3 are tied to Material Design tokens, providing a consistent set of properties for customization.

Design tokens are a common set of properties used for communication between engineering and design teams in large projects.

Custom button Styler allows for real-time changes to component properties by manipulating CSS variables.

Host binding in Angular provides a way to bind signal values to CSS variables for component-specific style changes.

Custom styles can be encapsulated within classes or directives for easier application to material components.

Angular Material 3's design tokens cover a wide range of properties, enabling granular customization for every component.

The color theming API can be supplemented with custom theming using CSS variables for more personalized component appearances.

The Angular Material team has committed to maintaining the consistency of design token names, ensuring stability for developers.

A future video will delve into custom theming with CSS variables, showcasing a real project implementation.

Transcripts

play00:00

so customizing angular material 3

play00:01

components has just got a lot easier or

play00:03

it's about to become a lot easier with

play00:05

the new angular version 18 due to be

play00:08

released sometime this month and as you

play00:10

can see you can actually create pretty

play00:12

cool stuff like this custom button

play00:13

Styler that I created recently to show

play00:16

case or test this out and you can see

play00:17

that I have four identical components

play00:20

here each have some sliders and uh then

play00:22

they have also a button which we are

play00:25

actually changing and then you can

play00:27

change the roundness you can change the

play00:29

height and you can change the text size

play00:31

according to your needs even the colors

play00:33

of the button all by using the powers of

play00:36

the CSS variables and the cool thing

play00:38

about this is that you can actually

play00:40

scope these changes to specific

play00:43

components or at any level that you want

play00:45

so for example these are four components

play00:47

and the change that I make here to the

play00:49

component is not going to flow here I

play00:51

can make another change here I can

play00:54

change the color here and you can see

play00:56

that the changes that I'm making are all

play00:58

scoped to this specific compon component

play01:00

similar for other components here so

play01:02

this is really cool because this allows

play01:04

us to customize the components at a

play01:06

really granular level according to our

play01:07

needs according to sometimes client

play01:09

requirements or even you know just

play01:12

further requirements of the project that

play01:13

we want so what has changed exactly

play01:15

let's go through this now if you're a

play01:17

longtime material user you would know

play01:19

how difficult it is to actually change

play01:21

anything or customize uh the appearance

play01:23

of your material components so if you go

play01:25

in the angular material official

play01:26

documentation site and I switch over to

play01:28

version 11 which which is Prem MDC

play01:31

components one you can see that we have

play01:33

a basic set of buttons but what if you

play01:35

want to change the appearance of these

play01:36

buttons well we could try out three

play01:38

different things so the first thing we

play01:40

we could try out would be to actually

play01:42

try out the theming apis maybe the

play01:43

theming apis of angular material provid

play01:46

such a facility or such a feature to

play01:48

change these appearance let's say we

play01:50

want to change the Border radius of the

play01:52

components maybe we want to make it more

play01:54

squarish or we want to make it more

play01:55

rounded so the first step would be maybe

play01:58

we want to use a theming apis but the

play02:01

problem is the theming apis only provide

play02:04

us three properties that we can change

play02:06

so one is the color you can have the

play02:08

primary accident wand color and you have

play02:10

to give a material plate to the colors

play02:12

so that we can use those colors then we

play02:14

have typography which we can change the

play02:16

font and then we have density so that is

play02:19

how things are Clos and far apart in the

play02:21

components themselves we don't have any

play02:23

specific setting to actually change the

play02:25

Border radius or you know change some

play02:27

specific property of the that we want so

play02:31

then the second thing we could do would

play02:33

be to uh maybe use a different component

play02:35

and that's what I have done a lot of

play02:37

times in my project so for example if I

play02:39

need a smaller button you know some I I

play02:42

have space for just a small button in my

play02:44

UI and I don't want to use this this big

play02:47

button maybe I would go to the chips

play02:49

section and I would look at the chips

play02:51

and I would take these chips components

play02:53

but of course the chips components are

play02:55

meant to be used in a specific context

play02:57

and they're not exactly like buttons so

play02:59

maybe we don't want to use that so then

play03:01

the third option which is the last

play03:03

option that a lot of people a lot of

play03:05

material devs they end up using till now

play03:07

has been to actually let's go back to

play03:09

the button to actually go deep into the

play03:12

CSS and you can inspect the CSS here

play03:15

like for example this raised when we

play03:17

want to change this and we can go in the

play03:20

internals of this and we can see that it

play03:22

has a box Shadow it has a margin it has

play03:25

a background color and you can see it

play03:27

has a border radius here of four pixels

play03:30

so basically we want to change this so

play03:33

what we do here then is that we'll try

play03:35

changing this let's say we make it 16

play03:37

pixels and yeah I like this new rounded

play03:39

shape maybe we want to change it in this

play03:41

way so we change it to 16 pixels that

play03:44

this works that means we can Target this

play03:46

selector here the CSS selector and then

play03:48

we can add this to our styles. SAS to

play03:51

make it uh change globally if you want

play03:53

to make it change locally we'll have to

play03:55

sort of use NG deep maybe to actually

play03:58

Target these internal CSS selectors so

play04:01

again not very ideal and you'll see a

play04:02

lot of code bases they have these

play04:04

changes I myself have been using it

play04:06

because simply there was no other way

play04:08

now that's all and well and it works as

play04:09

you can see but what's the problem now

play04:11

the problem here is that when the

play04:13

internals of the component changes we

play04:16

would have to update our code as well

play04:17

and that is exactly what happened when

play04:19

the angular material team changed to the

play04:21

angular material design components

play04:24

Factor internal refactor so for example

play04:26

if you go in the version of maybe 15 I

play04:30

guess so angular material team

play04:32

introduced the MDC based components in

play04:34

angular material 15 and let's see in

play04:36

going your components the same thing

play04:38

button now you have the appearan is the

play04:40

same you can see but when you inspect it

play04:43

you're going to see that the internals

play04:45

have changed so you can see now the name

play04:48

of the CSS selector has changed Matt MDC

play04:51

race button previously it was Matt race

play04:53

button so you'll have to update all of

play04:55

your code within that and it's going to

play04:56

break all of the existing overrided

play04:59

styles that you have been doing and that

play05:00

is what happened personally with me as

play05:02

well in a very complex project I had

play05:04

sort of over overr different styles to

play05:07

make it look the way that we wanted but

play05:09

with the MDC change in angular version

play05:11

15 a lot of things broke the appearance

play05:14

went completely haywire and I had to

play05:16

spend a lot of time to actually fix all

play05:19

of the UI issues but thankfully with the

play05:22

material three changes all of that is

play05:24

behind us now so as you can see here

play05:26

with the MDC based changes you would

play05:28

notice that we have these variable

play05:31

references here and these are basically

play05:32

referring to CSS variables but what are

play05:36

these CSS variables basically so it was

play05:38

revealed these are basically material

play05:40

design tokens and if you go on the

play05:42

material design website the official

play05:43

material design website you can see

play05:45

there's a whole section on design tokens

play05:47

and they have explained what the purpose

play05:49

of a design token is so so the concept

play05:52

is basically of a common set of

play05:53

properties which has a key has a value

play05:57

and this can be referred to between the

play05:59

engine engineering and the design teams

play06:01

of any big project and basically the

play06:03

implementation of these tokens in

play06:06

different platforms varies but the

play06:09

tokens names and the values will remain

play06:11

the same so there will be a common set

play06:13

of styles that we can actually refer to

play06:15

by all teams now obviously it's a larger

play06:18

concept and this is obviously meant for

play06:20

larger teams but for our case for the

play06:23

angular material components these design

play06:26

tokens have been implemented as CSS

play06:28

variables so let's go back to our

play06:30

material 3 demo here that we created and

play06:32

you can see that we have a button here

play06:34

so how do we find out which property we

play06:36

actually want to change so we are going

play06:38

to go in the developer tools as I showed

play06:41

before and you're going to inspect this

play06:42

button and when you look at this button

play06:44

you are going to see that almost all of

play06:47

the properties are now referring to a

play06:49

CSS variable so we are going to inspect

play06:51

that for example we want to change the

play06:54

Border radius as we discussed before

play06:56

when we look at the border radius we're

play06:57

going to see that it refers to the

play06:58

variable MD the protected button

play07:00

container shape and you can see that the

play07:02

value for this has been defined in the

play07:03

HTML portion of the app that means it's

play07:05

the most top level and uh it has a value

play07:08

of 999 pixels let's try and change this

play07:11

to see whether it changes the radius and

play07:13

you can see that yes the radius changes

play07:15

a bit and in fact the radius of all the

play07:17

buttons change so how do we make this

play07:19

custom Uh custom button Styler using

play07:21

this so all we need to do to change now

play07:24

the components at any level is to just

play07:26

set the value of the specific design

play07:29

token so here for example we are

play07:31

changing it in the HTML portion so this

play07:33

is basically going to be followed all

play07:36

over the app so this is good for custom

play07:38

changes but if for example you want to

play07:41

restrict it to specific places even to a

play07:43

specific component itself so let's look

play07:46

at the code a bit for this button styler

play07:48

and see how we can implement this so if

play07:50

for example we go in our code here you

play07:52

can see that our button config is here

play07:55

and let's just split this up so that we

play07:57

can actually see the mat demo alongside

play08:00

this and you can see here that we have

play08:03

the slider the roundness slider the

play08:05

height slider it's a matte slider and

play08:07

the text size slider so there are three

play08:09

sliders and each of the sliders are

play08:11

basically linked to the signals which

play08:15

actually contain the value of the slider

play08:17

at any time so we have three signals

play08:20

here which we have declared we have

play08:21

given them some initial value and then

play08:23

all we have to do is that we want to

play08:25

associate this CSS variable that CSS

play08:29

variable for that property to this

play08:31

signal value and this is the button that

play08:33

is actually going to be changed so we

play08:35

can actually change the style for this

play08:37

button directly as well but let's first

play08:40

see how we can change the style for this

play08:42

whole component scope it within that

play08:44

component so for example we can go in

play08:47

our host binding here now the host

play08:49

binding here is going to provide us a a

play08:51

good way to actually bind the signal to

play08:54

the CSS variable or the style binding so

play08:57

let's add a style binding here now now

play08:59

in our now in our developer tools we saw

play09:03

that we wanted to override a specific

play09:05

CSS variable so what was that and let's

play09:08

copy that in and let's go to that here

play09:11

and we can see that we have we have this

play09:13

button let's see which one we want so we

play09:15

want to overwrite this border radius and

play09:18

let's just copy this value here so this

play09:21

is basically the name of the CSS

play09:23

variable that we want to update and

play09:25

let's make this change here we are going

play09:27

to say Style dot the CSS variable and

play09:31

this is going to be assigned to the

play09:33

roundness setting which is linked to the

play09:36

roundness value here so we going to say

play09:39

let's say we can also do it like this

play09:41

roundness and we're also going to give

play09:44

it pixels here great so let's see if

play09:46

this works let's save this and now you

play09:48

can and now we're going to try to change

play09:50

the roundness so when we reduce the

play09:51

roundness you can see that the roundness

play09:53

changes here we make it maximum it goes

play09:55

here so it's from 0 to 30 pixels we keep

play09:58

this here and because it is scoped just

play10:00

to the component we can see that this

play10:03

roundness remains at 19 and when we

play10:05

change this this can be made to made

play10:08

zero and it's going to only affect this

play10:10

button because this is contained within

play10:12

this component the same thing with

play10:13

others so nice and simple now if you

play10:16

wanted to make it Global we could

play10:17

actually give it in a Styles file the

play10:19

style. SAS which is a global file or a

play10:22

level above this specific component

play10:24

similarly we are going to do the same

play10:26

with the two or three variables that we

play10:28

have the other variables that we have so

play10:31

let's say we want to change the height

play10:33

of this so let's see what the height is

play10:36

and you can see the height is here MDC

play10:40

protected button container height click

play10:42

on this and we can actually copy this

play10:45

like this let's change this here we need

play10:48

another key here this is going to be

play10:50

style and this this is going to be

play10:53

height the signal value pixels and we

play10:56

are going to make it another like this

play10:59

okay so this should work as well let's

play11:01

save this here and now we can test the

play11:03

height and you can see that yes the

play11:04

height changes here you can reduce the

play11:06

height here you can change the height

play11:08

here so pretty nice and the last one is

play11:10

the text size so let's change the text

play11:12

size here text size we are going to look

play11:15

at the label itself and you can see the

play11:19

color and you can see the font size so

play11:21

MDC protected button label text size now

play11:24

you can be rest assured that these

play11:25

styles are not going to change these

play11:28

design token names are not going to

play11:29

change that frequently okay so the

play11:32

angular material team has promised that

play11:34

these design token names are going to

play11:36

remain the same for the foreseeable

play11:38

future and if they're going to be

play11:39

changed slightly in the future there

play11:41

will be migrations provided so that it

play11:43

becomes easier for us to change it in

play11:45

our code basis but again it would be

play11:47

good if they provide a long list a

play11:49

comprehensive list of all of the design

play11:51

tokens available for each component so

play11:53

that we don't have to go in our uh style

play11:55

inspectors to get these but for now this

play11:57

will have to do so if if you can go MDC

play12:00

protected label text size I can copy

play12:02

this here and let's now close this once

play12:04

and for all and we're going to put this

play12:06

here again let's say style Dot and this

play12:10

is going to be text size and for these

play12:12

we are going to keep it as RM cuz that

play12:15

makes more sense okay so now when we

play12:18

increase the text size you can see the

play12:19

text size beautifully increases this is

play12:21

a small button maybe you want a bigger

play12:23

button so we have a bigger button here

play12:25

with a less roundness and we have all of

play12:27

our features that I just showed you

play12:29

initially so of course probably you're

play12:31

not going to be creating this button

play12:33

Styler for uh your users but more likely

play12:36

you would want to keep these custom

play12:37

Styles in a sort of a custom class that

play12:40

you would be using maybe you would be

play12:42

creating a big button Class A small

play12:44

button class and then you can

play12:45

encapsulate these CSS variables or css

play12:48

variable changes within that class and

play12:50

then you apply it to wherever you need

play12:52

you can also use directives to attach it

play12:54

to your material components and you can

play12:56

convert them to your own custom

play12:58

components that way now this opens the

play13:00

doors for a lot of different

play13:01

customization possibilities now I've

play13:04

only covered some three properties here

play13:06

but you can see that there is a design

play13:09

token basically for every property out

play13:11

there baring very minor properties which

play13:14

actually nobody cares about so this

play13:16

opens a door for a lot of granular

play13:18

customization in and the angular

play13:20

material components now and you can now

play13:23

accommodate your client requests

play13:24

accommodate your project needs and you

play13:26

can actually customize the material

play13:28

components appearance according to your

play13:30

own needs now this also includes

play13:32

changing the color so customizing the

play13:34

color now the color theming API is

play13:36

provided by material but in a recent

play13:38

project I discovered that I can also use

play13:41

my own custom theming using these CSS

play13:43

variables now I showed that initially

play13:46

but I'm going to go into more detail in

play13:48

the next video for that and I'm going to

play13:50

show you a real project as well that I

play13:52

did for a client in which I basically

play13:54

implemented a simple custom theme of two

play13:57

or three colors which was much simp

play13:59

simpler than you know using the angular

play14:00

material theming API so I hope you like

play14:02

this video if you liked it please

play14:04

subscribe uh and comment so that it will

play14:07

reach more people thanks for watching

play14:10

and I'll see you next time

Rate This

5.0 / 5 (0 votes)

Related Tags
Angular MaterialCustomizationCSS VariablesComponent StylingMaterial DesignAngular Version 18UI DevelopmentDesign TokensWeb DevelopmentTheming API