Skip to content

Commit

Permalink
feat(tooltip): allow dynamic dir switching (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and GitHub Enterprise committed Dec 3, 2020
1 parent 6890385 commit 79e37c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
17 changes: 14 additions & 3 deletions projects/ng-aquila/src/tooltip/tooltip.directive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AriaDescriber, FocusMonitor } from '@angular/cdk/a11y';
import { Directionality } from '@angular/cdk/bidi';
import { Direction, Directionality } from '@angular/cdk/bidi';
import { coerceBooleanProperty, BooleanInput } from '@angular/cdk/coercion';
import { ESCAPE } from '@angular/cdk/keycodes';
import {
Expand Down Expand Up @@ -29,7 +29,7 @@ import {
ComponentRef,
OnInit,
} from '@angular/core';
import { Subject } from 'rxjs';
import { Subject, Subscription } from 'rxjs';
import { take, takeUntil } from 'rxjs/operators';
import { NxTooltipComponent } from './tooltip.component';

Expand Down Expand Up @@ -109,6 +109,7 @@ export class NxTooltipDirective implements OnDestroy, OnInit {
private _scrollStrategy: () => ScrollStrategy;
private _embeddedViewRef: ComponentRef<NxTooltipComponent>;
private _possibleTooltipPositions: TooltipPosition[] = ['bottom', 'top', 'left', 'right'];
private _dirChangeSubscription: Subscription;

/** Allows the user to define the position of the tooltip relative to the parent element */
@Input('nxTooltipPosition')
Expand Down Expand Up @@ -188,6 +189,7 @@ export class NxTooltipDirective implements OnDestroy, OnInit {
private _defaultOptions: NxTooltipDefaultOptions) {

this._scrollStrategy = this._overlay.scrollStrategies.reposition;
this._dirChangeSubscription = _dir.change.subscribe(this._dirChangeHandler.bind(this));
const element: HTMLElement = _elementRef.nativeElement;

// The mouse events shouldn't be bound on mobile devices, because they can prevent the
Expand Down Expand Up @@ -248,6 +250,7 @@ export class NxTooltipDirective implements OnDestroy, OnInit {

this._ariaDescriber.removeDescription(this._elementRef.nativeElement, this.message);
this._focusMonitor.stopMonitoring(this._elementRef);
this._dirChangeSubscription.unsubscribe();
}

/** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */
Expand Down Expand Up @@ -334,7 +337,7 @@ export class NxTooltipDirective implements OnDestroy, OnInit {
});

this._overlayRef = this._overlay.create({
direction: this._dir,
direction: this._dir.value || 'ltr',
positionStrategy: strategy,
panelClass: NX_TOOLTIP_PANEL_CLASS,
scrollStrategy: this._scrollStrategy(),
Expand Down Expand Up @@ -618,6 +621,14 @@ export class NxTooltipDirective implements OnDestroy, OnInit {
}
}

_dirChangeHandler(value: Direction) {
if (this._overlayRef) {
this.hide(0);
this._overlayRef.setDirection(value);
this._updatePosition();
}
}

get _isLtr(): boolean {
return !this._dir || this._dir.value === 'ltr';
}
Expand Down
14 changes: 12 additions & 2 deletions projects/ng-aquila/src/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ElementRef,
ViewChild,
NgZone,
EventEmitter,
} from '@angular/core';
import { AnimationEvent } from '@angular/animations';
import { By } from '@angular/platform-browser';
Expand Down Expand Up @@ -64,7 +65,7 @@ const initialTooltipMessage = 'initial tooltip message';
describe('NxTooltipDirective', () => {
let overlayContainer: OverlayContainer;
let overlayContainerElement: HTMLElement;
let dir: {value: Direction};
let dir: {value: Direction, change: EventEmitter<Direction>};
let platform: {IOS: boolean, isBrowser: boolean, ANDROID: boolean};
let focusMonitor: FocusMonitor;

Expand All @@ -84,7 +85,7 @@ describe('NxTooltipDirective', () => {
providers: [
{provide: Platform, useFactory: () => platform},
{provide: Directionality, useFactory: () => {
return dir = {value: 'ltr'};
return dir = {value: 'ltr', change: new EventEmitter()};
}}
]
});
Expand Down Expand Up @@ -441,19 +442,26 @@ describe('NxTooltipDirective', () => {

// Test expectations in RTL
dir.value = 'rtl';
dir.change.emit('rtl');
fixture.detectChanges();
expect(tooltipDirective._getOrigin(tooltipDirective.position)).toEqual(leftOrigin);
tooltipDirective.position = 'left';
expect(tooltipDirective._getOrigin(tooltipDirective.position)).toEqual(rightOrigin);
});

it('should consistently position before and after overlay position in ltr and rtl dir', () => {
dir.value = 'ltr';
dir.change.emit('ltr');
fixture.detectChanges();
tooltipDirective.position = 'left';
const leftOverlayPosition = tooltipDirective._getOverlayPosition(tooltipDirective.position);
tooltipDirective.position = 'right';
const rightOverlayPosition = tooltipDirective._getOverlayPosition(tooltipDirective.position);

// Test expectations in RTL
dir.value = 'rtl';
dir.change.emit('rtl');
fixture.detectChanges();
expect(tooltipDirective._getOverlayPosition(tooltipDirective.position)).toEqual(leftOverlayPosition);
tooltipDirective.position = 'left';
expect(tooltipDirective._getOverlayPosition(tooltipDirective.position)).toEqual(rightOverlayPosition);
Expand Down Expand Up @@ -483,6 +491,7 @@ describe('NxTooltipDirective', () => {

it('should keep the overlay direction in sync with the trigger direction', fakeAsync(() => {
dir.value = 'rtl';
dir.change.emit('rtl');
tooltipDirective.show();
tick(200);
fixture.detectChanges();
Expand All @@ -498,6 +507,7 @@ describe('NxTooltipDirective', () => {
tick(500);

dir.value = 'ltr';
dir.change.emit('ltr');
tooltipDirective.show();
tick(200);
fixture.detectChanges();
Expand Down

0 comments on commit 79e37c3

Please sign in to comment.