2023-07-27 07:31:52 +02:00
|
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-24 04:34:24 +01:00
|
|
|
|
import { Ref, ref, watch, onUnmounted } from 'vue';
|
2021-11-12 15:53:10 +01:00
|
|
|
|
|
2021-12-05 12:01:52 +01:00
|
|
|
|
export function useTooltip(
|
|
|
|
|
elRef: Ref<HTMLElement | { $el: HTMLElement } | null | undefined>,
|
|
|
|
|
onShow: (showing: Ref<boolean>) => void,
|
2022-06-30 16:51:18 +02:00
|
|
|
|
delay = 300,
|
2021-12-05 12:01:52 +01:00
|
|
|
|
): void {
|
2021-11-12 15:53:10 +01:00
|
|
|
|
let isHovering = false;
|
2021-12-05 12:01:52 +01:00
|
|
|
|
|
|
|
|
|
// iOS(Androidも?)では、要素をタップした直後に(おせっかいで)mouseoverイベントを発火させたりするため、それを無視するためのフラグ
|
|
|
|
|
// 無視しないと、画面に触れてないのにツールチップが出たりし、ユーザビリティが損なわれる
|
|
|
|
|
// TODO: 一度でもタップすると二度とマウスでツールチップ出せなくなるのをどうにかする 定期的にfalseに戻すとか...?
|
|
|
|
|
let shouldIgnoreMouseover = false;
|
|
|
|
|
|
2021-11-12 15:53:10 +01:00
|
|
|
|
let timeoutId: number;
|
|
|
|
|
|
|
|
|
|
let changeShowingState: (() => void) | null;
|
|
|
|
|
|
2023-10-03 04:27:51 +02:00
|
|
|
|
let autoHidingTimer;
|
|
|
|
|
|
2021-11-12 15:53:10 +01:00
|
|
|
|
const open = () => {
|
|
|
|
|
close();
|
|
|
|
|
if (!isHovering) return;
|
2021-12-23 09:05:26 +01:00
|
|
|
|
if (elRef.value == null) return;
|
|
|
|
|
const el = elRef.value instanceof Element ? elRef.value : elRef.value.$el;
|
|
|
|
|
if (!document.body.contains(el)) return; // openしようとしたときに既に元要素がDOMから消えている場合があるため
|
2021-11-12 15:53:10 +01:00
|
|
|
|
|
|
|
|
|
const showing = ref(true);
|
|
|
|
|
onShow(showing);
|
|
|
|
|
changeShowingState = () => {
|
|
|
|
|
showing.value = false;
|
|
|
|
|
};
|
2023-10-03 04:27:51 +02:00
|
|
|
|
|
|
|
|
|
autoHidingTimer = window.setInterval(() => {
|
2023-11-02 11:59:18 +01:00
|
|
|
|
if (elRef.value == null || !document.body.contains(elRef.value instanceof Element ? elRef.value : elRef.value.$el)) {
|
2023-10-03 04:27:51 +02:00
|
|
|
|
if (!isHovering) return;
|
|
|
|
|
isHovering = false;
|
|
|
|
|
window.clearTimeout(timeoutId);
|
|
|
|
|
close();
|
|
|
|
|
window.clearInterval(autoHidingTimer);
|
|
|
|
|
}
|
|
|
|
|
}, 1000);
|
2021-11-12 15:53:10 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const close = () => {
|
|
|
|
|
if (changeShowingState != null) {
|
|
|
|
|
changeShowingState();
|
|
|
|
|
changeShowingState = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onMouseover = () => {
|
|
|
|
|
if (isHovering) return;
|
2021-12-05 12:01:52 +01:00
|
|
|
|
if (shouldIgnoreMouseover) return;
|
2021-11-12 15:53:10 +01:00
|
|
|
|
isHovering = true;
|
2022-06-30 16:51:18 +02:00
|
|
|
|
timeoutId = window.setTimeout(open, delay);
|
2021-11-12 15:53:10 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onMouseleave = () => {
|
|
|
|
|
if (!isHovering) return;
|
|
|
|
|
isHovering = false;
|
|
|
|
|
window.clearTimeout(timeoutId);
|
2023-10-03 04:27:51 +02:00
|
|
|
|
window.clearInterval(autoHidingTimer);
|
2021-11-12 15:53:10 +01:00
|
|
|
|
close();
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-05 12:01:52 +01:00
|
|
|
|
const onTouchstart = () => {
|
|
|
|
|
shouldIgnoreMouseover = true;
|
|
|
|
|
if (isHovering) return;
|
|
|
|
|
isHovering = true;
|
2022-06-30 16:51:18 +02:00
|
|
|
|
timeoutId = window.setTimeout(open, delay);
|
2021-12-05 12:01:52 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onTouchend = () => {
|
|
|
|
|
if (!isHovering) return;
|
|
|
|
|
isHovering = false;
|
|
|
|
|
window.clearTimeout(timeoutId);
|
2023-10-03 04:27:51 +02:00
|
|
|
|
window.clearInterval(autoHidingTimer);
|
2021-12-05 12:01:52 +01:00
|
|
|
|
close();
|
2021-11-12 15:53:10 +01:00
|
|
|
|
};
|
2021-12-05 12:01:52 +01: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 });
|
2021-12-24 04:34:24 +01:00
|
|
|
|
el.addEventListener('click', close, { passive: true });
|
2021-12-05 12:01:52 +01:00
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
immediate: true,
|
|
|
|
|
flush: 'post',
|
|
|
|
|
});
|
2021-12-24 04:34:24 +01:00
|
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
close();
|
|
|
|
|
});
|
2021-11-12 15:53:10 +01:00
|
|
|
|
}
|