Skip to content

Commit 95fc08b

Browse files
asynclizcopybara-github
authored andcommitted
fix(button): clean up form label activation logic
These helpers and methods are no longer needed for Firefox or Safari, and button does not add click listeners to inner elements that require dispatching the activation click on the inner button. Tested locally with Chrome, Firefox, and Safari to ensure `<label for="md-button">` works and focus/blur methods work as expected. PiperOrigin-RevId: 876297295
1 parent 516cbc0 commit 95fc08b

File tree

1 file changed

+13
-35
lines changed

1 file changed

+13
-35
lines changed

button/internal/button.ts

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import '../../focus/md-focus-ring.js';
88
import '../../ripple/ripple.js';
99

1010
import {html, isServer, LitElement, nothing} from 'lit';
11-
import {property, query, queryAssignedElements} from 'lit/decorators.js';
11+
import {property, queryAssignedElements} from 'lit/decorators.js';
1212

1313
import {ARIAMixinStrict} from '../../internal/aria/aria.js';
1414
import {mixinDelegatesAria} from '../../internal/aria/delegate.js';
@@ -17,10 +17,6 @@ import {
1717
setupFormSubmitter,
1818
type FormSubmitterType,
1919
} from '../../internal/controller/form-submitter.js';
20-
import {
21-
dispatchActivationClick,
22-
isActivationClick,
23-
} from '../../internal/events/form-label-activation.js';
2420
import {
2521
internals,
2622
mixinElementInternals,
@@ -121,24 +117,23 @@ export abstract class Button extends buttonBaseClass implements FormSubmitter {
121117
return this[internals].form;
122118
}
123119

124-
@query('.button') private readonly buttonElement!: HTMLElement | null;
125-
126120
@queryAssignedElements({slot: 'icon', flatten: true})
127121
private readonly assignedIcons!: HTMLElement[];
128122

129123
constructor() {
130124
super();
131-
if (!isServer) {
132-
this.addEventListener('click', this.handleClick.bind(this));
133-
}
134-
}
135-
136-
override focus() {
137-
this.buttonElement?.focus();
138-
}
139-
140-
override blur() {
141-
this.buttonElement?.blur();
125+
if (isServer) return;
126+
this.addEventListener('click', (event) => {
127+
// If the button is soft-disabled or a disabled link, we need to
128+
// explicitly prevent the click from propagating to other event listeners
129+
// as well as prevent the default action. This is because the underlying
130+
// `<button>` or `<a>` element is not actually `:disabled`.
131+
if (this.softDisabled || (this.disabled && this.href)) {
132+
event.stopImmediatePropagation();
133+
event.preventDefault();
134+
return;
135+
}
136+
});
142137
}
143138

144139
protected override render() {
@@ -211,23 +206,6 @@ export abstract class Button extends buttonBaseClass implements FormSubmitter {
211206
`;
212207
}
213208

214-
private handleClick(event: MouseEvent) {
215-
// If the button is soft-disabled or a disabled link, we need to explicitly
216-
// prevent the click from propagating to other event listeners as well as
217-
// prevent the default action.
218-
if (this.softDisabled || (this.disabled && this.href)) {
219-
event.stopImmediatePropagation();
220-
event.preventDefault();
221-
return;
222-
}
223-
224-
if (!isActivationClick(event) || !this.buttonElement) {
225-
return;
226-
}
227-
this.focus();
228-
dispatchActivationClick(this.buttonElement);
229-
}
230-
231209
private handleSlotChange() {
232210
this.hasIcon = this.assignedIcons.length > 0;
233211
}

0 commit comments

Comments
 (0)