-
Notifications
You must be signed in to change notification settings - Fork 673
Add delay functionality to tooltips #6889
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
Changes from 6 commits
4989625
f021444
cc268fe
37cdedb
d83f903
05fa73e
172af1c
ae77ce5
2b290d8
51d5ba5
52f36a8
b93f924
f11d231
ca8d86a
ec9d053
9d5bce0
c235468
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,13 @@ export type TooltipProps = React.PropsWithChildren< | |
| text: string | ||
| type?: 'label' | 'description' | ||
| keybindingHint?: KeybindingHintProps['keys'] | ||
| /** | ||
| * Delay in milliseconds before showing the tooltip | ||
| * @default instant 50ms | ||
| * medium 400ms | ||
| * long 1200ms | ||
|
Member
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. Can we leave a comment on how we picked these numbers? They feel arbitrary in code so it would be nice to have the context (or a link to the context) so that, in the future when someone wants to change these, they have more information
Member
Author
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. Yes, good idea! I'll add some context.
Member
Author
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. https://github.com/github/primer/issues/3313#issuecomment-3336696699 - Added this link in the code comments as well |
||
| */ | ||
| delay?: 'instant' | 'medium' | 'long' | ||
|
broccolinisoup marked this conversation as resolved.
Outdated
|
||
| } & SxProp | ||
| > & | ||
| React.HTMLAttributes<HTMLElement> | ||
|
|
@@ -83,7 +90,17 @@ export const TooltipContext = React.createContext<{tooltipId?: string}>({}) | |
|
|
||
| export const Tooltip = React.forwardRef( | ||
| ( | ||
| {direction = 's', text, type = 'description', children, id, className, keybindingHint, ...rest}: TooltipProps, | ||
| { | ||
| direction = 's', | ||
| text, | ||
| type = 'description', | ||
| children, | ||
| id, | ||
| className, | ||
| keybindingHint, | ||
| delay = 'instant', | ||
| ...rest | ||
| }: TooltipProps, | ||
| forwardedRef, | ||
| ) => { | ||
| const tooltipId = useId(id) | ||
|
|
@@ -284,14 +301,20 @@ export const Tooltip = React.forwardRef( | |
| child.props.onFocus?.(event) | ||
| }, | ||
| onMouseOverCapture: (event: React.MouseEvent) => { | ||
| const delayTimeMap = { | ||
| instant: 50, | ||
| medium: 400, | ||
| long: 1200, | ||
| } | ||
| const delayTime = delayTimeMap[delay] || 50 | ||
| // We use a `capture` event to ensure this is called first before | ||
| // events that might cancel the opening timeout (like `onTouchEnd`) | ||
| // show tooltip after mouse has been hovering for at least 50ms | ||
| // show tooltip after mouse has been hovering for the specified delay time | ||
| // (prevent showing tooltip when mouse is just passing through) | ||
| openTimeoutRef.current = safeSetTimeout(() => { | ||
| openTooltip() | ||
| child.props.onMouseEnter?.(event) | ||
| }, 50) | ||
| }, delayTime) | ||
| }, | ||
| onMouseLeave: (event: React.MouseEvent) => { | ||
| closeTooltip() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.