All files / coral-component-sidenav/src/scripts SideNavLevel.js

96.97% Statements 32/33
92.86% Branches 13/14
100% Functions 7/7
96.97% Lines 32/33

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                                          2x                 2x 2x   2x     50x   50x     2x         46x   46x 46x   46x 34x   34x   34x   8x 8x 6x   2x       8x 2x   6x           8x 8x     26x   26x 18x 18x                      
/**
 * 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 {commons} from '../../../coral-utils';
import {BaseComponent} from '../../../coral-base-component';
import {Decorator} from '../../../coral-decorator';
 
const CLASSNAME = '_coral-SideNav';
 
/**
 @class Coral.SideNav.Level
 @classdesc A SideNav Level component
 @htmltag coral-sidenav-level
 @extends {HTMLElement}
 @extends {BaseComponent}
 */
const SideNavLevel = Decorator(class extends BaseComponent(HTMLElement) {
  /** @ignore */
  static get observedAttributes() {
    return super.observedAttributes.concat(['_expanded']);
  }
 
  /** @ignore */
  attributeChangedCallback(name, oldValue, value) {
    if (name === '_expanded') {
      const isExpanded = value === 'on';
 
      if (oldValue !== value) {
        this.classList.toggle('is-expanded', isExpanded);
 
        // Do animation in next frame to avoid a forced reflow
        window.requestAnimationFrame(() => {
          // Don't animate on initialization
          if (this._animate) {
            // Remove height as we want the level to naturally grow if content is added later
            commons.transitionEnd(this, () => {
              if (isExpanded) {
                this.style.height = '';
              } else {
      E          this.hidden = true;
              }
            });
 
            // Force height to enable transition
            if (!isExpanded) {
              this.style.height = `${this.scrollHeight}px`;
            } else {
              this.hidden = false;
            }
 
            // We read the offset height to force a reflow, this is needed to start the transition between absolute values
            // https://blog.alexmaccaw.com/css-transitions under Redrawing
            // eslint-disable-next-line no-unused-vars
            const offsetHeight = this.offsetHeight;
 
            this.style.height = isExpanded ? `${this.scrollHeight}px` : 0;
          } else {
            // Make sure it's animated next time
            this._animate = true;
 
            // Hide it on initialization if closed
            if (!isExpanded) {
              this.style.height = 0;
              this.hidden = true;
            }
          }
        });
      }
    } else {
      super.attributeChangedCallback(name, oldValue, value);
    }
  }
 
  /** @ignore */
  render() {
    super.render();
 
    this.classList.add(CLASSNAME);
 
    // a11y
    this.setAttribute('role', 'region');
  }
});
 
export default SideNavLevel;