Skip to content

Commit 6a9a01d

Browse files
refactor: useRef() for lastFloatPosition
1 parent c1d9ca3 commit 6a9a01d

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { computeTooltipPosition } from '../../utils/compute-positions'
77
import styles from './styles.module.css'
88
import type { IPosition, ITooltip } from './TooltipTypes'
99

10-
let lastFloatPosition: IPosition | null = null
11-
1210
const Tooltip = ({
1311
// props
1412
id,
@@ -42,6 +40,7 @@ const Tooltip = ({
4240
const [inlineArrowStyles, setInlineArrowStyles] = useState({})
4341
const [show, setShow] = useState<boolean>(false)
4442
const [calculatingPosition, setCalculatingPosition] = useState(false)
43+
const lastFloatPosition = useRef<IPosition | null>(null)
4544
const { anchorRefs, setActiveAnchor: setProviderActiveAnchor } = useTooltip()(id)
4645
const [activeAnchor, setActiveAnchor] = useState<React.RefObject<HTMLElement>>({ current: null })
4746

@@ -148,7 +147,7 @@ const Tooltip = ({
148147
y: mouseEvent.clientY,
149148
}
150149
handleTooltipPosition(mousePosition)
151-
lastFloatPosition = mousePosition
150+
lastFloatPosition.current = mousePosition
152151
}
153152

154153
const handleClickTooltipAnchor = () => {
@@ -235,15 +234,15 @@ const Tooltip = ({
235234
}
236235

237236
if (float) {
238-
if (lastFloatPosition) {
237+
if (lastFloatPosition.current) {
239238
/*
240239
Without this, changes to `content`, `place`, `offset`, ..., will only
241240
trigger a position calculation after a `mousemove` event.
242241
243242
To see why this matters, comment this line, run `yarn dev` and click the
244243
"Hover me!" anchor.
245244
*/
246-
handleTooltipPosition(lastFloatPosition)
245+
handleTooltipPosition(lastFloatPosition.current)
247246
}
248247
// if `float` is set, override regular positioning
249248
return () => null

0 commit comments

Comments
 (0)