Skip to content

Commit 4bcf78b

Browse files
fix(TooltipV2): delay tooltip opening time by 50ms (#5350)
* fix(TooltipV2): delay tooltip opening time by ms * Create five-mayflies-flash.md * fix(Overlay): use fire event for click in test and set popover to false when non-matching element * fix(Overlay): remove redundant await * fix(TooltipV2): use safeTimeout * fix(TooltipV2): change type of timeoutRef
1 parent d544f64 commit 4bcf78b

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

.changeset/five-mayflies-flash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
fix(TooltipV2): delay tooltip opening time by ms

packages/react/src/Overlay/Overlay.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ describe('Overlay', () => {
128128
expect(container.getByText('Add to list')).toBeInTheDocument()
129129

130130
// open second menu
131-
await user.click(container.getByText('Create list'))
131+
fireEvent.click(container.getByText('Create list'))
132132
expect(container.getByPlaceholderText('Name this list')).toBeInTheDocument()
133133

134134
// hitting escape on input should close the second menu but not the first

packages/react/src/TooltipV2/Tooltip.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import classes from './Tooltip.module.css'
1515
import {useFeatureFlag} from '../FeatureFlags'
1616
import {KeybindingHint, type KeybindingHintProps} from '../KeybindingHint'
1717
import VisuallyHidden from '../_VisuallyHidden'
18+
import useSafeTimeout from '../hooks/useSafeTimeout'
1819

1920
const CSS_MODULE_FEATURE_FLAG = 'primer_react_css_modules_team'
2021

@@ -213,6 +214,10 @@ export const Tooltip = React.forwardRef(
213214

214215
const [isPopoverOpen, setIsPopoverOpen] = useState(false)
215216

217+
const timeoutRef = React.useRef<number | null>(null)
218+
219+
const {safeSetTimeout, safeClearTimeout} = useSafeTimeout()
220+
216221
const openTooltip = () => {
217222
try {
218223
if (
@@ -265,6 +270,8 @@ export const Tooltip = React.forwardRef(
265270
) {
266271
tooltipElRef.current.hidePopover()
267272
setIsPopoverOpen(false)
273+
} else {
274+
setIsPopoverOpen(false)
268275
}
269276
} catch (error) {
270277
// older browsers don't support the :popover-open selector and will throw, even though we use a polyfill
@@ -362,10 +369,18 @@ export const Tooltip = React.forwardRef(
362369
child.props.onFocus?.(event)
363370
},
364371
onMouseEnter: (event: React.MouseEvent) => {
365-
openTooltip()
366-
child.props.onMouseEnter?.(event)
372+
// show tooltip after mosue has been hovering for at least 50ms
373+
// (prevent showing tooltip when mouse is just passing through)
374+
timeoutRef.current = safeSetTimeout(() => {
375+
openTooltip()
376+
child.props.onMouseEnter?.(event)
377+
}, 50)
367378
},
368379
onMouseLeave: (event: React.MouseEvent) => {
380+
if (timeoutRef.current) {
381+
safeClearTimeout(timeoutRef.current)
382+
timeoutRef.current = null
383+
}
369384
closeTooltip()
370385
child.props.onMouseLeave?.(event)
371386
},

0 commit comments

Comments
 (0)