Angular and Angular Material 15
You can add the matTooltip
to the child element, most logically in case of a material button the span
holding the button text or the mat-icon
with the material icon.
When using Material buttons the disabled button has by default the style pointer-events: none
applied; see global material style for the buttons:
Icon buttons:
.mdc-icon-button:disabled,
.mat-mdc-icon-button[disabled] {
pointer-events: none;
}
Normal buttons:
.mdc-button[disabled],
.mat-mdc-button[disabled] {
pointer-events: none;
}
You will need to cancel this out for the child elements if you want the tooltip to show on hovering the button. This can be easily solved like this:
<button #myIconButton mat-icon-button [disabled]="!isAllowedDelete">
<mat-icon [style.pointer-events]="'all'"
matTooltip="You are not allowed to delete this item"
[matTooltipDisabled]="!myIconButton.disabled">delete</mat-icon>
</button>
<button #myButton mat-button [disabled]="!isAllowedDelete">
<span [style.pointer-events]="'all'"
matTooltip="You are not allowed to delete this item"
[matTooltipDisabled]="!myButton.disabled">delete</span>
</button>
Or alternatively if you want it in your stylesheet:
button[disabled] span.mat-mdc-tooltip-trigger
button[disabled] mat-icon.mat-mdc-tooltip-trigger {
/* Allows for tooltips to be triggered on disabled buttons */
pointer-events: all;
}
In my opinion this is better then wrapping the button in an element, you can now disable/enable the tooltip directly using the button "disabled" state (myButton.disabled
) and you don't need to add any additional wrappers or elements to the DOM.
Checkout a working example in this Stackblitz.