Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
fix(TooltipV2): delay tooltip opening time by 50ms#5350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d4e1eb73fcdbddf62bf384d4cbcca5bbca5bb7ebcb2073d417bc59c45f5738d15ce545bed7c74File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@primer/react": patch | ||
| --- | ||
| fix(TooltipV2): delay tooltip opening time by ms |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,6 +15,7 @@ import classes from './Tooltip.module.css' | ||
| import {useFeatureFlag} from '../FeatureFlags' | ||
| import {KeybindingHint, type KeybindingHintProps} from '../KeybindingHint' | ||
| import VisuallyHidden from '../_VisuallyHidden' | ||
| import useSafeTimeout from '../hooks/useSafeTimeout' | ||
| const CSS_MODULE_FEATURE_FLAG = 'primer_react_css_modules_team' | ||
| @@ -213,6 +214,10 @@ export const Tooltip = React.forwardRef( | ||
| const [isPopoverOpen, setIsPopoverOpen] = useState(false) | ||
| const timeoutRef = React.useRef<number | null>(null) | ||
| const {safeSetTimeout, safeClearTimeout} = useSafeTimeout() | ||
| const openTooltip = () => { | ||
| try { | ||
| if ( | ||
| @@ -265,6 +270,8 @@ export const Tooltip = React.forwardRef( | ||
| ) { | ||
| tooltipElRef.current.hidePopover() | ||
| setIsPopoverOpen(false) | ||
| } else { | ||
| setIsPopoverOpen(false) | ||
Comment on lines
+273
to
+274
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixes bug where overlay would stay "stuck" and not able to close via keyboard due to popOver state being open but it not actually being in the DOM | ||
| } | ||
| } catch (error) { | ||
| // 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( | ||
| child.props.onFocus?.(event) | ||
| }, | ||
| onMouseEnter: (event: React.MouseEvent) => { | ||
| openTooltip() | ||
| child.props.onMouseEnter?.(event) | ||
| // show tooltip after mosue has been hovering for at least 50ms | ||
| // (prevent showing tooltip when mouse is just passing through) | ||
| timeoutRef.current = safeSetTimeout(() => { | ||
| openTooltip() | ||
| child.props.onMouseEnter?.(event) | ||
| }, 50) | ||
| }, | ||
| onMouseLeave: (event: React.MouseEvent) => { | ||
| if (timeoutRef.current) { | ||
| safeClearTimeout(timeoutRef.current) | ||
| timeoutRef.current = null | ||
| } | ||
| closeTooltip() | ||
| child.props.onMouseLeave?.(event) | ||
| }, | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried for minute to get this to work with user.click, I replicated this test manually in storybook and it works as expected. I noticed userEvent was triggering different events than manual testing did 🤷🏽♀️