Angular Material - change color of clicked mat-list-option - angular-material2

Is there a possibility to change the default color of a checked checkbox (mat-pseudo-checkbox-checked):
<mat-selection-list #shoes>
<mat-list-option *ngFor="let shoe of typesOfShoes">
{{shoe}}
</mat-list-option>
</mat-selection-list>
I have tried:
.mat-pseudo-checkbox-checked {
background-color: #00f;
}
but it has no impact.

Just add class="mat-primary" inside <mat-list-option>
<mat-selection-list>
<mat-list-option class="mat-primary" checkboxPosition="before" *ngFor="let shoe of typesOfShoes">
{{shoe}}
</mat-list-option>
Output:

I am not sure but you can try using this
.mat-select-content, .mat-select-panel-done-animating {
background: mat-color($background, card);
}
to
.mat-select-content, .mat-select-panel-done-animating {
background: mat-color($background, card);
.mat-option {
color : mat-color($foreground, text);
}
}
for details, you can also check the following link
https://github.com/angular/material2/blob/master/src/lib/list/_list-theme.scss

.mat-pseudo-checkbox{
background-color: red;
}
this worked for me, just apply background-color property to checkbox class, remove checked class

Checking in with Material v 7.3.6, similar to Sathwik, I had success with
.mat-pseudo-checkbox-checked {
background: #FF0;
}
Adding -checked at the end ensures that the checkbox is only colored in when it is actively checked, otherwise the box is always that color (which may be your goal?). Note that I had to include this style in the highest level styles.css file instead of the individual component style file for it to successfully work.
If you have multiple selection lists, you can apply the styles to the desired lists with classes. Furthermore, you can apply different colors to each checkbox option within a selection list using dynamically generated classes too! Example here: https://stackblitz.com/edit/angular-dtfd6x?file=app/list-selection-example.ts
You can see that the first selection list has the basic styling, whereas the second list (wrapped in unique-selection-list class) has different styling with each option having a unique color (generated by applying unique classes to each option with the *ngFor loop.
// HTML file
<mat-selection-list #shoes>
<mat-list-option *ngFor="let shoe of typesOfShoes">
{{shoe}}
</mat-list-option>
</mat-selection-list>
<p>
Options selected: {{shoes.selectedOptions.selected.length}}
</p>
<br>
<hr>
<mat-selection-list #colors>
<div class="unique-selection-list">
<div *ngFor="let color of colorsList" [class]="color.className">
<mat-list-option
[value]="color.displayName">
{{ color.displayName }}
</mat-list-option>
</div>
</div>
</mat-selection-list>
// component.ts file constants
typesOfShoes: string[] = ['Boots', 'Clogs', 'Loafers', 'Moccasins', 'Sneakers'];
colorsList = [
{
hexCode: '#00F',
displayName: 'Blue',
className: 'blue-checkbox',
},
{
hexCode: '#F0F',
displayName: 'Fuchsia',
className: 'fuchsia-checkbox',
},
{
hexCode: '#0F0',
displayName: 'Green',
className: 'green-checkbox',
},
];
}
// styles.css
#import '~#angular/material/prebuilt-themes/deeppurple-amber.css';
.unique-selection-list .blue-checkbox .mat-pseudo-checkbox-checked {
background: #00F !important;
}
.unique-selection-list .fuchsia-checkbox .mat-pseudo-checkbox-checked {
background: #F0F !important;
}
.unique-selection-list .green-checkbox .mat-pseudo-checkbox-checked {
background: #0F0 !important;
}

