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(components): post-tooltip doesn't show up when the pointer is on a child element #2814

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/fresh-bikes-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-components': patch
---

Fixed post-tooltip that doesn't show up when the pointer is on a child element (like an icon).
15 changes: 15 additions & 0 deletions packages/components/cypress/e2e/tooltip.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ describe('tooltips', { baseUrl: null, includeShadowDom: true }, () => {
});
});

describe('with child element', () => {
beforeEach(() => {
cy.visit('./cypress/fixtures/post-tooltip.test.html');
cy.get('#target-child-element').as('target');
cy.get('#target-child-element span').as('target-child');
cy.get('#tooltip-one').find('div[popover]').as('tooltip');
});

it('should show tooltip on hovered child element', () => {
cy.get('@tooltip').should('not.be.visible');
cy.get('@target-child').trigger('pointerover');
cy.get('.\\:popover-open, :popover-open').should('exist');
});
});

describe('non-focusable element', () => {
beforeEach(() => {
cy.visit('./cypress/fixtures/post-tooltip.test.html');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
<button aria-describedby="existing-value" data-tooltip-target="tooltip-one" id="target1">
toggle
</button>
<button data-tooltip-target="tooltip-one" id="target2">toggle 2</button>
<button data-tooltip-target="tooltip-one" id="target2">
toggle 2 <span>child element</span>
</button>
<button data-tooltip-target="tooltip-one" id="target-child-element">
toggle with <span>child element</span>
</button>
<post-tooltip id="tooltip-one">
<p>This is a test</p>
</post-tooltip>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getAttributeObserver } from '../../utils/attribute-observer';
let tooltipInstances = 0;
let hideTooltipTimeout: number = null;
const tooltipTargetAttribute = 'data-tooltip-target';
const tooltipTargetAttributeSelector = `[${tooltipTargetAttribute}]`;

/**
* Global event listener to show tooltips. This is globalized so that triggers that are rendered
Expand All @@ -23,12 +24,14 @@ const tooltipTargetAttribute = 'data-tooltip-target';
* @returns
*/
const globalInterestHandler = (e: PointerEvent | FocusEvent) => {
const target = e.target as HTMLElement;
if (!target || !('getAttribute' in target)) return;
const tooltipTarget = target.getAttribute(tooltipTargetAttribute);
const targetElement = (e.target as HTMLElement).closest(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using e.currentTarget is an option as well, but it will move the tooltip to the child element which is unwanted.

tooltipTargetAttributeSelector,
) as HTMLElement;
if (!targetElement || !('getAttribute' in targetElement)) return;
const tooltipTarget = targetElement.getAttribute(tooltipTargetAttribute);
if (!tooltipTarget || tooltipTarget === '') return;
const tooltip = document.getElementById(tooltipTarget) as HTMLPostTooltipElement;
tooltip?.show(target);
void tooltip?.show(targetElement);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to make intellij happy.

if (hideTooltipTimeout) {
window.clearTimeout(hideTooltipTimeout);
hideTooltipTimeout = null;
Expand All @@ -42,8 +45,11 @@ const globalInterestHandler = (e: PointerEvent | FocusEvent) => {
* @returns
*/
const globalInterestLostHandler = (e: PointerEvent | FocusEvent) => {
const target = e.target as HTMLElement;
const tooltipTarget = target.getAttribute(tooltipTargetAttribute);
const targetElement = (e.target as HTMLElement).closest(
tooltipTargetAttributeSelector,
) as HTMLElement;
if (!targetElement || !('getAttribute' in targetElement)) return;
const tooltipTarget = targetElement.getAttribute(tooltipTargetAttribute);
if (!tooltipTarget || tooltipTarget === '') return;
const tooltip = document.getElementById(tooltipTarget) as HTMLPostTooltipElement;
globalHideTooltip(tooltip);
Expand Down
Loading