Skip to content

Tooltip: Do not wrap the tooltip and the trigger in a div #4056

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

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-starfishes-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Tooltip2: Do not wrap the tooltip span and its trigger in a div
1 change: 1 addition & 0 deletions src/ActionList/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const InactiveIndicator: React.FC<{
padding: 0,
font: 'inherit',
cursor: 'pointer',
display: 'flex',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to removing the wrapper in the tooltip, I needed to add this to align the inactive visual in center. Let me know if you prefer a different way to do it @mperrotti

}}
aria-labelledby={labelId}
>
Expand Down
13 changes: 10 additions & 3 deletions src/drafts/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, {Children, useEffect, useRef, useState} from 'react'
import Box from '../../Box'
import sx, {SxProp} from '../../sx'
import {useId, useProvidedRefOrCreate} from '../../hooks'
import {invariant} from '../../utils/invariant'
Expand Down Expand Up @@ -139,6 +138,7 @@ export type TriggerPropsType = {
onBlur?: React.FocusEventHandler
onFocus?: React.FocusEventHandler
onMouseEnter?: React.MouseEventHandler
onMouseLeave?: React.MouseEventHandler
ref?: React.RefObject<HTMLElement>
}

Expand Down Expand Up @@ -268,7 +268,7 @@ export const Tooltip = React.forwardRef(

return (
<TooltipContext.Provider value={{tooltipId}}>
<Box onMouseLeave={() => closeTooltip()}>
<>
{React.isValidElement(child) &&
React.cloneElement(child as React.ReactElement<TriggerPropsType>, {
ref: triggerRef,
Expand All @@ -288,6 +288,10 @@ export const Tooltip = React.forwardRef(
openTooltip()
child.props.onMouseEnter?.(event)
},
onMouseLeave: (event: React.MouseEvent) => {
closeTooltip()
child.props.onMouseLeave?.(event)
},
})}
<StyledTooltip
ref={tooltipElRef}
Expand All @@ -298,10 +302,13 @@ export const Tooltip = React.forwardRef(
// stop AT from announcing the tooltip twice when it is a label type because it will be announced with "aria-labelledby"
aria-hidden={type === 'label' ? true : undefined}
id={`tooltip-${tooltipId}`}
// mouse leave and enter on the tooltip itself is needed to keep the tooltip open when the mouse is over the tooltip
onMouseEnter={openTooltip}
onMouseLeave={closeTooltip}
>
{text}
</StyledTooltip>
</Box>
</>
</TooltipContext.Provider>
)
},
Expand Down