@material/mwc-button
Version:
Material Design button web component
190 lines • 6.2 kB
JavaScript
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { __decorate } from "tslib";
// Style preference for leading underscores.
// tslint:disable:strip-private-property-underscore
import '@material/mwc-icon/mwc-icon.js';
import '@material/mwc-ripple/mwc-ripple.js';
import { ariaProperty } from '@material/mwc-base/aria-property.js';
import { RippleHandlers } from '@material/mwc-ripple/ripple-handlers.js';
import { html, LitElement } from 'lit';
import { eventOptions, property, query, queryAsync, state } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';
/** @soyCompatible */
export class ButtonBase extends LitElement {
constructor() {
super(...arguments);
this.raised = false;
this.unelevated = false;
this.outlined = false;
this.dense = false;
this.disabled = false;
this.trailingIcon = false;
this.fullwidth = false;
this.icon = '';
this.label = '';
this.expandContent = false;
this.shouldRenderRipple = false;
this.rippleHandlers = new RippleHandlers(() => {
this.shouldRenderRipple = true;
return this.ripple;
});
}
/** @soyTemplate */
renderOverlay() {
return html ``;
}
/** @soyTemplate */
renderRipple() {
const filled = this.raised || this.unelevated;
return this.shouldRenderRipple ?
html `<mwc-ripple class="ripple" .primary="${!filled}" .disabled="${this.disabled}"></mwc-ripple>` :
'';
}
focus() {
const buttonElement = this.buttonElement;
if (buttonElement) {
this.rippleHandlers.startFocus();
buttonElement.focus();
}
}
blur() {
const buttonElement = this.buttonElement;
if (buttonElement) {
this.rippleHandlers.endFocus();
buttonElement.blur();
}
}
/** @soyTemplate */
getRenderClasses() {
return {
'mdc-button--raised': this.raised,
'mdc-button--unelevated': this.unelevated,
'mdc-button--outlined': this.outlined,
'mdc-button--dense': this.dense,
};
}
/**
* @soyTemplate
* @soyAttributes buttonAttributes: #button
* @soyClasses buttonClasses: #button
*/
render() {
return html `
<button
id="button"
class="mdc-button ${classMap(this.getRenderClasses())}"
?disabled="${this.disabled}"
aria-label="${this.label || this.icon}"
aria-haspopup="${ifDefined(this.ariaHasPopup)}"
="${this.handleRippleFocus}"
="${this.handleRippleBlur}"
="${this.handleRippleActivate}"
="${this.handleRippleMouseEnter}"
="${this.handleRippleMouseLeave}"
="${this.handleRippleActivate}"
="${this.handleRippleDeactivate}"
="${this.handleRippleDeactivate}">
${this.renderOverlay()}
${this.renderRipple()}
<span class="leading-icon">
<slot name="icon">
${this.icon && !this.trailingIcon ? this.renderIcon() : ''}
</slot>
</span>
<span class="mdc-button__label">${this.label}</span>
<span class="slot-container ${classMap({
flex: this.expandContent
})}">
<slot></slot>
</span>
<span class="trailing-icon">
<slot name="trailingIcon">
${this.icon && this.trailingIcon ? this.renderIcon() : ''}
</slot>
</span>
</button>`;
}
/** @soyTemplate */
renderIcon() {
return html `
<mwc-icon class="mdc-button__icon">
${this.icon}
</mwc-icon>`;
}
handleRippleActivate(evt) {
const onUp = () => {
window.removeEventListener('mouseup', onUp);
this.handleRippleDeactivate();
};
window.addEventListener('mouseup', onUp);
this.rippleHandlers.startPress(evt);
}
handleRippleDeactivate() {
this.rippleHandlers.endPress();
}
handleRippleMouseEnter() {
this.rippleHandlers.startHover();
}
handleRippleMouseLeave() {
this.rippleHandlers.endHover();
}
handleRippleFocus() {
this.rippleHandlers.startFocus();
}
handleRippleBlur() {
this.rippleHandlers.endFocus();
}
}
ButtonBase.shadowRootOptions = { mode: 'open', delegatesFocus: true };
__decorate([
ariaProperty,
property({ type: String, attribute: 'aria-haspopup' })
], ButtonBase.prototype, "ariaHasPopup", void 0);
__decorate([
property({ type: Boolean, reflect: true })
], ButtonBase.prototype, "raised", void 0);
__decorate([
property({ type: Boolean, reflect: true })
], ButtonBase.prototype, "unelevated", void 0);
__decorate([
property({ type: Boolean, reflect: true })
], ButtonBase.prototype, "outlined", void 0);
__decorate([
property({ type: Boolean })
], ButtonBase.prototype, "dense", void 0);
__decorate([
property({ type: Boolean, reflect: true })
], ButtonBase.prototype, "disabled", void 0);
__decorate([
property({ type: Boolean, attribute: 'trailingicon' })
], ButtonBase.prototype, "trailingIcon", void 0);
__decorate([
property({ type: Boolean, reflect: true })
], ButtonBase.prototype, "fullwidth", void 0);
__decorate([
property({ type: String })
], ButtonBase.prototype, "icon", void 0);
__decorate([
property({ type: String })
], ButtonBase.prototype, "label", void 0);
__decorate([
property({ type: Boolean })
], ButtonBase.prototype, "expandContent", void 0);
__decorate([
query('#button')
], ButtonBase.prototype, "buttonElement", void 0);
__decorate([
queryAsync('mwc-ripple')
], ButtonBase.prototype, "ripple", void 0);
__decorate([
state()
], ButtonBase.prototype, "shouldRenderRipple", void 0);
__decorate([
eventOptions({ passive: true })
], ButtonBase.prototype, "handleRippleActivate", null);
//# sourceMappingURL=mwc-button-base.js.map