all files / src/components/Tooltip/ index.vue

100% Statements 1/1
100% Branches 0/0
100% Functions 0/0
100% Lines 1/1
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                                  14×                                                                                                                                                                                                                              
<template>
  <div class="tooltip" :class="`${position}`">
    <div class="container">
      <div class="arrow"></div>
      <slot></slot>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'Tooltip',
  props: {
    position: {
      type: String,
      default: 'top',
      validator (str) {
        return ['top', 'bottom', 'left', 'right'].includes(str)
      }
    }
  }
}
</script>
 
<style lang="scss" scoped>
  .tooltip {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9;
    .container {
      position: absolute;
      padding: 6px 8px;
      background: #336;
      white-space: nowrap;
      font-size: 13px;
      line-height: 16px;
      pointer-events: none;
      opacity: 0;
      transition: opacity 0.2s;
      will-change: opacity;
      color: #fff;
 
      .arrow {
        position: absolute;
        width: 0;
        height: 0;
      }
    }
 
    &.top {
      .container {
        bottom: calc(100% + 10px);
        left: 50%;
        transform: translateX(-50%);
      }
 
      .arrow {
        bottom: -5px;
        left: 50%;
        transform: translateX(-50%);
        border-left: 5px solid transparent;
        border-right: 5px solid transparent;
        border-top: 5px solid #336;
      }
    }
 
    &.bottom {
      .container {
        top: calc(100% + 10px);
        left: 50%;
        transform: translateX(-50%);
      }
 
      .arrow {
        top: -5px;
        left: 50%;
        transform: translateX(-50%);
        border-left: 5px solid transparent;
        border-right: 5px solid transparent;
        border-bottom: 5px solid #336;
      }
    }
 
    &.left {
      .container {
        top: 50%;
        right: calc(100% + 10px);
        transform: translateY(-50%)
      }
 
      .arrow {
        right: -5px;
        top: 50%;
        transform: translateY(-50%);
        border-top: 5px solid transparent;
        border-bottom: 5px solid transparent;
        border-left: 5px solid #336;
      }
    }
 
    &.right {
      .container {
        top: 50%;
        left: calc(100% + 10px);
        transform: translateY(-50%)
      }
 
      .arrow {
        left: -5px;
        top: 50%;
        transform: translateY(-50%);
        border-top: 5px solid transparent;
        border-bottom: 5px solid transparent;
        border-right: 5px solid #336;
      }
    }
 
    &:hover {
      .container {
        opacity: 1
      }
    }
  }
 
</style>