All files / coral-component-splitbutton/src/scripts SplitButton.js

100% Statements 58/58
95.65% Branches 22/23
100% Functions 12/12
100% Lines 58/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                                                                              2x         2x                         2x 2x   2x           68x   68x   68x 32x   32x   32x     68x         68x                     2x     194x 194x 194x               92x 56x   56x 6x 6x 50x 50x 50x             102x 102x 102x   102x 56x     102x 54x           92x 92x 92x   92x 54x     92x 52x   52x   52x 50x 50x                           60x   60x  
/**
 * Copyright 2019 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
 
import {BaseComponent} from '../../../coral-base-component';
import '../../../coral-component-button';
import '../../../coral-component-anchorbutton';
import '../../../coral-component-popover';
import '../../../coral-component-list';
import {transform, validate, commons} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
 
/**
 Enumeration for {@link SplitButton} variants.
 
 @typedef {Object} SplitButtonVariantEnum
 
 @property {String} DEFAULT
 The default button look and feel.
 @property {String} CTA
 A button that is meant to grab the user's attention.
 @property {String} SECONDARY
 A button that indicates that the button's action is the secondary action.
 */
const variant = {
  DEFAULT: 'default',
  CTA: 'cta',
  SECONDARY: 'secondary'
};
 
const CLASSNAME = '_coral-SplitButton';
 
/**
 @class Coral.SplitButton
 @classdesc A Split Button component composed of an action and a trigger {@link AnchorButton} or {@link Button}.
 These elements should be marked with following attributes:
 - <code>[coral-splitbutton-action]</code> for the main action button.
 - <code>[coral-splitbutton-trigger]</code> for the trigger button.
 
 @htmltag coral-splitbutton
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const SplitButton = Decorator(class extends BaseComponent(HTMLElement) {
  /** @ignore */
  constructor() {
    super();
 
    // Watch for inner button changes
    this._observer = new MutationObserver(() => {
      this._updateLeftVariant();
      this._updateInnerButtons();
      this._updateInnerButtonsVariant(this.variant);
    });
 
    this._observer.observe(this, {
      subtree: true,
      childList: true
    });
  }
 
  /**
   The button's variant. See {@link SplitButtonVariantEnum}.
 
   @type {String}
   @default SplitButtonVariantEnum.DEFAULT
   @htmlattribute variant
   @htmlattributereflected
   */
  get variant() {
    return this._variant || variant.DEFAULT;
  }
 
  set variant(value) {
    value = transform.string(value).toLowerCase();
    this._variant = validate.enumeration(variant)(value) && value || variant.DEFAULT;
    this._reflectAttribute('variant', this._variant);
 
    this._updateInnerButtonsVariant(this._variant);
  }
 
  _getInnerButtons() {
    const action = this.querySelector('[coral-splitbutton-action]');
    const trigger = this.querySelector('[coral-splitbutton-trigger]');
    return {action, trigger};
  }
 
  _updateLeftVariant() {
    for (let i = 0 ; i < this.children.length ; i++) {
      const child = this.children[i];
      if (child.hasAttribute('coral-splitbutton-trigger')) {
        this.classList.add('_coral-SplitButton--left');
        return;
      } else if (child.hasAttribute('coral-splitbutton-action')) {
        this.classList.remove('_coral-SplitButton--left');
        return;
      }
    }
  }
 
  _updateInnerButtonsVariant(variant) {
    const {action, trigger} = this._getInnerButtons();
    if (action) {
      action.setAttribute('variant', variant);
    }
    if (triggerE) {
      trigger.setAttribute('variant', variant);
    }
  }
 
  _updateInnerButtons() {
    const {action, trigger} = this._getInnerButtons();
    if (action) {
      action.classList.add('_coral-SplitButton-action');
    }
    if (trigger) {
      trigger.classList.add('_coral-SplitButton-trigger');
      // a11y assume a popover is targeting the trigger
      trigger.setAttribute('aria-haspopup', 'true');
      if (action) {
        action.id = action.id || commons.getUID();
        trigger.setAttribute('aria-labelledby', action.id);
      }
    }
  }
 
  /**
   Returns {@link SplitButton} variants.
 
   @return {SplitButtonVariantEnum}
   */
  static get variant() {
    return variant;
  }
 
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['variant']);
  }
 
  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // a11y
    this.setAttribute('role', 'group');
 
    // Default reflected attributes
    if (!this._variant) {
      this.variant = variant.DEFAULT;
    }
 
    // Update styles
    this._updateLeftVariant();
    this._updateInnerButtons();
  }
});
 
export default SplitButton;