I\'m creating an Angular component that wraps a native element with some additional features. Buttons do not fire a click event if they\'re disab
You could do the following:
click event of the component, and emit this event when the button is clickedpointer-events: none on the component hostpointer-events: auto on the buttonevent.stopPropagation() on the button click event handlerIf you need to process the click event of other elements inside of your component, set the style attribute pointer-events: auto on them, and call event.stopPropagation() in their click event handler.
You can test the code in this stackblitz.
import { Component, HostListener, Input, Output, ElementRef, EventEmitter } from '@angular/core';
@Component({
selector: 'my-button',
host: {
"[style.pointer-events]": "'none'"
},
template: `
Span element`,
styles: [`button, span { pointer-events: auto; }`]
})
export class MyCustomComponent {
@Input() public isDisabled: boolean = false;
@Output() public click: EventEmitter = new EventEmitter();
onButtonClick(event: MouseEvent) {
event.stopPropagation();
this.click.emit(event);
}
onSpanClick(event: MouseEvent) {
event.stopPropagation();
}
}
UPDATE:
Since the button can contain HTML child elements (span, img, etc.), you can add the following CSS style to prevent the click from being propagated to the parent:
:host ::ng-deep button * {
pointer-events: none;
}
Thanks to @ErikWitkowski for his comment on this special case. See this stackblitz for a demo.