Skip to content

feat(overlay): allow for Directionality instance to be passed in #11411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/cdk/overlay/overlay-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {PositionStrategy} from './position/position-strategy';
import {Direction} from '@angular/cdk/bidi';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {ScrollStrategy} from './scroll/scroll-strategy';
import {NoopScrollStrategy} from './scroll/noop-scroll-strategy';

Expand Down Expand Up @@ -47,8 +47,11 @@ export class OverlayConfig {
/** The max-height of the overlay panel. If a number is provided, pixel units are assumed. */
maxHeight?: number | string;

/** The direction of the text in the overlay panel. */
direction?: Direction;
/**
* Direction of the text in the overlay panel. If a `Directionality` instance
* is passed in, the overlay will handle changes to its value automatically.
*/
direction?: Direction | Directionality;

constructor(config?: OverlayConfig) {
if (config) {
Expand Down
3 changes: 1 addition & 2 deletions src/cdk/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
private _buildConfig(): OverlayConfig {
const positionStrategy = this._position = this._createPositionStrategy();
const overlayConfig = new OverlayConfig({
direction: this._dir,
positionStrategy,
scrollStrategy: this.scrollStrategy,
hasBackdrop: this.hasBackdrop
Expand Down Expand Up @@ -348,8 +349,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
});
}

this._overlayRef.setDirection(this.dir);

if (!this._overlayRef.hasAttached()) {
this._overlayRef.attach(this._templatePortal);
this.attach.emit();
Expand Down
22 changes: 16 additions & 6 deletions src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Direction} from '@angular/cdk/bidi';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {ComponentPortal, Portal, PortalOutlet, TemplatePortal} from '@angular/cdk/portal';
import {ComponentRef, EmbeddedViewRef, NgZone} from '@angular/core';
import {Observable, Subject} from 'rxjs';
Expand All @@ -21,9 +21,6 @@ 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 Expand Up @@ -242,14 +239,27 @@ export class OverlayRef implements PortalOutlet {
}

/** Sets the LTR/RTL direction for the overlay. */
setDirection(dir: Direction) {
setDirection(dir: Direction | Directionality) {
this._config = {...this._config, direction: dir};
this._updateElementDirection();
}

/**
* Returns the layout direction of the overlay panel.
*/
getDirection(): Direction {
const direction = this._config.direction;

if (!direction) {
return 'ltr';
}

return typeof direction === 'string' ? direction : direction.value;
}

/** Updates the text direction of the overlay panel. */
private _updateElementDirection() {
this._host.setAttribute('dir', this._config.direction!);
this._host.setAttribute('dir', this.getDirection());
}

/** Updates the size of the overlay element based on the overlay config. */
Expand Down
10 changes: 10 additions & 0 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ViewContainerRef,
ErrorHandler,
Injectable,
EventEmitter,
} from '@angular/core';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {
Expand Down Expand Up @@ -318,6 +319,15 @@ describe('Overlay', () => {
expect(() => TestBed.compileComponents()).not.toThrow();
});

it('should keep the direction in sync with the passed in Directionality', () => {
const customDirectionality = {value: 'rtl', change: new EventEmitter()};
const overlayRef = overlay.create({direction: customDirectionality as Directionality});

expect(overlayRef.getDirection()).toBe('rtl');
customDirectionality.value = 'ltr';
expect(overlayRef.getDirection()).toBe('ltr');
});

describe('positioning', () => {
let config: OverlayConfig;

Expand Down
2 changes: 1 addition & 1 deletion src/cdk/overlay/position/connected-position-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class ConnectedPositionStrategy implements PositionStrategy {

/** Whether the we're dealing with an RTL context */
get _isRtl() {
return this._overlayRef.getConfig().direction === 'rtl';
return this._overlayRef.getDirection() === 'rtl';
}

/** Ordered list of preferred positions, from most to least desirable. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {

/** Whether the we're dealing with an RTL context */
private _isRtl() {
return this._overlayRef.getConfig().direction === 'rtl';
return this._overlayRef.getDirection() === 'rtl';
}

/** Determines whether the overlay uses exact or flexible positioning. */
Expand Down
7 changes: 1 addition & 6 deletions src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,6 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
} else {
// Update the panel width and direction, in case anything has changed.
this._overlayRef.updateSize({width: this._getHostWidth()});
this._overlayRef.setDirection(this._getDirection());
}

if (this._overlayRef && !this._overlayRef.hasAttached()) {
Expand All @@ -553,7 +552,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
positionStrategy: this._getOverlayPosition(),
scrollStrategy: this._scrollStrategy(),
width: this._getHostWidth(),
direction: this._getDirection()
direction: this._dir
});
}

Expand All @@ -570,10 +569,6 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
return this._positionStrategy;
}

private _getDirection() {
return this._dir ? this._dir.value : 'ltr';
}

private _getConnectedElement(): ElementRef {
if (this.connectedTo) {
return this.connectedTo.elementRef;
Expand Down
10 changes: 2 additions & 8 deletions src/lib/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
/** Open the calendar as a dialog. */
private _openAsDialog(): void {
this._dialogRef = this._dialog.open<MatDatepickerContent<D>>(MatDatepickerContent, {
direction: this._getDirection(),
direction: this._dir ? this._dir.value : 'ltr',
viewContainerRef: this._viewContainerRef,
panelClass: 'mat-datepicker-dialog',
});
Expand All @@ -403,7 +403,6 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
}

if (!this._popupRef.hasAttached()) {
this._popupRef.setDirection(this._getDirection());
this._popupComponentRef = this._popupRef.attach(this._calendarPortal);
this._popupComponentRef.instance.datepicker = this;
this._setColor();
Expand All @@ -421,7 +420,7 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
positionStrategy: this._createPopupPositionStrategy(),
hasBackdrop: true,
backdropClass: 'mat-overlay-transparent-backdrop',
direction: this._getDirection(),
direction: this._dir,
scrollStrategy: this._scrollStrategy(),
panelClass: 'mat-datepicker-popup',
});
Expand Down Expand Up @@ -493,9 +492,4 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
this._dialogRef.componentInstance.color = color;
}
}

/** Returns the layout direction of the datepicker. */
private _getDirection() {
return this._dir ? this._dir.value : 'ltr';
}
}
4 changes: 2 additions & 2 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
}

const overlayRef = this._createOverlay();
overlayRef.setDirection(this.dir);
overlayRef.attach(this._portal);

if (this.menu.lazyContent) {
Expand Down Expand Up @@ -353,7 +352,8 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
positionStrategy: this._getPosition(),
hasBackdrop: this.menu.hasBackdrop == null ? !this.triggersSubmenu() : this.menu.hasBackdrop,
backdropClass: this.menu.backdropClass || 'cdk-overlay-transparent-backdrop',
scrollStrategy: this._scrollStrategy()
scrollStrategy: this._scrollStrategy(),
direction: this._dir
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ 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 @@ -334,6 +333,7 @@ export class MatTooltip implements OnDestroy {
});

this._overlayRef = this._overlay.create({
direction: this._dir,
positionStrategy: strategy,
panelClass: TOOLTIP_PANEL_CLASS,
scrollStrategy: this._scrollStrategy()
Expand Down