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

USWDS - Tooltip: Make tooltip content hoverable #5919

Merged
merged 22 commits into from
May 22, 2024

Conversation

amyleadem
Copy link
Contributor

@amyleadem amyleadem commented May 9, 2024

Summary

Updated the behavior of tooltip to allow hover over tooltip content. This allows the component to meet the "hoverable" standard outlined in WCAG 1.4.13.

Note

This issue is slated to be in the same release as #5909, which also edits the tooltip JavaScript. We should confirm there are no conflicts between the two PRs.

Breaking change

This is not a breaking change.

Related issue

Closes #5918

Related pull requests

Changelog PR

Preview link

Before After
Tooltip Feature preview

Problem statement

Success Criterion 1.4.13 Content on Hover or Focus states that for tooltip content must be hoverable:

Hoverable
If pointer hover can trigger the additional content, then the pointer can be moved over the additional content without the additional content disappearing;

The tooltip component does not currently meet this criteria.

Solution

Updated the method for detecting how and when the mouse leaves the tooltip. These were the main changes to accomplish this:

  • Attached the event listener to the usa-tooltip wrapper, rather than usa-tooltip__trigger. This added the usa-tooltip__body element to the hit target.
  • Replaced mouseout with mouseleave. This allowed us to trigger hideTooltip only when the mouse left all elements inside the wrapper, rather than triggering after it left each element.
  • Removed the pointer-events: none; style declaration. This allowed mouseleave to include usa-tooltip__body in the hit target.

Testing and review

  1. Confirm that you move your mouse into the tooltip content without the tooltip closing
  2. Confirm that you can move your mouse into the small space between the tooltip and the tooltip trigger without the tooltip closing
  3. Confirm that the toolip closes when you leave the .usa-tooltip hit boundary
  4. Confirm the code meets standards.
  5. Confirm the code is compatible with the solution in USWDS - Tooltip: Make tooltip dismissible with escape key #5909

Note

There is a pre-existing issue with the social media icon tooltips in the tooltip test story.

- Mouseleave makes the tooltip hoverable - it lets us target usa-tooltip
- This enables the tooltip to be hoverable
- This prevents mouseleave from triggering in the empty space b/t tooltip and trigger
content: "";
display: block;
position: absolute;
}
}
Copy link
Contributor Author

@amyleadem amyleadem May 9, 2024

Choose a reason for hiding this comment

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

Note

I added these before pseudo elements to fill the gap between the tooltip trigger and tooltip. Filling this gap prevents mouseleave from triggering when the mouse enters that gap when trying to hover over the tooltip.

image

@@ -28,7 +28,6 @@ $triangle-size: 5px;
font-size: size("ui", $theme-tooltip-font-size);
opacity: 0; // Required for recalculating position.
padding: units(1);
pointer-events: none;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note

We needed to remove the pointer-event styles here because they were causing the tooltip to be removed from the usa-tooltip hit boundary. This caused mouseleave to be triggered when hovering over the tooltip.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note

To prevent the tooltip from closing when you move the mouse out of the tooltip trigger, we needed to:

  1. Target the usa-tooltip wrapper instead of the tooltip trigger, and
  2. Replace mouseout with mouseleave. mouseleave only triggers when the mouse has exited the element and all its descendents, whereas mouseout triggers with each element in the group. See reference below for more details:

"...mouseleave is fired when the pointer has exited the element and all of its descendants, whereas mouseout is fired when the pointer leaves the element or leaves one of the element's descendants (even if the pointer is still within the element)." 1

Footnotes

  1. https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseleave_event

@amyleadem
Copy link
Contributor Author

@mejiaj and @mahoneycm, this still needs JSDoc comments and probably a unit test, but opening this review now so that we can start discussing if there are ways to write this code that better align with standards.

@amyleadem amyleadem marked this pull request as ready for review May 10, 2024 23:06
Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on.

I hope you don't mind I've edited the PR description. I've simplified the summary for clarification (now its clear we're adding support to the tooltip body). As well as adding a table with preview links to the before/after state for easier testing.


