Skip to content
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 15 commits into from
Mar 12, 2024

Conversation

broccolinisoup
Copy link
Member

@broccolinisoup broccolinisoup commented Feb 29, 2024

Changelog

Tooltip element by default has position: fixed but this doesn't work cases where the tooltip element is after the first scroll. So the position needs to be position: absolute. However with this change, there is another bug occurs where the tooltip element first appears in a wrong left and right position therefore causes screen to wiggle. The recording below shows this behaviour.

I think this occurs because we set the position when the toggle event is triggered and the toggle event is triggered after we set the tooltip element to be visible. I updated the position logic to be called in the same function we set the visibility (tooltip.showPopover()) rather than being called as a callback in the toggle event. I ran the tests and seems like the bug is gone.

tooltip-flickering.mp4

New

Changed

  • Position setting was used to be called as a callback function in the the toggle event listener but now it is called when the :popover-open pseudo class is added to the tooltip element.

Removed

  • Removed toggle event listening.

I don’t know why we would need to listen to the toggle event, since we know exactly when the tooltip is shown. 🤷🏻‍♀️ It seems like one is enough if I read the docs correctly

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

Merge checklist

Copy link

changeset-bot bot commented Feb 29, 2024

🦋 Changeset detected

Latest commit: 78ff65b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Feb 29, 2024

size-limit report 📦

Path Size
packages/react/dist/browser.esm.js 113.69 KB (-0.02% 🔽)
packages/react/dist/browser.umd.js 114.36 KB (-0.01% 🔽)

@broccolinisoup broccolinisoup changed the title do not set position if it was set before TooltipV2: Set the tooltip position when the popover open not when the toggle event is triggered Mar 1, 2024
tooltipElRef.current.showPopover()
const tooltip = tooltipElRef.current
const trigger = triggerRef.current
tooltip.setAttribute('popover', 'auto')
Copy link
Member

Choose a reason for hiding this comment

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

This should always be set so that the browser can correctly expose the right AT nodes. We should not set it as the tooltip is being opened.

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense. Although the tooltip is not exposed to AT when it is not opened but I think it still makes sense to set the initial attributes in the render rather than when it is opened.

@@ -23,6 +23,7 @@ const StyledTooltip = styled.div`
/* Overriding the default popover styles */
display: none;
&[popover] {
position: absolute;
Copy link
Member

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?

Copy link
Member Author

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:

  1. hover/focus event is triggered
  2. tooltip.showPopover() function is called
  3. :popover-open attribute is added to the tooltip div
  4. Adding :popover-open triggers the toggle event as well as triggers the animation that changes the tooltip's opacity from 0 to 1
  5. Position is set in the toggle 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.

side: directionToPosition[direction].side,
align: directionToPosition[direction].align,
}
const {top, left, anchorAlign, anchorSide} = getAnchoredPosition(tooltip, trigger, settings)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

@siddharthkp siddharthkp left a comment

Choose a reason for hiding this comment

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

Change in order of functions makes sense to me!

@broccolinisoup broccolinisoup requested a review from a team as a code owner March 8, 2024 02:37
@broccolinisoup broccolinisoup added this pull request to the merge queue Mar 12, 2024
Merged via the queue into main with commit f08f85b Mar 12, 2024
30 checks passed
@broccolinisoup broccolinisoup deleted the tooltip-position-wiggle branch March 12, 2024 00:40
@primer primer bot mentioned this pull request Mar 11, 2024
lukasoppermann pushed a commit that referenced this pull request Apr 16, 2024
…e toggle event is triggered (#4327)

* do not set position if it was set before

* console

* only position set on tooltip show

* remove event listener with the same func signature

* just test

* calculate anchor position after popover-open class is added

* clean up

* comment tidy up

* fix tests

* changeset

* move setting popover auto back in the render

* update snapshots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants