all files / src/tooltip/ tooltip.vue

0% Statements 0/27
0% Branches 0/28
100% Functions 0/0
0% Lines 0/26
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                                                                                                                                                                                                                                                                                               
<template>
<div class="mu-tooltip" :class="{'touched': touch, 'when-shown': show}" :style="tooltipStyle">
  <div class="mu-tooltip-ripple" :style="rippleStyle" :class="{'when-shown': show}" ref="ripple">
  </div>
  <span class="mu-tooltip-label">{{label}}</span>
</div>
</template>
 
<script>
export default {
  name: 'mu-tooltip',
  props: {
    label: {
      type: String
    },
    trigger: {
      type: Element
    },
    verticalPosition: {
      type: String, // top bottom
      default: 'bottom'
    },
    horizontalPosition: {
      type: String, // left right center
      default: 'center'
    },
    show: {
      type: Boolean,
      default: false
    },
    touch: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      offsetWidth: 0,
      triggerWidth: 0,
      triggerHeight: 0
    }
  },
  computed: {
    tooltipStyle () {
      const {horizontalPosition, verticalPosition, offsetWidth, touch, triggerWidth, triggerHeight, show} = this
      const touchMarginOffset = touch ? 10 : 0
      const touchOffsetTop = touch ? -20 : -10
      const offset = verticalPosition === 'bottom' ? 14 + touchMarginOffset : -14 - touchMarginOffset
      return {
        right: horizontalPosition === 'left' ? '0' : null,
        left: horizontalPosition === 'center' ? ((offsetWidth - triggerWidth) / 2 * -1) + 'px' : horizontalPosition === 'right' ? '0' : '',
        top: !show ? '-3000px' : verticalPosition === 'top' ? touchOffsetTop + 'px' : (triggerHeight - offset + touchMarginOffset + 2) + 'px',
        transform: `translate(0px, ${offset}px)`
      }
    },
    rippleStyle () {
      const {horizontalPosition, verticalPosition} = this
      return {
        left: horizontalPosition === 'center' ? '50%' : horizontalPosition === 'left' ? '100%' : '0%',
        top: verticalPosition === 'bottom' ? '0' : '100%'
      }
    }
  },
  methods: {
    setRippleSize () {
      const ripple = this.$refs.ripple
      const tooltip = this.$el
      const tooltipWidth = parseInt(tooltip.offsetWidth, 10) / (this.horizontalPosition === 'center' ? 2 : 1)
      const tooltipHeight = parseInt(tooltip.offsetHeight, 10)
      const rippleDiameter = Math.ceil((Math.sqrt(Math.pow(tooltipHeight, 2) + Math.pow(tooltipWidth, 2)) * 2))
      if (this.show) {
        ripple.style.height = `${rippleDiameter}px`
        ripple.style.width = `${rippleDiameter}px`
      } else {
        ripple.style.width = '0px'
        ripple.style.height = '0px'
      }
    },
    setTooltipSize () {
      this.offsetWidth = this.$el.offsetWidth
      if (!this.trigger) return
      this.triggerWidth = this.trigger.offsetWidth
      this.triggerHeight = this.trigger.offsetHeight
    }
  },
  mounted () {
    this.setRippleSize()
    this.setTooltipSize()
  },
  beforeUpdate () {
    this.setTooltipSize()
  },
  updated () {
    this.setRippleSize()
  }
}
</script>
 
<style lang="less">
@import "../styles/import.less";
.mu-tooltip{
  position: absolute;
  font-size: 10px;
  line-height: 22px;
  padding: 0 8px;
  z-index: 300;
  color: @alternateTextColor;
  overflow: hidden;
  top: -1000px;
  border-radius: 2px;
  user-select: none;
  opacity: 0;
  transition: top 0ms @easeOutFunction 450ms, transform 450ms @easeOutFunction 0ms, opacity 450ms @easeOutFunction 0ms;
  &.when-shown {
    opacity: 0.9;
    transition: top 0ms @easeOutFunction 0ms,  transform 450ms @easeOutFunction 0ms, opacity 450ms @easeOutFunction 0ms;
  }
  &.touched{
    font-size: 14px;
    line-height: 32px;
    padding: 0 16px;
  }
}
 
 
 
.mu-tooltip-ripple{
  position: absolute;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background-color: transparent;
  transition: width 0ms @easeOutFunction 450ms, height 0ms @easeOutFunction 450ms, background-color 450ms @easeOutFunction 0ms;
  &.when-shown {
    background-color: @grey700;
    transition: width 450ms @easeOutFunction 0ms, height 450ms @easeOutFunction 0ms, background-color 450ms @easeOutFunction 0ms;
  }
}
 
.mu-tooltip-label {
  white-space: nowrap;
  position: relative;
}
</style>