diff options
| author | かっこかり <67428053+kakkokari-gtyih@users.noreply.github.com> | 2025-05-10 07:58:26 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-10 07:58:26 +0900 |
| commit | e1cd7c94fb13f8e49667b17554d22ce8de627a2a (patch) | |
| tree | a064c4b0937160cf1e26697dcfe18de8f2eb0144 /packages/frontend/src/composables/use-tooltip.ts | |
| parent | Bump version to 2025.5.1-alpha.0 (diff) | |
| download | misskey-e1cd7c94fb13f8e49667b17554d22ce8de627a2a.tar.gz misskey-e1cd7c94fb13f8e49667b17554d22ce8de627a2a.tar.bz2 misskey-e1cd7c94fb13f8e49667b17554d22ce8de627a2a.zip | |
refactor(frontend): use* 関数の格納場所のフォルダ名を composables に変更 (#16004)
* refactor(frontend): use* 関数の格納場所を正式名称(composables)に変更
* migrate
* move useLoading
Diffstat (limited to 'packages/frontend/src/composables/use-tooltip.ts')
| -rw-r--r-- | packages/frontend/src/composables/use-tooltip.ts | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/packages/frontend/src/composables/use-tooltip.ts b/packages/frontend/src/composables/use-tooltip.ts new file mode 100644 index 0000000000..af76a3a1e8 --- /dev/null +++ b/packages/frontend/src/composables/use-tooltip.ts @@ -0,0 +1,106 @@ +/* + * SPDX-FileCopyrightText: syuilo and misskey-project + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { ref, watch, onUnmounted } from 'vue'; +import type { Ref } from 'vue'; + +export function useTooltip( + elRef: Ref<HTMLElement | { $el: HTMLElement } | null | undefined>, + onShow: (showing: Ref<boolean>) => void, + delay = 300, +): void { + let isHovering = false; + + // iOS(Androidも?)では、要素をタップした直後に(おせっかいで)mouseoverイベントを発火させたりするため、それを無視するためのフラグ + // 無視しないと、画面に触れてないのにツールチップが出たりし、ユーザビリティが損なわれる + // TODO: 一度でもタップすると二度とマウスでツールチップ出せなくなるのをどうにかする 定期的にfalseに戻すとか...? + let shouldIgnoreMouseover = false; + + let timeoutId: number; + + let changeShowingState: (() => void) | null; + + let autoHidingTimer; + + const open = () => { + close(); + if (!isHovering) return; + if (elRef.value == null) return; + const el = elRef.value instanceof Element ? elRef.value : elRef.value.$el; + if (!window.document.body.contains(el)) return; // openしようとしたときに既に元要素がDOMから消えている場合があるため + + const showing = ref(true); + onShow(showing); + changeShowingState = () => { + showing.value = false; + }; + + autoHidingTimer = window.setInterval(() => { + if (elRef.value == null || !window.document.body.contains(elRef.value instanceof Element ? elRef.value : elRef.value.$el)) { + if (!isHovering) return; + isHovering = false; + window.clearTimeout(timeoutId); + close(); + window.clearInterval(autoHidingTimer); + } + }, 1000); + }; + + const close = () => { + if (changeShowingState != null) { + changeShowingState(); + changeShowingState = null; + } + }; + + const onMouseover = () => { + if (isHovering) return; + if (shouldIgnoreMouseover) return; + isHovering = true; + timeoutId = window.setTimeout(open, delay); + }; + + const onMouseleave = () => { + if (!isHovering) return; + isHovering = false; + window.clearTimeout(timeoutId); + window.clearInterval(autoHidingTimer); + close(); + }; + + const onTouchstart = () => { + shouldIgnoreMouseover = true; + if (isHovering) return; + isHovering = true; + timeoutId = window.setTimeout(open, delay); + }; + + const onTouchend = () => { + if (!isHovering) return; + isHovering = false; + window.clearTimeout(timeoutId); + window.clearInterval(autoHidingTimer); + close(); + }; + + 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 }); + el.addEventListener('click', close, { passive: true }); + } + }, { + immediate: true, + flush: 'post', + }); + + onUnmounted(() => { + close(); + }); +} |