Skip to content

Commit 47674f1

Browse files
crisbetojelbourn
authored andcommitted
fix(overlay): default to global directionality (#9994)
Currently overlays default to the `ltr` direction, which means that consumers, whose apps are in RTL, have to pass in the direction explicitly for every overlay. These changes switch to taking the default direction from the global `Directionality` which is based on the `body` and `html` elements. Fixes #9817.
1 parent e24bb42 commit 47674f1

File tree

7 files changed

+33
-9
lines changed

7 files changed

+33
-9
lines changed

src/cdk-experimental/dialog/dialog-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class DialogConfig<D = any> {
7979
data?: D | null = null;
8080

8181
/** The layout direction for the dialog content. */
82-
direction?: Direction = 'ltr';
82+
direction?: Direction;
8383

8484
/** ID of the element that describes the dialog. */
8585
ariaDescribedBy?: string | null = null;

src/cdk/overlay/overlay-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class OverlayConfig {
4848
maxHeight?: number | string;
4949

5050
/** The direction of the text in the overlay panel. */
51-
direction?: Direction = 'ltr';
51+
direction?: Direction;
5252

5353
constructor(config?: OverlayConfig) {
5454
if (config) {

src/cdk/overlay/overlay.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {async, fakeAsync, tick, ComponentFixture, inject, TestBed} from '@angular/core/testing';
22
import {Component, NgModule, ViewChild, ViewContainerRef} from '@angular/core';
3+
import {Direction, Directionality} from '@angular/cdk/bidi';
34
import {
45
ComponentPortal,
56
PortalModule,
@@ -24,10 +25,20 @@ describe('Overlay', () => {
2425
let overlayContainerElement: HTMLElement;
2526
let overlayContainer: OverlayContainer;
2627
let viewContainerFixture: ComponentFixture<TestComponentWithTemplatePortals>;
28+
let dir: Direction;
2729

2830
beforeEach(async(() => {
31+
dir = 'ltr';
2932
TestBed.configureTestingModule({
30-
imports: [OverlayModule, PortalModule, OverlayTestModule]
33+
imports: [OverlayModule, PortalModule, OverlayTestModule],
34+
providers: [{
35+
provide: Directionality,
36+
useFactory: () => {
37+
const fakeDirectionality = {};
38+
Object.defineProperty(fakeDirectionality, 'value', {get: () => dir});
39+
return fakeDirectionality;
40+
}
41+
}],
3142
}).compileComponents();
3243
}));
3344

@@ -132,13 +143,21 @@ describe('Overlay', () => {
132143
.toBeFalsy('Expected cake to still be on top.');
133144
}));
134145

146+
it('should take the default direction from the global Directionality', () => {
147+
dir = 'rtl';
148+
overlay.create().attach(componentPortal);
149+
150+
const pane = overlayContainerElement.children[0] as HTMLElement;
151+
expect(pane.getAttribute('dir')).toBe('rtl');
152+
});
153+
135154
it('should set the direction', () => {
136155
const config = new OverlayConfig({direction: 'rtl'});
137156

138157
overlay.create(config).attach(componentPortal);
139158

140159
const pane = overlayContainerElement.children[0] as HTMLElement;
141-
expect(pane.getAttribute('dir')).toEqual('rtl');
160+
expect(pane.getAttribute('dir')).toBe('rtl');
142161
});
143162

144163
it('should emit when an overlay is attached', () => {

src/cdk/overlay/overlay.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {OverlayKeyboardDispatcher} from './keyboard/overlay-keyboard-dispatcher'
2222
import {OverlayContainer} from './overlay-container';
2323
import {ScrollStrategyOptions} from './scroll/index';
2424
import {DOCUMENT} from '@angular/common';
25+
import {Directionality} from '@angular/cdk/bidi';
2526

2627

2728
/** Next overlay unique ID. */
@@ -47,7 +48,8 @@ export class Overlay {
4748
private _appRef: ApplicationRef,
4849
private _injector: Injector,
4950
private _ngZone: NgZone,
50-
@Inject(DOCUMENT) private _document: any) { }
51+
@Inject(DOCUMENT) private _document: any,
52+
private _directionality: Directionality) { }
5153

5254
/**
5355
* Creates an overlay.
@@ -57,11 +59,14 @@ export class Overlay {
5759
create(config?: OverlayConfig): OverlayRef {
5860
const pane = this._createPaneElement();
5961
const portalOutlet = this._createPortalOutlet(pane);
62+
const overlayConfig = new OverlayConfig(config);
63+
64+
overlayConfig.direction = overlayConfig.direction || this._directionality.value;
6065

6166
return new OverlayRef(
6267
portalOutlet,
6368
pane,
64-
new OverlayConfig(config),
69+
overlayConfig,
6570
this._ngZone,
6671
this._keyboardDispatcher,
6772
this._document

src/lib/bottom-sheet/bottom-sheet-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class MatBottomSheetConfig<D = any> {
2323
panelClass?: string | string[];
2424

2525
/** Text layout direction for the bottom sheet. */
26-
direction?: Direction = 'ltr';
26+
direction?: Direction;
2727

2828
/** Data being injected into the child component. */
2929
data?: D | null = null;

src/lib/dialog/dialog-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class MatDialogConfig<D = any> {
8484
data?: D | null = null;
8585

8686
/** Layout direction for the dialog's content. */
87-
direction?: Direction = 'ltr';
87+
direction?: Direction;
8888

8989
/** ID of the element that describes the dialog. */
9090
ariaDescribedBy?: string | null = null;

src/lib/snack-bar/snack-bar-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class MatSnackBarConfig<D = any> {
4646
extraClasses?: string | string[];
4747

4848
/** Text layout direction for the snack bar. */
49-
direction?: Direction = 'ltr';
49+
direction?: Direction;
5050

5151
/** Data being injected into the child component. */
5252
data?: D | null = null;

0 commit comments

Comments
 (0)