Skip to content

Commit 04045d2

Browse files
crisbetojosephperrott
authored andcommitted
feat(tooltip): allow for position to be updated while open (#10362)
Allows for the position of a tooltip to be updated while it's still open. Previously we closed the tooltip, because it wasn't possible for the overlay position to be updated.
1 parent dda1d04 commit 04045d2

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/demo-app/tooltip/tooltip-demo.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
overflow: auto;
66

77
button {
8-
margin: 16px;
8+
margin: 75px 16px 16px;
99
}
1010
}
1111
.mat-radio-button {

src/lib/tooltip/tooltip.spec.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
MatTooltipModule,
2929
SCROLL_THROTTLE_MS,
3030
TOOLTIP_PANEL_CLASS,
31-
TooltipPosition,
3231
MAT_TOOLTIP_DEFAULT_OPTIONS,
3332
} from './index';
3433

@@ -306,24 +305,21 @@ describe('MatTooltip', () => {
306305
expect(tooltipDirective._isTooltipVisible()).toBe(true);
307306
}));
308307

309-
it('should remove the tooltip when changing position', () => {
310-
const initialPosition: TooltipPosition = 'below';
311-
const changedPosition: TooltipPosition = 'above';
312-
313-
assertTooltipInstance(tooltipDirective, false);
314-
315-
tooltipDirective.position = initialPosition;
308+
it('should be able to update the tooltip position while open', fakeAsync(() => {
309+
tooltipDirective.position = 'below';
316310
tooltipDirective.show();
317-
expect(tooltipDirective._tooltipInstance).toBeTruthy();
311+
tick();
318312

319-
// Same position value should not remove the tooltip
320-
tooltipDirective.position = initialPosition;
321-
expect(tooltipDirective._tooltipInstance).toBeTruthy();
313+
assertTooltipInstance(tooltipDirective, true);
322314

323-
// Different position value should destroy the tooltip
324-
tooltipDirective.position = changedPosition;
325-
assertTooltipInstance(tooltipDirective, false);
326-
});
315+
tooltipDirective.position = 'above';
316+
spyOn(tooltipDirective._overlayRef!, 'updatePosition').and.callThrough();
317+
fixture.detectChanges();
318+
tick();
319+
320+
assertTooltipInstance(tooltipDirective, true);
321+
expect(tooltipDirective._overlayRef!.updatePosition).toHaveBeenCalled();
322+
}));
327323

328324
it('should be able to modify the tooltip message', fakeAsync(() => {
329325
assertTooltipInstance(tooltipDirective, false);

src/lib/tooltip/tooltip.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,9 @@ export class MatTooltip implements OnDestroy {
119119
this._position = value;
120120

121121
if (this._overlayRef) {
122-
// TODO(andrewjs): When the overlay's position can be
123-
// dynamically changed, do not destroy the tooltip.
124-
this._detach();
125122
this._updatePosition();
123+
this._tooltipInstance!.show(value, 0);
124+
this._overlayRef.updatePosition();
126125
}
127126
}
128127
}

0 commit comments

Comments
 (0)