Angular Material - How To Add A Tooltip To A Disabled Button


Answer :

This doesn't work because it is triggered by mouseenter event which doesn't get fired by most browsers for disabled elements. A workaround is to add matTooltip to a parent element:

<div matTooltip="You cannot delete that" [matTooltipDisabled]="!isButtonDisabled()">     <button mat-raised-button [disabled]="isButtonDisabled()">         <mat-icon>delete</mat-icon>     </button> </div> 

The example above assumes that there is a method for determining if the button should be enabled or not. By using matTooltipDisabled the tooltip will be shown only if the button is disabled.

References:

  • https://github.com/angular/material2/issues/5040
  • https://github.com/angular/material2/issues/7953

I had a similar issue while displaying tooltip on a disabled icon button. The given solution was not practical for me, because adding an additional div on top of the button, messed up the layout of the button relative to the other buttons in the tool bar.

A simpler solution for me was to add the tooltip inside the icon inside the button. Something like this:

<button mat-raised-button [disabled]="true">     <mat-icon matTooltip="You cannot delete that">delete</mat-icon> </button> 

Since the icon is not disabled, it works.


Yes, the simplest solution is like above. But for my case I needed more flexibility.

   <button  [disabled]="form.invalid">       <span [matTooltip]="form.invalid ? 'some text' : ''">button text</span>     </button> 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?