import type { ComputedRef, Ref, UnwrapRef } from 'vue'; import { ref, onBeforeUnmount, watch } from 'vue'; import type { ValueTextConfig } from './useValueTexts'; import useValueTexts from './useValueTexts'; export default function useHoverValue( valueText: Ref, { formatList, generateConfig, locale }: ValueTextConfig, ): [ComputedRef, (date: DateType) => void, (immediately?: boolean) => void] { const innerValue = ref(null); const raf = ref(null); function setValue(val: DateType, immediately = false) { cancelAnimationFrame(raf.value); if (immediately) { innerValue.value = val as UnwrapRef; return; } raf.value = requestAnimationFrame(() => { innerValue.value = val as UnwrapRef; }); } const [, firstText] = useValueTexts(innerValue as Ref, { formatList, generateConfig, locale, }); function onEnter(date: DateType) { setValue(date); } function onLeave(immediately = false) { setValue(null, immediately); } watch(valueText, () => { onLeave(true); }); onBeforeUnmount(() => { cancelAnimationFrame(raf.value); }); return [firstText, onEnter, onLeave]; }