Skip to content

Commit

Permalink
fix(button)!: remove icon property from Button, require slotted icons
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Remove icon property from Button, require slotted icons

PiperOrigin-RevId: 500770988
  • Loading branch information
dfreedm authored and Copybara-Service committed Jan 9, 2023
1 parent ea33cb8 commit d3b517a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 42 deletions.
17 changes: 5 additions & 12 deletions button/lib/_icon.scss
Expand Up @@ -13,8 +13,7 @@
.md3-button__icon-slot-container {
display: inline-flex;

::slotted([slot='icon']),
.md3-button__icon {
::slotted([slot='icon']) {
display: inline-flex;
position: relative;
writing-mode: horizontal-tb;
Expand All @@ -41,17 +40,11 @@
}
}

.md3-button__icon--leading {
::slotted([slot='icon']),
.md3-button__icon {
margin-inline-end: 8px;
}
.md3-button__icon--leading ::slotted([slot='icon']) {
margin-inline-end: 8px;
}

.md3-button__icon--trailing {
::slotted([slot='icon']),
.md3-button__icon {
margin-inline-start: 8px;
}
.md3-button__icon--trailing ::slotted([slot='icon']) {
margin-inline-start: 8px;
}
}
35 changes: 5 additions & 30 deletions button/lib/button.ts
Expand Up @@ -9,16 +9,13 @@
// This is required for @ariaProperty
// tslint:disable:no-new-decorators

import '../../icon/icon.js';
import '../../focus/focus-ring.js';
import '../../ripple/ripple.js';

import {html, LitElement, TemplateResult} from 'lit';
import {html, LitElement, nothing, TemplateResult} from 'lit';
import {property, query, queryAssignedElements, queryAsync, state} from 'lit/decorators.js';
import {ClassInfo, classMap} from 'lit/directives/class-map.js';
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';
Expand All @@ -34,8 +31,6 @@ export abstract class Button extends LitElement implements ButtonState {
static override shadowRootOptions:
ShadowRootInit = {mode: 'open', delegatesFocus: true};

protected readonly iconTag = literal`md-icon`;

@property({type: String, attribute: 'data-aria-has-popup', noAccessor: true})
@ariaProperty
override ariaHasPopup!: ARIAHasPopup;
Expand All @@ -57,13 +52,6 @@ export abstract class Button extends LitElement implements ButtonState {
*/
@property({type: Boolean, attribute: 'trailingicon'}) trailingIcon = false;

/**
* The label of the icon to render.
*
* See md-icon's documentation for usage.
*/
@property({type: String}) icon = '';

/**
* The button's visible label.
*/
Expand All @@ -90,7 +78,7 @@ export abstract class Button extends LitElement implements ButtonState {
@state() protected showRipple = false;

@queryAssignedElements({slot: 'icon', flatten: true})
protected iconElement!: HTMLElement[]|null;
protected assignedIcons!: HTMLElement[];

constructor() {
super();
Expand Down Expand Up @@ -124,8 +112,8 @@ export abstract class Button extends LitElement implements ButtonState {
<button
class="md3-button ${classMap(this.getRenderClasses())}"
?disabled="${this.disabled}"
aria-label="${ifDefined(this.ariaLabel || undefined)}"
aria-haspopup="${ifDefined(this.ariaHasPopup || undefined)}"
aria-label="${this.ariaLabel || nothing}"
aria-haspopup="${this.ariaHasPopup || nothing}"
@pointerdown="${this.handlePointerDown}"
@focus="${this.handleFocus}"
@blur="${this.handleBlur}"
Expand Down Expand Up @@ -196,23 +184,10 @@ export abstract class Button extends LitElement implements ButtonState {
return html`<span class="md3-button__icon-slot-container ${
classMap(this.getIconContainerClasses())}">
<slot name="icon" @slotchange="${this.handleSlotChange}">
${this.icon ? this.renderFontIcon() : ''}
</slot>
</span>`;
}

protected renderFontIcon(): TemplateResult {
return staticHtml`
<${this.iconTag} class="md3-button__icon">
${this.icon}
</${this.iconTag}>`;
}

override update(changedProperties: Map<string, string>) {
super.update(changedProperties);
this.hasIcon = !!this.iconElement && this.iconElement.length > 0;
}

protected handlePointerDown(e: PointerEvent) {
pointerPress();
this.showFocusRing = shouldShowStrongFocus();
Expand All @@ -233,6 +208,6 @@ export abstract class Button extends LitElement implements ButtonState {
}

protected handleSlotChange() {
this.requestUpdate();
this.hasIcon = this.assignedIcons.length > 0;
}
}

0 comments on commit d3b517a

Please sign in to comment.