Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
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
Uh oh!
There was an error while loading. Please reload this page.
TooltipV2: Set the tooltip position when the popover open not when the toggle event is triggered #4327
Changes from all commits
f9516d284c4bb3db335e9d9ffbe81697c8bb2f55765ffe0bd1ca64c7ba8bbe61a4cd23f67a6fbd68c96747f7569bcad60d78ff65bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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.span` | ||
| /* 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) | ||
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 = () => { | ||
| @@ -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 ( | ||
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.