2021-11-12 08:53:10 -06:00
|
|
|
|
import { Ref, ref } from 'vue';
|
2021-12-04 22:10:19 -06:00
|
|
|
|
import { isScreenTouching, isTouchUsing } from './touch';
|
2021-11-12 08:53:10 -06:00
|
|
|
|
|
|
|
|
|
export function useTooltip(onShow: (showing: Ref<boolean>) => void) {
|
|
|
|
|
let isHovering = false;
|
|
|
|
|
let timeoutId: number;
|
|
|
|
|
|
|
|
|
|
let changeShowingState: (() => void) | null;
|
|
|
|
|
|
|
|
|
|
const open = () => {
|
|
|
|
|
close();
|
|
|
|
|
if (!isHovering) return;
|
|
|
|
|
|
2021-11-27 03:29:23 -06:00
|
|
|
|
// iOS(Androidも?)では、要素をタップした直後に(おせっかいで)mouseoverイベントを発火させたりするため、その対策
|
|
|
|
|
// これが無いと、画面に触れてないのにツールチップが出たりしてしまう
|
2021-12-04 22:10:19 -06:00
|
|
|
|
if (isTouchUsing && !isScreenTouching) return;
|
2021-11-27 03:29:23 -06:00
|
|
|
|
|
2021-11-12 08:53:10 -06:00
|
|
|
|
const showing = ref(true);
|
|
|
|
|
onShow(showing);
|
|
|
|
|
changeShowingState = () => {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
|
if (changeShowingState != null) {
|
|
|
|
|
changeShowingState();
|
|
|
|
|
changeShowingState = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onMouseover = () => {
|
|
|
|
|
if (isHovering) return;
|
|
|
|
|
isHovering = true;
|
|
|
|
|
timeoutId = window.setTimeout(open, 300);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onMouseleave = () => {
|
|
|
|
|
if (!isHovering) return;
|
|
|
|
|
isHovering = false;
|
|
|
|
|
window.clearTimeout(timeoutId);
|
|
|
|
|
close();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
onMouseover,
|
|
|
|
|
onMouseleave,
|
|
|
|
|
};
|
|
|
|
|
}
|