SRC/STYLES.SCSS
.mat-list-option {
.mat-list-item-content {
padding: 2px !important;
.mat-list-text {
padding: 2px !important;
}
.mat-pseudo-checkbox.mat-pseudo-checkbox-checked {
background-color: darkorange;
}
}
}
If you have other groups you can do this
HTML
<mat-selection-list ...>
<mat-list-option formfiltros-operaciones ...>
{{operacion.nombre}}
</mat-list-option>
</mat-selection-list>
<mat-selection-list ...>
<mat-list-option formfiltros-inmuebles ...>
{{inmueble.nombre}}
</mat-list-option>
</mat-selection-list>
<mat-selection-list ...>
<mat-list-option formfiltros-localidades ...>
{{localidad.nombre}}
</mat-list-option>
</mat-selection-list>
SRC/STYLES.SCSS
Fix padding
.mat-list-option {
.mat-list-item-content {
padding: 2px !important;
.mat-list-text {
padding: 2px !important;
}
}
}
apply pseudo-check color to 'formfiltros-operaciones' group
.mat-list-option[formfiltros-operaciones] {
.mat-list-item-content {
.mat-pseudo-checkbox.mat-pseudo-checkbox-checked {
background-color: darkorange;
}
}
}
apply color for pseudo-check in 'formfiltros-inmuebles' group
.mat-list-option[formfiltros-inmuebles] {
.mat-list-item-content {
.mat-pseudo-checkbox.mat-pseudo-checkbox-checked {
background-color: green;
}
}
}
apply color for pseudo-check in 'formfiltros-localidades' group
.mat-list-option[formfiltros-localidades] {
.mat-list-item-content {
.mat-pseudo-checkbox.mat-pseudo-checkbox-checked {
background-color: blue;
}
}
}
it looks like this

Related

Can i use css attribute selector within a SASS class to style itself

I am trying to style a class using css attribute selectors to apply to the class itself. My use case is to wrap them in media selectors to style a class when a breakpoint applies but I want to keep the example simple and work from first principles.
I have a sandbox for the following code here
Here's my logic... In SCSS we can style a class itself using &
i.e.
.myclass {
& {
border: ...
}
}
and we can refine that to specific class combinations
.myclass {
&.otherclass {
border: ...
}
}
we can also style classes using attribute selectors
div[class*="md"] {
border: 10px solid orange;
}
Can we combine these to use them within a sass class to say "style classes according to an attribute selector within the class scope only to combine the & and div[class*="value"] specifiers?
i.e. for
<div className="test1">
<div className="xs md">I am styled using div[class*="md"] selector</div>
</div>
<div className="test2 xs md">
I am not styled by my selector. Can I style myself using attribute
selectors?
</div>
with style
.test1 {
& {
background: lightblue;
}
div[class*="md"] {
border: 10px solid blue;
}
}
.test2 {
& {
background: lightgreen;
}
div[class*="md"] {
border: 10px solid green;
}
}
can I style .test2.md from within the class similar to using &?
thanks
Do you want to do this
.test2 {
& {
background: lightgreen;
}
&[class*="md"] {
border: 10px solid green;
}
}

child selector in hover in scss

so I have this html code
<aside class="sidebar">
<div class="sidebar__logo">....</div>
....
</aside>
I want the sidebar__logo width change to 80px when I hover over aside
so I write this scss style
.sidebar {
width:100px;
&__logo{
width : 40px;
}
&:hover {
width:200px;
& .sidebar__logo {
width: 80px;
}
}
}
which works fine, but it's not a BEM approach.
how can I change it to be something like this
.sidebar {
width:100px;
&__logo{
width : 40px;
& hovered over the parent {
width : 80px
}
}
&:hover {
width:200px;
}
}
You could use $self to cache the current selector (&) and then access the .sidebar__logo with #{$self}__logo instead. More info: caching current selector

sass / scss - create class shortcuts

