Skip to content

Commit 4f3be9c

Browse files
authored
TooltipV2: Hide Tooltip when menu is active (#7142)
1 parent 5e6e04e commit 4f3be9c

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

.changeset/wild-aliens-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': minor
3+
---
4+
5+
IconButton: Hide tooltip when popup is open via new `closeTooltip` prop in `Tooltip`.

packages/react/src/Button/IconButton.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ const IconButton = forwardRef(
2828
const {tooltipId} = React.useContext(TooltipContext) // Tooltip v2
2929
const {tooltipId: tooltipIdV1} = React.useContext(TooltipContextV1) // Tooltip v1
3030

31+
const {'aria-expanded': isExpanded, 'aria-haspopup': hasPopup} = props
32+
3133
const hasExternalTooltip = tooltipId || tooltipIdV1
34+
35+
// If the button has an active "popup" (like a menu), we don't want to show the tooltip.
36+
// This is mostly for `ActionMenu`, but could be applicable elsewhere.
37+
const hasActivePopup = (isExpanded === true || isExpanded === 'true') && hasPopup === 'true'
38+
3239
const withoutTooltip =
3340
unsafeDisableTooltip || disabled || ariaLabel === undefined || ariaLabel === '' || hasExternalTooltip
3441

@@ -55,6 +62,7 @@ const IconButton = forwardRef(
5562
type={description ? undefined : 'label'}
5663
direction={tooltipDirection}
5764
keybindingHint={keybindingHint ?? keyshortcuts}
65+
_privateDisableTooltip={hasActivePopup}
5866
>
5967
<ButtonBase
6068
icon={Icon}

packages/react/src/TooltipV2/Tooltip.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ export type TooltipProps = React.PropsWithChildren<{
2525
* long (1200ms)
2626
*/
2727
delay?: 'short' | 'medium' | 'long'
28+
/**
29+
* Private API for use internally only. Prevents the tooltip from opening if `true`.
30+
*
31+
* Accessibility note: This prop should be used with caution. Only use when needing to
32+
* programmatically close the tooltip in response to a specific user action, such as
33+
* opening a menu, or content where the tooltip could overlap with interactive content.
34+
*
35+
* @default false
36+
*/
37+
_privateDisableTooltip?: boolean
2838
}> &
2939
React.HTMLAttributes<HTMLElement>
3040

@@ -107,6 +117,7 @@ export const Tooltip: ForwardRefExoticComponent<
107117
className,
108118
keybindingHint,
109119
delay = 'short',
120+
_privateDisableTooltip = false,
110121
...rest
111122
}: TooltipProps,
112123
forwardedRef,
@@ -130,7 +141,8 @@ export const Tooltip: ForwardRefExoticComponent<
130141
tooltipElRef.current &&
131142
triggerRef.current &&
132143
tooltipElRef.current.hasAttribute('popover') &&
133-
!tooltipElRef.current.matches(':popover-open')
144+
!tooltipElRef.current.matches(':popover-open') &&
145+
!_privateDisableTooltip
134146
) {
135147
const tooltip = tooltipElRef.current
136148
const trigger = triggerRef.current

0 commit comments

Comments
 (0)