Skip to content

Commit f8760fe

Browse files
iansan5653jonrohan
andauthored
Hide tooltips on touchend event (#5830)
Co-authored-by: Jon Rohan <[email protected]>
1 parent ecb449f commit f8760fe

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

.changeset/chilly-jars-jog.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": minor
3+
---
4+
5+
Hide `TooltipV2` tooltips on `touchend` event

packages/react/src/TooltipV2/Tooltip.tsx

+29-16
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,18 @@ export type TooltipProps = React.PropsWithChildren<
146146
> &
147147
React.HTMLAttributes<HTMLElement>
148148

149-
type TriggerPropsType = {
150-
'aria-describedby'?: string
151-
'aria-labelledby'?: string
152-
'aria-label'?: string
153-
onBlur?: React.FocusEventHandler
154-
onFocus?: React.FocusEventHandler
155-
onMouseEnter?: React.MouseEventHandler
156-
onMouseLeave?: React.MouseEventHandler
149+
type TriggerPropsType = Pick<
150+
React.HTMLAttributes<HTMLElement>,
151+
| 'aria-describedby'
152+
| 'aria-labelledby'
153+
| 'onBlur'
154+
| 'onTouchEnd'
155+
| 'onFocus'
156+
| 'onMouseOverCapture'
157+
| 'onMouseLeave'
158+
| 'onTouchCancel'
159+
| 'onTouchEnd'
160+
> & {
157161
ref?: React.RefObject<HTMLElement>
158162
}
159163

@@ -214,7 +218,7 @@ export const Tooltip = React.forwardRef(
214218

215219
const [isPopoverOpen, setIsPopoverOpen] = useState(false)
216220

217-
const timeoutRef = React.useRef<number | null>(null)
221+
const openTimeoutRef = React.useRef<number | null>(null)
218222

219223
const {safeSetTimeout, safeClearTimeout} = useSafeTimeout()
220224

@@ -261,6 +265,10 @@ export const Tooltip = React.forwardRef(
261265
}
262266
}
263267
const closeTooltip = () => {
268+
if (openTimeoutRef.current) {
269+
safeClearTimeout(openTimeoutRef.current)
270+
openTimeoutRef.current = null
271+
}
264272
try {
265273
if (
266274
tooltipElRef.current &&
@@ -365,6 +373,13 @@ export const Tooltip = React.forwardRef(
365373
closeTooltip()
366374
child.props.onBlur?.(event)
367375
},
376+
onTouchEnd: (event: React.TouchEvent) => {
377+
child.props.onTouchEnd?.(event)
378+
379+
// Hide tooltips on tap to essentially disable them on touch devices;
380+
// this still allows viewing the tooltip on tap-and-hold
381+
safeSetTimeout(() => closeTooltip(), 10)
382+
},
368383
onFocus: (event: React.FocusEvent) => {
369384
// only show tooltip on :focus-visible, not on :focus
370385
try {
@@ -377,19 +392,17 @@ export const Tooltip = React.forwardRef(
377392
openTooltip()
378393
child.props.onFocus?.(event)
379394
},
380-
onMouseEnter: (event: React.MouseEvent) => {
381-
// show tooltip after mosue has been hovering for at least 50ms
395+
onMouseOverCapture: (event: React.MouseEvent) => {
396+
// We use a `capture` event to ensure this is called first before
397+
// events that might cancel the opening timeout (like `onTouchEnd`)
398+
// show tooltip after mouse has been hovering for at least 50ms
382399
// (prevent showing tooltip when mouse is just passing through)
383-
timeoutRef.current = safeSetTimeout(() => {
400+
openTimeoutRef.current = safeSetTimeout(() => {
384401
openTooltip()
385402
child.props.onMouseEnter?.(event)
386403
}, 50)
387404
},
388405
onMouseLeave: (event: React.MouseEvent) => {
389-
if (timeoutRef.current) {
390-
safeClearTimeout(timeoutRef.current)
391-
timeoutRef.current = null
392-
}
393406
closeTooltip()
394407
child.props.onMouseLeave?.(event)
395408
},

packages/react/src/__tests__/__snapshots__/TextInput.test.tsx.snap

+4-2
Original file line numberDiff line numberDiff line change
@@ -3325,8 +3325,9 @@ exports[`TextInput renders trailingAction icon button 1`] = `
33253325
onBlur={[Function]}
33263326
onClick={[MockFunction]}
33273327
onFocus={[Function]}
3328-
onMouseEnter={[Function]}
33293328
onMouseLeave={[Function]}
3329+
onMouseOverCapture={[Function]}
3330+
onTouchEnd={[Function]}
33303331
type="button"
33313332
>
33323333
<svg
@@ -4090,8 +4091,9 @@ exports[`TextInput renders trailingAction text button with a tooltip 1`] = `
40904091
onBlur={[Function]}
40914092
onClick={[MockFunction]}
40924093
onFocus={[Function]}
4093-
onMouseEnter={[Function]}
40944094
onMouseLeave={[Function]}
4095+
onMouseOverCapture={[Function]}
4096+
onTouchEnd={[Function]}
40954097
style={
40964098
{
40974099
"--button-color": "fg.subtle",

0 commit comments

Comments
 (0)