Skip to content

Commit

Permalink
feat: tooltip content wrapper and resize observer
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieljablonski committed Mar 29, 2023
1 parent 7d81d09 commit aa70472
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
14 changes: 5 additions & 9 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const Tooltip = ({
afterHide,
// props handled by controller
content,
contentRef,
contentWrapperRef,
isOpen,
setIsOpen,
activeAnchor,
Expand Down Expand Up @@ -476,21 +476,17 @@ const Tooltip = ({
}, [show, activeAnchor, content, externalStyles, place, offset, positionStrategy, position])

useEffect(() => {
if (!contentRef?.current) {
if (!contentWrapperRef?.current) {
return () => null
}
const contentObserver = new MutationObserver(() => {
const contentObserver = new ResizeObserver(() => {
updateTooltipPosition()
})
contentObserver.observe(contentRef.current, {
attributes: true,
childList: true,
subtree: true,
})
contentObserver.observe(contentWrapperRef.current)
return () => {
contentObserver.disconnect()
}
}, [content, contentRef?.current])
}, [content, contentWrapperRef?.current])

useEffect(() => {
const anchorById = document.querySelector<HTMLElement>(`[id='${anchorId}']`)
Expand Down
2 changes: 1 addition & 1 deletion src/components/Tooltip/TooltipTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface ITooltip {
className?: string
classNameArrow?: string
content?: ChildrenType
contentRef?: RefObject<HTMLElement>
contentWrapperRef?: RefObject<HTMLDivElement>
place?: PlacesType
offset?: number
id?: string
Expand Down
10 changes: 7 additions & 3 deletions src/components/TooltipController/TooltipController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,13 @@ const TooltipController = ({
* children should be lower priority so that it can be used as the "default" content
*/
let renderedContent: ChildrenType = children
const contentRef = useRef<HTMLElement>(null)
const contentWrapperRef = useRef<HTMLDivElement>(null)
if (render) {
renderedContent = render({ ref: contentRef, content: tooltipContent ?? null, activeAnchor })
renderedContent = (
<div ref={contentWrapperRef} className="react-tooltip-content-wrapper">
{render({ content: tooltipContent ?? null, activeAnchor }) as React.ReactNode}
</div>
)
} else if (tooltipContent) {
renderedContent = tooltipContent
}
Expand All @@ -222,7 +226,7 @@ const TooltipController = ({
className,
classNameArrow,
content: renderedContent,
contentRef,
contentWrapperRef,
place: tooltipPlace,
variant: tooltipVariant,
offset: tooltipOffset,
Expand Down
8 changes: 2 additions & 6 deletions src/components/TooltipController/TooltipControllerTypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CSSProperties, RefObject } from 'react'
import type { CSSProperties } from 'react'

import type {
PlacesType,
Expand All @@ -19,11 +19,7 @@ export interface ITooltipController {
* @deprecated Use `children` or `render` instead
*/
html?: string
render?: (render: {
ref: RefObject<HTMLElement>
content: string | null
activeAnchor: HTMLElement | null
}) => ChildrenType
render?: (render: { content: string | null; activeAnchor: HTMLElement | null }) => ChildrenType
place?: PlacesType
offset?: number
id?: string
Expand Down

0 comments on commit aa70472

Please sign in to comment.