Skip to content

Commit cd37a54

Browse files
crisbetotinayuangao
authored andcommitted
fix(dialog,bottom-sheet): don't provide directionality if no direction is set (#11285)
* Currently the dialog and bottom sheet always provide the `Directionality` in order to allow for any components placed inside of them to pick up their direction. This is problematic if the consumer didn't set a direction, because the `value` of the provided `Directionality` will be set to `undefined`. These changes switch to only providing the direction if it is defined in the config, otherwise the components will fall back to the global `Directionality`. * Flips around some logic in the drawer so an undefined direction is consider `ltr`, rather than `rtl`. Fixes #11262.
1 parent 4dbbb43 commit cd37a54

File tree

7 files changed

+34
-7
lines changed

7 files changed

+34
-7
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,14 @@ describe('Dialog', () => {
522522
expect(dialogRef.componentInstance.directionality.value).toBe('rtl');
523523
});
524524

525+
it('should fall back to injecting the global direction if none is passed by the config', () => {
526+
const dialogRef = dialog.openFromComponent(PizzaMsg, {});
527+
528+
viewContainerFixture.detectChanges();
529+
530+
expect(dialogRef.componentInstance.directionality.value).toBe('ltr');
531+
});
532+
525533
it('should close all of the dialogs', fakeAsync(() => {
526534
dialog.openFromComponent(PizzaMsg);
527535
dialog.openFromComponent(PizzaMsg);

src/cdk-experimental/dialog/dialog.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ export class Dialog {
269269
[DIALOG_DATA, config.data]
270270
]);
271271

272-
if (!userInjector || !userInjector.get<Directionality | null>(Directionality, null)) {
272+
if (config.direction &&
273+
(!userInjector || !userInjector.get<Directionality | null>(Directionality, null))) {
273274
injectionTokens.set(Directionality, {
274275
value: config.direction,
275276
change: observableOf()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ describe('MatBottomSheet', () => {
224224
expect(bottomSheetRef.instance.directionality.value).toBe('rtl');
225225
});
226226

227+
it('should fall back to injecting the global direction if none is passed by the config', () => {
228+
const bottomSheetRef = bottomSheet.open(PizzaMsg, {});
229+
230+
viewContainerFixture.detectChanges();
231+
232+
expect(bottomSheetRef.instance.directionality.value).toBe('ltr');
233+
});
234+
227235
it('should be able to set a custom panel class', () => {
228236
bottomSheet.open(PizzaMsg, {
229237
panelClass: 'custom-panel-class',

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ export class MatBottomSheet {
152152
[MAT_BOTTOM_SHEET_DATA, config.data]
153153
]);
154154

155-
if (!userInjector || !userInjector.get<Directionality | null>(Directionality, null)) {
155+
if (config.direction &&
156+
(!userInjector || !userInjector.get<Directionality | null>(Directionality, null))) {
156157
injectionTokens.set(Directionality, {
157158
value: config.direction,
158159
change: observableOf()

src/lib/dialog/dialog.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,14 @@ describe('MatDialog', () => {
530530
expect(dialogRef.componentInstance.directionality.value).toBe('rtl');
531531
});
532532

533+
it('should fall back to injecting the global direction if none is passed by the config', () => {
534+
const dialogRef = dialog.open(PizzaMsg, {});
535+
536+
viewContainerFixture.detectChanges();
537+
538+
expect(dialogRef.componentInstance.directionality.value).toBe('ltr');
539+
});
540+
533541
it('should close all of the dialogs', fakeAsync(() => {
534542
dialog.open(PizzaMsg);
535543
dialog.open(PizzaMsg);

src/lib/dialog/dialog.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ export class MatDialog {
292292
[MatDialogRef, dialogRef]
293293
]);
294294

295-
if (!userInjector || !userInjector.get<Directionality | null>(Directionality, null)) {
295+
if (config.direction &&
296+
(!userInjector || !userInjector.get<Directionality | null>(Directionality, null))) {
296297
injectionTokens.set(Directionality, {
297298
value: config.direction,
298299
change: observableOf()

src/lib/sidenav/drawer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,12 +627,12 @@ export class MatDrawerContainer implements AfterContentInit, DoCheck, OnDestroy
627627
this._right = this._left = null;
628628

629629
// Detect if we're LTR or RTL.
630-
if (!this._dir || this._dir.value == 'ltr') {
631-
this._left = this._start;
632-
this._right = this._end;
633-
} else {
630+
if (this._dir && this._dir.value === 'rtl') {
634631
this._left = this._end;
635632
this._right = this._start;
633+
} else {
634+
this._left = this._start;
635+
this._right = this._end;
636636
}
637637
}
638638

0 commit comments

Comments
 (0)