Skip to content

Commit abc3d38

Browse files
crisbetotinayuangao
authored andcommitted
fix(tooltip): not handling direction changes after the first open (#11324)
Fixes the tooltip's direction not changing after the first time it's opened. Also trims down some duplicated logic.
1 parent 0cda47c commit abc3d38

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/cdk/overlay/overlay-ref.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export type ImmutableObject<T> = {
2121
readonly [P in keyof T]: T[P];
2222
};
2323

24+
// TODO(crisbeto): add support for passing in a `Directionality`
25+
// to make syncing the direction easier.
26+
2427
/**
2528
* Reference to an overlay that has been created with the Overlay service.
2629
* Used to manipulate or dispose of said overlay.

src/lib/tooltip/tooltip.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,28 @@ describe('MatTooltip', () => {
503503
expect(tooltipWrapper.getAttribute('dir')).toBe('rtl', 'Expected tooltip to be in RTL mode.');
504504
}));
505505

506+
it('should keep the overlay direction in sync with the trigger direction', fakeAsync(() => {
507+
dir.value = 'rtl';
508+
tooltipDirective.show();
509+
tick();
510+
fixture.detectChanges();
511+
512+
let tooltipWrapper = overlayContainerElement.querySelector('.cdk-overlay-pane')!;
513+
expect(tooltipWrapper.getAttribute('dir')).toBe('rtl', 'Expected tooltip to be in RTL.');
514+
515+
tooltipDirective.hide(0);
516+
tick();
517+
fixture.detectChanges();
518+
519+
dir.value = 'ltr';
520+
tooltipDirective.show();
521+
tick();
522+
fixture.detectChanges();
523+
524+
tooltipWrapper = overlayContainerElement.querySelector('.cdk-overlay-pane')!;
525+
expect(tooltipWrapper.getAttribute('dir')).toBe('ltr', 'Expected tooltip to be in LTR.');
526+
}));
527+
506528
it('should be able to set the tooltip message as a number', fakeAsync(() => {
507529
fixture.componentInstance.message = 100;
508530
fixture.detectChanges();

src/lib/tooltip/tooltip.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ export class MatTooltip implements OnDestroy {
264264
const overlayRef = this._createOverlay();
265265

266266
this._detach();
267+
overlayRef.setDirection(this._dir ? this._dir.value : 'ltr');
267268
this._portal = this._portal || new ComponentPortal(TooltipComponent, this._viewContainerRef);
268269
this._tooltipInstance = overlayRef.attach(this._portal).instance;
269270
this._tooltipInstance.afterHidden()
@@ -310,20 +311,12 @@ export class MatTooltip implements OnDestroy {
310311
return this._overlayRef;
311312
}
312313

313-
const origin = this._getOrigin();
314-
const overlay = this._getOverlayPosition();
315-
const direction = this._dir ? this._dir.value : 'ltr';
316-
317314
// Create connected position strategy that listens for scroll events to reposition.
318315
const strategy = this._overlay.position()
319316
.flexibleConnectedTo(this._elementRef)
320317
.withTransformOriginOn('.mat-tooltip')
321318
.withFlexibleDimensions(false)
322-
.withViewportMargin(8)
323-
.withPositions([
324-
{...origin.main, ...overlay.main},
325-
{...origin.fallback, ...overlay.fallback}
326-
]);
319+
.withViewportMargin(8);
327320

328321
const scrollableAncestors = this._scrollDispatcher
329322
.getAncestorScrollContainers(this._elementRef);
@@ -341,12 +334,13 @@ export class MatTooltip implements OnDestroy {
341334
});
342335

343336
this._overlayRef = this._overlay.create({
344-
direction,
345337
positionStrategy: strategy,
346338
panelClass: TOOLTIP_PANEL_CLASS,
347339
scrollStrategy: this._scrollStrategy()
348340
});
349341

342+
this._updatePosition();
343+
350344
this._overlayRef.detachments()
351345
.pipe(takeUntil(this._destroyed))
352346
.subscribe(() => this._detach());
@@ -370,11 +364,10 @@ export class MatTooltip implements OnDestroy {
370364
const origin = this._getOrigin();
371365
const overlay = this._getOverlayPosition();
372366

373-
position
374-
.withPositions([
375-
{...origin.main, ...overlay.main},
376-
{...origin.fallback, ...overlay.fallback}
377-
]);
367+
position.withPositions([
368+
{...origin.main, ...overlay.main},
369+
{...origin.fallback, ...overlay.fallback}
370+
]);
378371
}
379372

380373
/**

0 commit comments

Comments
 (0)