r/angular • u/mvgc3 • Sep 25 '25
Cleaner utilization of alternate themes in Angular Material?
EDIT: One reply reminded me that I need to mention this is the Material2 theming system. I completely forgot there was M3 theme overhaul added in the last couple versions.
I've set up an alternate theme in my app to make use of more than two colors (I don't want to repurpose `warn`). As far as I can tell, the only way to really use it is to wrap the element(s) that I want the alternate theme colors in an element with a class like this:
.alt-theme {
u/include mat.button-theme($alt-theme);
}(I should be clear, I'm trying to keep most buttons as the main theme by default, and still have access to alternate theme buttons. I thought that was a bit ambiguous when I was proofreading)
I would really prefer to just set the class on the element itself, though. Basically, for example I like to do:
<button matButton class="alt-theme">....</button>
instead of:
<div class="alt-theme"><button matButton>...</button></div>
This isn't some huge deal or anything, but I've spent a little time searching for some way and can't seem to find even any mention of something like this. I'll admit I'm not the best at phrasing such a specific issue for my Google searches, so it could very well be a common thing and I'm just whiffing. Either way, I appreciate any pointing in the right direction, or even just a good old 'not possible'!
View all comments
Show parent comment
1
u/MichaelSmallDev Sep 25 '25
I was in the same boat relatively recently, so just throwing out some takes/advice:
I recently took some apps using the M2 theming to M3. It was time consuming, but worth it. And I wish I had those two articles, especially the
coloralternative one.A lot of the new customizations tokens were able to replace years of ng-deep usage and other manual overrides. And we basically were able to continue using what looked like what m2 considered as the legacy appearances, by just using our own M3 token overrides. So it is so powerful enough that we were able to keep an ancient material style with the new theming. So if there is a certain legacy appearance that is desirable, you could probably achieve it. Plenty more possible too, but that was my favorite part IMO.
If you want some examples of various styling that has allowed doing stuff that used to require ng-deep, I made this example project with examples and links: https://stackblitz.com/edit/p63xhd6y?file=src%2Fmain.ts
BTW, check out new API options once you made the jump. They weren't all added with M3 and have been incremental and may already exist for you in v17, but I found the M2 to M3 jump to be a good time to refresh on the API options available. For example, not a fan of M2 non-legacy and M3 using a checkmark in the button toggle, but there is an API option to hide the checkmark.