|
1 | | -import React, {Children, useEffect, useState, useMemo, type ForwardRefExoticComponent, useRef} from 'react' |
2 | | -import {useId, useOnEscapePress, useIsMacOS, useMergedRefs} from '../hooks' |
| 1 | +import React, {Children, useEffect, useRef, useState, useMemo, type ForwardRefExoticComponent} from 'react' |
| 2 | +import {useId, useProvidedRefOrCreate, useOnEscapePress, useIsMacOS} from '../hooks' |
3 | 3 | import {invariant} from '../utils/invariant' |
4 | 4 | import {warning} from '../utils/warning' |
5 | 5 | import {getAnchoredPosition} from '@primer/behaviors' |
@@ -50,7 +50,7 @@ type TriggerPropsType = Pick< |
50 | 50 | | 'onTouchCancel' |
51 | 51 | | 'onTouchEnd' |
52 | 52 | > & { |
53 | | - ref?: React.Ref<HTMLElement> |
| 53 | + ref?: React.RefObject<HTMLElement> |
54 | 54 | } |
55 | 55 |
|
56 | 56 | // map tooltip direction to anchoredPosition props |
@@ -126,8 +126,7 @@ export const Tooltip: ForwardRefExoticComponent< |
126 | 126 | ) => { |
127 | 127 | const tooltipId = useId(id) |
128 | 128 | const child = Children.only(children) |
129 | | - const triggerRef = useRef<HTMLElement>(null) |
130 | | - const mergedTriggerRef = useMergedRefs(triggerRef, forwardedRef) |
| 129 | + const triggerRef = useProvidedRefOrCreate(forwardedRef as React.RefObject<HTMLElement>) |
131 | 130 | const tooltipElRef = useRef<HTMLDivElement>(null) |
132 | 131 |
|
133 | 132 | const [calculatedDirection, setCalculatedDirection] = useState<TooltipDirection>(direction) |
@@ -285,7 +284,8 @@ export const Tooltip: ForwardRefExoticComponent< |
285 | 284 | {React.isValidElement(child) && |
286 | 285 | // eslint-disable-next-line react-hooks/refs |
287 | 286 | React.cloneElement(child as React.ReactElement<TriggerPropsType>, { |
288 | | - ref: mergedTriggerRef, |
| 287 | + // @ts-expect-error it needs a non nullable ref |
| 288 | + ref: triggerRef, |
289 | 289 | // If it is a type description, we use tooltip to describe the trigger |
290 | 290 | 'aria-describedby': (() => { |
291 | 291 | // If tooltip is not a description type, keep the original aria-describedby |
|
0 commit comments