1
- import { useCallback , useState , cloneElement , useRef , useEffect } from 'react'
1
+ import { useState , cloneElement } from 'react'
2
2
import TippyJS from '@tippyjs/react'
3
3
import { TooltipProps } from './types'
4
4
@@ -11,43 +11,27 @@ const Tooltip = ({
11
11
...rest
12
12
} : TooltipProps ) => {
13
13
const [ isTextTruncated , setIsTextTruncated ] = useState ( false )
14
- const nodeRef = useRef < HTMLElement > ( null )
15
14
16
- const refCallback = useCallback ( ( node : HTMLElement ) => {
17
- if ( node ) {
18
- // NOTE: for line-clamp we need to check scrollHeight against clientHeight since orientation
19
- // is set to vertical through -webkit-box-orient prop that is needed for line-clamp to work
20
- // see: https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp
21
- nodeRef . current = node
22
- setIsTextTruncated ( node . scrollWidth > node . clientWidth || node . scrollHeight > node . clientHeight )
23
- }
24
- } , [ ] )
25
-
26
- useEffect ( ( ) => {
27
- if ( ! nodeRef . current ) {
28
- return
29
- }
30
- const hasTextOverflown =
31
- nodeRef . current ?. scrollWidth > nodeRef . current ?. clientWidth ||
32
- nodeRef . current ?. scrollHeight > nodeRef . current ?. clientHeight
33
- if ( hasTextOverflown && ! isTextTruncated ) {
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 ) {
34
19
setIsTextTruncated ( true )
35
- }
36
- if ( ! hasTextOverflown && isTextTruncated ) {
20
+ } else if ( ! isTextOverflowing && isTextTruncated ) {
37
21
setIsTextTruncated ( false )
38
22
}
39
- } )
23
+ }
40
24
41
25
return ( ! isTextTruncated || ! showOnTruncate ) && ! alwaysShowTippyOnHover ? (
42
- cloneElement ( child , { ...child . props , ref : refCallback } )
26
+ cloneElement ( child , { ...child . props , onMouseEnter : handleMouseEnterEvent } )
43
27
) : (
44
28
< TippyJS
45
29
arrow = { false }
46
30
placement = "top"
47
31
{ ...rest }
48
32
className = { `default-tt ${ wordBreak ? 'dc__word-break-all' : '' } dc__mxw-200 ${ rest . className } ` }
49
33
>
50
- { cloneElement ( child , { ...child . props , ref : refCallback } ) }
34
+ { cloneElement ( child , { ...child . props , onMouseEnter : handleMouseEnterEvent } ) }
51
35
</ TippyJS >
52
36
)
53
37
}
0 commit comments