Skip to content

Commit d3b517a

Browse files
dfreedmcopybara-github
authored andcommitted
fix(button)!: remove icon property from Button, require slotted icons
BREAKING CHANGE: Remove icon property from Button, require slotted icons PiperOrigin-RevId: 500770988
1 parent ea33cb8 commit d3b517a

File tree

2 files changed

+10
-42
lines changed

2 files changed

+10
-42
lines changed

button/lib/_icon.scss

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
.md3-button__icon-slot-container {
1414
display: inline-flex;
1515

16-
::slotted([slot='icon']),
17-
.md3-button__icon {
16+
::slotted([slot='icon']) {
1817
display: inline-flex;
1918
position: relative;
2019
writing-mode: horizontal-tb;
@@ -41,17 +40,11 @@
4140
}
4241
}
4342

44-
.md3-button__icon--leading {
45-
::slotted([slot='icon']),
46-
.md3-button__icon {
47-
margin-inline-end: 8px;
48-
}
43+
.md3-button__icon--leading ::slotted([slot='icon']) {
44+
margin-inline-end: 8px;
4945
}
5046

51-
.md3-button__icon--trailing {
52-
::slotted([slot='icon']),
53-
.md3-button__icon {
54-
margin-inline-start: 8px;
55-
}
47+
.md3-button__icon--trailing ::slotted([slot='icon']) {
48+
margin-inline-start: 8px;
5649
}
5750
}

button/lib/button.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
// This is required for @ariaProperty
1010
// tslint:disable:no-new-decorators
1111

12-
import '../../icon/icon.js';
1312
import '../../focus/focus-ring.js';
1413
import '../../ripple/ripple.js';
1514

16-
import {html, LitElement, TemplateResult} from 'lit';
15+
import {html, LitElement, nothing, TemplateResult} from 'lit';
1716
import {property, query, queryAssignedElements, queryAsync, state} from 'lit/decorators.js';
1817
import {ClassInfo, classMap} from 'lit/directives/class-map.js';
19-
import {ifDefined} from 'lit/directives/if-defined.js';
2018
import {when} from 'lit/directives/when.js';
21-
import {html as staticHtml, literal} from 'lit/static-html.js';
2219

2320
import {dispatchActivationClick, isActivationClick} from '../../controller/events.js';
2421
import {ariaProperty} from '../../decorators/aria-property.js';
@@ -34,8 +31,6 @@ export abstract class Button extends LitElement implements ButtonState {
3431
static override shadowRootOptions:
3532
ShadowRootInit = {mode: 'open', delegatesFocus: true};
3633

37-
protected readonly iconTag = literal`md-icon`;
38-
3934
@property({type: String, attribute: 'data-aria-has-popup', noAccessor: true})
4035
@ariaProperty
4136
override ariaHasPopup!: ARIAHasPopup;
@@ -57,13 +52,6 @@ export abstract class Button extends LitElement implements ButtonState {
5752
*/
5853
@property({type: Boolean, attribute: 'trailingicon'}) trailingIcon = false;
5954

60-
/**
61-
* The label of the icon to render.
62-
*
63-
* See md-icon's documentation for usage.
64-
*/
65-
@property({type: String}) icon = '';
66-
6755
/**
6856
* The button's visible label.
6957
*/
@@ -90,7 +78,7 @@ export abstract class Button extends LitElement implements ButtonState {
9078
@state() protected showRipple = false;
9179

9280
@queryAssignedElements({slot: 'icon', flatten: true})
93-
protected iconElement!: HTMLElement[]|null;
81+
protected assignedIcons!: HTMLElement[];
9482

9583
constructor() {
9684
super();
@@ -124,8 +112,8 @@ export abstract class Button extends LitElement implements ButtonState {
124112
<button
125113
class="md3-button ${classMap(this.getRenderClasses())}"
126114
?disabled="${this.disabled}"
127-
aria-label="${ifDefined(this.ariaLabel || undefined)}"
128-
aria-haspopup="${ifDefined(this.ariaHasPopup || undefined)}"
115+
aria-label="${this.ariaLabel || nothing}"
116+
aria-haspopup="${this.ariaHasPopup || nothing}"
129117
@pointerdown="${this.handlePointerDown}"
130118
@focus="${this.handleFocus}"
131119
@blur="${this.handleBlur}"
@@ -196,23 +184,10 @@ export abstract class Button extends LitElement implements ButtonState {
196184
return html`<span class="md3-button__icon-slot-container ${
197185
classMap(this.getIconContainerClasses())}">
198186
<slot name="icon" @slotchange="${this.handleSlotChange}">
199-
${this.icon ? this.renderFontIcon() : ''}
200187
</slot>
201188
</span>`;
202189
}
203190

204-
protected renderFontIcon(): TemplateResult {
205-
return staticHtml`
206-
<${this.iconTag} class="md3-button__icon">
207-
${this.icon}
208-
</${this.iconTag}>`;
209-
}
210-
211-
override update(changedProperties: Map<string, string>) {
212-
super.update(changedProperties);
213-
this.hasIcon = !!this.iconElement && this.iconElement.length > 0;
214-
}
215-
216191
protected handlePointerDown(e: PointerEvent) {
217192
pointerPress();
218193
this.showFocusRing = shouldShowStrongFocus();
@@ -233,6 +208,6 @@ export abstract class Button extends LitElement implements ButtonState {
233208
}
234209

235210
protected handleSlotChange() {
236-
this.requestUpdate();
211+
this.hasIcon = this.assignedIcons.length > 0;
237212
}
238213
}

0 commit comments

Comments
 (0)