Skip to content

Commit 69517fc

Browse files
committed
fix: use onMouseEnter instead to calculate isTextTruncated
1 parent 35e8bfd commit 69517fc

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

src/Common/Tooltip/Tooltip.tsx

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useState, cloneElement, useRef, useEffect } from 'react'
1+
import { useState, cloneElement } from 'react'
22
import TippyJS from '@tippyjs/react'
33
import { TooltipProps } from './types'
44

@@ -11,43 +11,27 @@ const Tooltip = ({
1111
...rest
1212
}: TooltipProps) => {
1313
const [isTextTruncated, setIsTextTruncated] = useState(false)
14-
const nodeRef = useRef<HTMLElement>(null)
1514

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) {
3419
setIsTextTruncated(true)
35-
}
36-
if (!hasTextOverflown && isTextTruncated) {
20+
} else if (!isTextOverflowing && isTextTruncated) {
3721
setIsTextTruncated(false)
3822
}
39-
})
23+
}
4024

4125
return (!isTextTruncated || !showOnTruncate) && !alwaysShowTippyOnHover ? (
42-
cloneElement(child, { ...child.props, ref: refCallback })
26+
cloneElement(child, { ...child.props, onMouseEnter: handleMouseEnterEvent })
4327
) : (
4428
<TippyJS
4529
arrow={false}
4630
placement="top"
4731
{...rest}
4832
className={`default-tt ${wordBreak ? 'dc__word-break-all' : ''} dc__mxw-200 ${rest.className}`}
4933
>
50-
{cloneElement(child, { ...child.props, ref: refCallback })}
34+
{cloneElement(child, { ...child.props, onMouseEnter: handleMouseEnterEvent })}
5135
</TippyJS>
5236
)
5337
}

0 commit comments

Comments
 (0)