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

fix: Fix Tooltip positioning #1878

Merged
merged 9 commits into from
Jan 12, 2022
Merged

fix: Fix Tooltip positioning #1878

merged 9 commits into from
Jan 12, 2022

Conversation

suzubara
Copy link
Contributor

@suzubara suzubara commented Jan 11, 2022

Summary

Updates the code used to position the Tooltip component to reflect USWDS 2.11.2 Tooltip source code. This seems to resolve the positioning bug.

Related Issues or PRs

Fixes #1877

How To Test

View the Tooltip component in Storybook at various screen sizes and verify the tooltip is always visible and positioned as expected.

Screenshots (optional)

image

image

@suzubara suzubara added high priority Active blocker for a current project or just urgent type: bug Something isn't working like it's supposed to labels Jan 11, 2022
brandonlenz
brandonlenz previously approved these changes Jan 12, 2022
@suzubara
Copy link
Contributor Author

suzubara commented Jan 12, 2022

While trying this branch out on my project, I realized that there were more issues with positioning - the code to loop through the various positions wasn’t working as expected. I think it was because the code to set the new CSS and check if the element was visible or not were all happening in the same useEffect hook, but to accurately detect whether or not the element is visible it needs to wait for the re-render. So I did some more refactoring and pushed up an update that I think solves this once and for all!

The new way this works is: when the tooltip gets shown, it sets the position to whatever was intended. in a second useEffect hook that specifically looks for changes in the position, it checks whether the tooltip is visible. If it is, it turns on the is-visible class and that’s that. if it’s not, it starts the loop to go through each position (and also try wrapping text if needed).

It is possible that it won’t find a position that works… this might happen if the tooltip contents is very long and/or the window is SO small that there’s no room. I checked and this can come up in the USWDS implementation as well. I put a console.warn in this case to surface the issue.

I also added more Storybook stories to demonstrate the auto-positioning/wrapping (try changing the viewport size).

Finally… while working on this I also discovered what I think is a USWDS bug so I filed uswds/uswds#4458
but to fix in the meantime, I just added that line of CSS to our lib.. hopefully it can be removed when the bug is fixed.

Comment on lines +6 to +10
// This should be set by USWDS but isn't
// It can be removed when https://github.com/uswds/uswds/issues/4458 is fixed
.usa-tooltip__body {
opacity: 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

@brandonlenz
Copy link
Contributor

Something I just noticed (not for us to fix here): Looks like the tooltip takes the component's margin into account causing top not to be centered on the element, for example:
Screen Shot 2022-01-12 at 4 11 26 PM

But its the same way in the USWDS example, so we're good!!

@brandonlenz brandonlenz self-requested a review January 12, 2022 21:14
@suzubara suzubara merged commit f15fea0 into main Jan 12, 2022
@suzubara suzubara deleted the 1877-tooltip-positioning-fix branch January 12, 2022 21:18
@suzubara suzubara mentioned this pull request Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
high priority Active blocker for a current project or just urgent type: bug Something isn't working like it's supposed to
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[fix] Tooltip top/bottom positioning is broken
3 participants