2020-12-26 22:51:27 +00:00
|
|
|
<template>
|
|
|
|
<div class="tooltip-container">
|
2020-12-27 03:21:10 +00:00
|
|
|
<div
|
|
|
|
ref="trigger"
|
|
|
|
class="trigger noselect"
|
2021-03-01 01:41:53 +00:00
|
|
|
@mouseenter="toggle"
|
2020-12-27 03:21:10 +00:00
|
|
|
@click.stop="toggle"
|
|
|
|
>
|
2020-12-26 22:51:27 +00:00
|
|
|
<slot />
|
|
|
|
</div>
|
|
|
|
|
2020-12-27 01:15:06 +00:00
|
|
|
<teleport to="body">
|
2020-12-27 03:21:10 +00:00
|
|
|
<div
|
|
|
|
v-if="opened"
|
|
|
|
ref="tooltip"
|
|
|
|
class="tooltip-wrapper"
|
|
|
|
:style="{ transform: `translate3d(${tooltipX}px, ${tooltipY}px, 0)` }"
|
2021-02-28 02:38:54 +00:00
|
|
|
@click.stop
|
2020-12-27 03:21:10 +00:00
|
|
|
>
|
|
|
|
<div class="tooltip-inner">
|
|
|
|
<div class="tooltip">
|
|
|
|
<slot name="tooltip" />
|
|
|
|
</div>
|
2020-12-28 23:42:02 +00:00
|
|
|
|
|
|
|
<div
|
|
|
|
class="tooltip-arrow"
|
|
|
|
:style="{ transform: `translate3d(${arrowOffset}px, 0, 0)` }"
|
|
|
|
/>
|
2020-12-27 01:15:06 +00:00
|
|
|
</div>
|
2020-12-26 22:51:27 +00:00
|
|
|
</div>
|
2020-12-27 01:15:06 +00:00
|
|
|
</teleport>
|
2020-12-26 22:51:27 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2020-12-27 01:15:06 +00:00
|
|
|
<script>
|
2020-12-27 03:21:10 +00:00
|
|
|
import { nextTick } from 'vue';
|
2020-12-27 01:15:06 +00:00
|
|
|
|
2020-12-27 03:21:10 +00:00
|
|
|
function getX(triggerBoundary, tooltipBoundary) {
|
|
|
|
const idealPosition = triggerBoundary.left + (triggerBoundary.width / 2) - (tooltipBoundary.width / 2);
|
2020-12-28 23:42:02 +00:00
|
|
|
const rightEdgeOverflow = Math.max((idealPosition + tooltipBoundary.width) - window.innerWidth, 0);
|
2020-12-27 03:21:10 +00:00
|
|
|
|
|
|
|
// don't overflow left edge
|
|
|
|
if (idealPosition < 0) {
|
2020-12-28 23:42:02 +00:00
|
|
|
return {
|
|
|
|
tooltipX: 0,
|
|
|
|
arrowOffset: idealPosition,
|
|
|
|
};
|
2020-12-27 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// don't overflow right edge
|
2020-12-28 23:42:02 +00:00
|
|
|
if (rightEdgeOverflow > 0) {
|
|
|
|
return {
|
|
|
|
tooltipX: window.innerWidth - tooltipBoundary.width,
|
|
|
|
arrowOffset: rightEdgeOverflow,
|
|
|
|
};
|
2020-12-27 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// position at the center of trigger
|
2020-12-28 23:42:02 +00:00
|
|
|
return {
|
|
|
|
tooltipX: idealPosition,
|
|
|
|
arrowOffset: 0,
|
|
|
|
};
|
2020-12-27 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function calculate() {
|
|
|
|
if (!this.opened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const triggerBoundary = this.$refs.trigger.getBoundingClientRect();
|
|
|
|
const tooltipBoundary = this.$refs.tooltip.getBoundingClientRect();
|
|
|
|
|
2020-12-28 23:42:02 +00:00
|
|
|
const { tooltipX, arrowOffset } = this.getX(triggerBoundary, tooltipBoundary);
|
|
|
|
|
2020-12-27 03:21:10 +00:00
|
|
|
this.tooltipY = triggerBoundary.top + triggerBoundary.height;
|
2020-12-28 23:42:02 +00:00
|
|
|
this.tooltipX = tooltipX;
|
|
|
|
this.arrowOffset = arrowOffset;
|
2020-12-27 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function open() {
|
|
|
|
this.events.emit('blur');
|
|
|
|
|
|
|
|
await nextTick();
|
|
|
|
|
|
|
|
this.opened = true;
|
|
|
|
await nextTick();
|
|
|
|
|
|
|
|
this.calculate();
|
2020-12-28 23:42:02 +00:00
|
|
|
this.$emit('open');
|
2020-12-27 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
this.opened = false;
|
|
|
|
|
|
|
|
this.tooltipY = 0;
|
|
|
|
this.tooltipX = 0;
|
2020-12-28 23:42:02 +00:00
|
|
|
this.arrowOffset = 0;
|
|
|
|
|
|
|
|
this.$emit('close');
|
2020-12-27 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toggle() {
|
|
|
|
if (this.opened) {
|
|
|
|
this.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
function mounted() {
|
|
|
|
this.events.on('blur', () => {
|
|
|
|
this.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.events.on('resize', () => {
|
|
|
|
this.calculate();
|
|
|
|
});
|
2021-02-28 02:38:54 +00:00
|
|
|
|
|
|
|
this.events.on('scroll', () => {
|
|
|
|
this.calculate();
|
|
|
|
});
|
2020-12-27 03:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
opened: false,
|
|
|
|
tooltipX: 0,
|
|
|
|
tooltipY: 0,
|
2020-12-28 23:42:02 +00:00
|
|
|
arrowOffset: 0,
|
2020-12-27 03:21:10 +00:00
|
|
|
};
|
|
|
|
},
|
2020-12-28 23:51:59 +00:00
|
|
|
emits: ['open', 'close'],
|
2020-12-27 03:21:10 +00:00
|
|
|
mounted,
|
|
|
|
methods: {
|
|
|
|
calculate,
|
|
|
|
getX,
|
|
|
|
open,
|
|
|
|
close,
|
|
|
|
toggle,
|
|
|
|
},
|
|
|
|
};
|
2020-12-27 01:15:06 +00:00
|
|
|
</script>
|
|
|
|
|
2020-12-26 22:51:27 +00:00
|
|
|
<style lang="scss" scoped>
|
2020-12-27 03:21:10 +00:00
|
|
|
.tooltip-wrapper {
|
|
|
|
display: flex;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
flex-direction: column;
|
|
|
|
justify-content: center;
|
|
|
|
position: absolute;
|
|
|
|
z-index: 10;
|
2020-12-26 22:51:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 03:21:10 +00:00
|
|
|
.tooltip-inner {
|
|
|
|
position: relative;
|
|
|
|
box-shadow: 0 0 3px var(--darken-weak);
|
2020-12-27 01:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 22:51:27 +00:00
|
|
|
.tooltip {
|
2020-12-27 03:21:10 +00:00
|
|
|
position: relative;
|
2020-12-27 01:15:06 +00:00
|
|
|
background: var(--background-light);
|
2020-12-26 22:51:27 +00:00
|
|
|
}
|
2020-12-28 23:42:02 +00:00
|
|
|
|
|
|
|
.tooltip-arrow {
|
|
|
|
content: '';
|
|
|
|
width: 0;
|
|
|
|
height: 0;
|
|
|
|
position: absolute;
|
|
|
|
top: -.5rem;
|
|
|
|
left: calc(50% - .5rem);
|
|
|
|
border-left: .5rem solid transparent;
|
|
|
|
border-right: .5rem solid transparent;
|
|
|
|
border-bottom: .5rem solid var(--background-light);
|
|
|
|
margin: 0 auto;
|
|
|
|
filter: drop-shadow(0 0 3px var(--darken-weak));
|
|
|
|
}
|
2020-12-26 22:51:27 +00:00
|
|
|
</style>
|