@@ -2,7 +2,7 @@ import React from 'react'
22import { useSSRSafeId } from '@react-aria/ssr'
33import type { AnchorPosition , AnchorSide , AnchorAlignment } from '@primer/behaviors'
44import Box from '../Box'
5- import { useAnchoredPosition } from '../hooks'
5+ import { useAnchoredPosition , useProvidedRefOrCreate } from '../hooks'
66import { SxProp , merge , BetterSystemStyleObject } from '../sx'
77
88type TooltipDirection = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w'
@@ -21,7 +21,7 @@ export type TooltipProps = {
2121 /** Use aria-describedby or aria-labelledby */
2222 type ?: 'description' | 'label'
2323 /** Tooltip target */
24- children : React . ReactElement
24+ children : React . ReactElement & { ref ?: React . RefObject < HTMLElement > }
2525 /** When set to true, tooltip appears without any delay */
2626 noDelay ?: boolean
2727 /** @deprecated Always set to true now. */
@@ -55,8 +55,16 @@ export const Tooltip: React.FC<TooltipProps> = ({
5555 sx = { } ,
5656 ...props
5757} ) => {
58+ const tooltipId = useSSRSafeId ( )
59+
60+ const childRef = children . ref
61+ const anchorElementRef = useProvidedRefOrCreate ( childRef )
5862 const tooltipRef = React . useRef < HTMLDivElement > ( null )
59- const anchorElementRef = React . useRef < HTMLElement > ( null )
63+
64+ const child = React . cloneElement ( children , {
65+ ref : anchorElementRef ,
66+ [ type === 'description' ? 'aria-describedby' : 'aria-labelledby' ] : tooltipId
67+ } )
6068
6169 const { position} = useAnchoredPosition ( {
6270 side : directionToPosition [ direction ] . side ,
@@ -66,13 +74,6 @@ export const Tooltip: React.FC<TooltipProps> = ({
6674 anchorElementRef
6775 } )
6876
69- const tooltipId = useSSRSafeId ( )
70-
71- const child = React . cloneElement ( children , {
72- ref : anchorElementRef ,
73- [ type === 'description' ? 'aria-describedby' : 'aria-labelledby' ] : tooltipId
74- } )
75-
7677 const tooltipText = text || props [ 'aria-label' ]
7778
7879 return (
0 commit comments