Skip to content

Commit f83a70f

Browse files
authored
Merge pull request #3172 from Akshat55/issue.3170
fix: prevent definition tooltip from flickering on first interaction
2 parents 0755169 + c2657b3 commit f83a70f

File tree

5 files changed

+114
-87
lines changed

5 files changed

+114
-87
lines changed

package-lock.json

Lines changed: 83 additions & 77 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"@angular/platform-browser-dynamic": "14.3.0",
8787
"@angular/router": "14.3.0",
8888
"@babel/core": "7.9.6",
89-
"@carbon/styles": "1.70.0",
89+
"@carbon/styles": "1.88.0",
9090
"@carbon/themes": "11.24.0",
9191
"@commitlint/cli": "17.0.3",
9292
"@commitlint/config-conventional": "17.0.3",

src/tooltip/definition-tooltip.component.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ describe("Definition tooltip", () => {
4545

4646
it("should open/close tooltip definition popover on button click", () => {
4747
spyOn(tooltipEl.componentInstance.isOpenChange, "emit");
48-
buttonEl.triggerEventHandler("click", new Event("click"));
48+
buttonEl.triggerEventHandler("mousedown", new MouseEvent("mousedown", { button: 0 }));
4949
fixture.detectChanges();
5050
expect(tooltipEl.componentInstance.isOpenChange.emit).toHaveBeenCalled();
51-
buttonEl.triggerEventHandler("click", new Event("click"));
51+
buttonEl.triggerEventHandler("mousedown", new MouseEvent("mousedown", { button: 0 }));
5252
fixture.detectChanges();
5353
expect(tooltipEl.componentInstance.isOpenChange.emit).toHaveBeenCalled();
5454
});
5555

5656
it("should markForCheck given the changeDetectorRef is set", () => {
5757
const spy = spyOn(tooltipEl.componentInstance.changeDetectorRef, "markForCheck");
58-
buttonEl.triggerEventHandler("click", new Event("click"));
58+
buttonEl.triggerEventHandler("mousedown", new MouseEvent("mousedown", { button: 0 }));
5959
fixture.detectChanges();
6060
expect(spy).toHaveBeenCalled();
6161
});

src/tooltip/definition-tooptip.component.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { PopoverContainer } from "carbon-components-angular/popover";
3030
[attr.aria-expanded]="isOpen"
3131
[attr.aria-describedby]="isOpen ? id : null"
3232
(blur)="onBlur($event)"
33-
(click)="onClick($event)"
33+
(mousedown)="onClick($event)"
3434
type="button">
3535
<ng-content></ng-content>
3636
</button>
@@ -39,7 +39,9 @@ import { PopoverContainer } from "carbon-components-angular/popover";
3939
class="cds--popover"
4040
[id]="id"
4141
[attr.aria-hidden]="!isOpen"
42-
role="tooltip">
42+
role="tooltip"
43+
(mousedown)="onPopoverMouseDown()"
44+
(mouseup)="onPopoverMouseUp()">
4345
<span class="cds--popover-content cds--definition-tooltip" aria-live="polite">
4446
<ng-container *ngIf="!isTemplate(description)">{{description}}</ng-container>
4547
<ng-template *ngIf="isTemplate(description)" [ngTemplateOutlet]="description" [ngTemplateOutletContext]="{ $implicit: templateContext }"></ng-template>
@@ -65,6 +67,11 @@ export class TooltipDefinition extends PopoverContainer {
6567

6668
@Input() openOnHover = false;
6769

70+
/**
71+
* Helper variable to ensure button blur doesn't fire on `click` of popover content
72+
*/
73+
private isInteractingWithPopover = false;
74+
6875
constructor(
6976
protected elementRef: ElementRef,
7077
protected ngZone: NgZone,
@@ -77,11 +84,24 @@ export class TooltipDefinition extends PopoverContainer {
7784
}
7885

7986
onBlur(event: Event) {
80-
this.handleChange(false, event);
87+
// Only close if user is not interacting with popover content
88+
if (!this.isInteractingWithPopover) {
89+
this.handleChange(false, event);
90+
}
91+
}
92+
93+
onClick(event: MouseEvent) {
94+
if (event.button === 0) {
95+
this.handleChange(!this.isOpen, event);
96+
}
97+
}
98+
99+
onPopoverMouseDown() {
100+
this.isInteractingWithPopover = true;
81101
}
82102

83-
onClick(event: Event) {
84-
this.handleChange(!this.isOpen, event);
103+
onPopoverMouseUp() {
104+
this.isInteractingWithPopover = false;
85105
}
86106

87107
@HostListener("keyup", ["$event"])

src/tooltip/definition-tooptip.stories.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export default {
2323
caret: true,
2424
description: "Uniform Resource Locator; the address of a resource (such as a document or website) on the Internet.",
2525
align: "bottom",
26-
autoAlign: false
26+
autoAlign: false,
27+
openOnHover: true
2728
},
2829
argTypes: {
2930
autoAlign: {

0 commit comments

Comments
 (0)