SHOW:
|
|
- or go back to the newest paste.
| 1 | - | import { __esDecorate, __runInitializers } from '../../node_modules/tslib/tslib.es6.js';
|
| 1 | + | |
| 2 | import '../../ripple/ripple.js'; | |
| 3 | import { isServer, nothing, html as html$1, LitElement } from 'lit';
| |
| 4 | import { property, query, queryAssignedElements } from 'lit/decorators.js';
| |
| 5 | import { classMap } from 'lit/directives/class-map.js';
| |
| 6 | import { literal, html } from 'lit/static-html.js';
| |
| 7 | import { requestUpdateOnAriaChange } from '../../internal/aria/delegate.js';
| |
| 8 | import { internals } from '../../internal/controller/element-internals.js';
| |
| 9 | import { isActivationClick, dispatchActivationClick } from '../../internal/controller/events.js';
| |
| 10 | import { setupFormSubmitter } from '../../internal/controller/form-submitter.js';
| |
| 11 | ||
| 12 | /** | |
| 13 | * @license | |
| 14 | * Copyright 2019 Google LLC | |
| 15 | * SPDX-License-Identifier: Apache-2.0 | |
| 16 | */ | |
| 17 | /** | |
| 18 | * A button component. | |
| 19 | */ | |
| 20 | let Button = (() => {
| |
| 21 | let _classSuper = LitElement; | |
| 22 | return class Button extends _classSuper {
| |
| 23 | - | let _instanceExtraInitializers = []; |
| 23 | + | |
| 24 | - | let _disabled_decorators; |
| 24 | + | |
| 25 | - | let _disabled_initializers = []; |
| 25 | + | |
| 26 | - | let _href_decorators; |
| 26 | + | |
| 27 | - | let _href_initializers = []; |
| 27 | + | |
| 28 | - | let _target_decorators; |
| 28 | + | |
| 29 | - | let _target_initializers = []; |
| 29 | + | |
| 30 | - | let _trailingIcon_decorators; |
| 30 | + | |
| 31 | - | let _trailingIcon_initializers = []; |
| 31 | + | #disabled = false |
| 32 | - | let _hasIcon_decorators; |
| 32 | + | |
| 33 | - | let _hasIcon_initializers = []; |
| 33 | + | |
| 34 | - | let _type_decorators; |
| 34 | + | |
| 35 | - | let _type_initializers = []; |
| 35 | + | get disabled() { return this.#disabled; }
|
| 36 | - | let _value_decorators; |
| 36 | + | set disabled(value) { const old = this.disabled; this.#disabled = value; this.requestUpdate('disabled', old, {type: Boolean, reflect: true});}
|
| 37 | - | let _value_initializers = []; |
| 37 | + | #href = ''; |
| 38 | - | let _buttonElement_decorators; |
| 38 | + | |
| 39 | - | let _buttonElement_initializers = []; |
| 39 | + | |
| 40 | - | let _assignedIcons_decorators; |
| 40 | + | |
| 41 | - | let _assignedIcons_initializers = []; |
| 41 | + | get href() { return this.#href; }
|
| 42 | set href(value) { const old = this.href; this.#href = value; this.requestUpdate('href', old, {});}
| |
| 43 | #target = ''; | |
| 44 | - | const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; |
| 44 | + | |
| 45 | - | __esDecorate(this, null, _disabled_decorators, { kind: "accessor", name: "disabled", static: false, private: false, access: { has: obj => "disabled" in obj, get: obj => obj.disabled, set: (obj, value) => { obj.disabled = value; } }, metadata: _metadata }, _disabled_initializers, _instanceExtraInitializers);
|
| 45 | + | |
| 46 | - | __esDecorate(this, null, _href_decorators, { kind: "accessor", name: "href", static: false, private: false, access: { has: obj => "href" in obj, get: obj => obj.href, set: (obj, value) => { obj.href = value; } }, metadata: _metadata }, _href_initializers, _instanceExtraInitializers);
|
| 46 | + | |
| 47 | - | __esDecorate(this, null, _target_decorators, { kind: "accessor", name: "target", static: false, private: false, access: { has: obj => "target" in obj, get: obj => obj.target, set: (obj, value) => { obj.target = value; } }, metadata: _metadata }, _target_initializers, _instanceExtraInitializers);
|
| 47 | + | |
| 48 | - | __esDecorate(this, null, _trailingIcon_decorators, { kind: "accessor", name: "trailingIcon", static: false, private: false, access: { has: obj => "trailingIcon" in obj, get: obj => obj.trailingIcon, set: (obj, value) => { obj.trailingIcon = value; } }, metadata: _metadata }, _trailingIcon_initializers, _instanceExtraInitializers);
|
| 48 | + | get target() { return this.#target; }
|
| 49 | - | __esDecorate(this, null, _hasIcon_decorators, { kind: "accessor", name: "hasIcon", static: false, private: false, access: { has: obj => "hasIcon" in obj, get: obj => obj.hasIcon, set: (obj, value) => { obj.hasIcon = value; } }, metadata: _metadata }, _hasIcon_initializers, _instanceExtraInitializers);
|
| 49 | + | set target(value) { const old = this.target; this.#target = value; this.requestUpdate('target', name, {});}
|
| 50 | - | __esDecorate(this, null, _type_decorators, { kind: "accessor", name: "type", static: false, private: false, access: { has: obj => "type" in obj, get: obj => obj.type, set: (obj, value) => { obj.type = value; } }, metadata: _metadata }, _type_initializers, _instanceExtraInitializers);
|
| 50 | + | #trailingIcon = false; |
| 51 | - | __esDecorate(this, null, _value_decorators, { kind: "accessor", name: "value", static: false, private: false, access: { has: obj => "value" in obj, get: obj => obj.value, set: (obj, value) => { obj.value = value; } }, metadata: _metadata }, _value_initializers, _instanceExtraInitializers);
|
| 51 | + | |
| 52 | - | __esDecorate(this, null, _buttonElement_decorators, { kind: "accessor", name: "buttonElement", static: false, private: false, access: { has: obj => "buttonElement" in obj, get: obj => obj.buttonElement, set: (obj, value) => { obj.buttonElement = value; } }, metadata: _metadata }, _buttonElement_initializers, _instanceExtraInitializers);
|
| 52 | + | |
| 53 | - | __esDecorate(this, null, _assignedIcons_decorators, { kind: "accessor", name: "assignedIcons", static: false, private: false, access: { has: obj => "assignedIcons" in obj, get: obj => obj.assignedIcons, set: (obj, value) => { obj.assignedIcons = value; } }, metadata: _metadata }, _assignedIcons_initializers, _instanceExtraInitializers);
|
| 53 | + | |
| 54 | - | if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
| 54 | + | |
| 55 | * _Note:_ Link buttons cannot have trailing icons. | |
| 56 | */ | |
| 57 | get trailingIcon() { return this.#trailingIcon; }
| |
| 58 | set trailingIcon(value) { const old = this.trailingIcon; this.#trailingIcon = value; this.requestUpdate('trailingIcon', old, {type: Boolean, attribute: 'trailing-icon'})};}
| |
| 59 | #hasIcon = false; | |
| 60 | /** | |
| 61 | * Whether to display the icon or not. | |
| 62 | */ | |
| 63 | get hasIcon() { return this.#hasIcon; }
| |
| 64 | - | #disabled_accessor_storage = (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _disabled_initializers, false)); |
| 64 | + | set hasIcon(value) { const old = this.hasIcon; this.#hasIcon = value; this.requestUpdate('hasIcon', old, {type: Boolean, attribute: 'has-icon'});}
|
| 65 | #type = 'submit'; | |
| 66 | get type() { return this.#type; }
| |
| 67 | set type(value) { const old = this.type; this.#type = value; this.requestUpdate('type', old, {});}
| |
| 68 | - | get disabled() { return this.#disabled_accessor_storage; }
|
| 68 | + | #value = ''; |
| 69 | - | set disabled(value) { this.#disabled_accessor_storage = value; }
|
| 69 | + | get value() { return this.#value; }
|
| 70 | - | #href_accessor_storage = __runInitializers(this, _href_initializers, ''); |
| 70 | + | set value(value) { const old = this.value; this.#value = value; this.requestUpdate('value', old, {});}
|
| 71 | get name() {
| |
| 72 | return this.getAttribute('name') ?? '';
| |
| 73 | } | |
| 74 | - | get href() { return this.#href_accessor_storage; }
|
| 74 | + | |
| 75 | - | set href(value) { this.#href_accessor_storage = value; }
|
| 75 | + | |
| 76 | - | #target_accessor_storage = __runInitializers(this, _target_initializers, ''); |
| 76 | + | |
| 77 | /** | |
| 78 | * The associated form element with which this element's value will submit. | |
| 79 | */ | |
| 80 | get form() {
| |
| 81 | - | get target() { return this.#target_accessor_storage; }
|
| 81 | + | |
| 82 | - | set target(value) { this.#target_accessor_storage = value; }
|
| 82 | + | |
| 83 | - | #trailingIcon_accessor_storage = __runInitializers(this, _trailingIcon_initializers, false); |
| 83 | + | get buttonElement() { return this.shadowRoot.querySelector('.button'); }
|
| 84 | get assignedIcons() { return this.shadowRoot.querySelector('slot[name=icon]').assignedElements({flatten: true}); }
| |
| 85 | constructor() {
| |
| 86 | super(); | |
| 87 | if (!isServer) {
| |
| 88 | this.addEventListener('click', this.handleActivationClick);
| |
| 89 | } | |
| 90 | - | get trailingIcon() { return this.#trailingIcon_accessor_storage; }
|
| 90 | + | |
| 91 | - | set trailingIcon(value) { this.#trailingIcon_accessor_storage = value; }
|
| 91 | + | |
| 92 | - | #hasIcon_accessor_storage = __runInitializers(this, _hasIcon_initializers, false); |
| 92 | + | |
| 93 | } | |
| 94 | blur() {
| |
| 95 | this.buttonElement?.blur(); | |
| 96 | - | get hasIcon() { return this.#hasIcon_accessor_storage; }
|
| 96 | + | |
| 97 | - | set hasIcon(value) { this.#hasIcon_accessor_storage = value; }
|
| 97 | + | |
| 98 | - | #type_accessor_storage = __runInitializers(this, _type_initializers, 'submit'); |
| 98 | + | |
| 99 | - | get type() { return this.#type_accessor_storage; }
|
| 99 | + | |
| 100 | - | set type(value) { this.#type_accessor_storage = value; }
|
| 100 | + | |
| 101 | - | #value_accessor_storage = __runInitializers(this, _value_initializers, ''); |
| 101 | + | |
| 102 | - | get value() { return this.#value_accessor_storage; }
|
| 102 | + | |
| 103 | - | set value(value) { this.#value_accessor_storage = value; }
|
| 103 | + | |
| 104 | <${button}
| |
| 105 | class="button ${classMap(this.getRenderClasses())}"
| |
| 106 | ?disabled=${isDisabled}
| |
| 107 | aria-label="${ariaLabel || nothing}"
| |
| 108 | aria-haspopup="${ariaHasPopup || nothing}"
| |
| 109 | aria-expanded="${ariaExpanded || nothing}"
| |
| 110 | href=${this.href || nothing}
| |
| 111 | target=${this.target || nothing}
| |
| 112 | >${this.renderContent()}</${button}>`;
| |
| 113 | } | |
| 114 | getRenderClasses() {
| |
| 115 | return {
| |
| 116 | - | #buttonElement_accessor_storage = __runInitializers(this, _buttonElement_initializers, void 0); |
| 116 | + | |
| 117 | - | get buttonElement() { return this.#buttonElement_accessor_storage; }
|
| 117 | + | |
| 118 | - | set buttonElement(value) { this.#buttonElement_accessor_storage = value; }
|
| 118 | + | |
| 119 | - | #assignedIcons_accessor_storage = __runInitializers(this, _assignedIcons_initializers, void 0); |
| 119 | + | |
| 120 | - | get assignedIcons() { return this.#assignedIcons_accessor_storage; }
|
| 120 | + | |
| 121 | - | set assignedIcons(value) { this.#assignedIcons_accessor_storage = value; }
|
| 121 | + | |
| 122 | - | /** @private */ |
| 122 | + | |
| 123 | - | [(_disabled_decorators = [property({ type: Boolean, reflect: true })], _href_decorators = [property()], _target_decorators = [property()], _trailingIcon_decorators = [property({ type: Boolean, attribute: 'trailing-icon' })], _hasIcon_decorators = [property({ type: Boolean, attribute: 'has-icon' })], _type_decorators = [property()], _value_decorators = [property()], _buttonElement_decorators = [query('.button')], _assignedIcons_decorators = [queryAssignedElements({ slot: 'icon', flatten: true })], internals)] = this /* needed for closure */.attachInternals();
|
| 123 | + | |
| 124 | return html$1 ` | |
| 125 | ${this.renderElevation?.()}
| |
| 126 | ${this.renderOutline?.()}
| |
| 127 | <md-focus-ring part="focus-ring"></md-focus-ring> | |
| 128 | <md-ripple class="button__ripple" ?disabled="${isDisabled}"></md-ripple>
| |
| 129 | <span class="touch"></span> | |
| 130 | ${this.trailingIcon ? nothing : icon}
| |
| 131 | <span class="button__label"><slot></slot></span> | |
| 132 | ${this.trailingIcon ? icon : nothing}
| |
| 133 | `; | |
| 134 | } | |
| 135 | handleActivationClick = (event) => {
| |
| 136 | if (!isActivationClick((event)) || !this.buttonElement) {
| |
| 137 | return; | |
| 138 | } | |
| 139 | this.focus(); | |
| 140 | dispatchActivationClick(this.buttonElement); | |
| 141 | }; | |
| 142 | handleSlotChange() {
| |
| 143 | this.hasIcon = this.assignedIcons.length > 0; | |
| 144 | } | |
| 145 | }; | |
| 146 | })(); | |
| 147 | ||
| 148 | export { Button }; |