2021-08-18 00:01:11 +02:00
|
|
|
<template>
|
|
|
|
<div ref="box" class="tooltip-box" @mouseover="mouseover" @mouseleave="mouseleave">
|
|
|
|
<slot />
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
text: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
2021-08-25 13:38:32 +02:00
|
|
|
},
|
|
|
|
direction: {
|
|
|
|
type: String,
|
|
|
|
default: 'right'
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
tooltip: null,
|
|
|
|
isShowing: false
|
|
|
|
}
|
|
|
|
},
|
2021-09-06 21:13:01 +02:00
|
|
|
watch: {
|
|
|
|
text() {
|
|
|
|
this.updateText()
|
|
|
|
}
|
|
|
|
},
|
2021-08-18 00:01:11 +02:00
|
|
|
methods: {
|
2021-09-06 21:13:01 +02:00
|
|
|
updateText() {
|
|
|
|
if (this.tooltip) {
|
|
|
|
this.tooltip.innerHTML = this.text
|
|
|
|
}
|
|
|
|
},
|
2021-08-18 00:01:11 +02:00
|
|
|
createTooltip() {
|
2021-09-06 21:13:01 +02:00
|
|
|
if (!this.$refs.box) return
|
2021-08-18 00:01:11 +02:00
|
|
|
var boxChow = this.$refs.box.getBoundingClientRect()
|
2021-08-25 13:38:32 +02:00
|
|
|
var top = 0
|
|
|
|
var left = 0
|
|
|
|
if (this.direction === 'right') {
|
|
|
|
top = boxChow.top
|
|
|
|
left = boxChow.left + boxChow.width + 4
|
|
|
|
} else if (this.direction === 'bottom') {
|
|
|
|
top = boxChow.top + boxChow.height + 4
|
|
|
|
left = boxChow.left
|
2021-09-06 21:13:01 +02:00
|
|
|
} else if (this.direction === 'top') {
|
|
|
|
top = boxChow.top - 24
|
|
|
|
left = boxChow.left
|
2021-08-25 13:38:32 +02:00
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
var tooltip = document.createElement('div')
|
2021-08-25 13:38:32 +02:00
|
|
|
tooltip.className = 'absolute px-2 bg-black bg-opacity-90 py-1 text-white pointer-events-none text-xs rounded shadow-lg'
|
2021-08-18 00:01:11 +02:00
|
|
|
tooltip.style.top = top + 'px'
|
|
|
|
tooltip.style.left = left + 'px'
|
|
|
|
tooltip.style.zIndex = 100
|
2021-09-05 02:58:39 +02:00
|
|
|
tooltip.innerHTML = this.text
|
2021-08-18 00:01:11 +02:00
|
|
|
this.tooltip = tooltip
|
|
|
|
},
|
|
|
|
showTooltip() {
|
|
|
|
if (!this.tooltip) {
|
|
|
|
this.createTooltip()
|
|
|
|
}
|
|
|
|
document.body.appendChild(this.tooltip)
|
|
|
|
this.isShowing = true
|
|
|
|
},
|
|
|
|
hideTooltip() {
|
|
|
|
if (!this.tooltip) return
|
|
|
|
this.tooltip.remove()
|
|
|
|
this.isShowing = false
|
|
|
|
},
|
|
|
|
mouseover() {
|
|
|
|
if (!this.isShowing) this.showTooltip()
|
|
|
|
},
|
|
|
|
mouseleave() {
|
|
|
|
if (this.isShowing) this.hideTooltip()
|
|
|
|
}
|
2021-08-18 13:50:24 +02:00
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
this.hideTooltip()
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|