Skip to content

Commit

Permalink
[MWPW-167312] tooltip dismiss escape (#3664)
Browse files Browse the repository at this point in the history
* [MWPW-167312] tooltip dismiss escape

* [MWPW-167312] unit test added
  • Loading branch information
DKos95 authored Feb 18, 2025
1 parent 5168bc8 commit 90f114d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
29 changes: 29 additions & 0 deletions libs/blocks/table/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ async function setAriaLabelForIcons(el) {
});
}

function setTooltipListeners(el) {
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
el.querySelectorAll('.milo-tooltip').forEach((tooltip) => {
tooltip.classList.add('hide-tooltip');
});
}
});

el.querySelectorAll('.milo-tooltip').forEach((tooltip) => {
tooltip.addEventListener('mouseenter', () => {
tooltip.classList.remove('hide-tooltip');
});

tooltip.addEventListener('mouseleave', () => {
tooltip.classList.add('hide-tooltip');
});

tooltip.addEventListener('focus', () => {
tooltip.classList.remove('hide-tooltip');
});

tooltip.addEventListener('blur', () => {
tooltip.classList.add('hide-tooltip');
});
});
}

function handleHighlight(table) {
const isHighlightTable = table.classList.contains('highlight');
const firstRow = table.querySelector('.row-1');
Expand Down Expand Up @@ -622,6 +650,7 @@ export default function init(el) {

isDecorated = true;
setAriaLabelForIcons(el);
setTooltipListeners(el);
};

window.addEventListener(MILO_EVENTS.DEFERRED, () => {
Expand Down
5 changes: 5 additions & 0 deletions libs/features/icons/icons.css
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@
display: block;
}

.milo-tooltip.hide-tooltip::before,
.milo-tooltip.hide-tooltip::after {
display: none;
}

@media (max-width: 600px) {
.milo-tooltip::before {
max-width: 180px;
Expand Down
2 changes: 1 addition & 1 deletion test/blocks/table/mocks/body.html
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@
<div>
<div>
<p>Add a business stamp</p>
<p><span class="icon icon-info"></span></p>
<p><span class="icon icon-info milo-tooltip"></span></p>
</div>
<div></div>
<div></div>
Expand Down
20 changes: 20 additions & 0 deletions test/blocks/table/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,25 @@ describe('table and tablemetadata', () => {
expect(selectElement.getAttribute('aria-label')).to.equal(ariaLabel);
});
});

it('should show and hide tooltip on hover, focus, and Escape key', async () => {
const tooltip = document.querySelector('.milo-tooltip');
expect(tooltip).to.exist;

tooltip.dispatchEvent(new Event('mouseenter'));
expect(tooltip.classList.contains('hide-tooltip')).to.be.false;

tooltip.dispatchEvent(new Event('mouseleave'));
expect(tooltip.classList.contains('hide-tooltip')).to.be.true;

tooltip.dispatchEvent(new Event('focus'));
expect(tooltip.classList.contains('hide-tooltip')).to.be.false;

tooltip.dispatchEvent(new Event('blur'));
expect(tooltip.classList.contains('hide-tooltip')).to.be.true;

await sendKeys({ press: 'Escape' });
expect(tooltip.classList.contains('hide-tooltip')).to.be.true;
});
});
});

0 comments on commit 90f114d

Please sign in to comment.