Skip to content

Commit b9042ac

Browse files
committed
fix: recalculate text truncation logic on re-renders
1 parent 7cfd923 commit b9042ac

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Common/Tooltip/Tooltip.tsx

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

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

15-
const refCallback = useCallback((node: HTMLDivElement) => {
16+
const refCallback = useCallback((node: HTMLElement) => {
1617
if (node) {
1718
// NOTE: for line-clamp we need to check scrollHeight against clientHeight since orientation
1819
// is set to vertical through -webkit-box-orient prop that is needed for line-clamp to work
1920
// see: https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp
21+
nodeRef.current = node
2022
setIsTextTruncated(node.scrollWidth > node.clientWidth || node.scrollHeight > node.clientHeight)
2123
}
2224
}, [])
2325

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+
2441
return (!isTextTruncated || !showOnTruncate) && !alwaysShowTippyOnHover ? (
2542
cloneElement(child, { ...child.props, ref: refCallback })
2643
) : (

0 commit comments

Comments
 (0)