This new behavior means we'll have to re-think our current approach to showing/hiding the tooltip. Right now it's listening for any mouseover on the trigger. We should consider either:

  1. Toggling visibility on TOOLTIP, instead of TOOLTIP_TRIGGER.
  2. Using mouseleave, but ensuring it's only added once and removed on teardown.

packages/usa-tooltip/src/index.js Outdated Show resolved Hide resolved
Copy link
Contributor Author

@amyleadem amyleadem May 15, 2024

Choose a reason for hiding this comment

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

Note

I added this test because I discovered that my original fixes had the mouse events targeting the wrong tooltips when there were multiple tooltips on the page and no wrappers around the tooltips. This might not be needed moving forward, but wanted to push it up to confirm the fix is still working. We can remove before merge if this test feels confusing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch here! Glad to see it's still working if the component is built this way.

@amyleadem amyleadem requested a review from mahoneycm May 15, 2024 22:28
Copy link
Contributor

@mahoneycm mahoneycm left a comment

Choose a reason for hiding this comment

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

Getting close! A couple small changes I wanted to run by you to make sure we're covering all bases.

packages/usa-tooltip/src/styles/_usa-tooltip.scss Outdated Show resolved Hide resolved
packages/usa-tooltip/src/test/tooltips.spec.js Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch here! Glad to see it's still working if the component is built this way.

amyleadem added a commit that referenced this pull request May 16, 2024
- will be added to #5919 instead
- Not needed for this PR; will be added in #5909
Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

Minor changes request to pseudoelement format & suggestion for JS. Otherwise LGTM.

Comment on lines 393 to 395
wrapper.addEventListener("mouseleave", () => {
hideToolTip(body);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Follows the same pattern in teardown line 400.

Suggested change
wrapper.addEventListener("mouseleave", () => {
hideToolTip(body);
});
wrapper.addEventListener("mouseleave", () => hideToolTip(body));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in d4ca8c3

@amyleadem
Copy link
Contributor Author

@amycole501 and @alex-hull can you confirm that the tooltip now conforms with the "hoverable" criteria in WCAG 1.4.13?

Copy link

@amycole501 amycole501 left a comment

Choose a reason for hiding this comment

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

I tested the hoverable action and yes, it is now able to be hovered over with a mouse and then when I move the mouse over the tooltip it does NOT disappear.

@amyleadem
Copy link
Contributor Author

Sorry @amycole501, I accidentally re-requested your review, which cleared the green approval check 🤦

mejiaj and others added 3 commits May 20, 2024 10:20
The `$triangle-size` variable isn't a parameter, so removed from SASSDoc comment.
USWDS - Tooltip: Create a mixin for the tooltip spacer
Copy link
Contributor

@mahoneycm mahoneycm left a comment

Choose a reason for hiding this comment

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

Great work here! Functionality is working like a charm

Testing checklist

  • Confirm that you move your mouse into the tooltip content without the tooltip closing
  • Confirm that you can move your mouse into the small space between the tooltip and the tooltip trigger without the tooltip closing
  • Confirm that the toolip closes when you leave the .usa-tooltip hit boundary
  • Confirm the code meets standards.
  • Confirm the code is compatible with the solution in USWDS - Tooltip: Make tooltip dismissible with escape key #5909

Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

No issues found. Tested in Safari, Firefox, and Chromium.

Thanks for adding the tests.

@mejiaj mejiaj requested a review from thisisdano May 20, 2024 16:44
Copy link
Member

@thisisdano thisisdano left a comment

Choose a reason for hiding this comment

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

Very nice. Both features work together as expected

@thisisdano thisisdano merged commit 114f6e5 into develop May 22, 2024
5 checks passed
@thisisdano thisisdano deleted the al-tooltip-hoverable branch May 22, 2024 22:44
@thisisdano thisisdano mentioned this pull request May 30, 2024
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.

USWDS - Bug: Tooltip needs "hoverable" functionality
5 participants