Skip to content

Commit

Permalink
Improves how button handles activation clicks. Previously click() w…
Browse files Browse the repository at this point in the history
…as overridden and this did not support directly dispatched click events. Now click events are explicitly handled if the event is targeted at the element itself. Since `click()` dispatches a click event, it no longer needs to be overridden.

PiperOrigin-RevId: 495694515
  • Loading branch information
material-web-copybara authored and copybara-github committed Dec 15, 2022
1 parent 4e3054b commit cb55c90
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions button/lib/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {ifDefined} from 'lit/directives/if-defined.js';
import {when} from 'lit/directives/when.js';
import {html as staticHtml, literal} from 'lit/static-html.js';

import {dispatchActivationClick, isActivationClick} from '../../controller/events.js';
import {ariaProperty} from '../../decorators/aria-property.js';
import {pointerPress, shouldShowStrongFocus} from '../../focus/strong-focus.js';
import {ripple} from '../../ripple/directive.js';
Expand Down Expand Up @@ -91,6 +92,27 @@ export abstract class Button extends LitElement implements ButtonState {
@queryAssignedElements({slot: 'icon', flatten: true})
protected iconElement!: HTMLElement[]|null;

constructor() {
super();
this.addEventListener('click', this.handleActivationClick);
}

private readonly handleActivationClick = (event: MouseEvent) => {
if (!isActivationClick((event))) {
return;
}
this.focus();
dispatchActivationClick(this.buttonElement);
};

override focus() {
this.buttonElement.focus();
}

override blur() {
this.buttonElement.blur();
}

protected readonly getRipple = () => {
this.showRipple = true;
return this.ripple;
Expand Down Expand Up @@ -196,10 +218,6 @@ export abstract class Button extends LitElement implements ButtonState {
this.showFocusRing = shouldShowStrongFocus();
}

override click() {
this.buttonElement.click();
}

protected handleClick(e: MouseEvent) {
if (this.preventClickDefault) {
e.preventDefault();
Expand Down

0 comments on commit cb55c90

Please sign in to comment.