Skip to content

Commit

Permalink
fix(tooltip): not handling direction changes after the first open (#1…
Browse files Browse the repository at this point in the history
…1324)

Fixes the tooltip's direction not changing after the first time it's opened. Also trims down some duplicated logic.
  • Loading branch information
crisbeto authored and tinayuangao committed May 16, 2018
1 parent 0cda47c commit abc3d38
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export type ImmutableObject<T> = {
readonly [P in keyof T]: T[P];
};

// TODO(crisbeto): add support for passing in a `Directionality`
// to make syncing the direction easier.

/**
* Reference to an overlay that has been created with the Overlay service.
* Used to manipulate or dispose of said overlay.
Expand Down
22 changes: 22 additions & 0 deletions src/lib/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,28 @@ describe('MatTooltip', () => {
expect(tooltipWrapper.getAttribute('dir')).toBe('rtl', 'Expected tooltip to be in RTL mode.');
}));

it('should keep the overlay direction in sync with the trigger direction', fakeAsync(() => {
dir.value = 'rtl';
tooltipDirective.show();
tick();
fixture.detectChanges();

let tooltipWrapper = overlayContainerElement.querySelector('.cdk-overlay-pane')!;
expect(tooltipWrapper.getAttribute('dir')).toBe('rtl', 'Expected tooltip to be in RTL.');

tooltipDirective.hide(0);
tick();
fixture.detectChanges();

dir.value = 'ltr';
tooltipDirective.show();
tick();
fixture.detectChanges();

tooltipWrapper = overlayContainerElement.querySelector('.cdk-overlay-pane')!;
expect(tooltipWrapper.getAttribute('dir')).toBe('ltr', 'Expected tooltip to be in LTR.');
}));

it('should be able to set the tooltip message as a number', fakeAsync(() => {
fixture.componentInstance.message = 100;
fixture.detectChanges();
Expand Down
23 changes: 8 additions & 15 deletions src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export class MatTooltip implements OnDestroy {
const overlayRef = this._createOverlay();

this._detach();
overlayRef.setDirection(this._dir ? this._dir.value : 'ltr');
this._portal = this._portal || new ComponentPortal(TooltipComponent, this._viewContainerRef);
this._tooltipInstance = overlayRef.attach(this._portal).instance;
this._tooltipInstance.afterHidden()
Expand Down Expand Up @@ -310,20 +311,12 @@ export class MatTooltip implements OnDestroy {
return this._overlayRef;
}

const origin = this._getOrigin();
const overlay = this._getOverlayPosition();
const direction = this._dir ? this._dir.value : 'ltr';

// Create connected position strategy that listens for scroll events to reposition.
const strategy = this._overlay.position()
.flexibleConnectedTo(this._elementRef)
.withTransformOriginOn('.mat-tooltip')
.withFlexibleDimensions(false)
.withViewportMargin(8)
.withPositions([
{...origin.main, ...overlay.main},
{...origin.fallback, ...overlay.fallback}
]);
.withViewportMargin(8);

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

this._overlayRef = this._overlay.create({
direction,
positionStrategy: strategy,
panelClass: TOOLTIP_PANEL_CLASS,
scrollStrategy: this._scrollStrategy()
});

this._updatePosition();

this._overlayRef.detachments()
.pipe(takeUntil(this._destroyed))
.subscribe(() => this._detach());
Expand All @@ -370,11 +364,10 @@ export class MatTooltip implements OnDestroy {
const origin = this._getOrigin();
const overlay = this._getOverlayPosition();

position
.withPositions([
{...origin.main, ...overlay.main},
{...origin.fallback, ...overlay.fallback}
]);
position.withPositions([
{...origin.main, ...overlay.main},
{...origin.fallback, ...overlay.fallback}
]);
}

/**
Expand Down

0 comments on commit abc3d38

Please sign in to comment.