@@ -7,13 +7,14 @@ import {
77 safePolygon ,
88 shift ,
99 useClick ,
10+ useDismiss ,
1011 useFloating ,
1112 useHover ,
1213 useInteractions ,
1314 useRole ,
1415} from '@floating-ui/react'
1516import { Transition } from '@headlessui/react'
16- import { ReactNode , useEffect , useRef , useState } from 'react'
17+ import { ReactNode , useRef , useState } from 'react'
1718
1819// See https://floating-ui.com/docs/react-dom
1920
@@ -41,7 +42,6 @@ export function Tooltip(props: {
4142
4243 const arrowRef = useRef ( null )
4344 const [ open , setOpen ] = useState ( false )
44- const touchOpenRef = useRef ( false ) // tracks whether open was triggered by touch
4545
4646 const { x, y, refs, strategy, context} = useFloating ( {
4747 open,
@@ -51,61 +51,26 @@ export function Tooltip(props: {
5151 middleware : [ offset ( 8 ) , flip ( ) , shift ( { padding : 4 } ) , arrow ( { element : arrowRef } ) ] ,
5252 } )
5353
54- // Close tooltip when tapping outside on touch devices
55- useEffect ( ( ) => {
56- if ( ! open || ! touchOpenRef . current ) return
57-
58- const handleOutsideTouch = ( e : TouchEvent ) => {
59- const ref = refs . reference . current as Element | null
60- const floating = refs . floating . current as Element | null
61- if (
62- ref &&
63- ! ref . contains ( e . target as Node ) &&
64- floating &&
65- ! floating . contains ( e . target as Node )
66- ) {
67- setOpen ( false )
68- touchOpenRef . current = false
69- }
70- }
71-
72- document . addEventListener ( 'touchstart' , handleOutsideTouch , { passive : true } )
73- return ( ) => document . removeEventListener ( 'touchstart' , handleOutsideTouch )
74- } , [ open , refs . reference , refs . floating ] )
75-
7654 const { getReferenceProps, getFloatingProps} = useInteractions ( [
55+ // Hover is a mouse-only concept; touch devices open via useClick below.
7756 useHover ( context , {
78- // Allow hover on all devices; touch devices will also use onTouchStart below
79- mouseOnly : noTap ,
57+ mouseOnly : true ,
8058 handleClose : hasSafePolygon ? safePolygon ( { buffer : - 0.5 } ) : null ,
8159 } ) ,
82- useClick ( context ) ,
60+ // Tap/click toggles the tooltip (persists on touch) unless tapping is disabled.
61+ useClick ( context , { enabled : ! noTap } ) ,
62+ // Tapping/clicking outside dismisses it, including on touch devices.
63+ useDismiss ( context ) ,
8364 useRole ( context , { role : 'tooltip' } ) ,
8465 ] )
8566
86- const handleTouchStart = ( e : React . TouchEvent ) => {
87- if ( noTap ) return
88- e . stopPropagation ( )
89- const next = ! open
90- touchOpenRef . current = next
91- setOpen ( next )
92- }
93-
9467 return text ? (
9568 < >
9669 < span
9770 data-testid = { testId }
9871 suppressHydrationWarning = { suppressHydrationWarning }
9972 className = { className }
10073 ref = { refs . setReference as any }
101- onMouseEnter = { ( ) => {
102- touchOpenRef . current = false
103- setOpen ( true )
104- } }
105- onMouseLeave = { ( ) => {
106- if ( ! touchOpenRef . current ) setOpen ( false )
107- } }
108- onTouchStart = { handleTouchStart }
10974 { ...getReferenceProps ( ) }
11075 >
11176 { children }
0 commit comments