Angular Material - How to add a tooltip to a disabled button
AngularAngular Material2Angular Problem Overview
I've noticed that the directive matTooltip
doesn't work on a disabled button. How can I achieve it?
Example:
<button mat-raised-button [disabled]="true" matTooltip="You cannot delete that">
<mat-icon>delete</mat-icon>
</button>
For an enabled button it works perfectly:
<button mat-raised-button [disabled]="false" matTooltip="You cannot delete that">
<mat-icon>delete</mat-icon>
</button>
Angular Solutions
Solution 1 - Angular
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:
Solution 2 - Angular
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.
Solution 3 - Angular
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>
Solution 4 - Angular
Adding tooltip inside mat-icon in a button as suggested by others will only work when you hover the icon not the button. Instead of that you can just wrap your button around another div without any css classes, just tooltip.
Additionally you can also add matTooltipDisabled property to make sure your tooltip is never disabled.
<div matTooltip="You cannot delete that" [matTooltipDisabled]="false">
<button mat-raised-button [disabled]="true">
<mat-icon>delete</mat-icon>
</button>
</div>
Solution 5 - Angular
You can use title attribute it will display in necessary cases
<button mat-raised-button [disabled]="true" title = "Some text">
<mat-icon>delete</mat-icon>
</button>
you can do property binding with ternary operators
Solution 6 - Angular
I find a solution!
Put the tooltip into the button content like this:
<button type="submit" [disabled]="disableEdit()" class="btn btn-primary btn-sm"
[routerLink]="['/entity', entity.id, 'edit']">
<div matTooltip="{{ 'entity.placeholders.cantEdit' | translate }}"
[matTooltipDisabled]="disableEdit()">
<fa-icon [icon]="'pencil-alt'"></fa-icon>
<span class="d-none d-md-inline">{{ 'entity.action.edit' | translate }}</span>
</div>
</button>
Solution 7 - Angular
Try this:
<div [matTooltip]="isDisabled ? 'You cannot delete that' : ''">
<button mat-raised-button [disabled]="isDisabled">
<mat-icon>delete</mat-icon>
</button>
<div>
Solution 8 - Angular
I know this is ugly but you also do this way, Use multiple buttons with ngIf
<!-- No click action -->
<button *ngIf="disable" mat-raised-button matTooltip="You cannot delete that">
<mat-icon>delete</mat-icon>
</button>
<button *ngIf="!disable" mat-raised-button (click)="delete()">
<mat-icon>delete</mat-icon>
</button>
Solution 9 - Angular
Just add style 'pointer-events: all' to your button like this
<button mat-raised-button style="pointer-events: all" [disabled]="true" matTooltip="You cannot delete that">
<mat-icon>delete</mat-icon>
</button>