Skip to content

Commit aa70472

Browse files
feat: tooltip content wrapper and resize observer
1 parent 7d81d09 commit aa70472

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

src/components/Tooltip/Tooltip.tsx

+5-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const Tooltip = ({
3434
afterHide,
3535
// props handled by controller
3636
content,
37-
contentRef,
37+
contentWrapperRef,
3838
isOpen,
3939
setIsOpen,
4040
activeAnchor,
@@ -476,21 +476,17 @@ const Tooltip = ({
476476
}, [show, activeAnchor, content, externalStyles, place, offset, positionStrategy, position])
477477

478478
useEffect(() => {
479-
if (!contentRef?.current) {
479+
if (!contentWrapperRef?.current) {
480480
return () => null
481481
}
482-
const contentObserver = new MutationObserver(() => {
482+
const contentObserver = new ResizeObserver(() => {
483483
updateTooltipPosition()
484484
})
485-
contentObserver.observe(contentRef.current, {
486-
attributes: true,
487-
childList: true,
488-
subtree: true,
489-
})
485+
contentObserver.observe(contentWrapperRef.current)
490486
return () => {
491487
contentObserver.disconnect()
492488
}
493-
}, [content, contentRef?.current])
489+
}, [content, contentWrapperRef?.current])
494490

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

src/components/Tooltip/TooltipTypes.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface ITooltip {
4040
className?: string
4141
classNameArrow?: string
4242
content?: ChildrenType
43-
contentRef?: RefObject<HTMLElement>
43+
contentWrapperRef?: RefObject<HTMLDivElement>
4444
place?: PlacesType
4545
offset?: number
4646
id?: string

src/components/TooltipController/TooltipController.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -205,9 +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 contentRef = useRef<HTMLElement>(null)
208+
const contentWrapperRef = useRef<HTMLDivElement>(null)
209209
if (render) {
210-
renderedContent = render({ ref: contentRef, 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+
)
211215
} else if (tooltipContent) {
212216
renderedContent = tooltipContent
213217
}
@@ -222,7 +226,7 @@ const TooltipController = ({
222226
className,
223227
classNameArrow,
224228
content: renderedContent,
225-
contentRef,
229+
contentWrapperRef,
226230
place: tooltipPlace,
227231
variant: tooltipVariant,
228232
offset: tooltipOffset,

src/components/TooltipController/TooltipControllerTypes.d.ts

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

33
import type {
44
PlacesType,
@@ -19,11 +19,7 @@ export interface ITooltipController {
1919
* @deprecated Use `children` or `render` instead
2020
*/
2121
html?: string
22-
render?: (render: {
23-
ref: RefObject<HTMLElement>
24-
content: string | null
25-
activeAnchor: HTMLElement | null
26-
}) => ChildrenType
22+
render?: (render: { content: string | null; activeAnchor: HTMLElement | null }) => ChildrenType
2723
place?: PlacesType
2824
offset?: number
2925
id?: string

0 commit comments

Comments
 (0)