Skip to content

Commit 9d79766

Browse files
gabrieljablonskidanielbarion
authored andcommitted
fix race condition and tooltip data sync
1 parent 45c4fb8 commit 9d79766

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import classNames from 'classnames'
33
import debounce from 'utils/debounce'
44
import { TooltipContent } from 'components/TooltipContent'
55
import { useTooltip } from 'components/TooltipProvider'
6-
import { computeToolTipPosition } from '../../utils/compute-positions'
6+
import { computeTooltipPosition } from '../../utils/compute-positions'
77
import styles from './styles.module.css'
88
import type { ITooltip } from './TooltipTypes'
99

@@ -155,16 +155,24 @@ const Tooltip = ({
155155
}, [anchorRefs, anchorId, events, delayHide, delayShow])
156156

157157
useEffect(() => {
158-
const elementReference = activeAnchor.current
159-
160-
computeToolTipPosition({
158+
let elementReference = activeAnchor.current
159+
if (anchorId) {
160+
// `anchorId` element takes precedence
161+
elementReference = document.querySelector(`[id='${anchorId}']`) as HTMLElement
162+
}
163+
let mounted = true
164+
computeTooltipPosition({
161165
place,
162166
offset,
163167
elementReference,
164168
tooltipReference: tooltipRef.current,
165169
tooltipArrowReference: tooltipArrowRef.current,
166170
strategy: positionStrategy,
167171
}).then((computedStylesData) => {
172+
if (!mounted) {
173+
// invalidate computed positions after unmount
174+
return
175+
}
168176
if (Object.keys(computedStylesData.tooltipStyles).length) {
169177
setInlineStyles(computedStylesData.tooltipStyles)
170178
}
@@ -173,7 +181,10 @@ const Tooltip = ({
173181
setInlineArrowStyles(computedStylesData.tooltipArrowStyles)
174182
}
175183
})
176-
}, [show, isOpen, activeAnchor])
184+
return () => {
185+
mounted = false
186+
}
187+
}, [show, isOpen, anchorId, activeAnchor, place, offset, positionStrategy])
177188

178189
useEffect(() => {
179190
return () => {

src/components/TooltipController/TooltipController.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ const TooltipController = ({
161161
observer.observe(ref.current, observerConfig)
162162
})
163163

164-
if (anchorById) {
165-
const dataAttributes = getDataAttributesFromAnchorElement(anchorById)
164+
const element = activeAnchor.current ?? anchorById
165+
166+
if (element) {
167+
const dataAttributes = getDataAttributesFromAnchorElement(element)
166168
applyAllDataAttributesFromAnchorElement(dataAttributes)
167169
}
168170

src/test/index.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import renderer from 'react-test-renderer'
22
import debounce from 'utils/debounce'
3-
import { computeToolTipPosition } from 'utils/compute-positions'
3+
import { computeTooltipPosition } from 'utils/compute-positions'
44
import { TooltipController as Tooltip } from '../components/TooltipController'
55

66
// Tell Jest to mock all timeout functions
@@ -58,7 +58,7 @@ describe('tooltip props', () => {
5858

5959
describe('compute positions', () => {
6060
test('empty reference elements', async () => {
61-
const value = await computeToolTipPosition({
61+
const value = await computeTooltipPosition({
6262
elementReference: null,
6363
tooltipReference: null,
6464
tooltipArrowReference: null,
@@ -69,7 +69,7 @@ describe('compute positions', () => {
6969

7070
test('empty tooltip reference element', async () => {
7171
const element = document.createElement('div')
72-
const value = await computeToolTipPosition({
72+
const value = await computeTooltipPosition({
7373
elementReference: element,
7474
tooltipReference: null,
7575
tooltipArrowReference: null,
@@ -81,7 +81,7 @@ describe('compute positions', () => {
8181
test('empty tooltip arrow reference element', async () => {
8282
const element = document.createElement('div')
8383
const elementTooltip = document.createElement('div')
84-
const value = await computeToolTipPosition({
84+
const value = await computeTooltipPosition({
8585
elementReference: element,
8686
tooltipReference: elementTooltip,
8787
tooltipArrowReference: null,
@@ -100,7 +100,7 @@ describe('compute positions', () => {
100100
const element = document.createElement('div')
101101
const elementTooltip = document.createElement('div')
102102
const elementTooltipArrow = document.createElement('div')
103-
const value = await computeToolTipPosition({
103+
const value = await computeTooltipPosition({
104104
elementReference: element,
105105
tooltipReference: elementTooltip,
106106
tooltipArrowReference: elementTooltipArrow,

src/utils/compute-positions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { computePosition, offset, flip, shift, arrow } from '@floating-ui/dom'
22
import type { IComputePositions } from './compute-positions-types'
33

4-
export const computeToolTipPosition = async ({
4+
export const computeTooltipPosition = async ({
55
elementReference = null,
66
tooltipReference = null,
77
tooltipArrowReference = null,

0 commit comments

Comments
 (0)