Ketan Akbari
Ketan Akbari

Reputation: 11267

How to remove button style or overwrite dynamically added classes to button in material2

I have Open button

<button mat-raised-button (click)="openModal()
"type="button" color="primary">Open Modal </button>

On button button click, popup modal open and after close modal. cdk-focused and cdk-program-focused classes added and to this button and get some ripple effect on that.

I want to get rid of that style, want remove or overwrite those classes those added to button after close modal*

enter image description here

Upvotes: 4

Views: 5229

Answers (4)

Gouranga Tarafder
Gouranga Tarafder

Reputation: 354

I faced same problem with Angular 11 and inside mat-drawer for the first link it was adding this effect. This span was being added inside first link whenever I open the mat-drawer

<span class="mat-button-focus-overlay"></span>

So I had to do this in my component's scss

::ng-deep .mat-button-focus-overlay {
    background-color: transparent !important;
}

This may be similar to above answers, hope this will help someone working with mat-drawer.

Upvotes: 0

Somesh T
Somesh T

Reputation: 39

Even I went through lots of trial and error method,but finally got fixed with it by adding following feature while creating model.

in the diolog creation add this

restoreFocus:false

*Whether the dialog should restore focus to the *previously-focused element, after it's closed.

Upvotes: 0

Vega
Vega

Reputation: 28708

I suggest two solutions:

1. First, set an additional class on that button in order to avoid that all the mat-raised-buttons be affected by the style change, it's not your aim, I suppose.

HTML:

<button mat-raised-button class="myButton" (click)="openDialog();">Pick one</button>

CSS:

.myButton:focus{
    box-shadow: 0 0 rgba(255, 255, 255, 1) !important;
    transition:none !important;
    background-color: rgba(255, 255, 255, 1) !important;
    -webkit-tap-highlight-color:rgba(255, 255, 255, 1) !important;
}

Then you can set the style of mat-raised-button either by

a) setting it in component's stylesheet with ::ng-deep:

::ng-deep .myButton>.mat-button-focus-overlay {
    background-color:transparent !important;
}

::ng-deep .mat-app-background{
  background: white !important
}

Demo

b) setting with ViewEncapsulation.none:

Class:

import { ViewEncapsulation } from '@angular/core';
...
@Component({
...
    encapsulation:ViewEncapsulation.None
})

CSS:

.myButton>.mat-button-focus-overlay {
    background-color:transparent !important;
}

.myButton>.mat-app-background{
  background: white !important
}

Demo

c) setting it in style.css:

.myButton>.mat-button-focus-overlay {
    background-color:transparent !important;
}

.myButton>.mat-app-background{
  background: white !important
}

Demo

  1. The button gets the focus when you click on it. Turn away the focus from that button by setting the focus on an other element:

... <input matInput #input [(ngModel)]="name" placeholder="What's your name?"> ... <button mat-raised-button class="myButton" (click)=" input.focus();openDialog() ">Pick one </button>

Demo

Upvotes: 5

Ketan Akbari
Ketan Akbari

Reputation: 11267

.mat-button-focus-overlay {
 background-color:transparent !important;
}

.mat-app-background{
  background: transparent !important
}

Upvotes: 3

Related Questions