Skip to content
Open
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
20 changes: 20 additions & 0 deletions projects/element-ng/tooltip/si-tooltip.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ describe('SiTooltipDirective', () => {
expect(document.querySelector('.tooltip')).not.toBeInTheDocument();
});

it('should hide tooltip on Escape key while trigger has focus', async () => {
button.dispatchEvent(new Event('focus'));
vi.advanceTimersByTime(0);
await fixture.whenStable();
expect(document.querySelector('.tooltip')).toBeInTheDocument();

button.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
expect(document.querySelector('.tooltip')).not.toBeInTheDocument();
});

it('should hide tooltip on Escape key while tooltip is open via hover', async () => {
button.dispatchEvent(new MouseEvent('mouseenter'));
vi.advanceTimersByTime(500);
await fixture.whenStable();
expect(document.querySelector('.tooltip')).toBeInTheDocument();

document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }));
expect(document.querySelector('.tooltip')).not.toBeInTheDocument();
});

it('should update tooltip content while open', async () => {
button.dispatchEvent(new Event('focus'));
vi.advanceTimersByTime(0);
Expand Down
6 changes: 5 additions & 1 deletion projects/element-ng/tooltip/si-tooltip.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import { SiTooltipService, TooltipRef } from './si-tooltip.service';
'(mouseenter)': 'show()',
'(touchstart)': 'hide()',
'(focusout)': 'hide()',
'(mouseleave)': 'hide()'
'(mouseleave)': 'hide()',
'(document:keydown.escape)': 'isTooltipVisible && hide()'
}
})
export class SiTooltipDirective implements OnDestroy {
Expand Down Expand Up @@ -60,6 +61,7 @@ export class SiTooltipDirective implements OnDestroy {
readonly tooltipContext = input();

protected describedBy = `__tooltip_${SiTooltipDirective.idCounter++}`;
protected isTooltipVisible = false;

private tooltipRef?: TooltipRef;
private showTimeout?: ReturnType<typeof setTimeout>;
Expand Down Expand Up @@ -98,6 +100,7 @@ export class SiTooltipDirective implements OnDestroy {
tooltipContext: this.tooltipContext
});
this.tooltipRef.show();
this.isTooltipVisible = true;
}, delay);
}

Expand All @@ -114,5 +117,6 @@ export class SiTooltipDirective implements OnDestroy {
protected hide(): void {
this.clearShowTimeout();
this.tooltipRef?.hide();
this.isTooltipVisible = false;
}
}
Loading