Skip to content

Commit 6977b3a

Browse files
committed
fix(tooltip): not show tooltip when target is clicked
1 parent 2e5174f commit 6977b3a

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/components/tooltip/controller.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ class TooltipController implements ReactiveController {
133133
for (const each of this._hideTriggers) {
134134
addWeakEventListener(anchor, each, this, { passive: true, signal });
135135
}
136+
137+
if (!this._showTriggers.has('click') && !this._hideTriggers.has('click')) {
138+
addWeakEventListener(anchor, 'click', this, { passive: true, signal });
139+
}
136140
}
137141

138142
private _addTooltipListeners(): void {
@@ -161,6 +165,15 @@ class TooltipController implements ReactiveController {
161165
}
162166

163167
private async _handleAnchorEvent(event: Event): Promise<void> {
168+
if (
169+
!this.open &&
170+
!this._showTriggers.has(event.type) &&
171+
event.type === 'click'
172+
) {
173+
this._options.onClick.call(this._host);
174+
return;
175+
}
176+
164177
if (!this._open && this._showTriggers.has(event.type)) {
165178
await this._options.onShow.call(this._host);
166179
}
@@ -283,4 +296,5 @@ type TooltipCallbacks = {
283296
onShow: (event?: Event) => unknown;
284297
onHide: (event?: Event) => unknown;
285298
onEscape: (event?: Event) => unknown;
299+
onClick: (event?: Event) => unknown;
286300
};

src/components/tooltip/tooltip.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ describe('Tooltip', () => {
534534

535535
describe('Behaviors', () => {
536536
beforeEach(async () => {
537-
clock = useFakeTimers({ toFake: ['setTimeout'] });
537+
clock = useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
538538
const container = await fixture(createTooltipWithTarget());
539539
anchor = container.querySelector('button')!;
540540
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
@@ -660,6 +660,40 @@ describe('Tooltip', () => {
660660
await hideComplete(tooltip);
661661
expect(tooltip.open).to.be.false;
662662
});
663+
664+
it('prevents tooltip from showing when clicking the target - #1828', async () => {
665+
const eventSpy = spy(tooltip, 'emitEvent');
666+
667+
tooltip.showTriggers = 'pointerenter';
668+
tooltip.hideTriggers = 'pointerleave';
669+
670+
simulatePointerEnter(anchor);
671+
await clock.tickAsync(199);
672+
expect(tooltip.open).to.be.false;
673+
674+
// Click on the target before the tooltip is shown
675+
simulateClick(anchor);
676+
await clock.tickAsync(1);
677+
await showComplete(tooltip);
678+
expect(tooltip.open).to.be.false;
679+
expect(eventSpy.callCount).to.equal(1);
680+
681+
eventSpy.resetHistory();
682+
683+
// Does not prevent showing when showTriggers includes 'click'
684+
tooltip.showTriggers = 'click';
685+
686+
simulateClick(anchor);
687+
await clock.tickAsync(DEFAULT_SHOW_DELAY);
688+
await showComplete(tooltip);
689+
expect(tooltip.open).to.be.true;
690+
691+
simulatePointerLeave(anchor);
692+
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
693+
await hideComplete(tooltip);
694+
expect(tooltip.open).to.be.false;
695+
expect(eventSpy.callCount).to.equal(4);
696+
});
663697
});
664698

665699
describe('Events', () => {

src/components/tooltip/tooltip.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default class IgcTooltipComponent extends EventEmitterMixin<
7777
onShow: this._showOnInteraction,
7878
onHide: this._hideOnInteraction,
7979
onEscape: this._hideOnEscape,
80+
onClick: this._stopTimeoutAndAnimation,
8081
});
8182

8283
private readonly _containerRef = createRef<HTMLElement>();

0 commit comments

Comments
 (0)