Skip to content

Commit

Permalink
feat: forward ref to be used on render
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieljablonski committed Mar 28, 2023
1 parent 1015a07 commit 223b78d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
24 changes: 23 additions & 1 deletion src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const Tooltip = ({
afterHide,
// props handled by controller
content,
contentRef,
isOpen,
setIsOpen,
activeAnchor,
Expand Down Expand Up @@ -425,7 +426,7 @@ const Tooltip = ({
}
}, [id, anchorSelect, activeAnchor])

useEffect(() => {
const updateTooltipPosition = () => {
if (position) {
// if `position` is set, override regular and `float` positioning
handleTooltipPosition(position)
Expand Down Expand Up @@ -468,8 +469,29 @@ const Tooltip = ({
}
setActualPlacement(computedStylesData.place as PlacesType)
})
}

useEffect(() => {
updateTooltipPosition()
}, [show, activeAnchor, content, externalStyles, place, offset, positionStrategy, position])

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

useEffect(() => {
const anchorById = document.querySelector<HTMLElement>(`[id='${anchorId}']`)
const anchors = [...anchorsBySelect, anchorById]
Expand Down
3 changes: 2 additions & 1 deletion src/components/Tooltip/TooltipTypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ElementType, ReactNode, CSSProperties } from 'react'
import type { ElementType, ReactNode, CSSProperties, RefObject } from 'react'

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

Expand Down Expand Up @@ -40,6 +40,7 @@ export interface ITooltip {
className?: string
classNameArrow?: string
content?: ChildrenType
contentRef?: RefObject<HTMLElement>
place?: PlacesType
offset?: number
id?: string
Expand Down
6 changes: 4 additions & 2 deletions src/components/TooltipController/TooltipController.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react'
import { useEffect, useRef, useState } from 'react'
import { Tooltip } from 'components/Tooltip'
import type {
EventsType,
Expand Down Expand Up @@ -205,8 +205,9 @@ 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)
if (render) {
renderedContent = render({ content: tooltipContent ?? null, activeAnchor })
renderedContent = render({ ref: contentRef, content: tooltipContent ?? null, activeAnchor })
} else if (tooltipContent) {
renderedContent = tooltipContent
}
Expand All @@ -221,6 +222,7 @@ const TooltipController = ({
className,
classNameArrow,
content: renderedContent,
contentRef,
place: tooltipPlace,
variant: tooltipVariant,
offset: tooltipOffset,
Expand Down
8 changes: 6 additions & 2 deletions src/components/TooltipController/TooltipControllerTypes.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CSSProperties } from 'react'
import type { CSSProperties, RefObject } from 'react'

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

0 comments on commit 223b78d

Please sign in to comment.