Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add styles and render as dependencies for the position calculation effect #994

Merged
merged 3 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions 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,
contentWrapperRef,
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,7 +469,24 @@ const Tooltip = ({
}
setActualPlacement(computedStylesData.place as PlacesType)
})
}, [show, activeAnchor, content, place, offset, positionStrategy, position])
}

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

useEffect(() => {
if (!contentWrapperRef?.current) {
return () => null
}
const contentObserver = new ResizeObserver(() => {
updateTooltipPosition()
})
contentObserver.observe(contentWrapperRef.current)
return () => {
contentObserver.disconnect()
}
}, [content, contentWrapperRef?.current])

useEffect(() => {
const anchorById = document.querySelector<HTMLElement>(`[id='${anchorId}']`)
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
contentWrapperRef?: RefObject<HTMLDivElement>
place?: PlacesType
offset?: number
id?: string
Expand Down
10 changes: 8 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,13 @@ const TooltipController = ({
* children should be lower priority so that it can be used as the "default" content
*/
let renderedContent: ChildrenType = children
const contentWrapperRef = useRef<HTMLDivElement>(null)
if (render) {
renderedContent = render({ 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 @@ -221,6 +226,7 @@ const TooltipController = ({
className,
classNameArrow,
content: renderedContent,
contentWrapperRef,
place: tooltipPlace,
variant: tooltipVariant,
offset: tooltipOffset,
Expand Down