Skip to content

Commit 11a77d5

Browse files
authored
Merge pull request #281 from devtron-labs/fix/tooltip-rerender
fix: recalculate text truncation logic on re-renders
2 parents a6664bc + 5aa15a5 commit 11a77d5

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Tooltip/Tooltip.tsx

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

@@ -12,25 +12,26 @@ const Tooltip = ({
1212
}: TooltipProps) => {
1313
const [isTextTruncated, setIsTextTruncated] = useState(false)
1414

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)
2122
}
22-
}, [])
23+
}
2324

2425
return (!isTextTruncated || !showOnTruncate) && !alwaysShowTippyOnHover ? (
25-
cloneElement(child, { ...child.props, ref: refCallback })
26+
cloneElement(child, { ...child.props, onMouseEnter: handleMouseEnterEvent })
2627
) : (
2728
<TippyJS
2829
arrow={false}
2930
placement="top"
3031
{...rest}
3132
className={`default-tt ${wordBreak ? 'dc__word-break-all' : ''} dc__mxw-200-imp ${rest.className}`}
3233
>
33-
{cloneElement(child, { ...child.props, ref: refCallback })}
34+
{cloneElement(child, { ...child.props, onMouseEnter: handleMouseEnterEvent })}
3435
</TippyJS>
3536
)
3637
}

0 commit comments

Comments
 (0)