-
Notifications
You must be signed in to change notification settings - Fork 536
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
TooltipV2: Set the tooltip position when the popover open not when the toggle event is triggered #4327
Merged
Merged
TooltipV2: Set the tooltip position when the popover open not when the toggle event is triggered #4327
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f9516d2
do not set position if it was set before
broccolinisoup 84c4bb3
console
broccolinisoup db335e9
only position set on tooltip show
broccolinisoup d9ffbe8
remove event listener with the same func signature
broccolinisoup 1697c8b
just test
broccolinisoup b2f5576
calculate anchor position after popover-open class is added
broccolinisoup 5ffe0bd
clean up
broccolinisoup 1ca64c7
comment tidy up
broccolinisoup ba8bbe6
Merge branch 'main' into tooltip-position-wiggle
broccolinisoup 1a4cd23
fix tests
broccolinisoup f67a6fb
changeset
broccolinisoup d68c967
Merge branch 'main' into tooltip-position-wiggle
broccolinisoup 47f7569
move setting popover auto back in the render
broccolinisoup bcad60d
Merge branch 'main' into tooltip-position-wiggle
broccolinisoup 78ff65b
update snapshots
broccolinisoup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react': patch | ||
--- | ||
|
||
Tooltip v2: Set the tooltip position when popover-open attribute is added to the tooltip element not the toggle event is triggered |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ const StyledTooltip = styled.div` | |
/* Overriding the default popover styles */ | ||
display: none; | ||
&[popover] { | ||
position: absolute; | ||
padding: 0.5em 0.75em; | ||
width: max-content; | ||
margin: auto; | ||
|
@@ -196,7 +197,22 @@ export const Tooltip = React.forwardRef( | |
|
||
const openTooltip = () => { | ||
if (tooltipElRef.current && triggerRef.current && !tooltipElRef.current.matches(':popover-open')) { | ||
tooltipElRef.current.showPopover() | ||
const tooltip = tooltipElRef.current | ||
const trigger = triggerRef.current | ||
tooltip.showPopover() | ||
/* | ||
* TOOLTIP POSITIONING | ||
*/ | ||
const settings = { | ||
side: directionToPosition[direction].side, | ||
align: directionToPosition[direction].align, | ||
} | ||
const {top, left, anchorAlign, anchorSide} = getAnchoredPosition(tooltip, trigger, settings) | ||
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. This is perfect! We can't calculate boundingRect for an element with display:none, so this is the right timing. If needed (race condition etc), we can wait for position to be set before animating the tooltip's opacity. |
||
// This is required to make sure the popover is positioned correctly i.e. when there is not enough space on the specified direction, we set a new direction to position the ::after | ||
const calculatedDirection = positionToDirection[`${anchorSide}-${anchorAlign}` as string] | ||
setCalculatedDirection(calculatedDirection) | ||
tooltip.style.top = `${top}px` | ||
tooltip.style.left = `${left}px` | ||
} | ||
} | ||
const closeTooltip = () => { | ||
|
@@ -239,32 +255,8 @@ export const Tooltip = React.forwardRef( | |
} | ||
} | ||
|
||
/* | ||
* TOOLTIP POSITIONING | ||
*/ | ||
const tooltip = tooltipElRef.current | ||
const trigger = triggerRef.current | ||
tooltip.setAttribute('popover', 'auto') | ||
const settings = { | ||
side: directionToPosition[direction].side, | ||
align: directionToPosition[direction].align, | ||
} | ||
|
||
const positionSet = () => { | ||
const {top, left, anchorAlign, anchorSide} = getAnchoredPosition(tooltip, trigger, settings) | ||
|
||
tooltip.style.top = `${top}px` | ||
tooltip.style.left = `${left}px` | ||
// This is required to make sure the popover is positioned correctly i.e. when there is not enough space on the specified direction, we set a new direction to position the ::after | ||
const calculatedDirection = positionToDirection[`${anchorSide}-${anchorAlign}` as string] | ||
setCalculatedDirection(calculatedDirection) | ||
} | ||
|
||
tooltip.addEventListener('toggle', positionSet) | ||
|
||
return () => { | ||
tooltip.removeEventListener('toggle', positionSet) | ||
} | ||
}, [tooltipElRef, triggerRef, direction, type]) | ||
|
||
return ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
AIUI this is the main thing that is doing all the heavy lifting. I think you could just apply this line and ignore the rest and it would still work?
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.
Yeah definitely this line does all the heavy lifting and actually the rest is not directly related to this change but the existing issue* just became more visible when we added this line (no idea why 🤷🏻♀️ ).
*The existing issue (at least it is an issue in my opinion) is where we set the position.
Previously the flow was:
tooltip.showPopover()
function is called:popover-open
attribute is added to the tooltip div:popover-open
triggers thetoggle
event as well as triggers the animation that changes the tooltip's opacity from 0 to 1toggle
event's callback function.I believe the wiggling issue (video in the PR description) happens because we set the position a little too late (step 5) but we make the tooltip visible in the step 3. So in this PR I am setting the position in Step 3 and that makes the toggle event redundant.
Let me know if you have any concern or suggestions.