While doing some css I came up with the idea of combining multiple classes into one. The reason is, to use short class names which could probably already be in use.
Assume the following:
.f-box-cell {
flex : 1 1 auto;
// colors
&.red { background-color: red; }
&.green { background-color: green; }
...
// sizes
&.b25 { flex-basis: 25%; }
&.b50 { flex-basis: 50%; }
...
// other options
...
}
Instead of doing ...
<div class="f-box-cell">
<div class="f-box-cell red">
<div class="f-box-cell b25">
<div class="f-box-cell red b25">
... I want do have something like ...
<div class="f-box-cell">
<div class="f-box-cell-red">
<div class="f-box-cell-b25">
<div class="f-box-cell-red-b25">
... with the same effect as the normal class listings.
Obviously, the more "subclasses" are added the more combinations would exist. So I'm interessted - if possible - in creating all these combinations automatically. Completely awesome would be, if the position of the "classes" inside the "combined class" would not matter.
Therefore my 2 main questions are:
Is there a way to achieve something like this with sass / scss?
Is the idea completely stupid?
Thanks in advance
If you only want to have all your classes prefixed and don't mind adding two classes (class="f-box-cell f-box-cell-red"), then you can just use the ampersand multiple times:
.f-box-cell {
flex : 1 1 auto;
// colors
& &-red { background-color: red; }
& &-green { background-color: green; }
...
// sizes
& &-b25 { flex-basis: 25%; }
& &-b50 { flex-basis: 50%; }
...
// other options
...
}
If you only want to use the most specific class, which should inherit from the base one, you can use the #extend rule:
.f-box-cell {
flex : 1 1 auto;
// colors
&-red { #extend .f-box-cell; background-color: red; }
&-green { #extend .f-box-cell; background-color: green; }
...
// sizes
&-b25 { #extend .f-box-cell; flex-basis: 25%; }
&-b50 { #extend .f-box-cell; flex-basis: 50%; }
...
// other options
...
}
Which compiles to:
.f-box-cell,
.f-box-cell-b50,
.f-box-cell-b25,
.f-box-cell-green,
.f-box-cell-red {
flex: 1 1 auto;
}
.f-box-cell-red {
background-color: red;
}
.f-box-cell-green {
background-color: green;
}
.f-box-cell-b25 {
flex-basis: 25%;
}
.f-box-cell-b50 {
flex-basis: 50%;
}

How to change the color of mat-radio-outer-circle (angular material)

I would like to change the radio button color in angular material the default value is 'accent' and I would like to set this value #ff9800, I found this solution in SO, Angular 6 Material - Hues and How to change the color of mat radio button , but the .mat-radio-outer-circle its value remained in accent ,my question is how can i change its value (#ff9800) when the radio button is checked.Here is my css and html code.
/* border-color:rgb(66, 134, 244); */
::ng-deep.mat-radio-button.mat-accent.mat-radio-checked .mat-radio-outer-circle {
color:#ff9800;
border-color: #ff9800;
}
::ng-deep.mat-radio-button.mat-accent .mat-radio-inner-circle{
color:#ff9800;
background-color:#ff9800;
}
::ng-deep.mat-radio-button.mat-accent .mat-radio-ripple .mat-ripple-element {
background-color:#ff9800;
}
<mat-radio-group aria-label="Select an option" class="actions" [(ngModel)]="action"
[ngModelOptions]="{standalone: true}">
<mat-radio-button value="Compile" checked ="true" color="accent">Compile</mat-radio-button>
<mat-radio-button value="Check Syntax" color="accent">Check Syntax</mat-radio-button>
</mat-radio-group>
You should use the !important rule after the value of the border-color and background-color like the following code:
::ng-deep .mat-radio-button.mat-accent .mat-radio-ripple .mat-ripple-element {
background-color: #ff9800 !important;
}
//before checked:
::ng-deep .mat-radio-button.mat-accent .mat-radio-outer-circle {
border-color: green !important;
}
::ng-deep .mat-radio-button.mat-accent .mat-radio-inner-circle {
background-color: green !important;
}
//after checked:
::ng-deep .mat-radio-button.mat-accent.mat-radio-checked
.mat-radio-outer-circle {
border-color: #ff9800 !important;
}
::ng-deep .mat-radio-button.mat-accent.mat-radio-checked
.mat-radio-inner-circle {
background-color: #ff9800 !important;
}

SASS nesting multiple classes in one element

Let's say I have this HTML code:
<div class="parent">
<div class="parent__son parent__son--red"></div>
<div class="parent__son parent__son--yellow"></div>
<div class="parent__son parent__son--red parent__son--yellow" ></div>
</div>
Parent has 3 sons - first with 'red', second with 'yellow', third with both.
Now I want to do this nesting SCSS:
.parent {
width: 100%;
&__son {
width: 20%;
&--red {
background: red;
}
&--yellow {
background: yellow;
}
}
}
Now I want that son that has both red and yellow will has background orange.
How can I write this in SASS?
A little bit of Sass Ampersand magic will get you there.
The interpolation brackets #{ } are needed as two touching ampersands
are invalid Sass
.parent {
width: 100%;
&__son {
width: 20%;
&--red {
background: red;
}
&--yellow {
background: yellow;
}
&--yellow#{&}--red {
background: orange;
}
}
}
[Live Example][https://codepen.io/anon/pen/LMazWK]

Resources