Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 13 additions & 35 deletions button/internal/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '../../focus/md-focus-ring.js';
import '../../ripple/ripple.js';

import {html, isServer, LitElement, nothing} from 'lit';
import {property, query, queryAssignedElements} from 'lit/decorators.js';
import {property, queryAssignedElements} from 'lit/decorators.js';

import {ARIAMixinStrict} from '../../internal/aria/aria.js';
import {mixinDelegatesAria} from '../../internal/aria/delegate.js';
Expand All @@ -17,10 +17,6 @@ import {
setupFormSubmitter,
type FormSubmitterType,
} from '../../internal/controller/form-submitter.js';
import {
dispatchActivationClick,
isActivationClick,
} from '../../internal/events/form-label-activation.js';
import {
internals,
mixinElementInternals,
Expand Down Expand Up @@ -121,24 +117,23 @@ export abstract class Button extends buttonBaseClass implements FormSubmitter {
return this[internals].form;
}

@query('.button') private readonly buttonElement!: HTMLElement | null;

@queryAssignedElements({slot: 'icon', flatten: true})
private readonly assignedIcons!: HTMLElement[];

constructor() {
super();
if (!isServer) {
this.addEventListener('click', this.handleClick.bind(this));
}
}

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

override blur() {
this.buttonElement?.blur();
if (isServer) return;
this.addEventListener('click', (event) => {
// If the button is soft-disabled or a disabled link, we need to
// explicitly prevent the click from propagating to other event listeners
// as well as prevent the default action. This is because the underlying
// `<button>` or `<a>` element is not actually `:disabled`.
if (this.softDisabled || (this.disabled && this.href)) {
event.stopImmediatePropagation();
event.preventDefault();
return;
}
});
}

protected override render() {
Expand Down Expand Up @@ -211,23 +206,6 @@ export abstract class Button extends buttonBaseClass implements FormSubmitter {
`;
}

private handleClick(event: MouseEvent) {
// If the button is soft-disabled or a disabled link, we need to explicitly
// prevent the click from propagating to other event listeners as well as
// prevent the default action.
if (this.softDisabled || (this.disabled && this.href)) {
event.stopImmediatePropagation();
event.preventDefault();
return;
}

if (!isActivationClick(event) || !this.buttonElement) {
return;
}
this.focus();
dispatchActivationClick(this.buttonElement);
}

private handleSlotChange() {
this.hasIcon = this.assignedIcons.length > 0;
}
Expand Down
Loading