-
Notifications
You must be signed in to change notification settings - Fork 648
TooltipV2: Set the tooltip position when the popover open not when the toggle event is triggered #4327
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
Changes from 11 commits
f9516d2
84c4bb3
db335e9
d9ffbe8
1697c8b
b2f5576
5ffe0bd
1ca64c7
ba8bbe6
1a4cd23
f67a6fb
d68c967
47f7569
bcad60d
78ff65b
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 |
|---|---|---|
| @@ -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 |
| 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,23 @@ 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.setAttribute('popover', 'auto') | ||
|
||
| tooltip.showPopover() | ||
| /* | ||
| * TOOLTIP POSITIONING | ||
| */ | ||
| const settings = { | ||
| side: directionToPosition[direction].side, | ||
| align: directionToPosition[direction].align, | ||
| } | ||
| const {top, left, anchorAlign, anchorSide} = getAnchoredPosition(tooltip, trigger, settings) | ||
|
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. 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 = () => { | ||
|
|
@@ -238,33 +255,6 @@ export const Tooltip = React.forwardRef( | |
| apply() | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * 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 ( | ||
|
|
||
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-openattribute is added to the tooltip div:popover-opentriggers thetoggleevent as well as triggers the animation that changes the tooltip's opacity from 0 to 1toggleevent'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.