1
- import { useCallback , useState , cloneElement } from 'react'
1
+ import { useState , cloneElement } from 'react'
2
2
import TippyJS from '@tippyjs/react'
3
3
import { TooltipProps } from './types'
4
4
@@ -12,25 +12,26 @@ const Tooltip = ({
12
12
} : TooltipProps ) => {
13
13
const [ isTextTruncated , setIsTextTruncated ] = useState ( false )
14
14
15
- const refCallback = useCallback ( ( node : HTMLDivElement ) => {
16
- if ( node ) {
17
- // NOTE: for line-clamp we need to check scrollHeight against clientHeight since orientation
18
- // is set to vertical through -webkit-box-orient prop that is needed for line-clamp to work
19
- // see: https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp
20
- setIsTextTruncated ( node . scrollWidth > node . clientWidth || node . scrollHeight > node . clientHeight )
15
+ const handleMouseEnterEvent : React . MouseEventHandler = ( event ) => {
16
+ const { currentTarget : node } = event
17
+ const isTextOverflowing = node . scrollWidth > node . clientWidth || node . scrollHeight > node . clientHeight
18
+ if ( isTextOverflowing && ! isTextTruncated ) {
19
+ setIsTextTruncated ( true )
20
+ } else if ( ! isTextOverflowing && isTextTruncated ) {
21
+ setIsTextTruncated ( false )
21
22
}
22
- } , [ ] )
23
+ }
23
24
24
25
return ( ! isTextTruncated || ! showOnTruncate ) && ! alwaysShowTippyOnHover ? (
25
- cloneElement ( child , { ...child . props , ref : refCallback } )
26
+ cloneElement ( child , { ...child . props , onMouseEnter : handleMouseEnterEvent } )
26
27
) : (
27
28
< TippyJS
28
29
arrow = { false }
29
30
placement = "top"
30
31
{ ...rest }
31
32
className = { `default-tt ${ wordBreak ? 'dc__word-break-all' : '' } dc__mxw-200-imp ${ rest . className } ` }
32
33
>
33
- { cloneElement ( child , { ...child . props , ref : refCallback } ) }
34
+ { cloneElement ( child , { ...child . props , onMouseEnter : handleMouseEnterEvent } ) }
34
35
</ TippyJS >
35
36
)
36
37
}
0 commit comments