2021-12-05 11:01:52 +00:00
|
|
|
|
import { Ref, ref, watch } from 'vue';
|
2021-11-12 14:53:10 +00:00
|
|
|
|
|
2021-12-05 11:01:52 +00:00
|
|
|
|
export function useTooltip(
|
|
|
|
|
elRef: Ref<HTMLElement | { $el: HTMLElement } | null | undefined>,
|
|
|
|
|
onShow: (showing: Ref<boolean>) => void,
|
|
|
|
|
): void {
|
2021-11-12 14:53:10 +00:00
|
|
|
|
let isHovering = false;
|
2021-12-05 11:01:52 +00:00
|
|
|
|
|
|
|
|
|
// iOS(Androidも?)では、要素をタップした直後に(おせっかいで)mouseoverイベントを発火させたりするため、それを無視するためのフラグ
|
|
|
|
|
// 無視しないと、画面に触れてないのにツールチップが出たりし、ユーザビリティが損なわれる
|
|
|
|
|
// TODO: 一度でもタップすると二度とマウスでツールチップ出せなくなるのをどうにかする 定期的にfalseに戻すとか...?
|
|
|
|
|
let shouldIgnoreMouseover = false;
|
|
|
|
|
|
2021-11-12 14:53:10 +00:00
|
|
|
|
let timeoutId: number;
|
|
|
|
|
|
|
|
|
|
let changeShowingState: (() => void) | null;
|
|
|
|
|
|
|
|
|
|
const open = () => {
|
|
|
|
|
close();
|
|
|
|
|
if (!isHovering) return;
|
|
|
|
|
|
|
|
|
|
const showing = ref(true);
|
|
|
|
|
onShow(showing);
|
|
|
|
|
changeShowingState = () => {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
|
if (changeShowingState != null) {
|
|
|
|
|
changeShowingState();
|
|
|
|
|
changeShowingState = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onMouseover = () => {
|
|
|
|
|
if (isHovering) return;
|
2021-12-05 11:01:52 +00:00
|
|
|
|
if (shouldIgnoreMouseover) return;
|
2021-11-12 14:53:10 +00:00
|
|
|
|
isHovering = true;
|
|
|
|
|
timeoutId = window.setTimeout(open, 300);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onMouseleave = () => {
|
|
|
|
|
if (!isHovering) return;
|
|
|
|
|
isHovering = false;
|
|
|
|
|
window.clearTimeout(timeoutId);
|
|
|
|
|
close();
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-05 11:01:52 +00:00
|
|
|
|
const onTouchstart = () => {
|
|
|
|
|
shouldIgnoreMouseover = true;
|
|
|
|
|
if (isHovering) return;
|
|
|
|
|
isHovering = true;
|
|
|
|
|
timeoutId = window.setTimeout(open, 300);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onTouchend = () => {
|
|
|
|
|
if (!isHovering) return;
|
|
|
|
|
isHovering = false;
|
|
|
|
|
window.clearTimeout(timeoutId);
|
|
|
|
|
close();
|
2021-11-12 14:53:10 +00:00
|
|
|
|
};
|
2021-12-05 11:01:52 +00:00
|
|
|
|
|
|
|
|
|
const stop = watch(elRef, () => {
|
|
|
|
|
if (elRef.value) {
|
|
|
|
|
stop();
|
|
|
|
|
const el = elRef.value instanceof Element ? elRef.value : elRef.value.$el;
|
|
|
|
|
el.addEventListener('mouseover', onMouseover, { passive: true });
|
|
|
|
|
el.addEventListener('mouseleave', onMouseleave, { passive: true });
|
|
|
|
|
el.addEventListener('touchstart', onTouchstart, { passive: true });
|
|
|
|
|
el.addEventListener('touchend', onTouchend, { passive: true });
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
immediate: true,
|
|
|
|
|
flush: 'post',
|
|
|
|
|
});
|
2021-11-12 14:53:10 +00:00
|
|
|
|
}
|