All files / tool-tip index.js

79.27% Statements 65/82
70.83% Branches 17/24
81.25% Functions 13/16
80.77% Lines 63/78

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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 2001x       5x   5x                                                                                       5x       5x 5x   5x 5x 5x 5x 5x       5x       15x 10x 10x         10x 10x 10x 10x 10x       1x       6x 6x 5x   6x         8x 8x 8x 8x 8x 8x 8x   8x 1x     8x         8x         8x 8x   8x 8x 8x                                 8x 8x 1x 7x       8x 8x 8x 8x 8x       7x 7x 7x 7x       1x 1x 1x 1x                                   2x 2x       1x 1x 1x                
window.customElements.define('h-tool-tip', class extends HTMLElement {
 
  _generateTemplate() {
 
    const template = document.createElement('template')
 
    template.innerHTML = `
      <style>
        .arrow {
          background: var(--surface, #212121);
          height: 15px;
          position: absolute;
          transform: rotateZ(45deg);
          width: 15px;
        }
        .tip {
          background-color: var(--surface, #212121);
          border-radius: 6px;
          box-shadow: var(--element-shadow, rgba(0,0,0,0.5));
          color: var(--on-surface, #eee);
          display: inline;
          left: -100px;
          opacity: 0;
          padding: 0.6rem;
          position: absolute;
          top: -100px;
          transition: opacity 0.3s;
          z-index: 1000;
        }
        .tip.active {
          opacity:1;
        }
        .bottom {
          box-shadow: -1px -1px var(--shadow);
        }
        .left {
          box-shadow: 1px -1px var(--shadow);
        }
        .top {
          box-shadow: 1px 1px 1px 0px var(--shadow);
        }
        .right {
          box-shadow: -1px 1px var(--shadow);
        }
      </style>
      <div class="tip">
        <div class="arrow"></div>
        <slot></slot>
      </div>
    `
    return template
  }
 
  constructor() {
    super()
    this.attachShadow({ mode: 'open' })
      .appendChild(this._generateTemplate().content.cloneNode(true))
    this.$tip = this.shadowRoot.querySelector('div.tip')
    this.$arrow = this.shadowRoot.querySelector('div.arrow')
    this._onMouseoverBind = this.show.bind(this)
    this._onMouseoutBind = this.hide.bind(this)
    this.$tipFor
  }
 
  connectedCallback() {
    this.updateTipFor()
  }
 
  disconnectedCallback() {
    if (this.$tipFor) {
      this.$tipFor.removeEventListener('mouseover', this._onMouseoverBind)
      this.$tipFor.removeEventListener('mouseout', this._onMouseoutBind)
    }
  }
 
  updateTipFor() {
    this.disconnectedCallback()
    this.$tipFor = document.getElementById(this.dataset.for)
    Eif (this.$tipFor && this.getAttribute('data-hover') !== 'false') {
      this.$tipFor.addEventListener('mouseover', this._onMouseoverBind)
      this.$tipFor.addEventListener('mouseout', this._onMouseoutBind)
    }
  }
  static get observedAttributes() {
    return ['data-position', 'data-for', 'data-hover']
  }
 
  attributeChangedCallback(attr, oldValue, newValue) {
    Eif (oldValue !== newValue) {
      if (['data-for', 'data-hover'].includes(attr)) {
        this.updateTipFor()
      }
      this._positionAt()
    }
  }
 
  _positionAt() {
    Eif (this.$tipFor) {
      const position = this.dataset.position || 'right'
      const parentCoords = this.$tipFor.getBoundingClientRect()
      const tooltip = this.$tip
      const dist = 15
      let left = parseInt(parentCoords.right, 10) + dist
      let top = (parseInt(parentCoords.top, 10) + parseInt(parentCoords.bottom, 10)) / 2 - tooltip.offsetHeight / 2
 
      if (position === 'left') {
        left = parseInt(parentCoords.left, 10) - dist - tooltip.offsetWidth
      }
 
      Iif (position === 'bottom') {
        left = (parseInt(parentCoords.left, 10) + parseInt(parentCoords.right, 10)) / 2 - tooltip.offsetWidth / 2
        top = parseInt(parentCoords.bottom, 10) + dist
      }
 
      Iif (position === 'top') {
        left = (parseInt(parentCoords.left, 10) + parseInt(parentCoords.right, 10)) / 2 - tooltip.offsetWidth / 2
        top = parseInt(parentCoords.top, 10) - dist - tooltip.offsetHeight
      }
 
      left += window.scrollX
      top += window.scrollY
 
      tooltip.style.left = `${left}px`
      tooltip.style.top = `${top}px`
      this._positionArrow(
        position,
        parentCoords.top,
        parentCoords.right,
        parentCoords.bottom,
        parentCoords.left
      )
    }
  }
 
  _positionArrow(position) {
    /**
     * use the getBoundingClientRect() function to get the rendered
     * borders of the transformed $arrow div, and use those to center its point
     * on the specified toggle element
     */
    let arrowCoords
    const thisRect = this.$tip.getBoundingClientRect()
    switch (position) {
    case 'left': arrowCoords = this._arrowAtRight(thisRect); break
    case 'right': arrowCoords = this._arrowAtLeft(thisRect); break
    case 'top': arrowCoords = this._arrowAtBottom(thisRect); break
    case 'bottom': arrowCoords = this._arrowAtTop(thisRect); break
    }
    const { arrowTop, arrowLeft } = arrowCoords
    this.$arrow.style.left = `${arrowLeft}px`
    this.$arrow.style.top = `${arrowTop}px`
    this.$arrow.classList.remove('top', 'bottom', 'left', 'right')
    this.$arrow.classList.add(position)
  }
 
  _arrowAtLeft(targetCoords) {
    const { top, bottom } = targetCoords
    const arrowTop = ((bottom - top) / 2) - this.$arrow.clientWidth / 2
    const arrowLeft = - (this.$arrow.clientWidth / 2)
    return { arrowLeft, arrowTop }
  }
 
  _arrowAtRight(targetCoords) {
    const { top, bottom, right, left } = targetCoords
    const arrowTop = ((bottom - top) / 2) - this.$arrow.clientHeight / 2
    const arrowLeft = right - left - (this.$arrow.clientWidth / 2)
    return { arrowLeft, arrowTop }
  }
 
  _arrowAtTop(targetCoords) {
    const { left, right } = targetCoords
    const arrowTop = - this.$arrow.clientWidth / 2
    const arrowLeft = (right - left) / 2 - (this.$arrow.clientWidth / 2)
    return { arrowLeft, arrowTop }
  }
 
  _arrowAtBottom(targetCoords) {
    const { top, bottom, left, right } = targetCoords
    const arrowTop = bottom - top - (this.$arrow.clientWidth / 2)
    const arrowLeft = (right - left) / 2 - (this.$arrow.clientWidth / 2)
    return { arrowLeft, arrowTop }
  }
 
  show() {
    this._positionAt()
    this.$tip.classList.add('active')
  }
 
  hide() {
    this.$tip.classList.remove('active')
    this.$tip.style.left = '-100px'
    this.$tip.style.top = '-100px'
  }
 
  isShowing() {
    return this.$tip.classList.contains('active')
  }
 
})