UNPKG

@material/mwc-button

Version:
180 lines 5.75 kB
import { __decorate } from "tslib"; /** * @license * Copyright 2019 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import '@material/mwc-icon/mwc-icon'; import '@material/mwc-ripple/mwc-ripple'; import { RippleHandlers } from '@material/mwc-ripple/ripple-handlers'; import { eventOptions, html, LitElement, property, query, queryAsync, state } from 'lit-element'; import { classMap } from 'lit-html/directives/class-map'; /** @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 classMap */ getRenderClasses() { return classMap({ '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 ${this.getRenderClasses()}" ?disabled="${this.disabled}" aria-label="${this.label || this.icon}" @focus="${this.handleRippleFocus}" @blur="${this.handleRippleBlur}" @mousedown="${this.handleRippleActivate}" @mouseenter="${this.handleRippleMouseEnter}" @mouseleave="${this.handleRippleMouseLeave}" @touchstart="${this.handleRippleActivate}" @touchend="${this.handleRippleDeactivate}" @touchcancel="${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([ 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