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

Tooltip: Focus tooltipped item when tooltip is active, allow tooltip to be dismissed on Esc key #2380

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,81 @@ describe('modus-tooltip', () => {
await new Promise((r) => setTimeout(r, 500));
expect(tooltip.getAttribute('data-show')).toBeNull();
});

it('preserves tabindex on hide', async () => {
const page = await newE2EPage();

await page.setContent(`
<modus-tooltip text="Tooltip text">
<modus-button tabindex="3" id="tooltip-button">Button</modus-button>
</modus-tooltip>
<modus-button id="not-tooltip-button">Other button</modus-button>
`);

const tooltip = await page.find('modus-tooltip');
const button = await tooltip.find('#tooltip-button');
await page.hover('#tooltip-button');
await page.waitForChanges();
expect(button.getAttribute('tabindex')).toEqual('3');

await page.hover('#not-tooltip-button');
await page.waitForChanges();
expect(button.getAttribute('tabindex')).toEqual('3');
});

it('preserves lack of tabindex on hide', async () => {
const page = await newE2EPage();

await page.setContent(`
<modus-tooltip text="Tooltip text">
<modus-button id="tooltip-button">Button</modus-button>
</modus-tooltip>
<modus-button id="not-tooltip-button">Other button</modus-button>
`);

const tooltip = await page.find('modus-tooltip');
const button = await tooltip.find('#tooltip-button');
await page.hover('#tooltip-button');
await page.waitForChanges();
expect(button.getAttribute('tabindex')).toEqual('-1');

await page.hover('#not-tooltip-button');
await page.waitForChanges();
expect(button.getAttribute('tabindex')).toBeNull();
});

it('focuses the tooltipped element on show', async () => {
const page = await newE2EPage();

await page.setContent(`
<modus-tooltip text="Tooltip text">
<modus-button id="tooltip-button">Button</modus-button>
</modus-tooltip>
`);

await page.hover('#tooltip-button');
await page.waitForChanges();
const activeElementId = await page.evaluate(() => document.activeElement!.id);
expect(activeElementId).toEqual('tooltip-button');
});

it('hides the tooltip on "escape" key', async () => {
const page = await newE2EPage();

await page.setContent(`
<modus-tooltip text="Tooltip text">
<modus-button tabindex="3" id="tooltip-button">Button</modus-button>
</modus-tooltip>
`);

const tooltip = await page.find('modus-tooltip >>> .tooltip');
await page.hover('#tooltip-button');

await page.waitForChanges();
expect(tooltip).toHaveAttribute('data-show');

page.keyboard.press('Escape');
await page.waitForChanges();
expect(tooltip).not.toHaveAttribute('data-show');
});
});
26 changes: 25 additions & 1 deletion stencil-workspace/src/components/modus-tooltip/modus-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line
import { Component, Element, Fragment, h, Prop, Watch } from '@stencil/core';
import { Component, Element, h, Prop, Watch, Fragment } from '@stencil/core';
import { createPopper, Instance } from '@popperjs/core';
import { ModusToolTipPlacement } from './modus-tooltip.models';

Expand Down Expand Up @@ -47,6 +47,7 @@ export class ModusTooltip {
}

private popperInstance: Instance;
private targetTabIndex?: string; // necessary for preserving tab index after blur
private tooltipElement: HTMLDivElement;
private readonly showEvents = ['mouseenter', 'mouseover', 'focus'];
private readonly hideEvents = ['mouseleave', 'blur', 'click'];
Expand All @@ -72,6 +73,8 @@ export class ModusTooltip {
const target = this.element.firstElementChild;
if (!target || !this.tooltipElement) return;

this.targetTabIndex = target.getAttribute('tabindex');

this.popperInstance = createPopper(target, this.tooltipElement, {
placement: position,
modifiers: [
Expand All @@ -91,6 +94,8 @@ export class ModusTooltip {
this.hideEvents.forEach((event) => {
target.addEventListener(event, this.hideEventsListener);
});

target.addEventListener('keydown', this.escapeKeyHandler);
}

cleanupPopper(): void {
Expand All @@ -109,7 +114,17 @@ export class ModusTooltip {
this.popperInstance = null;
}

escapeKeyHandler = (event: KeyboardEvent) => {
if (event.code === 'Escape') {
this.hide();
}
};

show(): void {
const target = this.element.firstElementChild as HTMLElement;
const tabIndex = this.targetTabIndex !== '' && this.targetTabIndex != null ? (this.targetTabIndex as string) : '-1';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding tabindex to make item focusable. Feel free to suggest alternatives.

target.setAttribute('tabindex', tabIndex);
target.focus();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Focusing for two reasons:

  1. According to ARIA standards, focus stays on the triggering element while the tooltip is displayed.
  2. Keyboard interaction is done on focused item only.

if (this.popperInstance) {
// Make the tooltip visible
this.tooltipElement.setAttribute('data-show', '');
Expand All @@ -135,6 +150,15 @@ export class ModusTooltip {
...options,
modifiers: [...options.modifiers, { name: 'eventListeners', enabled: false }],
}));

const target = this.element.firstElementChild as HTMLElement;

if (this.targetTabIndex == null) {
target.removeAttribute('tabindex');
} else {
target.setAttribute('tabindex', this.targetTabIndex);
}
target.blur();
}
}

Expand Down
Loading