@ -1,46 +1,79 @@
< script lang = "ts" setup >
< script lang = "ts" setup >
import { onKeyStroke } from '@vueuse/core'
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 {
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
/ / 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
modifierKey ? : string
wrapperClass ? : string
tooltipStyle ? : CSSProperties
}
}
const { modifierKey , wrapperClass } = defineProps < Props > ( )
const { modifierKey , tooltipStyle } = defineProps < Props > ( )
const el = ref ( )
const showTooltip = ref ( false )
const showTooltip = ref ( false )
const isMouseOver = ref ( fals e )
const isHovering = useElementHover ( ( ) => el . valu e )
if ( modifierKey ) {
const isKeyPressed = ref ( false )
onKeyStroke ( modifierKey , ( e ) => {
onKeyStroke (
( e ) => e . key === modifierKey ,
( e ) => {
e . preventDefault ( )
e . preventDefault ( )
if ( modifierKey && isMouseOver . value ) {
if ( modifierKey && isHovering . value ) {
showTooltip . value = true
showTooltip . value = true
}
}
} )
isKeyPressed . value = true
} ,
{ eventName : 'keydown' } ,
)
onKeyStroke (
( e ) => e . key === modifierKey ,
( e ) => {
e . preventDefault ( )
if ( modifierKey ) {
showTooltip . value = false
}
}
watch ( isMouseOver , ( val ) => {
isKeyPressed . value = false
if ( ! val ) {
} ,
{ eventName : 'keyup' } ,
)
watch ( [ isHovering , ( ) => modifierKey ] , ( [ hovering , key ] ) => {
if ( ! hovering ) {
showTooltip . value = false
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
/ / 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
showTooltip . value = true
}
}
} )
} )
< / script >
< / script >
< template >
< template >
< a -tooltip v -model :visible ="showTooltip" :trigger ="[]" >
< a -tooltip v -model :visible ="showTooltip" :overlay-style ="tooltipStyle" : trigger ="[]" >
< template # title >
< template # title >
< slot name = "title" / >
< slot name = "title" / >
< / template >
< / 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 / >
< slot / >
< / div >
< / div >
< / a - t o o l t i p >
< / a - t o o l t i p >