Visitar URL original
USWDS - Tooltip: Make tooltip content hoverable by amyleadem · Pull Request #5919 · uswds/uswds · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Merge branch 'develop' of https://github.com/uswds/uswds into al-tool…
…tip-hoverable
  • Loading branch information
amyleadem
amyleadem committed May 22, 2024
commit 1cbd67ff2a4236fad78e69887ee1005599adf3ab
14 changes: 14 additions & 0 deletions packages/usa-tooltip/src/index.js

Copy link
Copy Markdown
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 ↩

Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ const setUpAttributes = (tooltipTrigger) => {
return { tooltipBody, position, tooltipContent, wrapper };
};

/**
* Hide all active tooltips when escape key is pressed.
*/

const handleEscape = () => {
const activeTooltips = selectOrMatches(`.${TOOLTIP_BODY_CLASS}.${SET_CLASS}`);

if (!activeTooltips) {
return;
}

activeTooltips.forEach((activeTooltip) => hideToolTip(activeTooltip));
};

// Setup our function to run on various events
const tooltip = behavior(
{
Expand Down
14 changes: 14 additions & 0 deletions packages/usa-tooltip/src/test/tooltips.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const EVENTS = {

el.dispatchEvent(mouseleaveEvent);
},
escape(el) {
const escapeKeyEvent = new KeyboardEvent("keydown", {
key: "Escape",
bubbles: true,
});

el.dispatchEvent(escapeKeyEvent);
},
};

tests.forEach(({ name, selector: containerSelector }) => {
Expand Down Expand Up @@ -90,6 +98,12 @@ tests.forEach(({ name, selector: containerSelector }) => {
assert.strictEqual(tooltipBody.classList.contains("is-set"), true);
});

it("tooltip is hidden on escape keydown", () => {
tooltipTrigger.focus();
EVENTS.escape(tooltipTrigger);
assert.strictEqual(tooltipBody.classList.contains("is-set"), false);
});

it("should not allow for innerHTML of child elements ", () => {
// override the template
body.innerHTML = `<button class="usa-button usa-tooltip" title="Apricot &lt;img src='' onerror=alert('ouch')&gt;">Button</button>`;
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.