@ -1,46 +1,79 @@
< script lang = "ts" setup >
import { onKeyStroke } from '@vueuse/core'
import { ref , watch } from '#imports'
import type { CSSProperties } from '@vue/runtime-dom'
import { ref , useElementHover , watch } from '#imports'
interface Props {
/ / K e y t o b e p r e s s e d o n h o v e r t o t r i g g e r t h e t o o l t i p
modifierKey ? : string
wrapperClass ? : string
tooltipStyle ? : CSSProperties
}
const { modifierKey , wrapperClass } = defineProps < Props > ( )
const { modifierKey , tooltipStyle } = defineProps < Props > ( )
const el = ref ( )
const showTooltip = ref ( false )
const isMouseOver = ref ( false )
const isHovering = useElementHover ( ( ) => el . value )
const isKeyPressed = ref ( false )
if ( modifierKey ) {
onKeyStroke ( modifierKey , ( e ) => {
onKeyStroke (
( e ) => e . key === modifierKey ,
( e ) => {
e . preventDefault ( )
if ( modifierKey && isMouseOver . value ) {
if ( modifierKey && isHovering . value ) {
showTooltip . value = true
}
} )
}
watch ( isMouseOver , ( val ) => {
if ( ! val ) {
isKeyPressed . value = true
} ,
{ eventName : 'keydown' } ,
)
onKeyStroke (
( e ) => e . key === modifierKey ,
( e ) => {
e . preventDefault ( )
if ( modifierKey ) {
showTooltip . value = false
}
isKeyPressed . value = false
} ,
{ eventName : 'keyup' } ,
)
watch ( [ isHovering , ( ) => modifierKey ] , ( [ hovering , key ] ) => {
if ( ! hovering ) {
showTooltip . value = false
}
/ / S h o w t o o l t i p o n m o u s e o v e r i f n o m o d i f i e r k e y i s p r o v i d e d
if ( val && ! modifierKey ) {
if ( hovering && ! key ) {
showTooltip . value = true
}
/ / W h i l e h o v e r i n g i f t h e m o d i f i e r k e y w a s c h a n g e d a n d t h e k e y i s n o t p r e s s e d , h i d e t o o l t i p
if ( hovering && key && ! isKeyPressed . value ) {
showTooltip . value = false
}
/ / W h e n m o u s e l e a v e s t h e e l e m e n t , t h e n r e - e n t e r s t h e e l e m e n t w h i l e k e y s t a y s p r e s s e d , s h o w t h e t o o l t i p
if ( ! showTooltip . value && hovering && key && isKeyPressed . value ) {
showTooltip . value = true
}
} )
< / script >
< template >
< a -tooltip v -model :visible ="showTooltip" :trigger ="[]" >
< a -tooltip v -model :visible ="showTooltip" :overlay-style ="tooltipStyle" : trigger ="[]" >
< template # title >
< slot name = "title" / >
< / template >
< div class = "w-full" :class ="wrapperClass" @ mouseenter = "isMouseOver = true" @ mouseleave = "isMouseOver = false" >
< div ref = "el" class = "w-full" :class ="$attrs.class" :style ="$attrs.style" >
< slot / >
< / div >
< / a - t o o l t i p >