refactor(client): use composition api for tooltip logic
This commit is contained in:
parent
0e3213ff6d
commit
4b7b51d5cc
6 changed files with 187 additions and 219 deletions
44
packages/client/src/scripts/use-tooltip.ts
Normal file
44
packages/client/src/scripts/use-tooltip.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { Ref, ref } from 'vue';
|
||||
|
||||
export function useTooltip(onShow: (showing: Ref<boolean>) => void) {
|
||||
let isHovering = false;
|
||||
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;
|
||||
isHovering = true;
|
||||
timeoutId = window.setTimeout(open, 300);
|
||||
};
|
||||
|
||||
const onMouseleave = () => {
|
||||
if (!isHovering) return;
|
||||
isHovering = false;
|
||||
window.clearTimeout(timeoutId);
|
||||
close();
|
||||
};
|
||||
|
||||
return {
|
||||
onMouseover,
|
||||
onMouseleave,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue