Skip to content

Commit 8d97acf

Browse files
authored
fix(material/tooltip): not updating position after direction changes (#22641)
Fixes that the tooltip won't update its position config if the directionality changes after it has been initialized.
1 parent 7367823 commit 8d97acf

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

src/material-experimental/mdc-tooltip/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ ng_test_library(
6767
"//src/cdk/testing/private",
6868
"@npm//@angular/animations",
6969
"@npm//@angular/platform-browser",
70+
"@npm//rxjs",
7071
],
7172
)
7273

src/material-experimental/mdc-tooltip/tooltip.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
} from '@angular/core/testing';
3434
import {By} from '@angular/platform-browser';
3535
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
36+
import {Subject} from 'rxjs';
3637
import {
3738
MAT_TOOLTIP_DEFAULT_OPTIONS,
3839
MatTooltip,
@@ -48,7 +49,7 @@ const initialTooltipMessage = 'initial tooltip message';
4849
describe('MDC-based MatTooltip', () => {
4950
let overlayContainer: OverlayContainer;
5051
let overlayContainerElement: HTMLElement;
51-
let dir: {value: Direction};
52+
let dir: {value: Direction, change: Subject<Direction>};
5253
let platform: Platform;
5354
let focusMonitor: FocusMonitor;
5455

@@ -66,7 +67,7 @@ describe('MDC-based MatTooltip', () => {
6667
],
6768
providers: [
6869
{provide: Directionality, useFactory: () => {
69-
return dir = {value: 'ltr'};
70+
return dir = {value: 'ltr', change: new Subject()};
7071
}}
7172
]
7273
});
@@ -363,6 +364,19 @@ describe('MDC-based MatTooltip', () => {
363364
expect(tooltipDirective._overlayRef!.updatePosition).toHaveBeenCalled();
364365
}));
365366

367+
it('should update the tooltip position when the directionality changes', fakeAsync(() => {
368+
tooltipDirective.position = 'right';
369+
tooltipDirective.show();
370+
tick();
371+
372+
assertTooltipInstance(tooltipDirective, true);
373+
const spy = spyOn(tooltipDirective as any, '_updatePosition').and.callThrough();
374+
dir.change.next('rtl');
375+
376+
assertTooltipInstance(tooltipDirective, true);
377+
expect(spy).toHaveBeenCalled();
378+
}));
379+
366380
it('should not throw when updating the position for a closed tooltip', fakeAsync(() => {
367381
tooltipDirective.position = 'left';
368382
tooltipDirective.show(0);

src/material/tooltip/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ ng_test_library(
6868
"//src/cdk/testing/private",
6969
"@npm//@angular/animations",
7070
"@npm//@angular/platform-browser",
71+
"@npm//rxjs",
7172
],
7273
)
7374

src/material/tooltip/tooltip.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
} from '@angular/core/testing';
3434
import {By} from '@angular/platform-browser';
3535
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
36+
import {Subject} from 'rxjs';
3637
import {
3738
MAT_TOOLTIP_DEFAULT_OPTIONS,
3839
MatTooltip,
@@ -48,7 +49,7 @@ const initialTooltipMessage = 'initial tooltip message';
4849
describe('MatTooltip', () => {
4950
let overlayContainer: OverlayContainer;
5051
let overlayContainerElement: HTMLElement;
51-
let dir: {value: Direction};
52+
let dir: {value: Direction, change: Subject<Direction>};
5253
let platform: Platform;
5354
let focusMonitor: FocusMonitor;
5455

@@ -66,7 +67,7 @@ describe('MatTooltip', () => {
6667
],
6768
providers: [
6869
{provide: Directionality, useFactory: () => {
69-
return dir = {value: 'ltr'};
70+
return dir = {value: 'ltr', change: new Subject()};
7071
}}
7172
]
7273
});
@@ -362,6 +363,19 @@ describe('MatTooltip', () => {
362363
expect(tooltipDirective._overlayRef!.updatePosition).toHaveBeenCalled();
363364
}));
364365

366+
it('should update the tooltip position when the directionality changes', fakeAsync(() => {
367+
tooltipDirective.position = 'right';
368+
tooltipDirective.show();
369+
tick();
370+
371+
assertTooltipInstance(tooltipDirective, true);
372+
const spy = spyOn(tooltipDirective as any, '_updatePosition').and.callThrough();
373+
dir.change.next('rtl');
374+
375+
assertTooltipInstance(tooltipDirective, true);
376+
expect(spy).toHaveBeenCalled();
377+
}));
378+
365379
it('should not throw when updating the position for a closed tooltip', fakeAsync(() => {
366380
tooltipDirective.position = 'left';
367381
tooltipDirective.show(0);

src/material/tooltip/tooltip.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,8 @@ export abstract class _MatTooltipBase<T extends _TooltipComponentBase> implement
158158
this._position = value;
159159

160160
if (this._overlayRef) {
161-
this._updatePosition();
162-
163-
if (this._tooltipInstance) {
164-
this._tooltipInstance!.show(0);
165-
}
166-
161+
this._updatePosition(this._overlayRef);
162+
this._tooltipInstance?.show(0);
167163
this._overlayRef.updatePosition();
168164
}
169165
}
@@ -284,6 +280,12 @@ export abstract class _MatTooltipBase<T extends _TooltipComponentBase> implement
284280
}
285281
}
286282

283+
_dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => {
284+
if (this._overlayRef) {
285+
this._updatePosition(this._overlayRef);
286+
}
287+
});
288+
287289
_ngZone.runOutsideAngular(() => {
288290
_elementRef.nativeElement.addEventListener('keydown', this._handleKeydown);
289291
});
@@ -418,7 +420,7 @@ export abstract class _MatTooltipBase<T extends _TooltipComponentBase> implement
418420
scrollStrategy: this._scrollStrategy()
419421
});
420422

421-
this._updatePosition();
423+
this._updatePosition(this._overlayRef);
422424

423425
this._overlayRef.detachments()
424426
.pipe(takeUntil(this._destroyed))
@@ -437,9 +439,8 @@ export abstract class _MatTooltipBase<T extends _TooltipComponentBase> implement
437439
}
438440

439441
/** Updates the position of the current tooltip. */
440-
private _updatePosition() {
441-
const position =
442-
this._overlayRef!.getConfig().positionStrategy as FlexibleConnectedPositionStrategy;
442+
private _updatePosition(overlayRef: OverlayRef) {
443+
const position = overlayRef.getConfig().positionStrategy as FlexibleConnectedPositionStrategy;
443444
const origin = this._getOrigin();
444445
const overlay = this._getOverlayPosition();
445446

0 commit comments

Comments
 (0)