1
- import { useCallback , useState , cloneElement } from 'react'
1
+ import { useCallback , useState , cloneElement , useRef , useEffect } from 'react'
2
2
import TippyJS from '@tippyjs/react'
3
3
import { TooltipProps } from './types'
4
4
@@ -11,16 +11,33 @@ const Tooltip = ({
11
11
...rest
12
12
} : TooltipProps ) => {
13
13
const [ isTextTruncated , setIsTextTruncated ] = useState ( false )
14
+ const nodeRef = useRef < HTMLElement > ( null )
14
15
15
- const refCallback = useCallback ( ( node : HTMLDivElement ) => {
16
+ const refCallback = useCallback ( ( node : HTMLElement ) => {
16
17
if ( node ) {
17
18
// NOTE: for line-clamp we need to check scrollHeight against clientHeight since orientation
18
19
// is set to vertical through -webkit-box-orient prop that is needed for line-clamp to work
19
20
// see: https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp
21
+ nodeRef . current = node
20
22
setIsTextTruncated ( node . scrollWidth > node . clientWidth || node . scrollHeight > node . clientHeight )
21
23
}
22
24
} , [ ] )
23
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 ) {
34
+ setIsTextTruncated ( true )
35
+ }
36
+ if ( ! hasTextOverflown && isTextTruncated ) {
37
+ setIsTextTruncated ( false )
38
+ }
39
+ } )
40
+
24
41
return ( ! isTextTruncated || ! showOnTruncate ) && ! alwaysShowTippyOnHover ? (
25
42
cloneElement ( child , { ...child . props , ref : refCallback } )
26
43
) : (
0 commit comments