Skip to content

Commit 9adcbdd

Browse files
authored
Merge pull request #2871 from Akshat55/icon-button-tooltip
fix: refresh view on disabled or autoalign changes
2 parents 4b4c8f1 + 7fc5f68 commit 9adcbdd

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/tooltip/tooltip.component.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import {
88
HostListener,
99
Input,
1010
NgZone,
11+
OnChanges,
1112
Renderer2,
13+
SimpleChanges,
1214
TemplateRef,
1315
ViewChild
1416
} from "@angular/core";
@@ -47,7 +49,7 @@ import { PopoverContainer } from "carbon-components-angular/popover";
4749
</span>
4850
`
4951
})
50-
export class Tooltip extends PopoverContainer implements AfterContentChecked {
52+
export class Tooltip extends PopoverContainer implements OnChanges, AfterContentChecked {
5153
static tooltipCount = 0;
5254

5355
@HostBinding("class.cds--tooltip") tooltipClass = true;
@@ -132,6 +134,34 @@ export class Tooltip extends PopoverContainer implements AfterContentChecked {
132134
return value instanceof TemplateRef;
133135
}
134136

137+
/**
138+
* Close the popover and reopen it with updated values without emitting an event
139+
* @param changes
140+
*/
141+
ngOnChanges(changes: SimpleChanges): void {
142+
// Close and reopen the popover, handle alignment/programmatic open/close
143+
const originalState = this.isOpen;
144+
this.handleChange(false);
145+
146+
// Ignore first change since content is not initialized
147+
if ((changes.autoAlign && !changes.autoAlign.firstChange)
148+
|| (changes.disabled && !changes.disabled.firstChange && !changes.disabled.currentValue)) {
149+
/**
150+
* When `disabled` is `true`, popover content node is removed. So when re-enabling `disabled`,
151+
* we manually update view so querySelector can detect the popover content node.
152+
* Otherwise, the position of the popover will be incorrect when autoAlign is enabled.
153+
*/
154+
this.changeDetectorRef.detectChanges();
155+
156+
// Reset the inline styles
157+
this.popoverContentRef = this.elementRef.nativeElement.querySelector(".cds--popover-content");
158+
this.popoverContentRef.setAttribute("style", "");
159+
this.caretRef = this.elementRef.nativeElement.querySelector("span.cds--popover-caret");
160+
}
161+
162+
this.handleChange(originalState);
163+
}
164+
135165
/**
136166
* Check for any changes in the projected content & apply accessibility attribute if needed
137167
*/

0 commit comments

Comments
 (0)