Support additional colors outside primary, accent, warn
Bug, feature request, or proposal:
Feature Request
What is the expected behavior?
Allow additional color pallets
What is the current behavior?
Only 3 color pallets
What is the use-case or motivation for changing an existing behavior?
Material design specifications no longer call for the use of three color states, instead offering the following recommendation
-
Primary color
-
Secondary color
-
Surface, background, and error colors
-
Alternative Colors: The Material Design color system supports alternative colors, which are colors used as alternatives to your brand’s primary and secondary colors (they constitute additional colors to your theme). Apps can use alternative colors to establish themes that distinguish different sections.
https://material.io/design/color/the-color-system.html#color-theme-creation
This would be extremely valuable in applications where a brand color is used for specific components such as the main app bar but is not the primary color. When the brand color is red for example, using it as the primary theme color is problematic, since primary colored input fields look like they are in error. But a red toolbar on the other hand does not suggest an error.
As regards as Material Theming, alternative colors should be supported: https://material.io/design/color/the-color-system.html#color-theme-creation (scroll down to alternative colors).
If you need to add alternative or custom colors, just add a class on the control itself in your template and override the color with what you need. If you are using brand colors or have some other color, just create re-usable sass variables and create custom css for your custom application. You can have multiple css classes on any control, just have to make sure you have the correct specificity. Remember, material ui is built upon html and css, so nothing is preventing you from adding custom inline style or css classes to a control to fit your needs. Also you are not limited by the material color system, so you can use any color you want - hex, rbga, hsla combinations...
@tqle thanks for the suggestion, but I think the point of this issue is theming support for additional colors as well as having the base components support additional colors. Knowing how to use CSS to implement whatever colors we want in our own components is not the problem.
Possibly related: #11390.
we need a success color (likely green), esp for form validations and stuff. Im fine if the material team doesnt want to use a success color design wise, but many users need a green - success color sometimes somewhere in there angular/material app. So they could implement a success color for those who need it.
Sometimes we want a different thing inside our application, sometimes we need to create social media butons, in which have their own color specification, et cetera.
@smnbbrv Any updates on this?
I figure out how I can make success color in Angular Material way.
- First make your own palette
- merge it to theme
- make corresponding mat-class
- use color="foo"
Here is example
$custom-collection: (
warning : #FFC116,
success : #0a630f,
danger: #c00000,
contrast: (
warning : #000000,
success : #FFFFFF,
danger: #FFFFFF,
)
);
$my-app-custom: mat-palette($custom-collection, custom);
$my-app-theme: mat-light-theme($my-app-primary, $my-app-accent, $my-app-warn); // this row your should have already in your code where you select palettes for primary, accent and warn
$my-app-theme: map_merge($my-app-theme, (custom: $my-app-custom));
$custom: map-get($my-app-theme, custom);
Then you can use $custom in your component like this
background-color: mat-color($custom, success);
color: mat-color($custom, success-contrast);
But better solution is to make mat-success-class like this
.mat-success {
background-color: mat-color($custom, success);
color: mat-color($custom, success-contrast);
}
Then you can use color directive like this
<button mat-flat-button color="success" >Success</button>
This works because color="foo" generates class="mat-foo" for element.
@JanneHarju Thanks for that workaround.
Also if you are just wanting to overwrite styles for 1 material design component (in my case SlideToggle) you can do so without adding the code above. Just add the mat-{your name} to the stylesheet.
Example for mat-postive where I want to change the ON toggle style only:
.mat-slide-toggle.mat-positive.mat-checked:not(.mat-disabled) .mat-slide-toggle-thumb {
background-color: #55cc00;
}
.mat-slide-toggle.mat-positive.mat-checked:not(.mat-disabled) .mat-slide-toggle-bar {
background-color: white;
}
then use it like this:
<mat-slide-toggle color="positive"> Edit Multiple </mat-slide-toggle>
@ccarns The comment in _theming.scss (@angular/[email protected]) says:
// If the theme has been constructed through the legacy theming API, we use the theme object // as color configuration instead of the dedicated
colorproperty. We do this because for // backwards compatibility, we copied the color configuration from$theme.colorto$theme. // Hence developers could customize the colors at top-level and want to respect these changes // TODO: Remove when legacy theming API is removed.
So instead of simple merge
$my-app-theme: map_merge($my-app-theme, (custom: $my-app-custom));
i had to replicate the definition under a 'color' property then use deep merge to make all stuff working properly (eg mat-get-color-config() function)
@use "sass:map";
$custom-collection: (
warning : #FFC116,
success : #0a630f,
danger: #c00000,
contrast: (
warning : #000000,
success : #FFFFFF,
danger: #FFFFFF,
)
);
$my-app-custom: mat-palette($custom-collection);
$my-app-theme: mat-light-theme($my-app-primary, $my-app-accent, $my-app-warn); // this row your should have already in your code where you select palettes for primary, accent and warn
$my-app-theme: map.deep-merge(
$my-app-theme,
(
custom: $my-app-custom,
color: (
custom: $my-app-custom
)
)
);
$custom: map-get($my-app-theme, custom);
Thank you for sharing that @notbear . Is that documented somewhere and should it be documented? I mean somewhere else than in code. For example in Angular Material documentation and in Angular Update Guide https://update.angular.io/ .
@aersosi IMO it's perfectly fine if you don't need themes in your app and/or you accept the fact that custom palette would need a custom approach.
But if you wanted to use scss functions from Material theme module or worked on an extendable design system with consistent api, you'd probably need to define a custom palette the same way as the original ones are done.
Any proper solution to this Material Designers ? OR at least an answer why it isn't standard in angular material to enable us to add more palettes?
@RichardBoyder
Just as angular is opinionated, material design is an opinionated design philosophy. There is no official statement in the documentation on material.io, but from a design point of view it is logical to leave out as many unnecessary elements as possible. Of course, many will now say that a success color is essential, but that is exactly what is debatable. Colors attract attention and distract from the essential, like the content. But an error color is essential because when things go wrong, the user really has to pay attention. A success color is rather "nice to have".
Angular Material only takes over the requirements of material design. And because material design does not specify a success color, angular material does not have it either.
At least that's how I explain it to myself.
@aersosi
Just as angular is opinionated, material design is an opinionated design philosophy. There is no official statement in the documentation on material.io
Taken directly from https://material.io/design/color/the-color-system.html#color-theme-creation
Alternative colors
The Material Design color system supports alternative colors, which are colors used as alternatives to your brand’s primary and secondary colors (they constitute additional colors to your theme). Alternative colors can be used to distinguish different sections of a UI.
Additional colors for data visualization
Apps can use additional colors to convey categories that are outside of your main color theme. They are still a part of your full color palette.
I do understand @aersosi what you mean. For sure. Just wish they gave us the ability to easily add if we want.. I get they are opinionated. But in todays World of Development and design... we cannot be opinionated any more , especially if they provide material as a open source product for all to use... Often clients want specific colors... And i find it just limiting , which development and design should never ever be limiting....
But as you say i do understand their reasoning, i just dont like it.. Having to find a decent hack to get a success or any other color i want in is a pain... having only 3 palletes .... its bizare.
@ju-sys
By alternative colors, they mean brand-specific colors. With brand specific, I mean something like, Microsoft Word is blue, and Microsoft Excel is green. This is important to be able to distinguish products from each other in a uniform design system. But this has nothing to do with signal colors like green/yellow/red.
Colors drag attention. If you put colors all over the place, the user doesn't know where to look at because everything is screaming: Here is something important! I guess they decided to put brand/identity over signals. Your app should be frictionless anyway and only when really something bad happens you need the users attention.
It's kind of like with typography. If you write an article you use an expressive font for the headline to drag his attention but when the user has to really focus on the content, like reading the long article, you use a neutral, flavorless font to avoid distraction.
@RichardBoyder
I would also like to have more flexibility without hacky, bulcky code. Everybody is opinionated, especially nowadays. Just look at politics or social media, it's bonkers. We don't live in the times of enlightenment anymore.
But we can choose. Hopefully nobody forces you to use material, and if so: You probably are a developer - so develop something. In the meantime, keep on complaining until they can't stand it anymore and add optional colors, I guess.
We'll see what the implementation of Material MDC will give us. And generally it's not too bad to have a solid opinion about something if you have good reasons for.
@RichardBoyder
I figured many (but not all) components take color='whatever'without complaining.
So I did:
@use './colors';
@each $key in map-keys(colors.$custom-palette-keys) {
.mat-flat-button,
.mat-raised-button,
.mat-fab,
.mat-mini-fab {
&.mat-#{$key} {
background-color: map-get(colors.$custom-palette, $key);
color: map-get(colors.$custom-palette, contrast, $key);
}
}
}
In my imported colors.scss I have:
$custom-palette: (
passive : hsl(0, 0%, 73%),
success : hsl(140 , 40%, 50%),
whatever: hsl(45, 100%, 50%),
contrast: (
passive : black,
success : white,
whatever: black,
)
);
$custom-palette: mat.define-palette($custom-palette);
$custom-palette-keys: map-remove($custom-palette, contrast);
Now you just have to add a new color and contrast color to your custom palette and after compile you can use it. Of course, you have to write the loops for each component. And I guess this solution has to be rewritten when MDC is ready.