Skip to content

Commit 8774089

Browse files
authored
Merge branch 'master' into chore/dependencies-external
2 parents 9aefde0 + aae3011 commit 8774089

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

docs/docs/options.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ import 'react-tooltip/dist/react-tooltip.css'
121121
| `closeOnEsc` | `boolean` | no | `false` | `true` `false` | Pressing escape key will close the tooltip |
122122
| `style` | `CSSProperties` | no | | a React inline style | Add inline styles directly to the tooltip |
123123
| `position` | `{ x: number; y: number }` | no | | any `number` value for both `x` and `y` | Override the tooltip position on the DOM |
124-
| `isOpen` | `boolen` | no | handled by internal state | `true` `false` | The tooltip can be controlled or uncontrolled, this attribute can be used to handle show and hide tooltip outside tooltip (can be used **without** `setIsOpen`) |
124+
| `isOpen` | `boolean` | no | handled by internal state | `true` `false` | The tooltip can be controlled or uncontrolled, this attribute can be used to handle show and hide tooltip outside tooltip (can be used **without** `setIsOpen`) |
125125
| `setIsOpen` | `function` | no | | | The tooltip can be controlled or uncontrolled, this attribute can be used to handle show and hide tooltip outside tooltip |
126126
| `afterShow` | `function` | no | | | A function to be called after the tooltip is shown |
127127
| `afterHide` | `function` | no | | | A function to be called after the tooltip is hidden |

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-tooltip",
3-
"version": "5.10.1",
3+
"version": "5.10.4",
44
"description": "react tooltip component",
55
"scripts": {
66
"dev-rollup": "node ./prebuild.js --env=development && node --max_old_space_size=2048 ./node_modules/rollup/dist/bin/rollup -c rollup.config.dev.js --watch",

src/components/Tooltip/Tooltip.tsx

+20-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const Tooltip = ({
3434
afterHide,
3535
// props handled by controller
3636
content,
37+
contentWrapperRef,
3738
isOpen,
3839
setIsOpen,
3940
activeAnchor,
@@ -425,7 +426,7 @@ const Tooltip = ({
425426
}
426427
}, [id, anchorSelect, activeAnchor])
427428

428-
useEffect(() => {
429+
const updateTooltipPosition = () => {
429430
if (position) {
430431
// if `position` is set, override regular and `float` positioning
431432
handleTooltipPosition(position)
@@ -468,7 +469,24 @@ const Tooltip = ({
468469
}
469470
setActualPlacement(computedStylesData.place as PlacesType)
470471
})
471-
}, [show, activeAnchor, content, place, offset, positionStrategy, position])
472+
}
473+
474+
useEffect(() => {
475+
updateTooltipPosition()
476+
}, [show, activeAnchor, content, externalStyles, place, offset, positionStrategy, position])
477+
478+
useEffect(() => {
479+
if (!contentWrapperRef?.current) {
480+
return () => null
481+
}
482+
const contentObserver = new ResizeObserver(() => {
483+
updateTooltipPosition()
484+
})
485+
contentObserver.observe(contentWrapperRef.current)
486+
return () => {
487+
contentObserver.disconnect()
488+
}
489+
}, [content, contentWrapperRef?.current])
472490

473491
useEffect(() => {
474492
const anchorById = document.querySelector<HTMLElement>(`[id='${anchorId}']`)

src/components/Tooltip/TooltipTypes.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ElementType, ReactNode, CSSProperties } from 'react'
1+
import type { ElementType, ReactNode, CSSProperties, RefObject } from 'react'
22

33
export type PlacesType = 'top' | 'right' | 'bottom' | 'left'
44

@@ -40,6 +40,7 @@ export interface ITooltip {
4040
className?: string
4141
classNameArrow?: string
4242
content?: ChildrenType
43+
contentWrapperRef?: RefObject<HTMLDivElement>
4344
place?: PlacesType
4445
offset?: number
4546
id?: string

src/components/TooltipController/TooltipController.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from 'react'
1+
import React, { useEffect, useRef, useState } from 'react'
22
import { Tooltip } from 'components/Tooltip'
33
import type {
44
EventsType,
@@ -205,8 +205,13 @@ const TooltipController = ({
205205
* children should be lower priority so that it can be used as the "default" content
206206
*/
207207
let renderedContent: ChildrenType = children
208+
const contentWrapperRef = useRef<HTMLDivElement>(null)
208209
if (render) {
209-
renderedContent = render({ content: tooltipContent ?? null, activeAnchor })
210+
renderedContent = (
211+
<div ref={contentWrapperRef} className="react-tooltip-content-wrapper">
212+
{render({ content: tooltipContent ?? null, activeAnchor }) as React.ReactNode}
213+
</div>
214+
)
210215
} else if (tooltipContent) {
211216
renderedContent = tooltipContent
212217
}
@@ -221,6 +226,7 @@ const TooltipController = ({
221226
className,
222227
classNameArrow,
223228
content: renderedContent,
229+
contentWrapperRef,
224230
place: tooltipPlace,
225231
variant: tooltipVariant,
226232
offset: tooltipOffset,

0 commit comments

Comments
 (0)