Skip to content

Commit 3737219

Browse files
committed
Simplify tooltip logic and add dismissal support for touch devices.
1 parent f79e1dd commit 3737219

2 files changed

Lines changed: 9 additions & 44 deletions

File tree

android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ android {
1111
applicationId "com.compassconnections.app"
1212
minSdkVersion rootProject.ext.minSdkVersion
1313
targetSdkVersion rootProject.ext.targetSdkVersion
14-
versionCode 123
14+
versionCode 124
1515
versionName "1.29.1"
1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
aaptOptions {

web/components/widgets/tooltip.tsx

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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'
1516
import {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

Comments
 (0)