sorvell

es2022 button.js standard decorators

Sep 8th, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { __esDecorate, __runInitializers } from '../../node_modules/tslib/tslib.es6.js';
  2. import '../../focus/md-focus-ring.js';
  3. import '../../ripple/ripple.js';
  4. import { isServer, nothing, html as html$1, LitElement } from 'lit';
  5. import { property, query, queryAssignedElements } from 'lit/decorators.js';
  6. import { classMap } from 'lit/directives/class-map.js';
  7. import { literal, html } from 'lit/static-html.js';
  8. import { requestUpdateOnAriaChange } from '../../internal/aria/delegate.js';
  9. import { internals } from '../../internal/controller/element-internals.js';
  10. import { isActivationClick, dispatchActivationClick } from '../../internal/controller/events.js';
  11. import { setupFormSubmitter } from '../../internal/controller/form-submitter.js';
  12.  
  13. /**
  14.  * @license
  15.  * Copyright 2019 Google LLC
  16.  * SPDX-License-Identifier: Apache-2.0
  17.  */
  18. /**
  19.  * A button component.
  20.  */
  21. let Button = (() => {
  22.     return class Button extends _classSuper {
  23.         static {
  24.             requestUpdateOnAriaChange(Button);
  25.             setupFormSubmitter(Button);
  26.         }
  27.         /** @nocollapse */
  28.         static formAssociated = true;
  29.         /** @nocollapse */
  30.         static shadowRootOptions = { mode: 'open', delegatesFocus: true };
  31.         #disabled_accessor_storage = (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _disabled_initializers, false));
  32.         /**
  33.          * Whether or not the button is disabled.
  34.          */
  35.         get disabled() { return this.#disabled_accessor_storage; }
  36.         set disabled(value) { this.#disabled_accessor_storage = value; }
  37.         #href_accessor_storage = __runInitializers(this, _href_initializers, '');
  38.         /**
  39.          * The URL that the link button points to.
  40.          */
  41.         get href() { return this.#href_accessor_storage; }
  42.         set href(value) { this.#href_accessor_storage = value; }
  43.         #target_accessor_storage = __runInitializers(this, _target_initializers, '');
  44.         /**
  45.          * Where to display the linked `href` URL for a link button. Common options
  46.          * include `_blank` to open in a new tab.
  47.          */
  48.         get target() { return this.#target_accessor_storage; }
  49.         set target(value) { this.#target_accessor_storage = value; }
  50.         #trailingIcon_accessor_storage = __runInitializers(this, _trailingIcon_initializers, false);
  51.         /**
  52.          * Whether to render the icon at the inline end of the label rather than the
  53.          * inline start.
  54.          *
  55.          * _Note:_ Link buttons cannot have trailing icons.
  56.          */
  57.         get trailingIcon() { return this.#trailingIcon_accessor_storage; }
  58.         set trailingIcon(value) { this.#trailingIcon_accessor_storage = value; }
  59.         #hasIcon_accessor_storage = __runInitializers(this, _hasIcon_initializers, false);
  60.         /**
  61.          * Whether to display the icon or not.
  62.          */
  63.         get hasIcon() { return this.#hasIcon_accessor_storage; }
  64.         set hasIcon(value) { this.#hasIcon_accessor_storage = value; }
  65.         #type_accessor_storage = __runInitializers(this, _type_initializers, 'submit');
  66.         get type() { return this.#type_accessor_storage; }
  67.         set type(value) { this.#type_accessor_storage = value; }
  68.         #value_accessor_storage = __runInitializers(this, _value_initializers, '');
  69.         get value() { return this.#value_accessor_storage; }
  70.         set value(value) { this.#value_accessor_storage = value; }
  71.         get name() {
  72.             return this.getAttribute('name') ?? '';
  73.         }
  74.         set name(name) {
  75.             this.setAttribute('name', name);
  76.         }
  77.         /**
  78.          * The associated form element with which this element's value will submit.
  79.          */
  80.         get form() {
  81.             return this[internals].form;
  82.         }
  83.         #buttonElement_accessor_storage = __runInitializers(this, _buttonElement_initializers, void 0);
  84.         get buttonElement() { return this.#buttonElement_accessor_storage; }
  85.         set buttonElement(value) { this.#buttonElement_accessor_storage = value; }
  86.         #assignedIcons_accessor_storage = __runInitializers(this, _assignedIcons_initializers, void 0);
  87.         get assignedIcons() { return this.#assignedIcons_accessor_storage; }
  88.         set assignedIcons(value) { this.#assignedIcons_accessor_storage = value; }
  89.         /** @private */
  90.         [(_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();
  91.         constructor() {
  92.             super();
  93.             if (!isServer) {
  94.                 this.addEventListener('click', this.handleActivationClick);
  95.             }
  96.         }
  97.         focus() {
  98.             this.buttonElement?.focus();
  99.         }
  100.         blur() {
  101.             this.buttonElement?.blur();
  102.         }
  103.         render() {
  104.             // Link buttons may not be disabled
  105.             const isDisabled = this.disabled && !this.href;
  106.             const button = this.href ? literal `a` : literal `button`;
  107.             // Needed for closure conformance
  108.             const { ariaLabel, ariaHasPopup, ariaExpanded } = this;
  109.             return html `
  110.       <${button}
  111.         class="button ${classMap(this.getRenderClasses())}"
  112.         ?disabled=${isDisabled}
  113.         aria-label="${ariaLabel || nothing}"
  114.         aria-haspopup="${ariaHasPopup || nothing}"
  115.         aria-expanded="${ariaExpanded || nothing}"
  116.         href=${this.href || nothing}
  117.         target=${this.target || nothing}
  118.       >${this.renderContent()}</${button}>`;
  119.         }
  120.         getRenderClasses() {
  121.             return {
  122.                 'button--icon-leading': !this.trailingIcon && this.hasIcon,
  123.                 'button--icon-trailing': this.trailingIcon && this.hasIcon,
  124.             };
  125.         }
  126.         renderContent() {
  127.             // Link buttons may not be disabled
  128.             const isDisabled = this.disabled && !this.href;
  129.             const icon = html$1 `<slot name="icon" @slotchange="${this.handleSlotChange}"></slot>`;
  130.             return html$1 `
  131.       ${this.renderElevation?.()}
  132.       ${this.renderOutline?.()}
  133.       <md-focus-ring part="focus-ring"></md-focus-ring>
  134.       <md-ripple class="button__ripple" ?disabled="${isDisabled}"></md-ripple>
  135.       <span class="touch"></span>
  136.       ${this.trailingIcon ? nothing : icon}
  137.       <span class="button__label"><slot></slot></span>
  138.       ${this.trailingIcon ? icon : nothing}
  139.     `;
  140.         }
  141.         handleActivationClick = (event) => {
  142.             if (!isActivationClick((event)) || !this.buttonElement) {
  143.                 return;
  144.             }
  145.             this.focus();
  146.             dispatchActivationClick(this.buttonElement);
  147.         };
  148.         handleSlotChange() {
  149.             this.hasIcon = this.assignedIcons.length > 0;
  150.         }
  151.     };
  152. })();
  153.  
  154. export { Button };
Advertisement
Add Comment
Please, Sign In to add comment