2020-06-02 21:30:17 -07:00
|
|
|
<template>
|
2022-01-28 10:03:23 -08:00
|
|
|
<transition :name="$store.state.animation ? 'tooltip' : ''" appear @after-leave="emit('closed')">
|
2021-12-10 01:20:41 -08:00
|
|
|
<div v-show="showing" ref="el" class="buebdbiu _acrylic _shadow" :style="{ zIndex, maxWidth: maxWidth + 'px' }">
|
2020-06-02 21:30:17 -07:00
|
|
|
<slot>{{ text }}</slot>
|
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</template>
|
|
|
|
|
2022-01-28 10:03:23 -08:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { nextTick, onMounted, onUnmounted, ref } from 'vue';
|
2021-12-10 01:20:41 -08:00
|
|
|
import * as os from '@/os';
|
2020-06-02 21:30:17 -07:00
|
|
|
|
2022-01-28 10:03:23 -08:00
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
showing: boolean;
|
2022-01-31 04:07:33 -08:00
|
|
|
targetElement?: HTMLElement;
|
|
|
|
x?: number;
|
|
|
|
y?: number;
|
2022-01-28 10:03:23 -08:00
|
|
|
text?: string;
|
2022-01-31 04:07:33 -08:00
|
|
|
maxWidth?: number;
|
2022-02-07 22:37:31 -08:00
|
|
|
direction?: 'top' | 'bottom' | 'right' | 'left';
|
|
|
|
innerMargin?: number;
|
2022-01-28 10:03:23 -08:00
|
|
|
}>(), {
|
|
|
|
maxWidth: 250,
|
2022-02-07 22:37:31 -08:00
|
|
|
direction: 'top',
|
|
|
|
innerMargin: 0,
|
2022-01-28 10:03:23 -08:00
|
|
|
});
|
2021-11-28 03:07:37 -08:00
|
|
|
|
2022-01-28 10:03:23 -08:00
|
|
|
const emit = defineEmits<{
|
|
|
|
(ev: 'closed'): void;
|
|
|
|
}>();
|
2021-11-28 03:07:37 -08:00
|
|
|
|
2022-01-28 10:03:23 -08:00
|
|
|
const el = ref<HTMLElement>();
|
|
|
|
const zIndex = os.claimZIndex('high');
|
2021-11-28 03:07:37 -08:00
|
|
|
|
2022-01-28 10:03:23 -08:00
|
|
|
const setPosition = () => {
|
|
|
|
if (el.value == null) return;
|
2021-11-28 03:07:37 -08:00
|
|
|
|
2022-01-28 10:03:23 -08:00
|
|
|
const contentWidth = el.value.offsetWidth;
|
|
|
|
const contentHeight = el.value.offsetHeight;
|
2021-11-28 03:07:37 -08:00
|
|
|
|
2022-01-31 04:07:33 -08:00
|
|
|
let rect: DOMRect;
|
|
|
|
|
|
|
|
if (props.targetElement) {
|
|
|
|
rect = props.targetElement.getBoundingClientRect();
|
2022-02-07 22:37:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const calcPosWhenTop = () => {
|
|
|
|
let left: number;
|
|
|
|
let top: number;
|
|
|
|
|
|
|
|
if (props.targetElement) {
|
|
|
|
left = rect.left + window.pageXOffset + (props.targetElement.offsetWidth / 2);
|
|
|
|
top = (rect.top + window.pageYOffset - contentHeight) - props.innerMargin;
|
|
|
|
} else {
|
|
|
|
left = props.x;
|
|
|
|
top = (props.y - contentHeight) - props.innerMargin;
|
|
|
|
}
|
|
|
|
|
|
|
|
left -= (el.value.offsetWidth / 2);
|
|
|
|
|
|
|
|
if (left + contentWidth - window.pageXOffset > window.innerWidth) {
|
|
|
|
left = window.innerWidth - contentWidth + window.pageXOffset - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [left, top];
|
|
|
|
}
|
|
|
|
|
|
|
|
const calcPosWhenBottom = () => {
|
|
|
|
let left: number;
|
|
|
|
let top: number;
|
|
|
|
|
|
|
|
if (props.targetElement) {
|
|
|
|
left = rect.left + window.pageXOffset + (props.targetElement.offsetWidth / 2);
|
|
|
|
top = (rect.top + window.pageYOffset + props.targetElement.offsetHeight) + props.innerMargin;
|
|
|
|
} else {
|
|
|
|
left = props.x;
|
|
|
|
top = (props.y) + props.innerMargin;
|
|
|
|
}
|
|
|
|
|
|
|
|
left -= (el.value.offsetWidth / 2);
|
2022-01-31 04:07:33 -08:00
|
|
|
|
2022-02-07 22:37:31 -08:00
|
|
|
if (left + contentWidth - window.pageXOffset > window.innerWidth) {
|
|
|
|
left = window.innerWidth - contentWidth + window.pageXOffset - 1;
|
|
|
|
}
|
2022-01-31 04:07:33 -08:00
|
|
|
|
2022-02-07 22:37:31 -08:00
|
|
|
return [left, top];
|
2022-01-31 04:07:33 -08:00
|
|
|
}
|
2022-01-28 10:03:23 -08:00
|
|
|
|
2022-02-07 22:37:31 -08:00
|
|
|
const calcPosWhenLeft = () => {
|
|
|
|
let left: number;
|
|
|
|
let top: number;
|
|
|
|
|
|
|
|
if (props.targetElement) {
|
|
|
|
left = (rect.left + window.pageXOffset - contentWidth) - props.innerMargin;
|
|
|
|
top = rect.top + window.pageYOffset + (props.targetElement.offsetHeight / 2);
|
|
|
|
} else {
|
|
|
|
left = (props.x - contentWidth) - props.innerMargin;
|
|
|
|
top = props.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
top -= (el.value.offsetHeight / 2);
|
|
|
|
|
|
|
|
if (top + contentHeight - window.pageYOffset > window.innerHeight) {
|
|
|
|
top = window.innerHeight - contentHeight + window.pageYOffset - 1;
|
|
|
|
}
|
2022-01-28 10:03:23 -08:00
|
|
|
|
2022-02-07 22:37:31 -08:00
|
|
|
return [left, top];
|
2022-01-28 10:03:23 -08:00
|
|
|
}
|
|
|
|
|
2022-02-07 22:37:31 -08:00
|
|
|
const calcPosWhenRight = () => {
|
|
|
|
let left: number;
|
|
|
|
let top: number;
|
|
|
|
|
2022-01-31 04:07:33 -08:00
|
|
|
if (props.targetElement) {
|
2022-02-07 22:37:31 -08:00
|
|
|
left = (rect.left + window.pageXOffset) + props.innerMargin;
|
|
|
|
top = rect.top + window.pageYOffset + (props.targetElement.offsetHeight / 2);
|
2022-01-31 04:07:33 -08:00
|
|
|
} else {
|
2022-02-07 22:37:31 -08:00
|
|
|
left = props.x + props.innerMargin;
|
2022-01-31 04:07:33 -08:00
|
|
|
top = props.y;
|
|
|
|
}
|
2022-02-07 22:37:31 -08:00
|
|
|
|
|
|
|
top -= (el.value.offsetHeight / 2);
|
|
|
|
|
|
|
|
if (top + contentHeight - window.pageYOffset > window.innerHeight) {
|
|
|
|
top = window.innerHeight - contentHeight + window.pageYOffset - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [left, top];
|
|
|
|
}
|
|
|
|
|
|
|
|
const calc = (): {
|
|
|
|
left: number;
|
|
|
|
top: number;
|
|
|
|
transformOrigin: string;
|
|
|
|
} => {
|
|
|
|
switch (props.direction) {
|
|
|
|
case 'top': {
|
|
|
|
const [left, top] = calcPosWhenTop();
|
|
|
|
|
|
|
|
// ツールチップを上に向かって表示するスペースがなければ下に向かって出す
|
|
|
|
if (top - window.pageYOffset < 0) {
|
|
|
|
const [left, top] = calcPosWhenBottom();
|
|
|
|
return { left, top, transformOrigin: 'center top' };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { left, top, transformOrigin: 'center bottom' };
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'bottom': {
|
|
|
|
const [left, top] = calcPosWhenBottom();
|
|
|
|
// TODO: ツールチップを下に向かって表示するスペースがなければ上に向かって出す
|
|
|
|
return { left, top, transformOrigin: 'center top' };
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'left': {
|
|
|
|
const [left, top] = calcPosWhenLeft();
|
|
|
|
|
|
|
|
// ツールチップを左に向かって表示するスペースがなければ右に向かって出す
|
|
|
|
if (left - window.pageXOffset < 0) {
|
|
|
|
const [left, top] = calcPosWhenRight();
|
|
|
|
return { left, top, transformOrigin: 'left center' };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { left, top, transformOrigin: 'right center' };
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'right': {
|
|
|
|
const [left, top] = calcPosWhenRight();
|
|
|
|
// TODO: ツールチップを右に向かって表示するスペースがなければ左に向かって出す
|
|
|
|
return { left, top, transformOrigin: 'left center' };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null as never;
|
2022-01-28 10:03:23 -08:00
|
|
|
}
|
|
|
|
|
2022-02-07 22:37:31 -08:00
|
|
|
const { left, top, transformOrigin } = calc();
|
|
|
|
el.value.style.transformOrigin = transformOrigin;
|
2022-01-28 10:03:23 -08:00
|
|
|
el.value.style.left = left + 'px';
|
|
|
|
el.value.style.top = top + 'px';
|
|
|
|
};
|
2021-11-28 03:07:37 -08:00
|
|
|
|
2022-02-12 00:29:15 -08:00
|
|
|
let loopHandler;
|
|
|
|
|
2022-01-28 10:03:23 -08:00
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
setPosition();
|
|
|
|
|
|
|
|
const loop = () => {
|
|
|
|
loopHandler = window.requestAnimationFrame(() => {
|
|
|
|
setPosition();
|
|
|
|
loop();
|
|
|
|
});
|
2021-11-28 03:07:37 -08:00
|
|
|
};
|
2022-01-28 10:03:23 -08:00
|
|
|
|
|
|
|
loop();
|
|
|
|
});
|
|
|
|
});
|
2022-02-12 00:29:15 -08:00
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
window.cancelAnimationFrame(loopHandler);
|
|
|
|
});
|
2020-06-02 21:30:17 -07:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2021-02-16 05:17:13 -08:00
|
|
|
.tooltip-enter-active,
|
|
|
|
.tooltip-leave-active {
|
|
|
|
opacity: 1;
|
|
|
|
transform: scale(1);
|
|
|
|
transition: transform 200ms cubic-bezier(0.23, 1, 0.32, 1), opacity 200ms cubic-bezier(0.23, 1, 0.32, 1);
|
|
|
|
}
|
|
|
|
.tooltip-enter-from,
|
|
|
|
.tooltip-leave-active {
|
|
|
|
opacity: 0;
|
|
|
|
transform: scale(0.75);
|
|
|
|
}
|
|
|
|
|
2020-06-02 21:30:17 -07:00
|
|
|
.buebdbiu {
|
|
|
|
position: absolute;
|
|
|
|
font-size: 0.8em;
|
2020-10-17 04:12:00 -07:00
|
|
|
padding: 8px 12px;
|
2021-10-23 22:39:24 -07:00
|
|
|
box-sizing: border-box;
|
2020-06-02 21:30:17 -07:00
|
|
|
text-align: center;
|
|
|
|
border-radius: 4px;
|
2021-10-23 22:39:24 -07:00
|
|
|
border: solid 0.5px var(--divider);
|
2020-06-02 21:30:17 -07:00
|
|
|
pointer-events: none;
|
2022-01-31 04:07:33 -08:00
|
|
|
transform-origin: center center;
|
2020-06-02 21:30:17 -07:00
|
|
|
}
|
|
|
|
</style>
|