11import type { FloatingTooltip } from '../state/floating-tooltip'
2- import { computed , defineComponent , h , ref , watchEffect } from 'vue'
2+ import { useDebounceFn } from '@vueuse/core'
3+ import { computed , defineComponent , h , ref , watch } from 'vue'
34import { useFloatingTooltip } from '../state/floating-tooltip'
45
56// @unocss -include
@@ -11,16 +12,12 @@ const FloatingTooltipComponent = defineComponent({
1112 name : 'FloatingTooltip' ,
1213 setup ( ) {
1314 const current = useFloatingTooltip ( )
14- const box = ref < FloatingTooltip > ( {
15- render : '' ,
16- width : 0 ,
17- height : 0 ,
18- left : 0 ,
19- top : 0 ,
20- } )
15+ const box = ref < FloatingTooltip | null > ( null )
2116
2217 // guess alignment of the tooltip based on viewport position
2318 const align = computed < 'bottom' | 'left' | 'right' | 'top' > ( ( ) => {
19+ if ( ! box . value )
20+ return 'bottom'
2421 const vw = window . innerWidth
2522 const vh = window . innerHeight
2623 if ( box . value . left < DETECT_MARGIN )
@@ -35,6 +32,8 @@ const FloatingTooltipComponent = defineComponent({
3532 } )
3633
3734 const style = computed ( ( ) => {
35+ if ( ! box . value )
36+ return { }
3837 switch ( align . value ) {
3938 case 'bottom' : {
4039 return {
@@ -70,14 +69,25 @@ const FloatingTooltipComponent = defineComponent({
7069 }
7170 } )
7271
73- watchEffect ( ( ) => {
74- if ( current . value ) {
75- box . value = { ...current . value }
76- }
77- } )
72+ const clearThrottled = useDebounceFn ( ( ) => {
73+ if ( current . value == null )
74+ box . value = null
75+ } , 800 )
76+
77+ watch (
78+ current ,
79+ ( value ) => {
80+ if ( value ) {
81+ box . value = { ...value }
82+ }
83+ else {
84+ clearThrottled ( )
85+ }
86+ } ,
87+ )
7888
7989 return ( ) => {
80- if ( ! box . value . render )
90+ if ( ! box . value ? .render )
8191 return null
8292
8393 const content = typeof box . value . render === 'string' ? h ( 'span' , box . value . render ) : box . value . render ( )
0 commit comments